File: testsmartkeyfile.vala

package info (click to toggle)
libfsobasics 0.9.0%2Bgit20100509-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,676 kB
  • ctags: 103
  • sloc: sh: 10,183; makefile: 233; ansic: 12
file content (184 lines) | stat: -rw-r--r-- 6,896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * Copyright (C) 2009-2010 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using GLib;
using FsoFramework;

const string TEST_FILE_NAME = "testsmartkeyfile.ini";

//===========================================================================
void test_smartkeyfile_values()
//===========================================================================
{
    var smk = new SmartKeyFile();
    var ok = smk.loadFromFile( TEST_FILE_NAME );
    assert( ok );

    var stringvar = smk.stringValue( "section1", "keypresent", "defaultvalue" );
    assert ( stringvar == "present" );

    var stringvar2 = smk.stringValue( "section1", "notpresent", "defaultvalue" );
    assert ( stringvar2 == "defaultvalue" );

    var intvar = smk.intValue( "section2", "keypresent", 123456789 );
    assert ( intvar == 42 );

    var intvar2 = smk.intValue( "section2", "notpresent", 123456789 );
    assert ( intvar2 == 123456789 );

    var boolvar = smk.boolValue( "section3", "keypresent", false );
    assert ( boolvar );

    var boolvar2 = smk.boolValue( "section3", "notpresent", true );
    assert ( boolvar2 );

    var stringlistvar = smk.stringListValue( "section4", "keypresent", { "these", "values", "irrelevant" } );
    assert ( stringlistvar.length == 4 );
    assert ( stringlistvar[0] == "foo" );
    assert ( stringlistvar[1] == "bar" );
    assert ( stringlistvar[2] == "yo" );
    assert ( stringlistvar[3] == "kurt" );

    var stringlistvar2 = smk.stringListValue( "section4", "notpresent", { "these", "values", "default" } );
    assert ( stringlistvar2.length == 3 );
    assert ( stringlistvar2[0] == "these" );
    assert ( stringlistvar2[1] == "values" );
    assert ( stringlistvar2[2] == "default" );

    var stringlistvar3 = smk.stringListValue( "section4", "stillNotPresent" );
    assert ( stringlistvar3 == null );

}

//===========================================================================
void test_smartkeyfile_values_generics()
//===========================================================================
{

    var smk = new SmartKeyFile();
    var ok = smk.loadFromFile( TEST_FILE_NAME );
    assert( ok );

    /* enable when generics work again
    var stringvar = smk.getValue<string>( "section1", "keypresent", "defaultvalue" );
    assert ( stringvar == "present" );
    */

    var stringvar2 = smk.stringValue( "section1", "notpresent", "defaultvalue" );
    assert ( stringvar2 == "defaultvalue" );

    var intvar = smk.intValue( "section2", "keypresent", 123456789 );
    assert ( intvar == 42 );

    var intvar2 = smk.intValue( "section2", "notpresent", 123456789 );
    assert ( intvar2 == 123456789 );

    var boolvar = smk.boolValue( "section3", "keypresent", false );
    assert ( boolvar );

    var boolvar2 = smk.boolValue( "section3", "notpresent", true );
    assert ( boolvar2 );

    var stringlistvar = smk.stringListValue( "section4", "keypresent", { "these", "values", "irrelevant" } );
    assert ( stringlistvar.length == 4 );
    assert ( stringlistvar[0] == "foo" );
    assert ( stringlistvar[1] == "bar" );
    assert ( stringlistvar[2] == "yo" );
    assert ( stringlistvar[3] == "kurt" );

    var stringlistvar2 = smk.stringListValue( "section4", "notpresent", { "these", "values", "default" } );
    assert ( stringlistvar2.length == 3 );
    assert ( stringlistvar2[0] == "these" );
    assert ( stringlistvar2[1] == "values" );
    assert ( stringlistvar2[2] == "default" );

    var stringlistvar3 = smk.stringListValue( "section4", "stillNotPresent" );
    assert ( stringlistvar3 == null );
}

//===========================================================================
void test_smartkeyfile_sections()
//===========================================================================
{
    var smk = new SmartKeyFile();
    var ok = smk.loadFromFile( TEST_FILE_NAME );
    assert( ok );

    assert ( smk.hasSection( "section0" ) );
    assert ( !smk.hasSection( "this.section.not.there" ) );

    var sections = smk.sectionsWithPrefix();
    assert ( sections.length() == 9 );
    assert ( sections.nth_data(0) == "section0" );
    assert ( sections.nth_data(8) == "foo.bar" );

    var foosections = smk.sectionsWithPrefix( "foo" );
    foreach ( var section in foosections )
        assert ( section.has_prefix( "foo" ) );
    assert ( foosections.length() == 4 );
    assert ( foosections.nth_data(0) == "foo.1" );
    assert ( foosections.nth_data(3) == "foo.bar" );

    var nosections = smk.sectionsWithPrefix( "this.section.not.present" );
    assert ( nosections.length() == 0 );
}

//===========================================================================
void test_smartkeyfile_keys()
//===========================================================================
{
    var smk = new SmartKeyFile();
    var ok = smk.loadFromFile( TEST_FILE_NAME );
    assert( ok );

    assert ( smk.hasKey( "section1", "keypresent" ) );
    assert ( !smk.hasKey( "section.not.present", "this.key.not.there" ) );
    assert ( !smk.hasKey( "section1", "this.key.not.there" ) );

    var keys = smk.keysWithPrefix( "section4" );
    assert ( keys.length() == 5 );
    assert ( keys.nth_data(0) == "keypresent" );
    assert ( keys.nth_data(4) == "yo4" );

    var yokeys = smk.keysWithPrefix( "section4", "yo" );
    foreach ( var key in yokeys )
        assert ( key.has_prefix( "yo" ) );
    assert ( yokeys.length() == 4 );
    assert ( yokeys.nth_data(0) == "yo1" );
    assert ( yokeys.nth_data(3) == "yo4" );

    var nokeys = smk.keysWithPrefix( "this.key.not.present" );
    assert ( nokeys.length() == 0 );
}

//===========================================================================
void main (string[] args)
//===========================================================================
{
    Test.init (ref args);

    // SmartKeyFile creation

    Test.add_func ("/SmartKeyFile/Values", test_smartkeyfile_values);
    Test.add_func ("/SmartKeyFile/Values/Generics", test_smartkeyfile_values_generics);
    Test.add_func ("/SmartKeyFile/Sections", test_smartkeyfile_sections);
    Test.add_func ("/SmartKeyFile/Keys", test_smartkeyfile_keys);

    Test.run ();
}