o-inifile 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +12 -0
- data/History.txt +59 -0
- data/README.md +243 -0
- data/Rakefile +24 -0
- data/inifile.gemspec +24 -0
- data/lib/inifile.rb +682 -0
- data/lib/o-inifile.rb +1 -0
- data/test/data/bad_1.ini +6 -0
- data/test/data/bad_2.ini +11 -0
- data/test/data/browscap.ini +5 -0
- data/test/data/comment.ini +10 -0
- data/test/data/continuation.ini +6 -0
- data/test/data/escape.ini +13 -0
- data/test/data/force_array.ini +5 -0
- data/test/data/global.ini +3 -0
- data/test/data/good.ini +19 -0
- data/test/data/line_continuation.ini +13 -0
- data/test/data/merge.ini +5 -0
- data/test/data/mixed_comment.ini +7 -0
- data/test/data/multiline.ini +24 -0
- data/test/data/param.ini +5 -0
- data/test/data/section.ini +4 -0
- data/test/data/switch.ini +3 -0
- data/test/data/typecast.ini +7 -0
- data/test/test_inifile.rb +629 -0
- metadata +117 -0
data/lib/o-inifile.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'inifile'
|
data/test/data/bad_1.ini
ADDED
data/test/data/bad_2.ini
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
; this test file demonstrates escape sequences supported by IniFile
|
2
|
+
[normal]
|
3
|
+
foo = http://en.wikipedia.org/wiki/Foobar
|
4
|
+
|
5
|
+
[escaped]
|
6
|
+
tabs = There is a tab\tcharacter in here somewhere
|
7
|
+
carriage return = Who uses these anyways?\r
|
8
|
+
newline = Trust newline!\nAlways there when you need him.\nSplittin' those lines.
|
9
|
+
null = Who'd be silly enough to put\0 a null character in the middle of a string? \
|
10
|
+
Stroustrup would not approve!
|
11
|
+
backslash = This string \\t contains \\n no \\r special \\0 characters!
|
12
|
+
quoted = "Escaping works\tinside quoted strings!"
|
13
|
+
|
data/test/data/good.ini
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
[section_one]
|
2
|
+
one = 1
|
3
|
+
two = 2
|
4
|
+
|
5
|
+
[section_two]
|
6
|
+
three = -3
|
7
|
+
multi = multiline \
|
8
|
+
support
|
9
|
+
|
10
|
+
; comments should be ignored
|
11
|
+
[section three]
|
12
|
+
four =true
|
13
|
+
five=false # comments can go here
|
14
|
+
six =6.0 ; and here, too
|
15
|
+
|
16
|
+
[section_four]
|
17
|
+
[section_five]
|
18
|
+
seven and eight= 7 & 8
|
19
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
; this test file demonstrates escape sequences supported by IniFile
|
2
|
+
[normal]
|
3
|
+
foo = http://en.wikipedia.org/wiki/Foobar
|
4
|
+
|
5
|
+
[escaped]
|
6
|
+
tabs = There is a tab\tcharacter in here somewhere
|
7
|
+
carriage return = Who uses these anyways?\r
|
8
|
+
newline = Trust newline!\nAlways there when you need him.\nSplittin' those lines.
|
9
|
+
null = Who'd be silly enough to put\0 a null character in the middle of a string?
|
10
|
+
line_continuation = C:\Temp\path\
|
11
|
+
backslash = This string \\t contains \\n no \\r special \\0 characters!
|
12
|
+
quoted = "Escaping works\tinside quoted strings!"
|
13
|
+
|
data/test/data/merge.ini
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
[section_one]
|
2
|
+
one = 1
|
3
|
+
two = 2
|
4
|
+
|
5
|
+
[section_two]
|
6
|
+
three = 3
|
7
|
+
|
8
|
+
; comments should be ignored
|
9
|
+
[section_three]
|
10
|
+
three = hello \
|
11
|
+
multiline
|
12
|
+
other = "stuff"
|
13
|
+
|
14
|
+
[section_four]
|
15
|
+
four = hello \ # comments work here, too
|
16
|
+
multiple \ # and here !!!
|
17
|
+
multilines # and even here (OMG)
|
18
|
+
five = "multiple lines
|
19
|
+
inside of quotations
|
20
|
+
preserve everything"
|
21
|
+
|
22
|
+
[empty_lines]
|
23
|
+
empty =
|
24
|
+
not_empty=full
|
data/test/data/param.ini
ADDED
@@ -0,0 +1,629 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
libpath = File.expand_path '../../lib', __FILE__
|
4
|
+
require File.join(libpath, 'inifile')
|
5
|
+
require 'fileutils'
|
6
|
+
require 'test/unit'
|
7
|
+
|
8
|
+
|
9
|
+
class TestIniFile < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@ini_file = IniFile.new(:filename => 'test/data/good.ini')
|
13
|
+
@contents = [
|
14
|
+
['section_one', 'one', 1],
|
15
|
+
['section_one', 'two', 2],
|
16
|
+
['section_two', 'three', -3],
|
17
|
+
['section_two', 'multi', "multiline support"],
|
18
|
+
['section three', 'four', true],
|
19
|
+
['section three', 'five', false],
|
20
|
+
['section three', 'six', 6.0],
|
21
|
+
['section_five', 'seven and eight', '7 & 8']
|
22
|
+
].sort
|
23
|
+
|
24
|
+
FileUtils.rm_rf "test/data/tmp.ini"
|
25
|
+
FileUtils.cp "test/data/good.ini", "test/data/tmp.ini"
|
26
|
+
end
|
27
|
+
|
28
|
+
def teardown
|
29
|
+
FileUtils.rm_rf "test/data/tmp.ini"
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_class_load
|
33
|
+
ini_file = IniFile.load 'test/data/good.ini'
|
34
|
+
assert_instance_of IniFile, ini_file
|
35
|
+
|
36
|
+
# see if we can parse different style comments
|
37
|
+
assert_raise(IniFile::Error) {IniFile.load 'test/data/comment.ini', :comment => ';'}
|
38
|
+
|
39
|
+
ini_file = IniFile.load 'test/data/comment.ini', :comment => '#'
|
40
|
+
assert_instance_of IniFile, ini_file
|
41
|
+
|
42
|
+
# see if we can parse mixed style comments
|
43
|
+
assert_raise(IniFile::Error) {IniFile.load 'test/data/mixed_comment.ini', :comment => '#'}
|
44
|
+
|
45
|
+
ini_file = IniFile.load 'test/data/mixed_comment.ini', :comment => ';#'
|
46
|
+
assert_instance_of IniFile, ini_file
|
47
|
+
|
48
|
+
# see if we can parse different style param separators
|
49
|
+
assert_raise(IniFile::Error) {IniFile.load 'test/data/param.ini'}
|
50
|
+
|
51
|
+
ini_file = IniFile.load 'test/data/param.ini', :parameter => ':'
|
52
|
+
assert_instance_of IniFile, ini_file
|
53
|
+
|
54
|
+
# check that backslash line continuation can be ignored by option
|
55
|
+
ini_file = IniFile.load 'test/data/line_continuation.ini', :continuation => false
|
56
|
+
assert_equal ini_file["escaped"]["line_continuation"], "C:\\Temp\\path\\"
|
57
|
+
|
58
|
+
# make sure we error out on files with bad lines
|
59
|
+
assert_raise(IniFile::Error) {IniFile.load 'test/data/bad_1.ini'}
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_clone
|
63
|
+
clone = @ini_file.clone
|
64
|
+
assert_equal @ini_file, clone
|
65
|
+
assert !clone.tainted?
|
66
|
+
assert !clone.frozen?
|
67
|
+
|
68
|
+
# the clone should be completely independent of the original
|
69
|
+
clone['new_section']['one'] = 1
|
70
|
+
assert_not_equal @ini_file, clone
|
71
|
+
|
72
|
+
# the tainted state is copied to clones
|
73
|
+
@ini_file.taint
|
74
|
+
assert @ini_file.tainted?
|
75
|
+
|
76
|
+
clone = @ini_file.clone
|
77
|
+
assert clone.tainted?
|
78
|
+
|
79
|
+
# the frozen state is also copied to clones
|
80
|
+
@ini_file.freeze
|
81
|
+
assert @ini_file.frozen?
|
82
|
+
|
83
|
+
clone = @ini_file.clone
|
84
|
+
assert clone.tainted?
|
85
|
+
assert clone.frozen?
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_delete_section
|
89
|
+
assert_nil @ini_file.delete_section('section_nil')
|
90
|
+
|
91
|
+
h = {'one' => 1, 'two' => 2}
|
92
|
+
assert_equal true, @ini_file.has_section?('section_one')
|
93
|
+
assert_equal h, @ini_file.delete_section('section_one')
|
94
|
+
assert_equal false, @ini_file.has_section?('section_one')
|
95
|
+
assert_nil @ini_file.delete_section('section_one')
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_dup
|
99
|
+
dup = @ini_file.dup
|
100
|
+
assert_equal @ini_file, dup
|
101
|
+
assert !dup.tainted?
|
102
|
+
assert !dup.frozen?
|
103
|
+
|
104
|
+
# the duplicate should be completely independent of the original
|
105
|
+
dup['new_section']['one'] = 1
|
106
|
+
assert_not_equal @ini_file, dup
|
107
|
+
|
108
|
+
# the tainted state is copied to duplicates
|
109
|
+
@ini_file.taint
|
110
|
+
assert @ini_file.tainted?
|
111
|
+
|
112
|
+
dup = @ini_file.dup
|
113
|
+
assert dup.tainted?
|
114
|
+
|
115
|
+
# the frozen state, however, is not
|
116
|
+
@ini_file.freeze
|
117
|
+
assert @ini_file.frozen?
|
118
|
+
|
119
|
+
dup = @ini_file.dup
|
120
|
+
assert dup.tainted?
|
121
|
+
assert !dup.frozen?
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_each
|
125
|
+
ary = []
|
126
|
+
@ini_file.each {|*args| ary << args}
|
127
|
+
|
128
|
+
assert_equal @contents, ary.sort
|
129
|
+
|
130
|
+
ary = []
|
131
|
+
IniFile.new(:filename => 'temp.ini').each {|*args| ary << args}
|
132
|
+
assert_equal [], ary
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_each_section
|
136
|
+
expected = [
|
137
|
+
'section_one', 'section_two', 'section three',
|
138
|
+
'section_four', 'section_five'
|
139
|
+
].sort
|
140
|
+
|
141
|
+
ary = []
|
142
|
+
@ini_file.each_section {|section| ary << section}
|
143
|
+
|
144
|
+
assert_equal expected, ary.sort
|
145
|
+
|
146
|
+
ary = []
|
147
|
+
IniFile.new(:filename => 'temp.ini').each_section {|section| ary << section}
|
148
|
+
assert_equal [], ary
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_eql_eh
|
152
|
+
assert @ini_file.eql?(@ini_file)
|
153
|
+
assert @ini_file.eql?(@ini_file.clone)
|
154
|
+
assert !@ini_file.eql?('string')
|
155
|
+
assert !@ini_file.eql?(IniFile.new(:content => ''))
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_freeze
|
159
|
+
assert_equal false, @ini_file.frozen?
|
160
|
+
@ini_file.each_section do |s|
|
161
|
+
assert_equal false, @ini_file[s].frozen?
|
162
|
+
end
|
163
|
+
|
164
|
+
@ini_file.freeze
|
165
|
+
|
166
|
+
assert_equal true, @ini_file.frozen?
|
167
|
+
@ini_file.each_section do |s|
|
168
|
+
assert_equal true, @ini_file[s].frozen?
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_has_section_eh
|
173
|
+
assert_equal true, @ini_file.has_section?('section_one')
|
174
|
+
assert_equal false, @ini_file.has_section?('section_ten')
|
175
|
+
assert_equal true, @ini_file.has_section?(:section_two)
|
176
|
+
assert_equal false, @ini_file.has_section?(nil)
|
177
|
+
|
178
|
+
ini_file = IniFile.new(:filename => 'temp.ini')
|
179
|
+
assert_equal false, ini_file.has_section?('section_one')
|
180
|
+
assert_equal false, ini_file.has_section?('one')
|
181
|
+
assert_equal false, ini_file.has_section?('two')
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_index
|
185
|
+
expected = {
|
186
|
+
'one' => 1,
|
187
|
+
'two' => 2
|
188
|
+
}
|
189
|
+
assert_equal expected, @ini_file[:section_one]
|
190
|
+
|
191
|
+
expected = {'three' => -3, 'multi' => "multiline support"}
|
192
|
+
assert_equal expected, @ini_file['section_two']
|
193
|
+
|
194
|
+
expected = {
|
195
|
+
'four' => true,
|
196
|
+
'five' => false,
|
197
|
+
'six' => 6.0,
|
198
|
+
}
|
199
|
+
assert_equal expected, @ini_file['section three']
|
200
|
+
|
201
|
+
expected = {}
|
202
|
+
assert_equal expected, @ini_file['section_four']
|
203
|
+
|
204
|
+
expected = {'seven and eight' => '7 & 8'}
|
205
|
+
assert_equal expected, @ini_file['section_five']
|
206
|
+
|
207
|
+
expected = {}
|
208
|
+
assert_equal expected, @ini_file['section_six']
|
209
|
+
|
210
|
+
assert_nil @ini_file[nil]
|
211
|
+
|
212
|
+
expected = {}
|
213
|
+
ini_file = IniFile.new(:filename => 'temp.ini')
|
214
|
+
assert_equal expected, ini_file['section_one']
|
215
|
+
assert_equal expected, ini_file['one']
|
216
|
+
assert_nil ini_file[nil]
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_match
|
220
|
+
expected = {
|
221
|
+
"section_two" => {
|
222
|
+
"three" => -3, "multi" => "multiline support"
|
223
|
+
},
|
224
|
+
"section three" => {
|
225
|
+
"four" => true, "five"=> false, "six" => 6.0
|
226
|
+
}
|
227
|
+
}
|
228
|
+
assert_equal expected, @ini_file.match(/(two|three)/)
|
229
|
+
|
230
|
+
# the match function should not delete entries from the inifile hash
|
231
|
+
assert_equal({'seven and eight' => '7 & 8'}, @ini_file['section_five'])
|
232
|
+
|
233
|
+
expected = {}
|
234
|
+
assert_equal expected, @ini_file.match(/houndreds/)
|
235
|
+
end
|
236
|
+
|
237
|
+
def test_initialize
|
238
|
+
# see if we can parse different style comments
|
239
|
+
#assert_raise(IniFile::Error) {IniFile.new 'test/data/comment.ini'}
|
240
|
+
|
241
|
+
ini_file = IniFile.new(:filename => 'test/data/comment.ini', :comment => '#')
|
242
|
+
assert ini_file.has_section?('section_one')
|
243
|
+
assert_equal '20 + 22 = 42', ini_file['section_two']['multi']
|
244
|
+
|
245
|
+
# see if we can parse different style param separators
|
246
|
+
assert_raise(IniFile::Error) {IniFile.new(:filename => 'test/data/param.ini')}
|
247
|
+
|
248
|
+
ini_file = IniFile.new(:filename => 'test/data/param.ini', :parameter => ':')
|
249
|
+
assert ini_file.has_section?('section_one')
|
250
|
+
assert_equal 1, ini_file['section_one']['one']
|
251
|
+
assert_equal 2, ini_file['section_one']['two']
|
252
|
+
|
253
|
+
# make sure we error out on files with bad lines
|
254
|
+
assert_raise(IniFile::Error) {IniFile.new :filename => 'test/data/bad_1.ini'}
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_initialize_from_string_without_ending_newline
|
258
|
+
content = "[section_one]\n foo=bar"
|
259
|
+
ini_file = IniFile.new(:content => content)
|
260
|
+
assert ini_file.has_section?('section_one')
|
261
|
+
assert_equal 'bar', ini_file['section_one']['foo']
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_initialize_from_string
|
265
|
+
content = File.read('test/data/good.ini')
|
266
|
+
|
267
|
+
ini_file = IniFile.new(:content => content, :comment => ';')
|
268
|
+
assert ini_file.has_section?('section_one')
|
269
|
+
assert ini_file.has_section?('section_two')
|
270
|
+
assert ini_file.has_section?('section three')
|
271
|
+
assert ini_file.has_section?('section_four')
|
272
|
+
assert ini_file.has_section?('section_five')
|
273
|
+
|
274
|
+
assert_equal '7 & 8', ini_file['section_five']['seven and eight']
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_initialize_from_hash
|
278
|
+
hash = {
|
279
|
+
'value' => 'bat',
|
280
|
+
'section one' => {
|
281
|
+
'foo' => 'bar',
|
282
|
+
'baz' => 'buz'
|
283
|
+
},
|
284
|
+
'colors' => {
|
285
|
+
'perrywinkle' => '7e6ff3',
|
286
|
+
'steelblue' => '4682b4'
|
287
|
+
},
|
288
|
+
'empty' => nil
|
289
|
+
}
|
290
|
+
|
291
|
+
ini_file = IniFile.new(:content => hash)
|
292
|
+
assert ini_file.has_section?('section one')
|
293
|
+
assert ini_file.has_section?('colors')
|
294
|
+
assert ini_file.has_section?('empty')
|
295
|
+
|
296
|
+
assert_equal %w[baz foo], ini_file['section one'].keys.sort
|
297
|
+
assert_equal 'bar', ini_file['section one']['foo']
|
298
|
+
assert_equal 'buz', ini_file['section one']['baz']
|
299
|
+
|
300
|
+
assert_equal %w[perrywinkle steelblue], ini_file['colors'].keys.sort
|
301
|
+
assert_equal '7e6ff3', ini_file['colors']['perrywinkle']
|
302
|
+
assert_equal '4682b4', ini_file['colors']['steelblue']
|
303
|
+
|
304
|
+
assert_equal 'bat', ini_file['global']['value']
|
305
|
+
|
306
|
+
assert_empty ini_file['empty']
|
307
|
+
end
|
308
|
+
|
309
|
+
def test_to_s
|
310
|
+
hash = {
|
311
|
+
'one' => 'bar',
|
312
|
+
'two' => 'bat',
|
313
|
+
'section one' => {
|
314
|
+
'foo' => 'baz',
|
315
|
+
}
|
316
|
+
}
|
317
|
+
|
318
|
+
ini_file = IniFile.new(:content => hash)
|
319
|
+
|
320
|
+
assert_equal "one = bar\ntwo = bat\n[section one]\nfoo = baz\n", ini_file.to_s
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_sections
|
324
|
+
expected = [
|
325
|
+
'section_one', 'section_two', 'section three',
|
326
|
+
'section_four', 'section_five'
|
327
|
+
].sort
|
328
|
+
|
329
|
+
assert_equal expected, @ini_file.sections.sort
|
330
|
+
|
331
|
+
ini_file = IniFile.new(:filename => 'temp.ini')
|
332
|
+
assert_equal [], ini_file.sections
|
333
|
+
end
|
334
|
+
|
335
|
+
def test_taint
|
336
|
+
assert_equal false, @ini_file.tainted?
|
337
|
+
@ini_file.each_section do |s|
|
338
|
+
assert_equal false, @ini_file[s].tainted?
|
339
|
+
end
|
340
|
+
|
341
|
+
@ini_file.taint
|
342
|
+
|
343
|
+
assert_equal true, @ini_file.tainted?
|
344
|
+
@ini_file.each_section do |s|
|
345
|
+
assert_equal true, @ini_file[s].tainted?
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_write
|
350
|
+
tmp = 'test/data/temp.ini'
|
351
|
+
File.delete tmp if Kernel.test(?f, tmp)
|
352
|
+
|
353
|
+
@ini_file.save(:filename => tmp)
|
354
|
+
assert_equal true, Kernel.test(?f, tmp)
|
355
|
+
|
356
|
+
File.delete tmp if Kernel.test(?f, tmp)
|
357
|
+
|
358
|
+
ini_file = IniFile.new(:filename => tmp)
|
359
|
+
ini_file.save
|
360
|
+
assert_nil Kernel.test(?s, tmp)
|
361
|
+
|
362
|
+
File.delete tmp if Kernel.test(?f, tmp)
|
363
|
+
end
|
364
|
+
|
365
|
+
def test_read
|
366
|
+
assert @ini_file.has_section?('section_one')
|
367
|
+
|
368
|
+
@ini_file['section_one']['one'] = 42
|
369
|
+
@ini_file['section_one']['two'] = 42
|
370
|
+
assert_equal 42, @ini_file['section_one']['one']
|
371
|
+
assert_equal 42, @ini_file['section_one']['two']
|
372
|
+
|
373
|
+
@ini_file.read
|
374
|
+
assert_equal 1, @ini_file['section_one']['one']
|
375
|
+
assert_equal 2, @ini_file['section_one']['two']
|
376
|
+
|
377
|
+
@ini_file.read(:filename => 'test/data/mixed_comment.ini')
|
378
|
+
assert_equal false, @ini_file.has_section?('section_two')
|
379
|
+
end
|
380
|
+
|
381
|
+
def test_modifies_current_keys
|
382
|
+
ini = IniFile.load("test/data/tmp.ini")
|
383
|
+
ini["section one"]["one"] = 17
|
384
|
+
ini.save
|
385
|
+
|
386
|
+
assert File.read("test/data/tmp.ini") =~ /one = 17/
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_can_add_key_to_inifile
|
390
|
+
ini_file = IniFile.new(:filename => "test/data/tmp.ini")
|
391
|
+
ini_file["new_section"] = {}
|
392
|
+
ini_file.save
|
393
|
+
|
394
|
+
assert File.read("test/data/tmp.ini") =~ /\[new_section\]/
|
395
|
+
end
|
396
|
+
|
397
|
+
def test_adds_correct_key_to_inifile
|
398
|
+
ini_file = IniFile.new(:filename => "test/data/tmp.ini")
|
399
|
+
ini_file["foo"] = {}
|
400
|
+
ini_file.save
|
401
|
+
|
402
|
+
assert File.read("test/data/tmp.ini") =~ /\[foo\]/
|
403
|
+
end
|
404
|
+
|
405
|
+
def test_assigns_values_to_inifile
|
406
|
+
ini_file = IniFile.new(:filename => "test/data/tmp.ini")
|
407
|
+
ini_file["foo"] = {
|
408
|
+
:bar => "baz"
|
409
|
+
}
|
410
|
+
|
411
|
+
assert_equal ini_file["foo"], { :bar => "baz" }
|
412
|
+
end
|
413
|
+
|
414
|
+
def test_assigns_correct_values_to_inifile
|
415
|
+
ini_file = IniFile.new(:filename => "test/data/tmp.ini")
|
416
|
+
ini_file["foo"] = {
|
417
|
+
:one => "two"
|
418
|
+
}
|
419
|
+
|
420
|
+
assert_equal ini_file["foo"], { :one => "two" }
|
421
|
+
end
|
422
|
+
|
423
|
+
def test_assignment_stringifies_key
|
424
|
+
ini_file = IniFile.new(:filename => "test/data/tmp.ini")
|
425
|
+
ini_file["foo"] = {:one => :two}
|
426
|
+
ini_file[:foo] = {}
|
427
|
+
assert_equal ini_file["foo"], {}
|
428
|
+
end
|
429
|
+
|
430
|
+
def test_multiline_parsing
|
431
|
+
ini_file = IniFile.load('test/data/multiline.ini')
|
432
|
+
|
433
|
+
multiline = ini_file['section_three']
|
434
|
+
expected = {"three" => "hello multiline", "other" => "stuff"}
|
435
|
+
assert_equal expected, multiline
|
436
|
+
|
437
|
+
multiple = ini_file['section_four']
|
438
|
+
expected = {"four" => "hello multiple multilines",
|
439
|
+
"five" => "multiple lines\ninside of quotations\npreserve everything" }
|
440
|
+
assert_equal expected, multiple
|
441
|
+
|
442
|
+
multiple = ini_file['empty_lines']
|
443
|
+
expected = {'empty' => nil, 'not_empty' => 'full'}
|
444
|
+
assert_equal expected, multiple
|
445
|
+
end
|
446
|
+
|
447
|
+
def test_merge
|
448
|
+
ini_file = @ini_file.merge(IniFile.load("test/data/merge.ini"))
|
449
|
+
assert_equal 3, ini_file['section_one']['one']
|
450
|
+
assert_equal 2, ini_file['section_one']['two']
|
451
|
+
|
452
|
+
# make sure that the rest haven't changed
|
453
|
+
assert_equal(-3, ini_file['section_two']['three'])
|
454
|
+
|
455
|
+
# and that we got any additional sections too
|
456
|
+
assert_equal 5, ini_file['section_five']['five']
|
457
|
+
|
458
|
+
# original object is unchanged
|
459
|
+
assert_equal 1, @ini_file['section_one']['one']
|
460
|
+
end
|
461
|
+
|
462
|
+
def test_merge_hash
|
463
|
+
ini_file = @ini_file.merge({
|
464
|
+
'section_one' => { 'one' => 3 },
|
465
|
+
'section_five' => { 'five' => 5 }
|
466
|
+
})
|
467
|
+
assert_equal 3, ini_file['section_one']['one']
|
468
|
+
assert_equal 2, ini_file['section_one']['two']
|
469
|
+
|
470
|
+
# make sure that the rest haven't changed
|
471
|
+
assert_equal(-3, ini_file['section_two']['three'])
|
472
|
+
|
473
|
+
# and that we got any additional sections too
|
474
|
+
assert_equal 5, ini_file['section_five']['five']
|
475
|
+
|
476
|
+
# original object is unchanged
|
477
|
+
assert_equal 1, @ini_file['section_one']['one']
|
478
|
+
end
|
479
|
+
|
480
|
+
def test_merge_invalid_hash
|
481
|
+
bad_hash = { 'section_one' => [1, 2, 3, 4] }
|
482
|
+
assert_raise(IniFile::Error) { @ini_file.merge(bad_hash) }
|
483
|
+
|
484
|
+
not_a_hash = [['section_one', ['foo','bar'], ['baz', 'buz']]]
|
485
|
+
assert_raise(IniFile::Error) { @ini_file.merge(not_a_hash) }
|
486
|
+
|
487
|
+
ini_file = @ini_file.merge nil
|
488
|
+
assert ini_file.eql?(@ini_file)
|
489
|
+
end
|
490
|
+
|
491
|
+
def test_parse_encoding
|
492
|
+
ini_file = IniFile.new(:filename => "test/data/browscap.ini", :encoding => 'ISO-8859-1')
|
493
|
+
assert_equal ini_file['www.substancia.com AutoHTTPAgent (ver *)']['Browser'], "Subst\xE2ncia".force_encoding('ISO-8859-1')
|
494
|
+
end
|
495
|
+
|
496
|
+
def test_write_encoding
|
497
|
+
tmp = 'test/data/tmp.ini'
|
498
|
+
File.delete tmp if Kernel.test(?f, tmp)
|
499
|
+
|
500
|
+
@ini_file = IniFile.new(:filename => tmp, :encoding => 'UTF-8')
|
501
|
+
@ini_file['testutf-8'] = {"utf-8" => "appr\u20accier"}
|
502
|
+
|
503
|
+
@ini_file.save(:filename => tmp)
|
504
|
+
|
505
|
+
test = File.open(tmp)
|
506
|
+
assert_equal test.external_encoding.to_s, 'UTF-8'
|
507
|
+
end
|
508
|
+
|
509
|
+
def test_value_escaping
|
510
|
+
ini_file = IniFile.load('test/data/escape.ini')
|
511
|
+
escaped = ini_file['escaped']
|
512
|
+
|
513
|
+
assert_equal %Q{There is a tab\tcharacter in here somewhere}, escaped['tabs']
|
514
|
+
assert_equal %Q{Who uses these anyways?\r}, escaped['carriage return']
|
515
|
+
assert_equal %Q{Trust newline!\nAlways there when you need him.\nSplittin' those lines.}, escaped['newline']
|
516
|
+
assert_equal %Q{Who'd be silly enough to put\0 a null character in the middle of a string? Stroustrup would not approve!}, escaped['null']
|
517
|
+
assert_equal %q{This string \t contains \n no \r special \0 characters!}, escaped['backslash']
|
518
|
+
assert_equal %Q{Escaping works\tinside quoted strings!}, escaped['quoted']
|
519
|
+
end
|
520
|
+
|
521
|
+
# disabling escaping is no longer supported
|
522
|
+
def test_value_escaping_disabled
|
523
|
+
ini_file = IniFile.load('test/data/escape.ini', :escape => false)
|
524
|
+
escaped = ini_file['escaped']
|
525
|
+
|
526
|
+
assert_equal %Q{There is a tab\tcharacter in here somewhere}, escaped['tabs']
|
527
|
+
assert_equal %Q{Who uses these anyways?\r}, escaped['carriage return']
|
528
|
+
assert_equal %Q{Trust newline!\nAlways there when you need him.\nSplittin' those lines.}, escaped['newline']
|
529
|
+
assert_equal %Q{Who'd be silly enough to put\0 a null character in the middle of a string? Stroustrup would not approve!}, escaped['null']
|
530
|
+
assert_equal %q{This string \t contains \n no \r special \0 characters!}, escaped['backslash']
|
531
|
+
assert_equal %Q{Escaping works\tinside quoted strings!}, escaped['quoted']
|
532
|
+
end
|
533
|
+
|
534
|
+
def test_global_section
|
535
|
+
ini_file = IniFile.load('test/data/global.ini')
|
536
|
+
|
537
|
+
assert_equal %w[global], ini_file.sections
|
538
|
+
assert_equal 1, ini_file['global']['one']
|
539
|
+
assert_equal 2, ini_file['global']['two']
|
540
|
+
end
|
541
|
+
|
542
|
+
def test_default_global_section
|
543
|
+
ini_file = IniFile.load('test/data/global.ini', :default => 'nonce')
|
544
|
+
|
545
|
+
assert_equal %w[nonce], ini_file.sections
|
546
|
+
assert_equal 1, ini_file['nonce']['one']
|
547
|
+
assert_equal 2, ini_file['nonce']['two']
|
548
|
+
end
|
549
|
+
|
550
|
+
def test_unescaped_section_header_as_value
|
551
|
+
ini_file = IniFile.load('test/data/section.ini')
|
552
|
+
|
553
|
+
assert_equal %w[section_one], ini_file.sections
|
554
|
+
assert_equal '[value]', ini_file['section_one']['one']
|
555
|
+
assert_equal 2, ini_file['section_one']['two']
|
556
|
+
end
|
557
|
+
|
558
|
+
def test_unmatched_quotes
|
559
|
+
# missing a closing quote should raise an error
|
560
|
+
assert_raise(IniFile::Error) { IniFile.load 'test/data/bad_2.ini' }
|
561
|
+
end
|
562
|
+
|
563
|
+
def test_continuation_at_end_of_file
|
564
|
+
ini_file = IniFile.load('test/data/continuation.ini')
|
565
|
+
|
566
|
+
assert_equal 1, ini_file['section_one']['one']
|
567
|
+
assert_equal 2, ini_file['section_one']['two']
|
568
|
+
|
569
|
+
assert_equal 'here is the last value', ini_file['section_two']['end-of-file']
|
570
|
+
end
|
571
|
+
|
572
|
+
def test_empty_comment_string
|
573
|
+
ini_file = IniFile.load('test/data/merge.ini', :comment => nil)
|
574
|
+
|
575
|
+
assert_equal 3, ini_file['section_one']['one']
|
576
|
+
assert_equal 5, ini_file['section_five']['five']
|
577
|
+
end
|
578
|
+
|
579
|
+
def test_integer_typecast
|
580
|
+
ini_file = IniFile.load('test/data/typecast.ini')
|
581
|
+
assert_equal 146, ini_file['section_one']['int1']
|
582
|
+
assert_equal "00342", ini_file['section_one']['string1']
|
583
|
+
end
|
584
|
+
|
585
|
+
def test_float_typecast
|
586
|
+
ini_file = IniFile.load('test/data/typecast.ini')
|
587
|
+
assert_equal 0.35, ini_file['section_one']['float1']
|
588
|
+
assert_equal 0.634, ini_file['section_one']['float2']
|
589
|
+
assert_equal 234.646, ini_file['section_one']['float3']
|
590
|
+
assert_equal "345.", ini_file['section_one']['string2']
|
591
|
+
end
|
592
|
+
|
593
|
+
def test_switch_support_read
|
594
|
+
ini_file = IniFile.load('test/data/switch.ini')
|
595
|
+
|
596
|
+
assert_equal({}, ini_file['section_six']['switch_support'])
|
597
|
+
end
|
598
|
+
|
599
|
+
def test_switch_support_write
|
600
|
+
ini_file = IniFile.new(:filename => "test/data/tmp.ini")
|
601
|
+
ini_file["foo"] = {"switch_support" => {}}
|
602
|
+
ini_file.save
|
603
|
+
|
604
|
+
assert File.read("test/data/tmp.ini") =~ /^switch_support$/
|
605
|
+
end
|
606
|
+
end
|
607
|
+
|
608
|
+
class TestCustomIniFile < Test::Unit::TestCase
|
609
|
+
|
610
|
+
def setup
|
611
|
+
FileUtils.rm_rf "test/data/tmp.ini"
|
612
|
+
FileUtils.cp "test/data/good.ini", "test/data/tmp.ini"
|
613
|
+
end
|
614
|
+
|
615
|
+
def test_force_array
|
616
|
+
ini_file = IniFile.load('test/data/force_array.ini', force_array: true)
|
617
|
+
assert_equal 1, ini_file['section_one']['one']
|
618
|
+
assert_equal [2, "zinga", 'new foils'], ini_file['section_one']['two']
|
619
|
+
|
620
|
+
tmp = 'test/data/tmp.ini'
|
621
|
+
File.delete tmp if Kernel.test(?f, tmp)
|
622
|
+
|
623
|
+
ini_file.save(:filename => tmp)
|
624
|
+
|
625
|
+
lines = IO.readlines(tmp).map(&:chomp).keep_if{|x| x.size > 0}
|
626
|
+
assert_equal 5, lines.size
|
627
|
+
assert_equal ["[section_one]", "one = 1", "two = 2", "two = zinga", "two = new foils"], lines
|
628
|
+
end
|
629
|
+
end
|