-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_confparse.py
448 lines (357 loc) · 12.8 KB
/
test_confparse.py
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import os, unittest
from confparse import properties, ini
from tempfile import mkstemp
from test import test_support
class propertiesFormatTestCase(unittest.TestCase):
def setUp(self):
self.line=properties.line
options=(("b=c ", ('b','c',None)),
("c=d#e", ('c','d#e',None)),
("d=e;f", ('d','e;f',None)),
("e=f=g", ('e','f=g',None)),
("f=g #cmt1", ('f','g',' #cmt1')),
("g =h #cmt2", ('g','h',' #cmt2')),
("i= j #cmt3", ('i','j',' #cmt3')),
("j = k #cmt4", ('j','k',' #cmt4')),
("k=l\t #cmt5", ('k','l','\t #cmt5')),
("l=m ;cmt6", ('l','m',' ;cmt6')),
("m=n \t ;cmt7", ('m','n',' \t ;cmt7')),
('n o=p', ('n o','p',None)),
('p=q r', ('p','q r',None)),
(' r s = t u \t# cmt', ('r s','t u',None)),
)
def test_options(self):
'''Parsing option'''
for (i,(o,v,c)) in self.options:
m=self.line.match(i)
eq=self.assertEqual
eq( m.group( 'option' ), o)
eq( m.group( 'value' ), v)
# eq( m.group( 'optcmt' ), c)
comments=("#cmt", " \t\t#cmt", " # \tcmt", ";cmt",
" ; cmt"," \t\t;cmt","####cmt" )
def test_commentsandwhitespaces(self):
'''Parsing comments and blank lines'''
for i in self.comments:
self.assertEqual( self.line.match(i).group('cmted'), "cmt" )
broken_lines=("a","a a","[a]","[a","[]","=b","a # =b",)
def test_brokenlines(self):
'''Broken lines'''
for i in self.broken_lines:
self.assertEqual( self.line.match(i), None )
continuations=((" a #cmt1", 'a'),
(" \t [ab]\t #cmt2", '[ab]'),
(" m",'m'),)
def test_continuation(self):
'''Line continuations'''
for (i, c) in self.continuations:
m=self.line.match(i)
self.assertEqual( m.group( 'cont' ), c)
# What is this line, is it an option, a comment, a continuation, or a section?
# it is a sectption, or maybe a continuamment!
def test_ambiguous(self):
'''Ambiguous corner case'''
m=self.line.match
eq=self.assertEqual
eq( m("a = #b").group('option'), 'a')
eq( m(" l=m").group('option'), 'l')
eq( m("[a=b]").group('option'), '[a')
eq( m("a = b = c").group('option'), 'a')
eq( m(" # cmt").group('cmted'), 'cmt')
eq( m(" # a =b").group('cmted'), 'a =b')
eq( m(" # [a=b]").group('cmted'),'[a=b]')
class propertiesTestCase(unittest.TestCase):
def setUp(self):
fd,self.keyvalfile=mkstemp()
os.write(fd, """
abc = val1 # comment
# commentaire
abd = le zouuuuk ; Houu its a comment
""")
os.close(fd)
fd,self.emptyfile=mkstemp()
os.close(fd)
def tearDown(self):
os.remove(self.keyvalfile)
os.remove(self.emptyfile)
def test_constructors(self):
'''Constructors'''
eq=self.assertEqual
eq( properties().keys(), [] )
i=properties( answer=42, witch='guilty' )
eq( i['answer'], '42' )
eq( i['witch'], 'guilty' )
i=properties( self.keyvalfile, answer=43, witch='alwaysguilty' )
eq( i['abc'], 'val1' )
eq( i['answer'], '43' )
eq( i['witch'], 'alwaysguilty' )
i=properties( {'toto':'tata', 'titi':'toutou' }, answer=43, witch='alwaysguilty' )
eq( i['toto'], 'tata' )
eq( i['titi'], 'toutou' )
eq( i['answer'], '43' )
def test_dustbin(self):
'''Deleting values'''
i=properties()
del i['tata']
del i['toto']
i['toto'] = '45'
self.assertEqual( i['toto'], '45' )
self.assertEqual( 'tata' in i.dustbin, True)
self.assertEqual( 'toto' not in i.dustbin, True)
def test_variables(self):
'''Variables'''
i=properties()
i['tata']= 1
i['toto']= "$tata"
i['titi'] ="123$toto"
self.assertEqual( i['tata'], '1' )
self.assertEqual( i['toto'], '$tata')
self.assertEqual( i.get('toto'), '1')
self.assertEqual( i.get('titi'), '1231')
def test_read(self):
'''Read'''
i=properties()
i.read(self.keyvalfile)
self.assertEqual(i['abc'], 'val1')
self.assertEqual(i['abd'], 'le zouuuuk')
def test_write(self):
'''Write'''
kv=properties()
kv['answer'], kv['witch'] = 43, 'alwaysguilty'
kv['tata']=3
del kv['tata']
del kv['witch']
kv.write(self.emptyfile)
correct='''answer=43
'''
self.assertEqual(file(self.emptyfile).read(),correct)
def test_update(self):
'''Update'''
properties( {'abc':'bingo', 'titi':'toto'}, abd="Le hip hopppp").write(self.keyvalfile)
correct="""
abc = bingo # comment
# commentaire
abd = Le hip hopppp ; Houu its a comment
titi=toto
"""
self.assertEqual(file(self.keyvalfile).read(),correct)
def addvalues(self):
'''Adding new values to a file'''
raise NotImplemented
def multilines(self):
'''Multi lines values'''
raise NotImplemented
def orderingvalues(self):
'''Ordering values'''
raise NotImplemented
def proxy(self):
'''Accessing items as attributes'''
raise NotImplemented
def test_scenario1(self):
'''Scenario 1.'''
properties( {'abc':'changed','titi':'toutou'}, abd="Trans go#a").write(self.keyvalfile)
correct="""
abc = changed # comment
# commentaire
abd = Trans go#a ; Houu its a comment
titi=toutou
"""
self.assertEqual(file(self.keyvalfile).read(),correct)
def test_scenario2(self):
'''Scenario 2.'''
kv=properties( self.keyvalfile, abc="Shakira" )
kv['tata'] = "Mul,\nti,\nPle,\nlines"
kv['pain']='beurre'
del kv['pain']
del kv['abc']
kv.write()
correct="""
# commentaire
abd = le zouuuuk ; Houu its a comment
tata=Mul,
ti,
Ple,
lines
"""
self.assertEqual(file(self.keyvalfile).read(),correct)
class iniFormatTestCase(unittest.TestCase):
def setUp(self):
self.line=ini.line
sections=(("[a]", 'a'),
("[a-,_?]", 'a-,_?'),
('["a b"]', '"a b"'))
def test_sections(self):
'''Parsing sections'''
for (i,o) in self.sections:
self.assertEqual( self.line.match(i).group('section'), o )
options=(("b=c ", ('b','c',None)),
("c=d#e", ('c','d#e',None)),
("d=e;f", ('d','e;f',None)),
("e=f=g", ('e','f=g',None)),
("f=g #cmt1", ('f','g',' #cmt1')),
("g =h #cmt2", ('g','h',' #cmt2')),
("i= j #cmt3", ('i','j',' #cmt3')),
("j = k #cmt4", ('j','k',' #cmt4')),
("k=l\t #cmt5", ('k','l','\t #cmt5')),
("l=m ;cmt6", ('l','m',' ;cmt6')),
("m=n \t ;cmt7", ('m','n',' \t ;cmt7')),
('n o=p', ('n o','p',None)),
('p=q r', ('p','q r',None)),
(' r s = t u \t# cmt', ('r s','t u',None)),
)
def test_options(self):
'''Parsing option'''
for (i,(o,v,c)) in self.options:
m=self.line.match(i)
eq=self.assertEqual
eq( m.group( 'option' ), o)
eq( m.group( 'value' ), v)
# eq( m.group( 'optcmt' ), c)
comments=("#cmt", " \t\t#cmt", " # \tcmt", ";cmt",
" ; cmt"," \t\t;cmt","####cmt" )
def test_commentsandwhitespaces(self):
'''Parsing comments and blank lines'''
for i in self.comments:
self.assertEqual( self.line.match(i).group('cmted'), "cmt" )
broken_lines=("a","a a","[a","[]","=b","a # =b",)
def test_brokenlines(self):
'''Broken lines'''
for i in self.broken_lines:
self.assertEqual( self.line.match(i), None )
continuations=((" a #cmt1", 'a'),
(" \t [ab]\t #cmt2", '[ab]'),
(" m",'m'),)
def test_continuation(self):
'''Line continuations'''
for (i, c) in self.continuations:
m=self.line.match(i)
self.assertEqual( m.group( 'cont' ), c)
# What is this line, is it an option, a comment, a continuation, or a section?
# it is a sectption, or maybe a continuamment!
def test_ambiguous(self):
'''Ambiguous corner case'''
m=self.line.match
eq=self.assertEqual
eq( m("a = #b").group('option'), 'a')
eq( m(" l=m").group('option'), 'l')
eq( m("a = b = c").group('option'), 'a')
eq( m(" # cmt").group('cmted'), 'cmt')
eq( m(" # a =b").group('cmted'), 'a =b')
eq( m(" # [a=b]").group('cmted'),'[a=b]')
class iniTestCase(unittest.TestCase):
def setUp(self):
fd,self.inifile=mkstemp()
os.write(fd, """
[Foo Bar]
foo=bar
[Spacey Bar]
foo = bar
[Commented Bar]
foo: bar ; comment
[Long Line]
foo: this line is much, much longer than my editor
likes it.
[Section\\with$weird%characters[\t]
[Internationalized Stuff]
foo[bg]: Bulgarian
foo=Default
foo[en]=English
foo[de]=Deutsch
[Spaces]
key with spaces : value
another with spaces = splat!
""")
os.close(fd)
fd,self.emptyfile=mkstemp()
os.close(fd)
def tearDown(self):
os.remove(self.inifile)
os.remove(self.emptyfile)
def test_constructors(self):
'''Constructors'''
eq=self.assertEqual
eq( ini().keys(), [] )
i=ini( answer='42', witch='guilty' )
i["foo"]=properties(answer='43')
i["bar"]=properties(witch='innocent')
eq( i['foo']['answer'], '43' )
eq( i['bar']['witch'], 'innocent' )
eq( i.get('foo', 'answer'), '43' )
eq( i.get('bar', 'witch'), 'innocent')
eq( i.get('foo', 'witch'), 'guilty' )
eq( i.get('bar', 'answer'), '42' )
i=ini( self.inifile, answer=43, witch='alwaysguilty' )
eq( i['Foo Bar']['foo'], 'bar' )
eq( i.get('Foo Bar','answer'), 43 )
eq( i.get('Internationalized Stuff','witch'), 'alwaysguilty' )
eq( i.get('Internationalized Stuff','foo'), 'Default' )
i=ini( {'toto':'tata', 'titi':'toutou' }, answer=43, witch='alwaysguilty' )
i['tata']=properties()
eq( i.get('tata','toto'), 'tata' )
eq( i.get('tata','titi'), 'toutou' )
eq( i.get('tata','answer'), 43 )
def test_dustbin(self):
'''Dustbin'''
i=ini()
del i['tata']
del i['toto']
i['toto'] = '45'
self.assertEqual( i['toto'], '45' )
self.assertEqual( 'tata' in i.dustbin, True)
self.assertEqual( 'toto' not in i.dustbin, True)
def test_variables(self):
'''Variables'''
i=ini()
i['section']=properties()
i['section']['tata']= 1
i['section']['toto']= "$tata"
i['section']['titi'] ="123$toto"
self.assertEqual( i['section']['tata'], '1' )
self.assertEqual( i['section'].get('toto'), '1')
self.assertEqual( i['section'].get('titi'), '1231')
def test_read(self):
'''Read'''
eq=self.assertEqual
i=ini()
i.read(self.inifile)
eq( i.get('Internationalized Stuff','foo'), 'Default' )
eq( i['Foo Bar']['foo'], 'bar' )
def test_write(self):
'''Write'''
i=ini()
i['section']=properties()
i['section']['answer'], i['section']['witch'] = 43, 'alwaysguilty'
i['section']['tata']=3
del i['section']['tata']
del i['section']['witch']
i.write(self.emptyfile)
correct='''[section]\nanswer=43\n
'''
self.assertEqual(file(self.emptyfile).read(),correct)
def test_update(self):
'''Update'''
eq=self.assertEqual
i=ini(self.inifile)
i['Foo Bar']['foo'] = 'bloublou'
i['Internationalized Stuff']['witch'] = 'Burn burn !'
i.write(self.inifile)
i=ini(self.inifile)
eq( i['Foo Bar']['foo'], 'bloublou')
eq( i['Internationalized Stuff']['witch'], 'Burn burn !')
def test_scenario1(self):
'''Scenario 1.'''
eq=self.assertEqual
i=ini(self.inifile)
i['Foo Bar']['foo'] = 'bloublou'
i['Internationalized Stuff']['witch'] = 'Burn burn !'
i.write(self.inifile)
i=ini(self.inifile)
eq( i['Foo Bar']['foo'], 'bloublou')
eq( i['Internationalized Stuff']['witch'], 'Burn burn !')
def scenario2(self):
'''Scenario 2.'''
if __name__ == '__main__':
test_support.run_unittest( propertiesFormatTestCase )
test_support.run_unittest( propertiesTestCase )
test_support.run_unittest( iniFormatTestCase )
test_support.run_unittest( iniTestCase )