inifile_alt 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ [section_one]
2
+ one = 1
3
+ two = 2
4
+
5
+ ; the following is not a valid line
6
+ invalid line
@@ -0,0 +1,5 @@
1
+
2
+ ;just the offending entry
3
+ [www.substancia.com AutoHTTPAgent (ver *)]
4
+ Parent=Version Checkers
5
+ Browser=Subst�ncia
@@ -0,0 +1,10 @@
1
+ # comments should be ignored
2
+ [section_one]
3
+ one = 1
4
+ two = 2
5
+
6
+ [section_two] # you can comment here
7
+ one = 42 # and even here!
8
+ multi = 20 \ # and here, too
9
+ + 22 \= 42
10
+
@@ -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
+
@@ -0,0 +1,3 @@
1
+ ; without any section, a default 'global' section will be created
2
+ one = 1
3
+ two = 2
@@ -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 =4
13
+ five=5
14
+ six =6
15
+
16
+ [section_four]
17
+ [section_five]
18
+ seven and eight= 7 & 8
19
+
@@ -0,0 +1,5 @@
1
+ [section_one]
2
+ one = 3
3
+
4
+ [section_five]
5
+ five=5
@@ -0,0 +1,7 @@
1
+ # comments should be ignored
2
+ ; multiple comments characters are supported
3
+ ; (I'm lookin' at you, samba)
4
+ [section_one]
5
+ one = 1
6
+ two = 2
7
+
@@ -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
@@ -0,0 +1,4 @@
1
+ [unescaped_param]
2
+ one=key=value
3
+ two=key\=value
4
+ filters=fill=1,0.570093;\r
@@ -0,0 +1,5 @@
1
+ ; comments should be ignored
2
+ [section_one]
3
+ one : 1
4
+ two:2
5
+
@@ -0,0 +1,505 @@
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', '4'],
19
+ ['section three', 'five', '5'],
20
+ ['section three', 'six', '6'],
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
+ # make sure we error out on files with bad lines
55
+ assert_raise(IniFile::Error) {IniFile.load 'test/data/bad_1.ini'}
56
+
57
+ # make sure we error out on sloppy escaping
58
+ assert_raise(IniFile::Error) {IniFile.load 'test/data/no_escaping.ini'}
59
+
60
+ ini_file = IniFile.load 'test/data/no_escaping.ini', :escape => false
61
+ assert_instance_of IniFile, ini_file
62
+
63
+ end
64
+
65
+ def test_clone
66
+ clone = @ini_file.clone
67
+ assert_equal @ini_file, clone
68
+ assert !clone.tainted?
69
+ assert !clone.frozen?
70
+
71
+ # the clone should be completely independent of the original
72
+ clone['new_section']['one'] = 1
73
+ assert_not_equal @ini_file, clone
74
+
75
+ # the tainted state is copied to clones
76
+ @ini_file.taint
77
+ assert @ini_file.tainted?
78
+
79
+ clone = @ini_file.clone
80
+ assert clone.tainted?
81
+
82
+ # the frozen state is also copied to clones
83
+ @ini_file.freeze
84
+ assert @ini_file.frozen?
85
+
86
+ clone = @ini_file.clone
87
+ assert clone.tainted?
88
+ assert clone.frozen?
89
+ end
90
+
91
+ def test_delete_section
92
+ assert_nil @ini_file.delete_section('section_nil')
93
+
94
+ h = {'one' => '1', 'two' => '2'}
95
+ assert_equal true, @ini_file.has_section?('section_one')
96
+ assert_equal h, @ini_file.delete_section('section_one')
97
+ assert_equal false, @ini_file.has_section?('section_one')
98
+ assert_nil @ini_file.delete_section('section_one')
99
+ end
100
+
101
+ def test_dup
102
+ dup = @ini_file.dup
103
+ assert_equal @ini_file, dup
104
+ assert !dup.tainted?
105
+ assert !dup.frozen?
106
+
107
+ # the duplicate should be completely independent of the original
108
+ dup['new_section']['one'] = 1
109
+ assert_not_equal @ini_file, dup
110
+
111
+ # the tainted state is copied to duplicates
112
+ @ini_file.taint
113
+ assert @ini_file.tainted?
114
+
115
+ dup = @ini_file.dup
116
+ assert dup.tainted?
117
+
118
+ # the frozen state, however, is not
119
+ @ini_file.freeze
120
+ assert @ini_file.frozen?
121
+
122
+ dup = @ini_file.dup
123
+ assert dup.tainted?
124
+ assert !dup.frozen?
125
+ end
126
+
127
+ def test_each
128
+ ary = []
129
+ @ini_file.each {|*args| ary << args}
130
+
131
+ assert_equal @contents, ary.sort
132
+
133
+ ary = []
134
+ IniFile.new(:filename => 'temp.ini').each {|*args| ary << args}
135
+ assert_equal [], ary
136
+ end
137
+
138
+ def test_each_section
139
+ expected = [
140
+ 'section_one', 'section_two', 'section three',
141
+ 'section_four', 'section_five'
142
+ ].sort
143
+
144
+ ary = []
145
+ @ini_file.each_section {|section| ary << section}
146
+
147
+ assert_equal expected, ary.sort
148
+
149
+ ary = []
150
+ IniFile.new(:filename => 'temp.ini').each_section {|section| ary << section}
151
+ assert_equal [], ary
152
+ end
153
+
154
+ def test_eql_eh
155
+ assert @ini_file.eql?(@ini_file)
156
+ assert @ini_file.eql?(@ini_file.clone)
157
+ assert !@ini_file.eql?('string')
158
+ assert !@ini_file.eql?(IniFile.new(''))
159
+ end
160
+
161
+ def test_freeze
162
+ assert_equal false, @ini_file.frozen?
163
+ @ini_file.each_section do |s|
164
+ assert_equal false, @ini_file[s].frozen?
165
+ end
166
+
167
+ @ini_file.freeze
168
+
169
+ assert_equal true, @ini_file.frozen?
170
+ @ini_file.each_section do |s|
171
+ assert_equal true, @ini_file[s].frozen?
172
+ end
173
+ end
174
+
175
+ def test_has_section_eh
176
+ assert_equal true, @ini_file.has_section?('section_one')
177
+ assert_equal false, @ini_file.has_section?('section_ten')
178
+ assert_equal true, @ini_file.has_section?(:section_two)
179
+ assert_equal false, @ini_file.has_section?(nil)
180
+
181
+ ini_file = IniFile.new(:filename => 'temp.ini')
182
+ assert_equal false, ini_file.has_section?('section_one')
183
+ assert_equal false, ini_file.has_section?('one')
184
+ assert_equal false, ini_file.has_section?('two')
185
+ end
186
+
187
+ def test_index
188
+ expected = {
189
+ 'one' => '1',
190
+ 'two' => '2'
191
+ }
192
+ assert_equal expected, @ini_file[:section_one]
193
+
194
+ expected = {'three' => '3', 'multi' => "multiline support"}
195
+ assert_equal expected, @ini_file['section_two']
196
+
197
+ expected = {
198
+ 'four' => '4',
199
+ 'five' => '5',
200
+ 'six' => '6',
201
+ }
202
+ assert_equal expected, @ini_file['section three']
203
+
204
+ expected = {}
205
+ assert_equal expected, @ini_file['section_four']
206
+
207
+ expected = {'seven and eight' => '7 & 8'}
208
+ assert_equal expected, @ini_file['section_five']
209
+
210
+ expected = {}
211
+ assert_equal expected, @ini_file['section_six']
212
+
213
+ assert_nil @ini_file[nil]
214
+
215
+ expected = {}
216
+ ini_file = IniFile.new(:filename => 'temp.ini')
217
+ assert_equal expected, ini_file['section_one']
218
+ assert_equal expected, ini_file['one']
219
+ assert_nil ini_file[nil]
220
+ end
221
+
222
+ def test_match
223
+ expected = {
224
+ "section_two" =>
225
+ {
226
+ "three"=>"3", "multi"=>"multiline support"
227
+ },
228
+ "section three" =>
229
+ {
230
+ "four"=>"4", "five"=>"5", "six"=>"6"
231
+ }
232
+ }
233
+ assert_equal expected, @ini_file.match(/(two|three)/)
234
+
235
+ # the match function should not delete entries from the inifile hash
236
+ assert_equal({'seven and eight' => '7 & 8'}, @ini_file['section_five'])
237
+
238
+ expected = {}
239
+ assert_equal expected, @ini_file.match(/houndreds/)
240
+ end
241
+
242
+ def test_initialize
243
+ # see if we can parse different style comments
244
+ #assert_raise(IniFile::Error) {IniFile.new 'test/data/comment.ini'}
245
+
246
+ ini_file = IniFile.new(:filename => 'test/data/comment.ini', :comment => '#')
247
+ assert ini_file.has_section?('section_one')
248
+ assert_equal '20 + 22 = 42', ini_file['section_two']['multi']
249
+
250
+ # see if we can parse different style param separators
251
+ assert_raise(IniFile::Error) {IniFile.new(:filename => 'test/data/param.ini')}
252
+
253
+ ini_file = IniFile.new(:filename => 'test/data/param.ini', :parameter => ':')
254
+ assert ini_file.has_section?('section_one')
255
+ assert_equal '1', ini_file['section_one']['one']
256
+ assert_equal '2', ini_file['section_one']['two']
257
+
258
+ # make sure we error out on files with bad lines
259
+ assert_raise(IniFile::Error) {IniFile.new :filename => 'test/data/bad_1.ini'}
260
+ end
261
+
262
+ def test_initialize_from_string_without_ending_newline
263
+ content = "[section_one]\n foo=bar"
264
+ ini_file = IniFile.new(content)
265
+ assert ini_file.has_section?('section_one')
266
+ assert_equal 'bar', ini_file['section_one']['foo']
267
+ end
268
+
269
+ def test_initialize_from_string
270
+ content = File.read('test/data/good.ini')
271
+
272
+ ini_file = IniFile.new(content, :comment => ';')
273
+ assert ini_file.has_section?('section_one')
274
+ assert ini_file.has_section?('section_two')
275
+ assert ini_file.has_section?('section three')
276
+ assert ini_file.has_section?('section_four')
277
+ assert ini_file.has_section?('section_five')
278
+
279
+ assert_equal '7 & 8', ini_file['section_five']['seven and eight']
280
+ end
281
+
282
+ def test_sections
283
+ expected = [
284
+ 'section_one', 'section_two', 'section three',
285
+ 'section_four', 'section_five'
286
+ ].sort
287
+
288
+ assert_equal expected, @ini_file.sections.sort
289
+
290
+ ini_file = IniFile.new(:filename => 'temp.ini')
291
+ assert_equal [], ini_file.sections
292
+ end
293
+
294
+ def test_taint
295
+ assert_equal false, @ini_file.tainted?
296
+ @ini_file.each_section do |s|
297
+ assert_equal false, @ini_file[s].tainted?
298
+ end
299
+
300
+ @ini_file.taint
301
+
302
+ assert_equal true, @ini_file.tainted?
303
+ @ini_file.each_section do |s|
304
+ assert_equal true, @ini_file[s].tainted?
305
+ end
306
+ end
307
+
308
+ def test_write
309
+ tmp = 'test/data/temp.ini'
310
+ File.delete tmp if Kernel.test(?f, tmp)
311
+
312
+ @ini_file.save(:filename => tmp)
313
+ assert_equal true, Kernel.test(?f, tmp)
314
+
315
+ File.delete tmp if Kernel.test(?f, tmp)
316
+
317
+ ini_file = IniFile.new(:filename => tmp)
318
+ ini_file.save
319
+ assert_nil Kernel.test(?s, tmp)
320
+
321
+ File.delete tmp if Kernel.test(?f, tmp)
322
+ end
323
+
324
+ def test_read
325
+ assert @ini_file.has_section?('section_one')
326
+
327
+ @ini_file['section_one']['one'] = 42
328
+ @ini_file['section_one']['two'] = 42
329
+ assert_equal 42, @ini_file['section_one']['one']
330
+ assert_equal 42, @ini_file['section_one']['two']
331
+
332
+ @ini_file.read
333
+ assert_equal '1', @ini_file['section_one']['one']
334
+ assert_equal '2', @ini_file['section_one']['two']
335
+
336
+ @ini_file.read(:filename => 'test/data/mixed_comment.ini')
337
+ assert_equal false, @ini_file.has_section?('section_two')
338
+ end
339
+
340
+ def test_modifies_current_keys
341
+ ini = IniFile.load("test/data/tmp.ini")
342
+ ini["section one"]["one"] = 17
343
+ ini.save
344
+
345
+ assert File.read("test/data/tmp.ini") =~ /one = 17/
346
+ end
347
+
348
+ def test_can_add_key_to_inifile
349
+ ini_file = IniFile.new(:filename => "test/data/tmp.ini")
350
+ ini_file["new_section"] = {}
351
+ ini_file.save
352
+
353
+ assert File.read("test/data/tmp.ini") =~ /\[new_section\]/
354
+ end
355
+
356
+ def test_adds_correct_key_to_inifile
357
+ ini_file = IniFile.new(:filename => "test/data/tmp.ini")
358
+ ini_file["foo"] = {}
359
+ ini_file.save
360
+
361
+ assert File.read("test/data/tmp.ini") =~ /\[foo\]/
362
+ end
363
+
364
+ def test_assigns_values_to_inifile
365
+ ini_file = IniFile.new(:filename => "test/data/tmp.ini")
366
+ ini_file["foo"] = {
367
+ :bar => "baz"
368
+ }
369
+
370
+ assert_equal ini_file["foo"], { :bar => "baz" }
371
+ end
372
+
373
+ def test_assigns_correct_values_to_inifile
374
+ ini_file = IniFile.new(:filename => "test/data/tmp.ini")
375
+ ini_file["foo"] = {
376
+ :one => "two"
377
+ }
378
+
379
+ assert_equal ini_file["foo"], { :one => "two" }
380
+ end
381
+
382
+ def test_assignment_stringifies_key
383
+ ini_file = IniFile.new(:filename => "test/data/tmp.ini")
384
+ ini_file["foo"] = {:one => :two}
385
+ ini_file[:foo] = {}
386
+ assert_equal ini_file["foo"], {}
387
+ end
388
+
389
+ def test_multiline_parsing
390
+ ini_file = IniFile.load('test/data/multiline.ini')
391
+
392
+ multiline = ini_file['section_three']
393
+ expected = {"three" => "hello multiline", "other" => "stuff"}
394
+ assert_equal expected, multiline
395
+
396
+ multiple = ini_file['section_four']
397
+ expected = {"four" => "hello multiple multilines",
398
+ "five" => "multiple lines\ninside of quotations\npreserve everything" }
399
+ assert_equal expected, multiple
400
+
401
+ multiple = ini_file['empty_lines']
402
+ expected = {'empty' => '', 'not_empty' => 'full'}
403
+ assert_equal expected, multiple
404
+ end
405
+
406
+ def test_merge
407
+ ini_file = @ini_file.merge(IniFile.load("test/data/merge.ini"))
408
+ assert_equal '3', ini_file['section_one']['one']
409
+ assert_equal '2', ini_file['section_one']['two']
410
+
411
+ # make sure that the rest haven't changed
412
+ assert_equal '3', ini_file['section_two']['three']
413
+
414
+ # and that we got any additional sections too
415
+ assert_equal '5', ini_file['section_five']['five']
416
+
417
+ # original object is unchanged
418
+ assert_equal '1', @ini_file['section_one']['one']
419
+ end
420
+
421
+ def test_merge_hash
422
+ ini_file = @ini_file.merge({
423
+ 'section_one' => { 'one' => '3' },
424
+ 'section_five' => { 'five' => '5' }
425
+ })
426
+ assert_equal '3', ini_file['section_one']['one']
427
+ assert_equal '2', ini_file['section_one']['two']
428
+
429
+ # make sure that the rest haven't changed
430
+ assert_equal '3', ini_file['section_two']['three']
431
+
432
+ # and that we got any additional sections too
433
+ assert_equal '5', ini_file['section_five']['five']
434
+
435
+ # original object is unchanged
436
+ assert_equal '1', @ini_file['section_one']['one']
437
+ end
438
+
439
+ if RUBY_VERSION >= '1.9'
440
+ def test_parse_encoding
441
+ ini_file = IniFile.new(:filename => "test/data/browscap.ini", :encoding => 'ISO-8859-1')
442
+ assert_equal ini_file['www.substancia.com AutoHTTPAgent (ver *)']['Browser'], "Subst\xE2ncia".force_encoding('ISO-8859-1')
443
+ end
444
+
445
+ def test_write_encoding
446
+ tmp = 'test/data/tmp.ini'
447
+ File.delete tmp if Kernel.test(?f, tmp)
448
+
449
+ @ini_file = IniFile.new(:filename => tmp, :encoding => 'UTF-8')
450
+ @ini_file['testutf-8'] = {"utf-8" => "appr\u20accier"}
451
+
452
+ @ini_file.save(:filename => tmp)
453
+
454
+ test = File.open(tmp)
455
+ assert_equal test.external_encoding.to_s, 'UTF-8'
456
+ end
457
+ end
458
+
459
+ def test_value_escaping
460
+ ini_file = IniFile.load('test/data/escape.ini')
461
+ escaped = ini_file['escaped']
462
+
463
+ assert_equal %Q{There is a tab\tcharacter in here somewhere}, escaped['tabs']
464
+ assert_equal %Q{Who uses these anyways?\r}, escaped['carriage return']
465
+ assert_equal %Q{Trust newline!\nAlways there when you need him.\nSplittin' those lines.}, escaped['newline']
466
+ 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']
467
+ assert_equal %q{This string \t contains \n no \r special \0 characters!}, escaped['backslash']
468
+ assert_equal %Q{Escaping works\tinside quoted strings!}, escaped['quoted']
469
+ end
470
+
471
+ def test_value_escaping_disabled
472
+ ini_file = IniFile.load('test/data/escape.ini', :escape => false)
473
+ escaped = ini_file['escaped']
474
+
475
+ assert_equal %q{There is a tab\tcharacter in here somewhere}, escaped['tabs']
476
+ assert_equal %q{Who uses these anyways?\r}, escaped['carriage return']
477
+ assert_equal %q{Trust newline!\nAlways there when you need him.\nSplittin' those lines.}, escaped['newline']
478
+ 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']
479
+ assert_equal %q{This string \\\\t contains \\\\n no \\\\r special \\\\0 characters!}, escaped['backslash']
480
+ assert_equal %q{Escaping works\tinside quoted strings!}, escaped['quoted']
481
+ end
482
+
483
+ def test_global_section
484
+ ini_file = IniFile.load('test/data/global.ini')
485
+
486
+ assert_equal %w[global], ini_file.sections
487
+ assert_equal '1', ini_file['global']['one']
488
+ assert_equal '2', ini_file['global']['two']
489
+ end
490
+
491
+ def test_default_global_section
492
+ ini_file = IniFile.load('test/data/global.ini', :default => 'nonce')
493
+
494
+ assert_equal %w[nonce], ini_file.sections
495
+ assert_equal '1', ini_file['nonce']['one']
496
+ assert_equal '2', ini_file['nonce']['two']
497
+ end
498
+
499
+ def test_unescaped_items
500
+ ini_file = IniFile.load('test/data/no_escaping.ini', :escape => false)
501
+
502
+ assert_equal 'key\=value', ini_file['unescaped_param']['two']
503
+ end
504
+ end
505
+