iniparse 0.2.1

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.
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ # Tests parsing of multiple lines, in context, using #parse.
4
+
5
+ describe 'Parsing a document' do
6
+ describe 'when a comment preceeds a single section and option' do
7
+ before(:all) do
8
+ @doc = IniParse::Parser.new(fixture(:comment_before_section)).parse
9
+ end
10
+
11
+ it 'should have a comment as the first line' do
12
+ @doc.lines.to_a.first.should be_kind_of(IniParse::Lines::Comment)
13
+ end
14
+
15
+ it 'should have one section' do
16
+ @doc.lines.keys.should == ['first_section']
17
+ end
18
+
19
+ it 'should have one option belonging to `first_section`' do
20
+ @doc['first_section']['key'].should == 'value'
21
+ end
22
+ end
23
+
24
+ it 'should allow blank lines to preceed the first section' do
25
+ lambda {
26
+ @doc = IniParse::Parser.new(fixture(:blank_before_section)).parse
27
+ }.should_not raise_error
28
+
29
+ @doc.lines.to_a.first.should be_kind_of(IniParse::Lines::Blank)
30
+ end
31
+
32
+ it 'should allow a blank line to belong to a section' do
33
+ lambda {
34
+ @doc = IniParse::Parser.new(fixture(:blank_in_section)).parse
35
+ }.should_not raise_error
36
+
37
+ @doc['first_section'].lines.to_a.first.should be_kind_of(IniParse::Lines::Blank)
38
+ end
39
+
40
+ it 'should raise an error if an option preceeds the first section' do
41
+ lambda {
42
+ IniParse::Parser.new(fixture(:option_before_section)).parse
43
+ }.should raise_error(IniParse::NoSectionError)
44
+ end
45
+
46
+ it 'should raise ParseError if a line could not be parsed' do
47
+ lambda { IniParse::Parser.new(fixture(:invalid_line)).parse }.should \
48
+ raise_error(IniParse::ParseError)
49
+ end
50
+ end
@@ -0,0 +1,367 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ # Tests parsing of individual, out of context, line types using #parse_line.
4
+
5
+ describe 'Parsing a line' do
6
+ it 'should strip leading whitespace and set the :indent option' do
7
+ IniParse::Parser.parse_line(' [section]').should \
8
+ be_section_tuple(:any, {:indent => ' '})
9
+ end
10
+
11
+ it 'should raise an error if the line could not be matched' do
12
+ lambda { IniParse::Parser.parse_line('invalid line') }.should \
13
+ raise_error(IniParse::ParseError)
14
+ end
15
+
16
+ it 'should parse using the types set in IniParse::Parser.parse_types' do
17
+ begin
18
+ # Remove last type.
19
+ type = IniParse::Parser.parse_types.pop
20
+ type.should_not_receive(:parse)
21
+ IniParse::Parser.parse_line('[section]')
22
+ ensure
23
+ IniParse::Parser.parse_types << type
24
+ end
25
+ end
26
+
27
+ # --
28
+ # ==========================================================================
29
+ # Option lines.
30
+ # ==========================================================================
31
+ # ++
32
+
33
+ describe 'with "k = v"' do
34
+ before(:all) do
35
+ @tuple = IniParse::Parser.parse_line('k = v')
36
+ end
37
+
38
+ it 'should return an option tuple' do
39
+ @tuple.should be_option_tuple('k', 'v')
40
+ end
41
+
42
+ it 'should set no indent, comment, offset or separator' do
43
+ @tuple.last[:indent].should be_nil
44
+ @tuple.last[:comment].should be_nil
45
+ @tuple.last[:comment_offset].should be_nil
46
+ @tuple.last[:comment_sep].should be_nil
47
+ end
48
+ end
49
+
50
+ describe 'with "k = a value with spaces"' do
51
+ it 'should return an option tuple' do
52
+ IniParse::Parser.parse_line('k = a value with spaces').should \
53
+ be_option_tuple('k', 'a value with spaces')
54
+ end
55
+ end
56
+
57
+ describe 'with "k = v ; a comment "' do
58
+ before(:all) do
59
+ @tuple = IniParse::Parser.parse_line('k = v ; a comment')
60
+ end
61
+
62
+ it 'should return an option tuple' do
63
+ @tuple.should be_option_tuple('k', 'v')
64
+ end
65
+
66
+ it 'should set the comment to "a comment"' do
67
+ @tuple.should be_option_tuple(:any, :any, :comment => 'a comment')
68
+ end
69
+
70
+ it 'should set the comment separator to ";"' do
71
+ @tuple.should be_option_tuple(:any, :any, :comment_sep => ';')
72
+ end
73
+
74
+ it 'should set the comment offset to 6' do
75
+ @tuple.should be_option_tuple(:any, :any, :comment_offset => 6)
76
+ end
77
+ end
78
+
79
+ describe 'with "k = v;w;x y;z"' do
80
+ before(:all) do
81
+ @tuple = IniParse::Parser.parse_line('k = v;w;x y;z')
82
+ end
83
+
84
+ it 'should return an option tuple with the correct value' do
85
+ @tuple.should be_option_tuple(:any, 'v;w;x y;z')
86
+ end
87
+
88
+ it 'should not set a comment' do
89
+ @tuple.last[:comment].should be_nil
90
+ @tuple.last[:comment_offset].should be_nil
91
+ @tuple.last[:comment_sep].should be_nil
92
+ end
93
+ end
94
+
95
+ describe 'with "k = v;w ; a comment"' do
96
+ before(:all) do
97
+ @tuple = IniParse::Parser.parse_line('k = v;w ; a comment')
98
+ end
99
+
100
+ it 'should return an option tuple with the correct value' do
101
+ @tuple.should be_option_tuple(:any, 'v;w')
102
+ end
103
+
104
+ it 'should set the comment to "a comment"' do
105
+ @tuple.should be_option_tuple(:any, :any, :comment => 'a comment')
106
+ end
107
+
108
+ it 'should set the comment separator to ";"' do
109
+ @tuple.should be_option_tuple(:any, :any, :comment_sep => ';')
110
+ end
111
+
112
+ it 'should set the comment offset to 8' do
113
+ @tuple.should be_option_tuple(:any, :any, :comment_offset => 8)
114
+ end
115
+ end
116
+
117
+ describe 'with "key=value"' do
118
+ it 'should return an option tuple with the correct key and value' do
119
+ IniParse::Parser.parse_line('key=value').should \
120
+ be_option_tuple('key', 'value')
121
+ end
122
+ end
123
+
124
+ describe 'with "key= value"' do
125
+ it 'should return an option tuple with the correct key and value' do
126
+ IniParse::Parser.parse_line('key= value').should \
127
+ be_option_tuple('key', 'value')
128
+ end
129
+ end
130
+
131
+ describe 'with "key =value"' do
132
+ it 'should return an option tuple with the correct key and value' do
133
+ IniParse::Parser.parse_line('key =value').should \
134
+ be_option_tuple('key', 'value')
135
+ end
136
+ end
137
+
138
+ describe 'with "key = value"' do
139
+ it 'should return an option tuple with the correct key and value' do
140
+ IniParse::Parser.parse_line('key = value').should \
141
+ be_option_tuple('key', 'value')
142
+ end
143
+ end
144
+
145
+ describe 'with "key ="' do
146
+ it 'should return an option tuple with the correct key' do
147
+ IniParse::Parser.parse_line('key =').should \
148
+ be_option_tuple('key')
149
+ end
150
+
151
+ it 'should set the option value to nil' do
152
+ IniParse::Parser.parse_line('key =').should \
153
+ be_option_tuple(:any, nil)
154
+ end
155
+ end
156
+
157
+ describe 'with "key.two = value"' do
158
+ it 'should return an option tuple with the correct key' do
159
+ IniParse::Parser.parse_line('key.two = value').should \
160
+ be_option_tuple('key.two')
161
+ end
162
+ end
163
+
164
+ describe 'with "key/with/slashes = value"' do
165
+ it 'should return an option tuple with the correct key' do
166
+ IniParse::Parser.parse_line('key/with/slashes = value').should \
167
+ be_option_tuple('key/with/slashes', 'value')
168
+ end
169
+ end
170
+
171
+ describe 'with "key_with_underscores = value"' do
172
+ it 'should return an option tuple with the correct key' do
173
+ IniParse::Parser.parse_line('key_with_underscores = value').should \
174
+ be_option_tuple('key_with_underscores', 'value')
175
+ end
176
+ end
177
+
178
+ describe 'with "key-with-dashes = value"' do
179
+ it 'should return an option tuple with the correct key' do
180
+ IniParse::Parser.parse_line('key-with-dashes = value').should \
181
+ be_option_tuple('key-with-dashes', 'value')
182
+ end
183
+ end
184
+
185
+ describe 'with "key with spaces = value"' do
186
+ it 'should return an option tuple with the correct key' do
187
+ IniParse::Parser.parse_line('key with spaces = value').should \
188
+ be_option_tuple('key with spaces', 'value')
189
+ end
190
+ end
191
+
192
+ # --
193
+ # ==========================================================================
194
+ # Section lines.
195
+ # ==========================================================================
196
+ # ++
197
+
198
+ describe 'with "[section]"' do
199
+ before(:all) do
200
+ @tuple = IniParse::Parser.parse_line('[section]')
201
+ end
202
+
203
+ it 'should return a section tuple' do
204
+ @tuple.should be_section_tuple('section')
205
+ end
206
+
207
+ it 'should set no indent, comment, offset or separator' do
208
+ @tuple.last[:indent].should be_nil
209
+ @tuple.last[:comment].should be_nil
210
+ @tuple.last[:comment_offset].should be_nil
211
+ @tuple.last[:comment_sep].should be_nil
212
+ end
213
+ end
214
+
215
+ describe 'with "[section with whitespace]"' do
216
+ it 'should return a section tuple with the correct key' do
217
+ IniParse::Parser.parse_line('[section with whitespace]').should \
218
+ be_section_tuple('section with whitespace')
219
+ end
220
+ end
221
+
222
+ describe 'with "[ section with surounding whitespace ]"' do
223
+ it 'should return a section tuple with the correct key' do
224
+ IniParse::Parser.parse_line('[ section with surounding whitespace ]').should \
225
+ be_section_tuple(' section with surounding whitespace ')
226
+ end
227
+ end
228
+
229
+ describe 'with "[section] ; a comment"' do
230
+ before(:all) do
231
+ @tuple = IniParse::Parser.parse_line('[section] ; a comment')
232
+ end
233
+
234
+ it 'should return a section tuple' do
235
+ @tuple.should be_section_tuple('section')
236
+ end
237
+
238
+ it 'should set the comment to "a comment"' do
239
+ @tuple.should be_section_tuple(:any, :comment => 'a comment')
240
+ end
241
+
242
+ it 'should set the comment separator to ";"' do
243
+ @tuple.should be_section_tuple(:any, :comment_sep => ';')
244
+ end
245
+
246
+ it 'should set the comment offset to 10' do
247
+ @tuple.should be_section_tuple(:any, :comment_offset => 10)
248
+ end
249
+ end
250
+
251
+ describe 'with "[section;with#comment;chars]"' do
252
+ before(:all) do
253
+ @tuple = IniParse::Parser.parse_line('[section;with#comment;chars]')
254
+ end
255
+
256
+ it 'should return a section tuple with the correct key' do
257
+ @tuple.should be_section_tuple('section;with#comment;chars')
258
+ end
259
+
260
+ it 'should not set a comment' do
261
+ @tuple.last[:indent].should be_nil
262
+ @tuple.last[:comment].should be_nil
263
+ @tuple.last[:comment_offset].should be_nil
264
+ @tuple.last[:comment_sep].should be_nil
265
+ end
266
+ end
267
+
268
+ describe 'with "[section;with#comment;chars] ; a comment"' do
269
+ before(:all) do
270
+ @tuple = IniParse::Parser.parse_line('[section;with#comment;chars] ; a comment')
271
+ end
272
+
273
+ it 'should return a section tuple with the correct key' do
274
+ @tuple.should be_section_tuple('section;with#comment;chars')
275
+ end
276
+
277
+ it 'should set the comment to "a comment"' do
278
+ @tuple.should be_section_tuple(:any, :comment => 'a comment')
279
+ end
280
+
281
+ it 'should set the comment separator to ";"' do
282
+ @tuple.should be_section_tuple(:any, :comment_sep => ';')
283
+ end
284
+
285
+ it 'should set the comment offset to 29' do
286
+ @tuple.should be_section_tuple(:any, :comment_offset => 29)
287
+ end
288
+ end
289
+
290
+ # --
291
+ # ==========================================================================
292
+ # Comment lines.
293
+ # ==========================================================================
294
+ # ++
295
+
296
+ describe 'with "; a comment"' do
297
+ before(:all) do
298
+ @tuple = IniParse::Parser.parse_line('; a comment')
299
+ end
300
+
301
+ it 'should return a comment tuple with the correct comment' do
302
+ @tuple.should be_comment_tuple('a comment')
303
+ end
304
+
305
+ it 'should set the comment separator to ";"' do
306
+ @tuple.should be_comment_tuple(:any, :comment_sep => ';')
307
+ end
308
+
309
+ it 'should set the comment offset to 0' do
310
+ @tuple.should be_comment_tuple(:any, :comment_offset => 0)
311
+ end
312
+ end
313
+
314
+ describe 'with " ; a comment"' do
315
+ before(:all) do
316
+ @tuple = IniParse::Parser.parse_line(' ; a comment')
317
+ end
318
+
319
+ it 'should return a comment tuple with the correct comment' do
320
+ @tuple.should be_comment_tuple('a comment')
321
+ end
322
+
323
+ it 'should set the comment separator to ";"' do
324
+ @tuple.should be_comment_tuple(:any, :comment_sep => ';')
325
+ end
326
+
327
+ it 'should set the comment offset to 1' do
328
+ @tuple.should be_comment_tuple(:any, :comment_offset => 1)
329
+ end
330
+ end
331
+
332
+ describe 'with ";"' do
333
+ before(:all) do
334
+ @tuple = IniParse::Parser.parse_line(';')
335
+ end
336
+
337
+ it 'should return a comment tuple with an empty value' do
338
+ @tuple.should be_comment_tuple('')
339
+ end
340
+
341
+ it 'should set the comment separator to ";"' do
342
+ @tuple.should be_comment_tuple(:any, :comment_sep => ';')
343
+ end
344
+
345
+ it 'should set the comment offset to 0' do
346
+ @tuple.should be_comment_tuple(:any, :comment_offset => 0)
347
+ end
348
+ end
349
+
350
+ # --
351
+ # ==========================================================================
352
+ # Blank lines.
353
+ # ==========================================================================
354
+ # ++
355
+
356
+ describe 'with ""' do
357
+ it 'should return a blank tuple' do
358
+ IniParse::Parser.parse_line('').should be_blank_tuple
359
+ end
360
+ end
361
+
362
+ describe 'with " "' do
363
+ it 'should return a blank tuple' do
364
+ IniParse::Parser.parse_line(' ').should be_blank_tuple
365
+ end
366
+ end
367
+ end
@@ -0,0 +1,46 @@
1
+ module IniParse
2
+ module Test
3
+ class Fixtures
4
+ @@fixtures = {}
5
+
6
+ def self.[](fix)
7
+ if @@fixtures.has_key?(fix)
8
+ @@fixtures[fix]
9
+ else
10
+ @@fixtures[fix] = File.read(Pathname(__FILE__).dirname.expand_path / 'fixtures' / fix)
11
+ end
12
+ end
13
+
14
+ def self.[]=(fix, val)
15
+ @@fixtures[fix] = val.margin
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ IniParse::Test::Fixtures[:comment_before_section] = <<-FIX
22
+ ; This is a comment
23
+ [first_section]
24
+ key = value
25
+ FIX
26
+
27
+ IniParse::Test::Fixtures[:blank_before_section] = <<-FIX
28
+
29
+ [first_section]
30
+ key = value
31
+ FIX
32
+
33
+ IniParse::Test::Fixtures[:blank_in_section] = <<-FIX
34
+ [first_section]
35
+
36
+ key = value
37
+ FIX
38
+
39
+ IniParse::Test::Fixtures[:option_before_section] = <<-FIX
40
+ key = value
41
+ [first_section]
42
+ FIX
43
+
44
+ IniParse::Test::Fixtures[:invalid_line] = <<-FIX
45
+ this line is not valid
46
+ FIX