File: inifile.test

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (242 lines) | stat: -rw-r--r-- 5,882 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# -*- tcl -*-
# Tests for module 'inifile'

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 1.0

testing {
    useLocal ini.tcl inifile
}

#---------------------------------------------------------------------

set inifile [localPath ini.tcl]
set testini [asset test.ini]
set sampini [asset sample.ini]

#---------------------------------------------------------------------

test inifile-1.1 {ini::open} {
    set res [ini::open $testini r]
    ini::close $res
    set res
} {ini0}

test inifile-1.2 {ini::sections} {
    set hdl [ini::open $testini r]
    set res [ini::sections $hdl]
    ini::close $hdl
    set res
} {emptysection section1 \{test section2}

test inifile-1.3 {ini::keys} {
    set hdl [ini::open $testini r]
    set res [ini::keys $hdl section1]
    ini::close $hdl
    set res
} {testkey key}

test inifile-1.4 {ini::keys} {
    set hdl [ini::open $testini r]
    set res [ini::keys $hdl \{test]
    ini::close $hdl
    set res
} {\}key}

test inifile-1.5 {ini::get} {
    set hdl [ini::open $testini r]
    set res [ini::get $hdl section1]
    ini::close $hdl
    set res
} {testkey hi key value}

test inifile-1.6 {ini::get} {
    set hdl [ini::open $testini r]
    set res [ini::get $hdl \{test]
    ini::close $hdl
    set res
} {\}key {$blah}}

test inifile-1.7 {ini::value} {
    set hdl [ini::open $testini r]
    set res [ini::value $hdl section1 key]
    ini::close $hdl
    set res
} {value}

test inifile-1.8 {ini::value} {
    set hdl [ini::open $testini r]
    set res [ini::value $hdl \{test \}key]
    ini::close $hdl
    set res
} {$blah}

test inifile-1.9 {ini::exists} {
    set hdl [ini::open $testini r]
    set res [ini::exists $hdl section1]
    ini::close $hdl
    set res
} {1}

test inifile-1.10 {ini::exists} {
    set hdl [ini::open $testini r]
    set res [ini::exists $hdl section]
    ini::close $hdl
    set res
} {0}

test inifile-1.11 {ini::exists} {
    set hdl [ini::open $testini r]
    set res [ini::exists $hdl section1 testkey]
    ini::close $hdl
    set res
} {1}

test inifile-1.12 {ini:::exists} {
    set hdl [ini::open $testini r]
    set res [ini::exists $hdl section1 blah]
    ini::close $hdl
    set res
} {0}

test inifile-1.13 {ini:::exists} {
    set hdl [ini::open $testini r]
    set res [ini::exists $hdl \{test]
    ini::close $hdl
    set res
} {1}

test inifile-1.14 {ini:::exists} {
    set hdl [ini::open $testini r]
    set res [ini::exists $hdl \{test \}key]
    ini::close $hdl
    set res
} {1}

#---------------------------------------------------------------------
# Tests for bug #1281136 --
set N 0
foreach name {nexthandle commentchar} {
    test inifile-2.$N {bug 1281136 - collision with global variable names} {
        set script {list [catch {
            array set ::%var {}
            source %file
        } err] $err}
        regsub {%file} $script $inifile script
        regsub {%var} $script $name script
        interp create slave0
        set r [slave0 eval $script]
        interp delete slave0
        set r
    } {0 {}}
    incr N
}
foreach name {data comments sections} {
    test inifile-2.$N {bug 1281136 - collision with global variable names} {
        set script {list [catch {
            ::set ::%var 0
            source %file
            set res [ini::open %testini r]
            ini::close $res
        } err] $err}
        foreach {s v} [list %file $inifile %var $name %testini $testini] {
            regsub $s $script $v script
        }
        interp create slave0
        set r [slave0 eval $script]
        interp delete slave0
        set r
    } {0 {}}
    incr N
}

#---------------------------------------------------------------------

test inifile-3.0 {bug 3612465, leading & trailing spaces} {
    set fh [ini::open $sampini]
    set res [ini::sections $fh]
    ini::close $fh
    unset fh
    set res
} General

test inifile-3.1 {bug 3612465, leading & trailing spaces} {
    set fh [ini::open $sampini]
    #set res [llength [ini::sections $fh]]
    set res [lsort -dict [ini::keys $fh General]]
    ini::close $fh
    unset fh
    set res
} {key key2}

#---------------------------------------------------------------------

test inifile-4.0 {bug c4b8162da5 - ini::open} {
    set res [ini::open $testini -encoding unicode r]
    ini::close $res
    set res
} {ini16}

# Test various error conditions.
test inifile-4.1 {bug c4b8162da5 - ini::open - invalid encoding} {
    catch {
	ini::open $testini -encoding foo r
    } res
    set res
} {unknown encoding "foo"}

test inifile-4.2 {bug c4b8162da5 - ini::open - invalid option} {
    catch {
	ini::open $testini -bogus foo r
    } res
    set res
} {Invalid option -bogus, expected -encoding}

test inifile-4.3 {bug c4b8162da5 - ini::open - invalid mode} {
    catch {
	ini::open $testini x
    } res
    set res
} {x is not a valid access mode}

test inifile-4.4 {bug c4b8162da5 - ini::open - invalid mode} {
    catch {
	set res [ini::open $testini w-]
    } res
    set res
} {w- is not a valid access mode}

#---------------------------------------------------------------------

test inifile-5.0 {ini::revert, wrong # args} {
    catch { ini::revert } res
    set res
} {wrong # args: should be "ini::revert fh"}

test inifile-5.1 {ini::revert, wrong # args} {
    catch { ini::revert FH X } res
    set res
} {wrong # args: should be "ini::revert fh"}

test inifile-5.2 {ini::revert, bad token} {
    catch { ini::revert FH } res
    set res
} {FH is not an open INI file}

test inifile-5.4 {ini::revert} {
    set res [ini::open $testini r]
    ini::revert $res
    ini::close $res
} {}

#---------------------------------------------------------------------
# Clean up
unset res
testsuiteCleanup
return