|
1
|
|
|
# |
|
2
|
|
|
# Copyright (C) 2012 - 2017 Satoru SATOH <ssato @ redhat.com> |
|
3
|
|
|
# License: MIT |
|
4
|
|
|
# |
|
5
|
|
|
# pylint: disable=missing-docstring, protected-access |
|
6
|
|
|
from __future__ import absolute_import |
|
7
|
|
|
|
|
8
|
|
|
import os.path |
|
9
|
|
|
import unittest |
|
10
|
|
|
|
|
11
|
|
|
import anyconfig.backend.ini as TT |
|
12
|
|
|
import tests.common |
|
13
|
|
|
|
|
14
|
|
|
from anyconfig.compat import OrderedDict as ODict |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
CNF_0_S = """[DEFAULT] |
|
18
|
|
|
a: 0 |
|
19
|
|
|
b: bbb |
|
20
|
|
|
c: 5 |
|
21
|
|
|
|
|
22
|
|
|
[sect0] |
|
23
|
|
|
d: x,y,z |
|
24
|
|
|
""" |
|
25
|
|
|
|
|
26
|
|
|
CNF_0 = ODict((("DEFAULT", ODict((("a", "0"), ("b", "bbb"), ("c", "5")))), |
|
27
|
|
|
("sect0", ODict((("a", "0"), ("b", "bbb"), ("c", "5"), |
|
28
|
|
|
("d", "x,y,z")))))) |
|
29
|
|
|
CNF_1 = ODict((("DEFAULT", ODict((("a", 0), ("b", "bbb"), ("c", 5)))), |
|
30
|
|
|
("sect0", ODict((("a", 0), ("b", "bbb"), ("c", 5), |
|
31
|
|
|
("d", "x y z".split())))))) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class MyDict(ODict): |
|
35
|
|
|
pass |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
class TestBase(unittest.TestCase): |
|
39
|
|
|
|
|
40
|
|
|
psr_cls = TT.Parser |
|
41
|
|
|
cnf = CNF_0 |
|
42
|
|
|
cnf_s = CNF_0_S |
|
43
|
|
|
|
|
44
|
|
|
def setUp(self): |
|
45
|
|
|
self.psr = self.psr_cls() |
|
46
|
|
|
|
|
47
|
|
|
def ordered(self): |
|
48
|
|
|
return self.psr.ordered() |
|
49
|
|
|
|
|
50
|
|
|
def _assert_dicts_equal(self, cnf, ordered=False, ccls=dict, ref=False): |
|
51
|
|
|
if not ref: |
|
52
|
|
|
ref = self.cnf |
|
53
|
|
|
self.assertTrue(tests.common.dicts_equal(cnf, ref, ordered=ordered), |
|
54
|
|
|
"\n %r\nvs.\n %r" % (cnf, ref)) |
|
55
|
|
|
cls = ODict if self.ordered() and ordered else ccls |
|
56
|
|
|
self.assertTrue(isinstance(cnf, cls)) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
class Test10(TestBase): |
|
60
|
|
|
|
|
61
|
|
|
load_options = dict(allow_no_value=False, defaults=None) |
|
62
|
|
|
dump_options = dict() |
|
63
|
|
|
|
|
64
|
|
|
def test_10_loads(self): |
|
65
|
|
|
cnf = self.psr.loads(self.cnf_s) |
|
66
|
|
|
self._assert_dicts_equal(cnf) |
|
67
|
|
|
|
|
68
|
|
|
def test_12_loads__w_options(self): |
|
69
|
|
|
cnf = self.psr.loads(self.cnf_s, **self.load_options) |
|
70
|
|
|
self._assert_dicts_equal(cnf) |
|
71
|
|
|
|
|
72
|
|
|
def test_20_dumps(self): |
|
73
|
|
|
cnf_s = self.psr.dumps(self.cnf) |
|
74
|
|
|
self.assertTrue(cnf_s) |
|
75
|
|
|
cnf = self.psr.loads(cnf_s) |
|
76
|
|
|
self._assert_dicts_equal(cnf) |
|
77
|
|
|
|
|
78
|
|
|
def test_22_dumps__w_options(self): |
|
79
|
|
|
cnf = self.psr.loads(self.psr.dumps(self.cnf, **self.dump_options)) |
|
80
|
|
|
self._assert_dicts_equal(cnf) |
|
81
|
|
|
|
|
82
|
|
|
def test_30_loads_with_order_kept(self): |
|
83
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_ordered=True) |
|
84
|
|
|
self._assert_dicts_equal(cnf, ordered=self.ordered()) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
class Test11(Test10): |
|
88
|
|
|
|
|
89
|
|
|
cnf = CNF_1 |
|
90
|
|
|
|
|
91
|
|
|
def test_10_loads(self): |
|
92
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_parse_value=True) |
|
93
|
|
|
self._assert_dicts_equal(cnf) |
|
94
|
|
|
|
|
95
|
|
|
def test_12_loads__w_options(self): |
|
96
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_parse_value=True, |
|
97
|
|
|
**self.load_options) |
|
98
|
|
|
self._assert_dicts_equal(cnf) |
|
99
|
|
|
|
|
100
|
|
|
def test_14_loads__w_dict_factory(self): |
|
101
|
|
|
cnf = self.psr.loads(self.cnf_s, dict_type=MyDict) |
|
102
|
|
|
self._assert_dicts_equal(cnf, ccls=MyDict, ref=CNF_0) |
|
103
|
|
|
|
|
104
|
|
|
def test_15_loads__w_ac_dict_option(self): |
|
105
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_dict=MyDict) |
|
106
|
|
|
self._assert_dicts_equal(cnf, ccls=MyDict, ref=CNF_0) |
|
107
|
|
|
|
|
108
|
|
|
def test_16_loads__w_dict_factory(self): |
|
109
|
|
|
if self.cnf: |
|
110
|
|
|
return # FIXME. |
|
111
|
|
|
cnf = self.psr.loads(self.cnf_s, dict_type=MyDict, ac_parse_value=True) |
|
112
|
|
|
self._assert_dicts_equal(cnf, ccls=MyDict) |
|
113
|
|
|
|
|
114
|
|
|
def test_20_dumps(self): |
|
115
|
|
|
cnf = self.psr.loads(self.psr.dumps(self.cnf), ac_parse_value=True) |
|
116
|
|
|
self._assert_dicts_equal(cnf) |
|
117
|
|
|
|
|
118
|
|
|
def test_22_dumps__w_options(self): |
|
119
|
|
|
cnf = self.psr.loads(self.psr.dumps(self.cnf, **self.dump_options), |
|
120
|
|
|
ac_parse_value=True) |
|
121
|
|
|
self._assert_dicts_equal(cnf) |
|
122
|
|
|
|
|
123
|
|
|
def test_30_loads_with_order_kept(self): |
|
124
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_parse_value=True, ac_ordered=True) |
|
125
|
|
|
self._assert_dicts_equal(cnf, ordered=self.ordered()) |
|
126
|
|
|
|
|
127
|
|
|
def test_32_loads_with_order_kept(self): |
|
128
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_parse_value=True, dict_type=ODict) |
|
129
|
|
|
self._assert_dicts_equal(cnf, ordered=True) |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
class Test12(Test10): |
|
133
|
|
|
|
|
134
|
|
|
test_10_loads = test_12_loads__w_options = lambda: True |
|
135
|
|
|
test_20_loads = test_22_loads__w_options = lambda: True |
|
136
|
|
|
|
|
137
|
|
|
def test_10_loads__invalid_input(self): |
|
138
|
|
|
invalid_ini = "key=name" |
|
139
|
|
|
self.assertRaises(Exception, self.psr.loads, invalid_ini) |
|
140
|
|
|
|
|
141
|
|
|
def test_20_dumps__format(self): |
|
142
|
|
|
ref = self.cnf_s.replace(': ', ' = ') |
|
143
|
|
|
self.assertEqual(self.psr.dumps(self.cnf), ref) |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
class Test20(TestBase): |
|
147
|
|
|
|
|
148
|
|
|
cnf_fn = "conf0.ini" |
|
149
|
|
|
|
|
150
|
|
|
def setUp(self): |
|
151
|
|
|
self.psr = self.psr_cls() |
|
152
|
|
|
self.workdir = tests.common.setup_workdir() |
|
153
|
|
|
self.cpath = os.path.join(self.workdir, self.cnf_fn) |
|
154
|
|
|
with self.psr.wopen(self.cpath) as out: |
|
155
|
|
|
out.write(self.cnf_s) |
|
156
|
|
|
|
|
157
|
|
|
def tearDown(self): |
|
158
|
|
|
tests.common.cleanup_workdir(self.workdir) |
|
159
|
|
|
|
|
160
|
|
|
def _assert_dicts_equal(self, cnf, ordered=False, instance_check=False, |
|
161
|
|
|
ref=False): |
|
162
|
|
|
if not ref: |
|
163
|
|
|
ref = self.cnf |
|
164
|
|
|
self.assertTrue(tests.common.dicts_equal(cnf, ref), |
|
165
|
|
|
"\n %r\nvs.\n %r" % (cnf, ref)) |
|
166
|
|
|
if instance_check: |
|
167
|
|
|
cls = ODict if self.ordered() and ordered else dict |
|
168
|
|
|
self.assertTrue(isinstance(cnf, cls)) |
|
169
|
|
|
|
|
170
|
|
|
def test_10_load(self): |
|
171
|
|
|
cnf = self.psr.load(self.cpath) |
|
172
|
|
|
self._assert_dicts_equal(cnf) |
|
173
|
|
|
|
|
174
|
|
|
def test_20_dump(self): |
|
175
|
|
|
self.psr.dump(self.cnf, self.cpath) |
|
176
|
|
|
cnf = self.psr.load(self.cpath) |
|
177
|
|
|
self._assert_dicts_equal(cnf) |
|
178
|
|
|
|
|
179
|
|
|
def test_30_load__from_stream(self): |
|
180
|
|
|
with self.psr.ropen(self.cpath) as strm: |
|
181
|
|
|
cnf = self.psr.load(strm) |
|
182
|
|
|
|
|
183
|
|
|
self._assert_dicts_equal(cnf) |
|
184
|
|
|
|
|
185
|
|
|
def test_40_dump__to_stream(self): |
|
186
|
|
|
with self.psr.wopen(self.cpath) as strm: |
|
187
|
|
|
self.psr.dump(self.cnf, strm) |
|
188
|
|
|
|
|
189
|
|
|
cnf = self.psr.load(self.cpath) |
|
190
|
|
|
self._assert_dicts_equal(cnf) |
|
191
|
|
|
|
|
192
|
|
|
def test_50_load_with_order_kept(self): |
|
193
|
|
|
cnf = self.psr.load(self.cpath, ac_ordered=True) |
|
194
|
|
|
if self.ordered(): |
|
195
|
|
|
self.assertTrue(list(cnf.keys()), list(self.cnf.keys())) |
|
196
|
|
|
else: |
|
197
|
|
|
self.assertTrue(tests.common.dicts_equal(cnf, self.cnf), str(cnf)) |
|
198
|
|
|
|
|
199
|
|
|
# vim:sw=4:ts=4:et: |
|
200
|
|
|
|