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