1
|
|
|
# |
2
|
|
|
# Copyright (C) 2012 - 2017 Satoru SATOH <ssato @ redhat.com> |
3
|
|
|
# License: MIT |
4
|
|
|
# |
5
|
|
|
# pylint: disable=missing-docstring,invalid-name,too-few-public-methods |
6
|
|
|
# pylint: disable=ungrouped-imports |
7
|
|
|
from __future__ import absolute_import |
8
|
|
|
|
9
|
|
|
import copy |
10
|
|
|
import os.path |
11
|
|
|
import unittest |
12
|
|
|
|
13
|
|
|
from os import linesep as lsep |
14
|
|
|
|
15
|
|
|
import tests.common |
16
|
|
|
import anyconfig.backends |
17
|
|
|
|
18
|
|
|
from anyconfig.compat import OrderedDict |
19
|
|
|
from tests.common import to_bytes as _bytes |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
CNF_0 = OrderedDict((("DEFAULT", OrderedDict((("a", "0"), ("b", "bbb"), |
23
|
|
|
("c", "5")))), |
24
|
|
|
("sect0", OrderedDict((("a", "0"), ("b", "bbb"), |
25
|
|
|
("c", "5"), |
26
|
|
|
("d", "x,y,z")))))) |
27
|
|
|
CNF_1 = copy.deepcopy(CNF_0) |
28
|
|
|
CNF_1["sect0"]["d"] = CNF_1["sect0"]["d"].split() |
29
|
|
|
|
30
|
|
|
CNF_2 = OrderedDict((("a", 0.1), ("b", _bytes("bbb")), |
31
|
|
|
("sect0", OrderedDict((("c", [_bytes("x"), _bytes("y"), |
32
|
|
|
_bytes("z")]), ))))) |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
class MyDict(dict): |
36
|
|
|
pass |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
class HasParserTrait(object): |
40
|
|
|
|
41
|
|
|
psr = None # Must be a parser instance. |
42
|
|
|
cnf_s = None # Do. |
43
|
|
|
cnf = cnf_0 = CNF_0 |
44
|
|
|
cnf_1 = CNF_1 |
45
|
|
|
|
46
|
|
|
def is_ready(self): |
47
|
|
|
return self.psr is not None |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
class TestBase(unittest.TestCase, HasParserTrait): |
51
|
|
|
|
52
|
|
|
def _to_ioinfo(self, path): |
53
|
|
|
ptype = self.psr.type() |
54
|
|
|
return anyconfig.backends.inspect_io_obj(path, forced_type=ptype) |
55
|
|
|
|
56
|
|
|
def _assert_dicts_equal(self, cnf, ordered=False, cls=None, ref=None): |
57
|
|
|
if ref is None: |
58
|
|
|
ref = self.cnf |
59
|
|
|
self.assertTrue(tests.common.dicts_equal(cnf, ref, ordered=ordered), |
60
|
|
|
"%s %r%svs.%s %r" % (lsep, cnf, lsep, lsep, ref)) |
61
|
|
|
# .. note:: |
62
|
|
|
# `cnf` may not be an instance of `cls` even if ac_dict option was |
63
|
|
|
# given because parsers may not allow customize dict class to used |
64
|
|
|
# for making results. |
65
|
|
|
if cls is None or not self.psr.dict_options(): |
66
|
|
|
cls = OrderedDict if ordered else dict |
67
|
|
|
self.assertTrue(isinstance(cnf, cls), |
68
|
|
|
"cnf=%r [type: %r], cls=%r" % (cnf, type(cnf), cls)) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
class Test_10_dumps_and_loads(TestBase): |
72
|
|
|
|
73
|
|
|
load_options = {} # Must be set to a dict in children classes. |
74
|
|
|
dump_options = {} # Do. |
75
|
|
|
empty_patterns = [('', {})] # Do. |
76
|
|
|
|
77
|
|
|
def test_10_loads(self): |
78
|
|
|
if self.is_ready(): |
79
|
|
|
cnf = self.psr.loads(self.cnf_s) |
80
|
|
|
self.assertTrue(cnf) # Check if it's not None nor {}. |
81
|
|
|
self._assert_dicts_equal(cnf) |
82
|
|
|
|
83
|
|
|
def test_12_loads_with_options(self): |
84
|
|
|
if self.is_ready(): |
85
|
|
|
cnf = self.psr.loads(self.cnf_s, **self.load_options) |
86
|
|
|
self.assertTrue(cnf) |
87
|
|
|
self._assert_dicts_equal(cnf) |
88
|
|
|
|
89
|
|
|
def test_14_loads_with_invalid_options(self): |
90
|
|
|
if self.is_ready(): |
91
|
|
|
cnf = self.psr.loads(self.cnf_s, not_exist_option_a=True) |
92
|
|
|
self.assertTrue(cnf) |
93
|
|
|
self._assert_dicts_equal(cnf) |
94
|
|
|
|
95
|
|
|
def test_16_loads_with_ac_ordered_option(self): |
96
|
|
|
if self.is_ready(): |
97
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_ordered=True) |
98
|
|
|
self.assertTrue(cnf) |
99
|
|
|
self._assert_dicts_equal(cnf, ordered=self.psr.ordered()) |
100
|
|
|
|
101
|
|
|
def test_18_loads_with_ac_dict_option(self): |
102
|
|
|
if self.is_ready(): |
103
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_dict=MyDict) |
104
|
|
|
self.assertTrue(cnf) |
105
|
|
|
self._assert_dicts_equal(cnf, cls=MyDict) |
106
|
|
|
# for debug: |
107
|
|
|
# raise RuntimeError("psr=%r, cnf=%r " |
108
|
|
|
# "[%r]" % (self.psr, cnf, type(cnf))) |
109
|
|
|
|
110
|
|
|
def test_20_loads_with_dict_option(self): |
111
|
|
|
if self.is_ready(): |
112
|
|
|
dopts = self.psr.dict_options() |
113
|
|
|
if dopts: |
114
|
|
|
opts = {dopts[0]: MyDict} |
115
|
|
|
cnf = self.psr.loads(self.cnf_s, **opts) |
116
|
|
|
self.assertTrue(cnf) |
117
|
|
|
self._assert_dicts_equal(cnf, cls=MyDict) |
118
|
|
|
|
119
|
|
|
def test_22_loads_empty_data(self): |
120
|
|
|
if self.is_ready(): |
121
|
|
|
for pat, exp in self.empty_patterns: |
122
|
|
|
cnf = self.psr.loads(pat) |
123
|
|
|
self.assertEqual(cnf, exp) |
124
|
|
|
|
125
|
|
|
def test_30_dumps(self): |
126
|
|
|
if self.is_ready(): |
127
|
|
|
cnf_s = self.psr.dumps(self.cnf) |
128
|
|
|
self.assertTrue(cnf_s) # Check if it's not empty. |
129
|
|
|
cnf = self.psr.loads(cnf_s) |
130
|
|
|
self.assertTrue(cnf) |
131
|
|
|
self._assert_dicts_equal(cnf) |
132
|
|
|
|
133
|
|
|
def test_32_dumps_with_options(self): |
134
|
|
|
if self.is_ready(): |
135
|
|
|
cnf = self.psr.loads(self.psr.dumps(self.cnf, **self.dump_options)) |
136
|
|
|
self._assert_dicts_equal(cnf) |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
class TestBaseWithIO(TestBase): |
140
|
|
|
|
141
|
|
|
def setUp(self): |
142
|
|
|
super(TestBaseWithIO, self).setUp() |
143
|
|
|
if self.is_ready(): |
144
|
|
|
self.workdir = tests.common.setup_workdir() |
145
|
|
|
|
146
|
|
|
exts = self.psr.extensions() |
147
|
|
|
ext = exts[0] if exts else "conf" |
148
|
|
|
self.cnf_path = os.path.join(self.workdir, "cnf_0." + ext) |
149
|
|
|
self.ioi = self._to_ioinfo(self.cnf_path) |
150
|
|
|
|
151
|
|
|
with self.psr.wopen(self.cnf_path) as out: |
152
|
|
|
out.write(self.cnf_s) |
153
|
|
|
|
154
|
|
|
def tearDown(self): |
155
|
|
|
if self.is_ready(): |
156
|
|
|
tests.common.cleanup_workdir(self.workdir) |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
class Test_20_dump_and_load(TestBaseWithIO): |
160
|
|
|
|
161
|
|
|
list_data = [dict(a=1, b=2), dict(a=2, b=3)] |
162
|
|
|
|
163
|
|
|
def test_10_load(self): |
164
|
|
|
if self.is_ready(): |
165
|
|
|
cnf = self.psr.load(self.ioi) |
166
|
|
|
self.assertTrue(cnf) |
167
|
|
|
self._assert_dicts_equal(cnf) |
168
|
|
|
|
169
|
|
|
def test_12_load_from_stream(self): |
170
|
|
|
if self.is_ready(): |
171
|
|
|
with self.psr.ropen(self.cnf_path) as strm: |
172
|
|
|
ioi = self._to_ioinfo(strm) |
173
|
|
|
cnf = self.psr.load(ioi) |
174
|
|
|
|
175
|
|
|
self.assertTrue(cnf) |
176
|
|
|
self._assert_dicts_equal(cnf) |
177
|
|
|
|
178
|
|
|
def test_14_load_with_ac_ordered_option(self): |
179
|
|
|
if self.is_ready(): |
180
|
|
|
cnf = self.psr.load(self.ioi, ac_ordered=True) |
181
|
|
|
self.assertTrue(cnf) |
182
|
|
|
self._assert_dicts_equal(cnf, ordered=self.psr.ordered()) |
183
|
|
|
|
184
|
|
|
def test_16_load_with_ac_dict_option(self): |
185
|
|
|
if self.is_ready(): |
186
|
|
|
cnf = self.psr.load(self.ioi, ac_dict=MyDict) |
187
|
|
|
self.assertTrue(cnf) |
188
|
|
|
self._assert_dicts_equal(cnf, cls=MyDict) |
189
|
|
|
|
190
|
|
|
def test_30_dump(self): |
191
|
|
|
if self.is_ready(): |
192
|
|
|
self.psr.dump(self.cnf, self.ioi) |
193
|
|
|
cnf = self.psr.load(self.ioi) |
194
|
|
|
self.assertTrue(cnf) |
195
|
|
|
self._assert_dicts_equal(cnf) |
196
|
|
|
|
197
|
|
|
def test_32_dump_to_stream(self): |
198
|
|
|
if self.is_ready(): |
199
|
|
|
with self.psr.wopen(self.cnf_path) as strm: |
200
|
|
|
ioi = self._to_ioinfo(strm) |
201
|
|
|
self.psr.dump(self.cnf, ioi) |
202
|
|
|
|
203
|
|
|
cnf = self.psr.load(self.ioi) |
204
|
|
|
self.assertTrue(cnf) |
205
|
|
|
self._assert_dicts_equal(cnf) |
206
|
|
|
|
207
|
|
|
def test_34_dump_and_load__list(self): |
208
|
|
|
if self.is_ready() and self.psr.allow_primitives(): |
209
|
|
|
self.psr.dump(self.list_data, self.ioi) |
210
|
|
|
cnf = self.psr.load(self.ioi, ac_dict=MyDict) |
211
|
|
|
self.assertTrue(cnf) |
212
|
|
|
self.assertEqual(cnf, self.list_data) |
213
|
|
|
|
214
|
|
|
# vim:sw=4:ts=4:et: |
215
|
|
|
|