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
|
|
|
import tests.common |
14
|
|
|
|
15
|
|
|
from anyconfig.compat import OrderedDict |
16
|
|
|
from tests.common import to_bytes as _bytes |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
CNF_0 = OrderedDict((("DEFAULT", OrderedDict((("a", "0"), ("b", "bbb"), |
20
|
|
|
("c", "5")))), |
21
|
|
|
("sect0", OrderedDict((("a", "0"), ("b", "bbb"), |
22
|
|
|
("c", "5"), |
23
|
|
|
("d", "x,y,z")))))) |
24
|
|
|
CNF_1 = copy.deepcopy(CNF_0) |
25
|
|
|
CNF_1["sect0"]["d"] = CNF_1["sect0"]["d"].split() |
26
|
|
|
|
27
|
|
|
CNF_2 = OrderedDict((("a", 0.1), ("b", _bytes("bbb")), |
28
|
|
|
("sect0", OrderedDict((("c", [_bytes("x"), _bytes("y"), |
29
|
|
|
_bytes("z")]), ))))) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class MyDict(dict): |
33
|
|
|
pass |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
class HasParserTrait(object): |
37
|
|
|
|
38
|
|
|
psr = None # Must be a parser instance. |
39
|
|
|
cnf_s = None # Do. |
40
|
|
|
cnf = cnf_0 = CNF_0 |
41
|
|
|
cnf_1 = CNF_1 |
42
|
|
|
|
43
|
|
|
def is_ready(self): |
44
|
|
|
return self.psr is not None |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
class TestBase(unittest.TestCase, HasParserTrait): |
48
|
|
|
|
49
|
|
|
def _assert_dicts_equal(self, cnf, ordered=False, cls=None, ref=None): |
50
|
|
|
if ref is None: |
51
|
|
|
ref = self.cnf |
52
|
|
|
self.assertTrue(tests.common.dicts_equal(cnf, ref, ordered=ordered), |
53
|
|
|
"\n %r\nvs.\n %r" % (cnf, ref)) |
54
|
|
|
if cls is None: |
55
|
|
|
cls = OrderedDict if ordered else dict |
56
|
|
|
self.assertTrue(isinstance(cnf, cls), |
57
|
|
|
"cnf: %r vs. cls: %r" % (cnf, cls)) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
class Test_10_dumps_and_loads(TestBase): |
61
|
|
|
|
62
|
|
|
load_options = {} # Must be set to a dict in children classes. |
63
|
|
|
dump_options = {} # Do. |
64
|
|
|
|
65
|
|
|
def test_10_loads(self): |
66
|
|
|
if self.is_ready(): |
67
|
|
|
cnf = self.psr.loads(self.cnf_s) |
68
|
|
|
self.assertTrue(cnf) # Check if it's not None nor {}. |
69
|
|
|
self._assert_dicts_equal(cnf) |
70
|
|
|
|
71
|
|
|
def test_12_loads_with_options(self): |
72
|
|
|
if self.is_ready(): |
73
|
|
|
cnf = self.psr.loads(self.cnf_s, **self.load_options) |
74
|
|
|
self.assertTrue(cnf) |
75
|
|
|
self._assert_dicts_equal(cnf) |
76
|
|
|
|
77
|
|
|
def test_14_loads_with_invalid_options(self): |
78
|
|
|
if self.is_ready(): |
79
|
|
|
cnf = self.psr.loads(self.cnf_s, not_exist_option_a=True) |
80
|
|
|
self.assertTrue(cnf) |
81
|
|
|
self._assert_dicts_equal(cnf) |
82
|
|
|
|
83
|
|
|
def test_16_loads_with_ac_ordered_option(self): |
84
|
|
|
if self.is_ready(): |
85
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_ordered=True) |
86
|
|
|
self.assertTrue(cnf) |
87
|
|
|
self._assert_dicts_equal(cnf, ordered=self.psr.ordered()) |
88
|
|
|
|
89
|
|
|
def test_18_loads_with_ac_dict_option(self): |
90
|
|
|
if self.is_ready(): |
91
|
|
|
cnf = self.psr.loads(self.cnf_s, ac_dict=MyDict) |
92
|
|
|
self.assertTrue(cnf) |
93
|
|
|
self._assert_dicts_equal(cnf, cls=MyDict) |
94
|
|
|
# for debug: |
95
|
|
|
# raise RuntimeError("psr=%r, cnf=%r" % (self.psr, self.cnf)) |
96
|
|
|
|
97
|
|
|
def test_20_loads_with_dict_option(self): |
98
|
|
|
if self.is_ready(): |
99
|
|
|
dopts = self.psr.dict_options() |
100
|
|
|
if dopts: |
101
|
|
|
opts = {dopts[0]: MyDict} |
102
|
|
|
cnf = self.psr.loads(self.cnf_s, **opts) |
103
|
|
|
self.assertTrue(cnf) |
104
|
|
|
self._assert_dicts_equal(cnf, cls=MyDict) |
105
|
|
|
|
106
|
|
|
def test_30_dumps(self): |
107
|
|
|
if self.is_ready(): |
108
|
|
|
cnf_s = self.psr.dumps(self.cnf) |
109
|
|
|
self.assertTrue(cnf_s) # Check if it's not empty. |
110
|
|
|
cnf = self.psr.loads(cnf_s) |
111
|
|
|
self.assertTrue(cnf) |
112
|
|
|
self._assert_dicts_equal(cnf) |
113
|
|
|
|
114
|
|
|
def test_32_dumps_with_options(self): |
115
|
|
|
if self.is_ready(): |
116
|
|
|
cnf = self.psr.loads(self.psr.dumps(self.cnf, **self.dump_options)) |
117
|
|
|
self._assert_dicts_equal(cnf) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
class TestBaseWithIO(TestBase): |
121
|
|
|
|
122
|
|
|
def setUp(self): |
123
|
|
|
super(TestBaseWithIO, self).setUp() |
124
|
|
|
if self.is_ready(): |
125
|
|
|
self.workdir = tests.common.setup_workdir() |
126
|
|
|
|
127
|
|
|
exts = self.psr.extensions() |
128
|
|
|
ext = exts[0] if exts else "conf" |
129
|
|
|
self.cnf_path = os.path.join(self.workdir, "cnf_0." + ext) |
130
|
|
|
|
131
|
|
|
with self.psr.wopen(self.cnf_path) as out: |
132
|
|
|
out.write(self.cnf_s) |
133
|
|
|
|
134
|
|
|
def tearDown(self): |
135
|
|
|
if self.is_ready(): |
136
|
|
|
tests.common.cleanup_workdir(self.workdir) |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
class Test_20_dump_and_load(TestBaseWithIO): |
140
|
|
|
|
141
|
|
|
def test_10_load(self): |
142
|
|
|
if self.is_ready(): |
143
|
|
|
cnf = self.psr.load(self.cnf_path) |
144
|
|
|
self.assertTrue(cnf) |
145
|
|
|
self._assert_dicts_equal(cnf) |
146
|
|
|
|
147
|
|
|
def test_12_load_from_stream(self): |
148
|
|
|
if self.is_ready(): |
149
|
|
|
with self.psr.ropen(self.cnf_path) as strm: |
150
|
|
|
cnf = self.psr.load(strm) |
151
|
|
|
|
152
|
|
|
self.assertTrue(cnf) |
153
|
|
|
self._assert_dicts_equal(cnf) |
154
|
|
|
|
155
|
|
|
def test_14_load_with_ac_ordered_option(self): |
156
|
|
|
if self.is_ready(): |
157
|
|
|
cnf = self.psr.load(self.cnf_path, ac_ordered=True) |
158
|
|
|
self.assertTrue(cnf) |
159
|
|
|
self._assert_dicts_equal(cnf, ordered=self.psr.ordered()) |
160
|
|
|
|
161
|
|
|
def test_16_load_with_ac_dict_option(self): |
162
|
|
|
if self.is_ready(): |
163
|
|
|
cnf = self.psr.load(self.cnf_path, ac_dict=MyDict) |
164
|
|
|
self.assertTrue(cnf) |
165
|
|
|
self._assert_dicts_equal(cnf, cls=MyDict) |
166
|
|
|
|
167
|
|
|
def test_30_dump(self): |
168
|
|
|
if self.is_ready(): |
169
|
|
|
self.psr.dump(self.cnf, self.cnf_path) |
170
|
|
|
cnf = self.psr.load(self.cnf_path) |
171
|
|
|
self.assertTrue(cnf) |
172
|
|
|
self._assert_dicts_equal(cnf) |
173
|
|
|
|
174
|
|
|
def test_32_dump_to_stream(self): |
175
|
|
|
if self.is_ready(): |
176
|
|
|
with self.psr.wopen(self.cnf_path) as strm: |
177
|
|
|
self.psr.dump(self.cnf, strm) |
178
|
|
|
|
179
|
|
|
cnf = self.psr.load(self.cnf_path) |
180
|
|
|
self.assertTrue(cnf) |
181
|
|
|
self._assert_dicts_equal(cnf) |
182
|
|
|
|
183
|
|
|
# vim:sw=4:ts=4:et: |
184
|
|
|
|