inifile 2.0.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ [section_one]
2
+ one = 1
3
+ two = 2
4
+
5
+ [section_two]
6
+ close-quote = "some text without
7
+ a closing quote - should
8
+ be interesting
9
+
10
+ [section_three]
11
+ wat = where is that closing quote?
@@ -6,5 +6,5 @@ two = 2
6
6
  [section_two] # you can comment here
7
7
  one = 42 # and even here!
8
8
  multi = 20 \ # and here, too
9
- + 22 \= 42
9
+ + 22 = 42
10
10
 
@@ -0,0 +1,6 @@
1
+ [section_one]
2
+ one = 1
3
+ two = 2
4
+
5
+ [section_two]
6
+ end-of-file = here is the last value \
@@ -3,15 +3,15 @@ one = 1
3
3
  two = 2
4
4
 
5
5
  [section_two]
6
- three = 3
6
+ three = -3
7
7
  multi = multiline \
8
8
  support
9
9
 
10
10
  ; comments should be ignored
11
11
  [section three]
12
- four =4
13
- five=5
14
- six =6
12
+ four =true
13
+ five=false # comments can go here
14
+ six =6.0 ; and here, too
15
15
 
16
16
  [section_four]
17
17
  [section_five]
@@ -0,0 +1,4 @@
1
+ ; section headers can be values
2
+ [section_one]
3
+ one=[value]
4
+ two=2
@@ -11,13 +11,13 @@ class TestIniFile < Test::Unit::TestCase
11
11
  def setup
12
12
  @ini_file = IniFile.new(:filename => 'test/data/good.ini')
13
13
  @contents = [
14
- ['section_one', 'one', '1'],
15
- ['section_one', 'two', '2'],
16
- ['section_two', 'three', '3'],
14
+ ['section_one', 'one', 1],
15
+ ['section_one', 'two', 2],
16
+ ['section_two', 'three', -3],
17
17
  ['section_two', 'multi', "multiline support"],
18
- ['section three', 'four', '4'],
19
- ['section three', 'five', '5'],
20
- ['section three', 'six', '6'],
18
+ ['section three', 'four', true],
19
+ ['section three', 'five', false],
20
+ ['section three', 'six', 6.0],
21
21
  ['section_five', 'seven and eight', '7 & 8']
22
22
  ].sort
23
23
 
@@ -84,7 +84,7 @@ class TestIniFile < Test::Unit::TestCase
84
84
  def test_delete_section
85
85
  assert_nil @ini_file.delete_section('section_nil')
86
86
 
87
- h = {'one' => '1', 'two' => '2'}
87
+ h = {'one' => 1, 'two' => 2}
88
88
  assert_equal true, @ini_file.has_section?('section_one')
89
89
  assert_equal h, @ini_file.delete_section('section_one')
90
90
  assert_equal false, @ini_file.has_section?('section_one')
@@ -148,7 +148,7 @@ class TestIniFile < Test::Unit::TestCase
148
148
  assert @ini_file.eql?(@ini_file)
149
149
  assert @ini_file.eql?(@ini_file.clone)
150
150
  assert !@ini_file.eql?('string')
151
- assert !@ini_file.eql?(IniFile.new(''))
151
+ assert !@ini_file.eql?(IniFile.new(:content => ''))
152
152
  end
153
153
 
154
154
  def test_freeze
@@ -179,18 +179,18 @@ class TestIniFile < Test::Unit::TestCase
179
179
 
180
180
  def test_index
181
181
  expected = {
182
- 'one' => '1',
183
- 'two' => '2'
182
+ 'one' => 1,
183
+ 'two' => 2
184
184
  }
185
185
  assert_equal expected, @ini_file[:section_one]
186
186
 
187
- expected = {'three' => '3', 'multi' => "multiline support"}
187
+ expected = {'three' => -3, 'multi' => "multiline support"}
188
188
  assert_equal expected, @ini_file['section_two']
189
189
 
190
190
  expected = {
191
- 'four' => '4',
192
- 'five' => '5',
193
- 'six' => '6',
191
+ 'four' => true,
192
+ 'five' => false,
193
+ 'six' => 6.0,
194
194
  }
195
195
  assert_equal expected, @ini_file['section three']
196
196
 
@@ -214,13 +214,11 @@ class TestIniFile < Test::Unit::TestCase
214
214
 
215
215
  def test_match
216
216
  expected = {
217
- "section_two" =>
218
- {
219
- "three"=>"3", "multi"=>"multiline support"
217
+ "section_two" => {
218
+ "three" => -3, "multi" => "multiline support"
220
219
  },
221
- "section three" =>
222
- {
223
- "four"=>"4", "five"=>"5", "six"=>"6"
220
+ "section three" => {
221
+ "four" => true, "five"=> false, "six" => 6.0
224
222
  }
225
223
  }
226
224
  assert_equal expected, @ini_file.match(/(two|three)/)
@@ -245,8 +243,8 @@ class TestIniFile < Test::Unit::TestCase
245
243
 
246
244
  ini_file = IniFile.new(:filename => 'test/data/param.ini', :parameter => ':')
247
245
  assert ini_file.has_section?('section_one')
248
- assert_equal '1', ini_file['section_one']['one']
249
- assert_equal '2', ini_file['section_one']['two']
246
+ assert_equal 1, ini_file['section_one']['one']
247
+ assert_equal 2, ini_file['section_one']['two']
250
248
 
251
249
  # make sure we error out on files with bad lines
252
250
  assert_raise(IniFile::Error) {IniFile.new :filename => 'test/data/bad_1.ini'}
@@ -254,7 +252,7 @@ class TestIniFile < Test::Unit::TestCase
254
252
 
255
253
  def test_initialize_from_string_without_ending_newline
256
254
  content = "[section_one]\n foo=bar"
257
- ini_file = IniFile.new(content)
255
+ ini_file = IniFile.new(:content => content)
258
256
  assert ini_file.has_section?('section_one')
259
257
  assert_equal 'bar', ini_file['section_one']['foo']
260
258
  end
@@ -262,7 +260,7 @@ class TestIniFile < Test::Unit::TestCase
262
260
  def test_initialize_from_string
263
261
  content = File.read('test/data/good.ini')
264
262
 
265
- ini_file = IniFile.new(content, :comment => ';')
263
+ ini_file = IniFile.new(:content => content, :comment => ';')
266
264
  assert ini_file.has_section?('section_one')
267
265
  assert ini_file.has_section?('section_two')
268
266
  assert ini_file.has_section?('section three')
@@ -272,6 +270,35 @@ class TestIniFile < Test::Unit::TestCase
272
270
  assert_equal '7 & 8', ini_file['section_five']['seven and eight']
273
271
  end
274
272
 
273
+ def test_initialize_from_hash
274
+ hash = {
275
+ 'section one' => {
276
+ 'foo' => 'bar',
277
+ 'baz' => 'buz'
278
+ },
279
+ 'colors' => {
280
+ 'perrywinkle' => '7e6ff3',
281
+ 'steelblue' => '4682b4'
282
+ },
283
+ 'empty' => nil
284
+ }
285
+
286
+ ini_file = IniFile.new(:content => hash)
287
+ assert ini_file.has_section?('section one')
288
+ assert ini_file.has_section?('colors')
289
+ assert ini_file.has_section?('empty')
290
+
291
+ assert_equal %w[baz foo], ini_file['section one'].keys.sort
292
+ assert_equal 'bar', ini_file['section one']['foo']
293
+ assert_equal 'buz', ini_file['section one']['baz']
294
+
295
+ assert_equal %w[perrywinkle steelblue], ini_file['colors'].keys.sort
296
+ assert_equal '7e6ff3', ini_file['colors']['perrywinkle']
297
+ assert_equal '4682b4', ini_file['colors']['steelblue']
298
+
299
+ assert_empty ini_file['empty']
300
+ end
301
+
275
302
  def test_sections
276
303
  expected = [
277
304
  'section_one', 'section_two', 'section three',
@@ -323,8 +350,8 @@ class TestIniFile < Test::Unit::TestCase
323
350
  assert_equal 42, @ini_file['section_one']['two']
324
351
 
325
352
  @ini_file.read
326
- assert_equal '1', @ini_file['section_one']['one']
327
- assert_equal '2', @ini_file['section_one']['two']
353
+ assert_equal 1, @ini_file['section_one']['one']
354
+ assert_equal 2, @ini_file['section_one']['two']
328
355
 
329
356
  @ini_file.read(:filename => 'test/data/mixed_comment.ini')
330
357
  assert_equal false, @ini_file.has_section?('section_two')
@@ -392,61 +419,73 @@ class TestIniFile < Test::Unit::TestCase
392
419
  assert_equal expected, multiple
393
420
 
394
421
  multiple = ini_file['empty_lines']
395
- expected = {'empty' => '', 'not_empty' => 'full'}
422
+ expected = {'empty' => nil, 'not_empty' => 'full'}
396
423
  assert_equal expected, multiple
397
424
  end
398
425
 
399
426
  def test_merge
400
427
  ini_file = @ini_file.merge(IniFile.load("test/data/merge.ini"))
401
- assert_equal '3', ini_file['section_one']['one']
402
- assert_equal '2', ini_file['section_one']['two']
428
+ assert_equal 3, ini_file['section_one']['one']
429
+ assert_equal 2, ini_file['section_one']['two']
403
430
 
404
431
  # make sure that the rest haven't changed
405
- assert_equal '3', ini_file['section_two']['three']
432
+ assert_equal(-3, ini_file['section_two']['three'])
406
433
 
407
434
  # and that we got any additional sections too
408
- assert_equal '5', ini_file['section_five']['five']
435
+ assert_equal 5, ini_file['section_five']['five']
409
436
 
410
437
  # original object is unchanged
411
- assert_equal '1', @ini_file['section_one']['one']
438
+ assert_equal 1, @ini_file['section_one']['one']
412
439
  end
413
440
 
414
441
  def test_merge_hash
415
442
  ini_file = @ini_file.merge({
416
- 'section_one' => { 'one' => '3' },
417
- 'section_five' => { 'five' => '5' }
443
+ 'section_one' => { 'one' => 3 },
444
+ 'section_five' => { 'five' => 5 }
418
445
  })
419
- assert_equal '3', ini_file['section_one']['one']
420
- assert_equal '2', ini_file['section_one']['two']
446
+ assert_equal 3, ini_file['section_one']['one']
447
+ assert_equal 2, ini_file['section_one']['two']
421
448
 
422
449
  # make sure that the rest haven't changed
423
- assert_equal '3', ini_file['section_two']['three']
450
+ assert_equal(-3, ini_file['section_two']['three'])
424
451
 
425
452
  # and that we got any additional sections too
426
- assert_equal '5', ini_file['section_five']['five']
453
+ assert_equal 5, ini_file['section_five']['five']
427
454
 
428
455
  # original object is unchanged
429
- assert_equal '1', @ini_file['section_one']['one']
456
+ assert_equal 1, @ini_file['section_one']['one']
430
457
  end
431
458
 
432
- if RUBY_VERSION >= '1.9'
433
- def test_parse_encoding
434
- ini_file = IniFile.new(:filename => "test/data/browscap.ini", :encoding => 'ISO-8859-1')
435
- assert_equal ini_file['www.substancia.com AutoHTTPAgent (ver *)']['Browser'], "Subst\xE2ncia".force_encoding('ISO-8859-1')
436
- end
459
+ def test_merge_invalid_hash
460
+ bad_hash = { 'section_one' => [1, 2, 3, 4] }
461
+ assert_raise(IniFile::Error) { @ini_file.merge(bad_hash) }
437
462
 
438
- def test_write_encoding
439
- tmp = 'test/data/tmp.ini'
440
- File.delete tmp if Kernel.test(?f, tmp)
463
+ bad_hash = { 'foo' => 'bar' }
464
+ assert_raise(IniFile::Error) { @ini_file.merge(bad_hash) }
441
465
 
442
- @ini_file = IniFile.new(:filename => tmp, :encoding => 'UTF-8')
443
- @ini_file['testutf-8'] = {"utf-8" => "appr\u20accier"}
466
+ not_a_hash = [['section_one', ['foo','bar'], ['baz', 'buz']]]
467
+ assert_raise(IniFile::Error) { @ini_file.merge(not_a_hash) }
444
468
 
445
- @ini_file.save(:filename => tmp)
469
+ ini_file = @ini_file.merge nil
470
+ assert ini_file.eql?(@ini_file)
471
+ end
446
472
 
447
- test = File.open(tmp)
448
- assert_equal test.external_encoding.to_s, 'UTF-8'
449
- end
473
+ def test_parse_encoding
474
+ ini_file = IniFile.new(:filename => "test/data/browscap.ini", :encoding => 'ISO-8859-1')
475
+ assert_equal ini_file['www.substancia.com AutoHTTPAgent (ver *)']['Browser'], "Subst\xE2ncia".force_encoding('ISO-8859-1')
476
+ end
477
+
478
+ def test_write_encoding
479
+ tmp = 'test/data/tmp.ini'
480
+ File.delete tmp if Kernel.test(?f, tmp)
481
+
482
+ @ini_file = IniFile.new(:filename => tmp, :encoding => 'UTF-8')
483
+ @ini_file['testutf-8'] = {"utf-8" => "appr\u20accier"}
484
+
485
+ @ini_file.save(:filename => tmp)
486
+
487
+ test = File.open(tmp)
488
+ assert_equal test.external_encoding.to_s, 'UTF-8'
450
489
  end
451
490
 
452
491
  def test_value_escaping
@@ -461,32 +500,62 @@ class TestIniFile < Test::Unit::TestCase
461
500
  assert_equal %Q{Escaping works\tinside quoted strings!}, escaped['quoted']
462
501
  end
463
502
 
503
+ # disabling escaping is no longer supported
464
504
  def test_value_escaping_disabled
465
505
  ini_file = IniFile.load('test/data/escape.ini', :escape => false)
466
506
  escaped = ini_file['escaped']
467
507
 
468
- assert_equal %q{There is a tab\tcharacter in here somewhere}, escaped['tabs']
469
- assert_equal %q{Who uses these anyways?\r}, escaped['carriage return']
470
- assert_equal %q{Trust newline!\nAlways there when you need him.\nSplittin' those lines.}, escaped['newline']
471
- 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']
472
- assert_equal %q{This string \\\\t contains \\\\n no \\\\r special \\\\0 characters!}, escaped['backslash']
473
- assert_equal %q{Escaping works\tinside quoted strings!}, escaped['quoted']
508
+ assert_equal %Q{There is a tab\tcharacter in here somewhere}, escaped['tabs']
509
+ assert_equal %Q{Who uses these anyways?\r}, escaped['carriage return']
510
+ assert_equal %Q{Trust newline!\nAlways there when you need him.\nSplittin' those lines.}, escaped['newline']
511
+ 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']
512
+ assert_equal %q{This string \t contains \n no \r special \0 characters!}, escaped['backslash']
513
+ assert_equal %Q{Escaping works\tinside quoted strings!}, escaped['quoted']
474
514
  end
475
515
 
476
516
  def test_global_section
477
517
  ini_file = IniFile.load('test/data/global.ini')
478
518
 
479
519
  assert_equal %w[global], ini_file.sections
480
- assert_equal '1', ini_file['global']['one']
481
- assert_equal '2', ini_file['global']['two']
520
+ assert_equal 1, ini_file['global']['one']
521
+ assert_equal 2, ini_file['global']['two']
482
522
  end
483
523
 
484
524
  def test_default_global_section
485
525
  ini_file = IniFile.load('test/data/global.ini', :default => 'nonce')
486
526
 
487
527
  assert_equal %w[nonce], ini_file.sections
488
- assert_equal '1', ini_file['nonce']['one']
489
- assert_equal '2', ini_file['nonce']['two']
528
+ assert_equal 1, ini_file['nonce']['one']
529
+ assert_equal 2, ini_file['nonce']['two']
530
+ end
531
+
532
+ def test_unescaped_section_header_as_value
533
+ ini_file = IniFile.load('test/data/section.ini')
534
+
535
+ assert_equal %w[section_one], ini_file.sections
536
+ assert_equal '[value]', ini_file['section_one']['one']
537
+ assert_equal 2, ini_file['section_one']['two']
538
+ end
539
+
540
+ def test_unmatched_quotes
541
+ # missing a closing quote should raise an error
542
+ assert_raise(IniFile::Error) { IniFile.load 'test/data/bad_2.ini' }
543
+ end
544
+
545
+ def test_continuation_at_end_of_file
546
+ ini_file = IniFile.load('test/data/continuation.ini')
547
+
548
+ assert_equal 1, ini_file['section_one']['one']
549
+ assert_equal 2, ini_file['section_one']['two']
550
+
551
+ assert_equal 'here is the last value', ini_file['section_two']['end-of-file']
552
+ end
553
+
554
+ def test_empty_comment_string
555
+ ini_file = IniFile.load('test/data/merge.ini', :comment => nil)
556
+
557
+ assert_equal 3, ini_file['section_one']['one']
558
+ assert_equal 5, ini_file['section_five']['five']
490
559
  end
491
560
  end
492
561
 
metadata CHANGED
@@ -1,80 +1,104 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inifile
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
5
- prerelease:
4
+ version: 3.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tim Pease
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-09-15 00:00:00.000000000 Z
11
+ date: 2014-08-01 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bones-git
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: '0'
19
+ version: '1.3'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '0'
26
+ version: '1.3'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: bones
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
- version: 3.8.0
33
+ version: 3.8.1
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
- version: 3.8.0
46
- description: ! "Although made popular by Windows, INI files can be used on any system
47
- thanks\nto their flexibility. They allow a program to store configuration data,
48
- which\ncan then be easily parsed and changed. Two notable systems that use the INI\nformat
49
- are Samba and Trac.\n\nMore information about INI files can be found on the [Wikipedia
50
- Page](http://en.wikipedia.org/wiki/INI_file).\n\n### Properties\n\nThe basic element
51
- contained in an INI file is the property. Every property has\na name and a value,
52
- delimited by an equals sign *=*. The name appears to the\nleft of the equals sign
53
- and the value to the right.\n\n name=value\n\n### Sections\n\nSection declarations
54
- start with *[* and end with *]* as in `[section1]` and\n`[section2]` shown in the
55
- example below. The section declaration marks the\nbeginning of a section. All properties
56
- after the section declaration will be\nassociated with that section.\n\n### Comments\n\nAll
57
- lines beginning with a semicolon *;* or a number sign *#* are considered\nto be
58
- comments. Comment lines are ignored when parsing INI files.\n\n### Example File
59
- Format\n\nA typical INI file might look like this:\n\n [section1]\n ; some
60
- comment on section1\n var1 = foo\n var2 = doodle\n var3 = multiline values
61
- \\\n are also possible\n\n [section2]\n # another comment\n var1 = baz\n
62
- \ var2 = shoodle"
40
+ version: 3.8.1
41
+ description: |-
42
+ Although made popular by Windows, INI files can be used on any system thanks
43
+ to their flexibility. They allow a program to store configuration data, which
44
+ can then be easily parsed and changed. Two notable systems that use the INI
45
+ format are Samba and Trac.
46
+
47
+ More information about INI files can be found on the [Wikipedia Page](http://en.wikipedia.org/wiki/INI_file).
48
+
49
+ ### Properties
50
+
51
+ The basic element contained in an INI file is the property. Every property has
52
+ a name and a value, delimited by an equals sign *=*. The name appears to the
53
+ left of the equals sign and the value to the right.
54
+
55
+ name=value
56
+
57
+ ### Sections
58
+
59
+ Section declarations start with *[* and end with *]* as in `[section1]` and
60
+ `[section2]` shown in the example below. The section declaration marks the
61
+ beginning of a section. All properties after the section declaration will be
62
+ associated with that section.
63
+
64
+ ### Comments
65
+
66
+ All lines beginning with a semicolon *;* or a number sign *#* are considered
67
+ to be comments. Comment lines are ignored when parsing INI files.
68
+
69
+ ### Example File Format
70
+
71
+ A typical INI file might look like this:
72
+
73
+ [section1]
74
+ ; some comment on section1
75
+ var1 = foo
76
+ var2 = doodle
77
+ var3 = multiline values \
78
+ are also possible
79
+
80
+ [section2]
81
+ # another comment
82
+ var1 = baz
83
+ var2 = shoodle
63
84
  email: tim.pease@gmail.com
64
85
  executables: []
65
86
  extensions: []
66
87
  extra_rdoc_files:
67
88
  - History.txt
68
89
  files:
69
- - .gitignore
70
- - .travis.yml
90
+ - ".gitignore"
91
+ - ".travis.yml"
71
92
  - History.txt
72
93
  - README.md
73
94
  - Rakefile
95
+ - inifile.gemspec
74
96
  - lib/inifile.rb
75
97
  - test/data/bad_1.ini
98
+ - test/data/bad_2.ini
76
99
  - test/data/browscap.ini
77
100
  - test/data/comment.ini
101
+ - test/data/continuation.ini
78
102
  - test/data/escape.ini
79
103
  - test/data/global.ini
80
104
  - test/data/good.ini
@@ -82,32 +106,32 @@ files:
82
106
  - test/data/mixed_comment.ini
83
107
  - test/data/multiline.ini
84
108
  - test/data/param.ini
109
+ - test/data/section.ini
85
110
  - test/test_inifile.rb
86
111
  homepage: http://rubygems.org/gems/inifile
87
112
  licenses: []
113
+ metadata: {}
88
114
  post_install_message:
89
115
  rdoc_options:
90
- - --main
116
+ - "--main"
91
117
  - README.md
92
118
  require_paths:
93
119
  - lib
94
120
  required_ruby_version: !ruby/object:Gem::Requirement
95
- none: false
96
121
  requirements:
97
- - - ! '>='
122
+ - - ">="
98
123
  - !ruby/object:Gem::Version
99
124
  version: '0'
100
125
  required_rubygems_version: !ruby/object:Gem::Requirement
101
- none: false
102
126
  requirements:
103
- - - ! '>='
127
+ - - ">="
104
128
  - !ruby/object:Gem::Version
105
129
  version: '0'
106
130
  requirements: []
107
131
  rubyforge_project: inifile
108
- rubygems_version: 1.8.24
132
+ rubygems_version: 2.2.2
109
133
  signing_key:
110
- specification_version: 3
134
+ specification_version: 4
111
135
  summary: INI file reader and writer
112
136
  test_files:
113
137
  - test/test_inifile.rb