This repository has been archived by the owner on Aug 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_ihih.py
264 lines (166 loc) · 7.12 KB
/
test_ihih.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
# vim:set fileencoding=utf8:
import unittest
import tempfile
import ihih
class SimpleConf(unittest.TestCase):
_key = 0
def setUp(self):
self.ihih = ihih.IHIH(())
def key(self, new=False):
if new:
self._key+= 1
return self._key
def test_value_default(self):
conf = self.ihih.__class__((), default=1)
self.assertTrue('default' in conf)
self.assertEqual(conf, {'default': 1})
def test_value_simple(self):
self.ihih[self.key(True)] = 'simple value'
self.assertEqual(self.ihih[self.key()], 'simple value')
def test_quoting_simple(self):
self.ihih[self.key(True)] = '''simple 'quoted' "value"'''
self.assertEqual(self.ihih[self.key()], 'simple quoted value')
def test_quoting_interleaved(self):
self.ihih[self.key(True)] = '''interleaved: 'a"'"'b"'''
self.assertEqual(self.ihih[self.key()], '''interleaved: a"'b''')
def test_quoting_escaped(self):
self.ihih[self.key(True)] = r'''"\"''"'''
self.assertEqual(self.ihih[self.key()], '"\'\'')
def test_escaping(self):
self.ihih[self.key(True)] = r'\r\n\0\"\$'
self.assertEqual(self.ihih[self.key()], r'\r\n\0"\$')
def test_file_loading(self):
tmp_a = tempfile.NamedTemporaryFile(bufsize=0)
tmp_a.write('debug = 1\nvar = x y')
tmp_b = tempfile.NamedTemporaryFile(bufsize=0)
tmp_b.write(' debug = 0 \nfoo = bar # comment\n\n#var = c')
conf = self.ihih.__class__((tmp_a.name, tmp_b.name))
self.assertEqual(conf['debug'], '0')
self.assertEqual(conf['var'], 'x y')
self.assertEqual(conf['foo'], 'bar')
tmp_a.close()
tmp_b.close()
def test_file_loading_unicode(self):
tmp = tempfile.NamedTemporaryFile(bufsize=0)
tmp.write(u'größe = 10.24\nwährung = €'.encode('utf8'))
conf = self.ihih.__class__(tmp.name)
self.assertEqual(conf[u'größe'], '10.24')
self.assertEqual(conf[u'währung'], '\xe2\x82\xac')
self.assertEqual(conf.get_unicode(u'währung'), u'€')
tmp.close()
def test_type_conversion(self):
f = 1.1
self.ihih[f] = f
self.assertEqual(self.ihih[f], str(f))
self.assertEqual(self.ihih[str(f)], str(f))
self.assertEqual(self.ihih.get(f), str(f))
self.assertEqual(self.ihih.get_float(f), f)
def test_comments(self):
self.ihih[self.key(True)] = 'value # with comment'
self.assertEqual(self.ihih[self.key()], 'value')
self.ihih[self.key(True)] = 'value // with comment'
self.assertEqual(self.ihih[self.key()], 'value')
def test_comments_escaped(self):
self.ihih[self.key(True)] = r'value with \# # and a comment'
self.assertEqual(self.ihih[self.key()], 'value with #')
self.ihih[self.key(True)] = r'value with \// /\/ \/\/ // and a comment'
self.assertEqual(self.ihih[self.key()], 'value with // // //')
def test_quoting_comments(self):
example_url = 'http://example.com/'
self.ihih[self.key(True)] = example_url
self.assertEqual(self.ihih[self.key()], 'http:')
self.ihih[self.key(True)] = '"' + example_url + '"'
self.assertEqual(self.ihih[self.key()], example_url)
self.ihih[self.key(True)] = "'" + example_url + "'"
self.assertEqual(self.ihih[self.key()], example_url)
def test_comments_quotes(self):
self.ihih[self.key(True)] = 'a # b "c"'
self.assertEqual(self.ihih[self.key()], 'a')
self.ihih[self.key(True)] = 'a#b "c"'
self.assertEqual(self.ihih[self.key()], 'a')
def test_get_bool(self):
self.ihih[self.key(True)] = 'yes # comment'
self.assertEqual(self.ihih.get_bool(self.key()), True)
self.ihih[self.key(True)] = 'False # comment'
self.assertEqual(self.ihih.get_bool(self.key()), False)
self.ihih[self.key(True)] = 'unknown'
self.assertEqual(self.ihih.get_bool(self.key(), 'na'), 'na')
self.ihih[self.key(True)] = ''
self.assertEqual(self.ihih.get_bool(self.key()), None)
def test_get_float(self):
self.ihih[self.key(True)] = '3.14'
self.assertEqual(self.ihih.get_float(self.key()), 3.14)
self.ihih[self.key(True)] = '314e-2'
self.assertEqual(self.ihih.get_float(self.key()), 3.14)
self.ihih[self.key(True)] = '-0'
self.assertEqual(self.ihih.get_float(self.key()), -0.0)
def test_get_int(self):
self.ihih[self.key(True)] = '42'
self.assertEqual(self.ihih.get_int(self.key()), 42)
self.ihih[self.key(True)] = ' 42 '
self.assertEqual(self.ihih.get_int(self.key()), 42)
self.ihih[self.key(True)] = '2a'
self.assertEqual(self.ihih.get_int(self.key(), base=16), 42)
self.ihih[self.key(True)] = ' 0x2a '
self.assertEqual(self.ihih.get_int(self.key(), base=16), 42)
self.ihih[self.key(True)] = '52'
self.assertEqual(self.ihih.get_int(self.key(), base=8), 42)
self.ihih[self.key(True)] = ' 052 '
self.assertEqual(self.ihih.get_int(self.key(), base=8), 42)
self.ihih[self.key(True)] = '42.0'
self.assertRaises(ValueError, self.ihih.get_int, self.key())
class Interpolated(SimpleConf):
def setUp(self):
self.ihih = ihih.IHIHI(())
def test_escaping(self):
self.ihih[self.key(True)] = r'\r\n\0\"\$'
self.assertEqual(self.ihih[self.key()], r'\r\n\0"$')
def test_variable_interpolation(self):
self.ihih['a'] = r'simple \"value\"'
self.assertEqual(self.ihih['a'], r'simple "value"')
self.ihih['b'] = r'\$a=$a=$a'
self.assertEqual(self.ihih['b'], r'$a=%(a)s=%(a)s' % self.ihih, 'simple interpolation')
self.ihih['c'] = r'$d $a'
self.assertEqual(self.ihih['c'], r' %(a)s' % self.ihih, 'interpolation with unknown value')
self.ihih['d'] = r'd'
self.assertEqual(self.ihih['c'], r'd %(a)s' % self.ihih, 'interpolation lazy resolution')
def test_variable_interpolation_recursion(self):
self.ihih['a'] = r'a'
self.ihih['b'] = r'\$b=$b'
self.assertEqual(self.ihih['b'], r'$b=', 'direct recursion')
self.ihih['c'] = r'd[$d]'
self.ihih['d'] = r'c[$c]'
self.assertEqual(self.ihih['c'], r'd[c[]]', 'indirect recursion')
self.assertEqual(self.ihih['d'], r'c[d[]]', 'indirect recursion')
def test_variable_boundaries(self):
self.ihih['a'] = 'var:a'
self.ihih['{a}'] = 'var:{a}'
self.ihih['a b'] = 'var:{a b}'
self.ihih['a_b'] = 'var:a_b'
self.ihih['a.b'] = 'var:a.b'
self.ihih['a:b'] = 'var:a:b'
self.ihih[self.key(True)] = r'$a=${a}'
self.assertEqual(self.ihih[self.key()], r'%(a)s=%(a)s' % self.ihih)
self.ihih[self.key(True)] = r'$a_b=${a_b}'
self.assertEqual(self.ihih[self.key()], r'%(a_b)s=%(a_b)s' % self.ihih)
self.ihih[self.key(True)] = r'$a.b=${a.b}'
self.assertEqual(self.ihih[self.key()], r'%(a.b)s=%(a.b)s' % self.ihih)
self.ihih[self.key(True)] = r'$a:b=${a:b}'
self.assertEqual(self.ihih[self.key()], r'%(a:b)s=%(a:b)s' % self.ihih)
self.ihih[self.key(True)] = r'$a b!=${a b}'
self.assertEqual(self.ihih[self.key()], r'%(a)s b!=%(a b)s' % self.ihih)
self.ihih[self.key(True)] = r'${{a}}'
self.assertEqual(self.ihih[self.key()], r'}')
self.ihih[self.key(True)] = r'${{a\}}=${\{a\}}'
self.assertEqual(self.ihih[self.key()], r'%({a})s=%({a})s' % self.ihih)
def test_variable_unicode(self):
self.ihih[u'größe'] = 10.24
self.ihih[u'währung'] = u'€'
self.ihih[self.key(True)] = u'$größe $währung'
self.assertEqual(self.ihih.get_unicode(self.key()), u'10.24 €')
def test_variable_quoting(self):
self.ihih['a'] = 'AA'
self.ihih['b'] = ''' "[$a]" '[$a]' '''
self.assertEqual(self.ihih['b'], ' [AA] [$a] ')
if __name__ == '__main__':
unittest.main()