File: line_parsing_spec.rb

package info (click to toggle)
ruby-iniparse 1.4.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 468 kB
  • sloc: ruby: 2,409; makefile: 3
file content (382 lines) | stat: -rw-r--r-- 11,808 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
52
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
78
79
80
81
82
83
84
85
86
87
88
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
require 'spec_helper'

# Tests parsing of individual, out of context, line types using #parse_line.

describe 'Parsing a line' do
  it 'should strip leading whitespace and set the :indent option' do
    expect(IniParse::Parser.parse_line('  [section]')).to \
      be_section_tuple(:any, {:indent => '  '})
  end

  it 'should raise an error if the line could not be matched' do
    expect { IniParse::Parser.parse_line('invalid line') }.to \
      raise_error(IniParse::ParseError)
  end

  it 'should parse using the types set in IniParse::Parser.parse_types' do
    begin
      # Remove last type.
      type = IniParse::Parser.parse_types.pop
      expect(type).not_to receive(:parse)
      IniParse::Parser.parse_line('[section]')
    ensure
      IniParse::Parser.parse_types << type
    end
  end

  # --
  # ==========================================================================
  #   Option lines.
  # ==========================================================================
  # ++

  describe 'with "k = v"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line('k = v')
    end

    it 'should return an option tuple' do
      expect(@tuple).to be_option_tuple('k', 'v')
    end

    it 'should set no indent, comment, offset or separator' do
      expect(@tuple.last[:indent]).to be_nil
      expect(@tuple.last[:comment]).to be_nil
      expect(@tuple.last[:comment_offset]).to be_nil
      expect(@tuple.last[:comment_sep]).to be_nil
    end
  end

  describe 'with "k = a value with spaces"' do
    it 'should return an option tuple' do
      expect(IniParse::Parser.parse_line('k = a value with spaces')).to \
        be_option_tuple('k', 'a value with spaces')
    end
  end

  describe 'with "k = v ; a comment "' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line('k = v ; a comment')
    end

    it 'should return an option tuple' do
      expect(@tuple).to be_option_tuple('k', 'v')
    end

    it 'should set the comment to "a comment"' do
      expect(@tuple).to be_option_tuple(:any, :any, :comment => 'a comment')
    end

    it 'should set the comment separator to ";"' do
      expect(@tuple).to be_option_tuple(:any, :any, :comment_sep => ';')
    end

    it 'should set the comment offset to 6' do
      expect(@tuple).to be_option_tuple(:any, :any, :comment_offset => 6)
    end
  end

  describe 'with "k = v;w;x y;z"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line('k = v;w;x y;z')
    end

    it 'should return an option tuple with the correct value' do
      expect(@tuple).to be_option_tuple(:any, 'v;w;x y;z')
    end

    it 'should not set a comment' do
      expect(@tuple.last[:comment]).to be_nil
      expect(@tuple.last[:comment_offset]).to be_nil
      expect(@tuple.last[:comment_sep]).to be_nil
    end
  end

  describe 'with "k = v;w ; a comment"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line('k = v;w ; a comment')
    end

    it 'should return an option tuple with the correct value' do
      expect(@tuple).to be_option_tuple(:any, 'v;w')
    end

    it 'should set the comment to "a comment"' do
      expect(@tuple).to be_option_tuple(:any, :any, :comment => 'a comment')
    end

    it 'should set the comment separator to ";"' do
      expect(@tuple).to be_option_tuple(:any, :any, :comment_sep => ';')
    end

    it 'should set the comment offset to 8' do
      expect(@tuple).to be_option_tuple(:any, :any, :comment_offset => 8)
    end
  end

  describe 'with "key=value"' do
    it 'should return an option tuple with the correct key and value' do
      expect(IniParse::Parser.parse_line('key=value')).to \
        be_option_tuple('key', 'value')
    end
  end

  describe 'with "key= value"' do
    it 'should return an option tuple with the correct key and value' do
      expect(IniParse::Parser.parse_line('key= value')).to \
        be_option_tuple('key', 'value')
    end
  end

  describe 'with "key =value"' do
    it 'should return an option tuple with the correct key and value' do
      expect(IniParse::Parser.parse_line('key =value')).to \
        be_option_tuple('key', 'value')
    end
  end

  describe 'with "key   =   value"' do
    it 'should return an option tuple with the correct key and value' do
      expect(IniParse::Parser.parse_line('key   =   value')).to \
        be_option_tuple('key', 'value')
    end
  end

  describe 'with "key ="' do
    it 'should return an option tuple with the correct key' do
      expect(IniParse::Parser.parse_line('key =')).to \
        be_option_tuple('key')
    end

    it 'should set the option value to nil' do
      expect(IniParse::Parser.parse_line('key =')).to \
        be_option_tuple(:any, nil)
    end
  end


  describe 'with "key = EEjDDJJjDJDJD233232=="' do
    it 'should include the "equals" in the option value' do
      expect(IniParse::Parser.parse_line('key = EEjDDJJjDJDJD233232==')).to \
        be_option_tuple('key', 'EEjDDJJjDJDJD233232==')
    end
  end

  describe 'with "key = ==EEjDDJJjDJDJD233232"' do
    it 'should include the "equals" in the option value' do
      expect(IniParse::Parser.parse_line('key = ==EEjDDJJjDJDJD233232')).to \
        be_option_tuple('key', '==EEjDDJJjDJDJD233232')
    end
  end

  describe 'with "key.two = value"' do
    it 'should return an option tuple with the correct key' do
      expect(IniParse::Parser.parse_line('key.two = value')).to \
        be_option_tuple('key.two')
    end
  end

  describe 'with "key/with/slashes = value"' do
    it 'should return an option tuple with the correct key' do
      expect(IniParse::Parser.parse_line('key/with/slashes = value')).to \
        be_option_tuple('key/with/slashes', 'value')
    end
  end

  describe 'with "key_with_underscores = value"' do
    it 'should return an option tuple with the correct key' do
      expect(IniParse::Parser.parse_line('key_with_underscores = value')).to \
        be_option_tuple('key_with_underscores', 'value')
    end
  end

  describe 'with "key-with-dashes = value"' do
    it 'should return an option tuple with the correct key' do
      expect(IniParse::Parser.parse_line('key-with-dashes = value')).to \
        be_option_tuple('key-with-dashes', 'value')
    end
  end

  describe 'with "key with spaces = value"' do
    it 'should return an option tuple with the correct key' do
      expect(IniParse::Parser.parse_line('key with spaces = value')).to \
        be_option_tuple('key with spaces', 'value')
    end
  end

  # --
  # ==========================================================================
  #   Section lines.
  # ==========================================================================
  # ++

  describe 'with "[section]"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line('[section]')
    end

    it 'should return a section tuple' do
      expect(@tuple).to be_section_tuple('section')
    end

    it 'should set no indent, comment, offset or separator' do
      expect(@tuple.last[:indent]).to be_nil
      expect(@tuple.last[:comment]).to be_nil
      expect(@tuple.last[:comment_offset]).to be_nil
      expect(@tuple.last[:comment_sep]).to be_nil
    end
  end

  describe 'with "[section with whitespace]"' do
    it 'should return a section tuple with the correct key' do
      expect(IniParse::Parser.parse_line('[section with whitespace]')).to \
        be_section_tuple('section with whitespace')
    end
  end

  describe 'with "[  section with surounding whitespace  ]"' do
    it 'should return a section tuple with the correct key' do
      expect(IniParse::Parser.parse_line('[  section with surounding whitespace  ]')).to \
        be_section_tuple('  section with surounding whitespace  ')
    end
  end

  describe 'with "[section] ; a comment"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line('[section] ; a comment')
    end

    it 'should return a section tuple' do
      expect(@tuple).to be_section_tuple('section')
    end

    it 'should set the comment to "a comment"' do
      expect(@tuple).to be_section_tuple(:any, :comment => 'a comment')
    end

    it 'should set the comment separator to ";"' do
      expect(@tuple).to be_section_tuple(:any, :comment_sep => ';')
    end

    it 'should set the comment offset to 10' do
      expect(@tuple).to be_section_tuple(:any, :comment_offset => 10)
    end
  end

  describe 'with "[section;with#comment;chars]"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line('[section;with#comment;chars]')
    end

    it 'should return a section tuple with the correct key' do
      expect(@tuple).to be_section_tuple('section;with#comment;chars')
    end

    it 'should not set a comment' do
      expect(@tuple.last[:indent]).to be_nil
      expect(@tuple.last[:comment]).to be_nil
      expect(@tuple.last[:comment_offset]).to be_nil
      expect(@tuple.last[:comment_sep]).to be_nil
    end
  end

  describe 'with "[section;with#comment;chars] ; a comment"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line('[section;with#comment;chars] ; a comment')
    end

    it 'should return a section tuple with the correct key' do
      expect(@tuple).to be_section_tuple('section;with#comment;chars')
    end

    it 'should set the comment to "a comment"' do
      expect(@tuple).to be_section_tuple(:any, :comment => 'a comment')
    end

    it 'should set the comment separator to ";"' do
      expect(@tuple).to be_section_tuple(:any, :comment_sep => ';')
    end

    it 'should set the comment offset to 29' do
      expect(@tuple).to be_section_tuple(:any, :comment_offset => 29)
    end
  end

  # --
  # ==========================================================================
  #   Comment lines.
  # ==========================================================================
  # ++

  describe 'with "; a comment"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line('; a comment')
    end

    it 'should return a comment tuple with the correct comment' do
      expect(@tuple).to be_comment_tuple('a comment')
    end

    it 'should set the comment separator to ";"' do
      expect(@tuple).to be_comment_tuple(:any, :comment_sep => ';')
    end

    it 'should set the comment offset to 0' do
      expect(@tuple).to be_comment_tuple(:any, :comment_offset => 0)
    end
  end

  describe 'with " ; a comment"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line(' ; a comment')
    end

    it 'should return a comment tuple with the correct comment' do
      expect(@tuple).to be_comment_tuple('a comment')
    end

    it 'should set the comment separator to ";"' do
      expect(@tuple).to be_comment_tuple(:any, :comment_sep => ';')
    end

    it 'should set the comment offset to 1' do
      expect(@tuple).to be_comment_tuple(:any, :comment_offset => 1)
    end
  end

  describe 'with ";"' do
    before(:all) do
      @tuple = IniParse::Parser.parse_line(';')
    end

    it 'should return a comment tuple with an empty value' do
      expect(@tuple).to be_comment_tuple('')
    end

    it 'should set the comment separator to ";"' do
      expect(@tuple).to be_comment_tuple(:any, :comment_sep => ';')
    end

    it 'should set the comment offset to 0' do
      expect(@tuple).to be_comment_tuple(:any, :comment_offset => 0)
    end
  end

  # --
  # ==========================================================================
  #   Blank lines.
  # ==========================================================================
  # ++

  describe 'with ""' do
    it 'should return a blank tuple' do
      expect(IniParse::Parser.parse_line('')).to be_blank_tuple
    end
  end

  describe 'with " "' do
    it 'should return a blank tuple' do
      expect(IniParse::Parser.parse_line(' ')).to be_blank_tuple
    end
  end
end