Class: TestIniFile

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 13

def setup
  @ini_file = ::IniFile.new 'test/data/good.ini'
  @contents = [
    ['section_one', 'one', '1'],
    ['section_one', 'two', '2'],
    ['section_two', 'three', '3'],
    ['section three', 'four', '4'],
    ['section three', 'five', '5'],
    ['section three', 'six', '6'],
    ['section_five', 'seven and eight', '7 & 8']
  ].sort
end

#test_class_loadObject



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
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 26

def test_class_load
  ini_file = ::IniFile.load 'test/data/good.ini'
  assert_instance_of ::IniFile, ini_file

  # see if we can parse different style comments
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/comment.ini'}

  ini_file = ::IniFile.load 'test/data/comment.ini', :comment => '#'
  assert_instance_of ::IniFile, ini_file

  # see if we can parse mixed style comments
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/mixed_comment.ini'}

  ini_file = ::IniFile.load 'test/data/mixed_comment.ini', :comment => ';#'
  assert_instance_of ::IniFile, ini_file

  # see if we can parse different style param separators
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/param.ini'}

  ini_file = ::IniFile.load 'test/data/param.ini', :parameter => ':'
  assert_instance_of ::IniFile, ini_file

  # make sure we error out on files with bad lines
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/bad_1.ini'}
  assert_raise(::IniFile::Error) {::IniFile.load 'test/data/bad_2.ini'}
end

#test_cloneObject



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
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 53

def test_clone
  clone = @ini_file.clone
  assert_equal @ini_file, clone
  assert !clone.tainted?
  assert !clone.frozen?

  # the clone should be completely independent of the original
  clone['new_section']['one'] = 1
  assert_not_equal @ini_file, clone

  # the tainted state is copied to clones
  @ini_file.taint
  assert @ini_file.tainted?

  clone = @ini_file.clone
  assert clone.tainted?

  # the frozen state is also copied to clones
  @ini_file.freeze
  assert @ini_file.frozen?

  clone = @ini_file.clone
  assert clone.tainted?
  assert clone.frozen?
end

#test_delete_sectionObject



79
80
81
82
83
84
85
86
87
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 79

def test_delete_section
  assert_nil @ini_file.delete_section('section_nil')

  h = {'one' => '1', 'two' => '2'}
  assert_equal true, @ini_file.has_section?('section_one')
  assert_equal h, @ini_file.delete_section('section_one')
  assert_equal false, @ini_file.has_section?('section_one')
  assert_nil @ini_file.delete_section('section_one')
end

#test_dupObject



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
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 89

def test_dup
  dup = @ini_file.dup
  assert_equal @ini_file, dup
  assert !dup.tainted?
  assert !dup.frozen?

  # the duplicate should be completely independent of the original
  dup['new_section']['one'] = 1
  assert_not_equal @ini_file, dup

  # the tainted state is copied to duplicates
  @ini_file.taint
  assert @ini_file.tainted?

  dup = @ini_file.dup
  assert dup.tainted?

  # the frozen state, however, is not
  @ini_file.freeze
  assert @ini_file.frozen?

  dup = @ini_file.dup
  assert dup.tainted?
  assert !dup.frozen?
end

#test_eachObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 115

def test_each
  ary = []
  @ini_file.each {|*args| ary << args}

  assert_equal @contents, ary.sort

  ary = []
  ::IniFile.new('temp.ini').each {|*args| ary << args}
  assert_equal [], ary
end

#test_each_sectionObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 126

def test_each_section
  expected = [
    'section_one', 'section_two', 'section three',
    'section_four', 'section_five'
  ].sort

  ary = []
  @ini_file.each_section {|section| ary << section}

  assert_equal expected, ary.sort

  ary = []
  ::IniFile.new('temp.ini').each_section {|section| ary << section}
  assert_equal [], ary
end

#test_eql_ehObject



142
143
144
145
146
147
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 142

def test_eql_eh
  assert @ini_file.eql?(@ini_file)
  assert @ini_file.eql?(@ini_file.clone)
  assert !@ini_file.eql?('string')
  assert !@ini_file.eql?(IniFile.new(''))
end

#test_freezeObject



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 149

def test_freeze
  assert_equal false, @ini_file.frozen?
  @ini_file.each_section do |s|
    assert_equal false, @ini_file[s].frozen?
  end

  @ini_file.freeze

  assert_equal true, @ini_file.frozen?
  @ini_file.each_section do |s|
    assert_equal true, @ini_file[s].frozen?
  end
end

#test_has_section_ehObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 163

def test_has_section_eh
  assert_equal true,  @ini_file.has_section?('section_one')
  assert_equal false, @ini_file.has_section?('section_ten')
  assert_equal true,  @ini_file.has_section?(:section_two)
  assert_equal false, @ini_file.has_section?(nil)

  ini_file = ::IniFile.new 'temp.ini'
  assert_equal false, ini_file.has_section?('section_one')
  assert_equal false, ini_file.has_section?('one')
  assert_equal false, ini_file.has_section?('two')
end

#test_indexObject



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
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 175

def test_index
  expected = {
    'one' => '1',
    'two' => '2'
  }
  assert_equal expected, @ini_file[:section_one]

  expected = {'three' => '3'}
  assert_equal expected, @ini_file['section_two']

  expected = {
    'four' => '4',
    'five' => '5',
    'six'  => '6',
  }
  assert_equal expected, @ini_file['section three']

  expected = {}
  assert_equal expected, @ini_file['section_four']

  expected = {'seven and eight' => '7 & 8'}
  assert_equal expected, @ini_file['section_five']

  expected = {}
  assert_equal expected, @ini_file['section_six']

  assert_nil @ini_file[nil]

  expected = {}
  ini_file = ::IniFile.new 'temp.ini'
  assert_equal expected, ini_file['section_one']
  assert_equal expected, ini_file['one']
  assert_nil ini_file[nil]
end

#test_initializeObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 210

def test_initialize
  # see if we can parse different style comments
  assert_raise(::IniFile::Error) {::IniFile.new 'test/data/comment.ini'}

  ini_file = ::IniFile.new 'test/data/comment.ini', :comment => '#'
  assert_equal true, ini_file.has_section?('section_one')

  # see if we can parse different style param separators
  assert_raise(::IniFile::Error) {::IniFile.new 'test/data/param.ini'}

  ini_file = ::IniFile.new 'test/data/param.ini', :parameter => ':'
  assert_equal true, ini_file.has_section?('section_one')
  assert_equal '1', ini_file['section_one']['one']
  assert_equal '2', ini_file['section_one']['two']

  # make sure we error out on files with bad lines
  assert_raise(::IniFile::Error) {::IniFile.new 'test/data/bad_1.ini'}
  assert_raise(::IniFile::Error) {::IniFile.new 'test/data/bad_2.ini'}
end

#test_sectionsObject



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 230

def test_sections
  expected = [
    'section_one', 'section_two', 'section three',
    'section_four', 'section_five'
  ].sort

  assert_equal expected, @ini_file.sections.sort

  ini_file = ::IniFile.new 'temp.ini'
  assert_equal [], ini_file.sections
end

#test_taintObject



242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 242

def test_taint
  assert_equal false, @ini_file.tainted?
  @ini_file.each_section do |s|
    assert_equal false, @ini_file[s].tainted?
  end

  @ini_file.taint

  assert_equal true, @ini_file.tainted?
  @ini_file.each_section do |s|
    assert_equal true, @ini_file[s].tainted?
  end
end

#test_writeObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/bbcloud/vendor/brightbox-ini/test/test_inifile.rb', line 256

def test_write
  tmp = 'test/data/temp.ini'
  ::File.delete tmp if ::Kernel.test(?f, tmp)

  @ini_file.save tmp
  assert_equal true, ::Kernel.test(?f, tmp)

  ::File.delete tmp if ::Kernel.test(?f, tmp)

  ini_file = ::IniFile.new tmp
  ini_file.save
  assert_nil ::Kernel.test(?s, tmp)

  ::File.delete tmp if ::Kernel.test(?f, tmp)
end