4 # Copyright (C) 2010-2018 CEA/DEN
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 import initializeTest # set PATH etc for test
26 import src.debug as DBG # Easy print stderr (for DEBUG only)
27 import src.pyconf as PYF # 0.3.7
34 stream : "sys.stderr" # modified
39 stream : "sys.stdout" # modified
44 stream : $messages[0].stream
58 bb: "Herve" # avoid Hervé -> 'utf8' codec can't decode byte
63 bb: "Hervé" # avoid Hervé -> 'utf8' codec can't decode byte
111 class TestCase(unittest.TestCase):
112 "Test the pyconf.py"""
115 # one shot setUp() for this TestCase
116 # DBG.push_debug(True)
117 # SAT.setNotLocale() # test english
121 # pyconf.py doc example 0.3.7
122 # https://www.red-dove.com/config-doc/ is 0.3.9 !
123 # which, when run, would yield the console output:
130 inStream = DBG.InStream(_EXAMPLES[1])
131 cfg = PYF.Config(inStream)
133 for m in cfg.messages:
134 res += '%s, %s\n' % (m.message, m.name)
135 self.assertEqual(res, expected)
136 outStream = DBG.OutStream()
137 cfg.__save__(outStream) # sat renamed save() in __save__()
138 res = outStream.value
139 DBG.write("test_010 cfg", res)
140 self.assertTrue("name : 'Harry'" in res)
141 self.assertTrue("name : 'Ruud'" in res)
142 self.assertTrue("name : 'Yves'" in res)
146 self.assertEqual(str(cfg), '{}')
147 self.assertEqual(cfg.__repr__(), '{}')
149 self.assertEqual(str(cfg), "{'aa': '1111'}")
151 self.assertTrue("'bb': 2222" in str(cfg))
152 self.assertTrue("'aa': '1111'" in str(cfg))
154 self.assertTrue("'cc': 3333." in str(cfg))
157 inStream = DBG.InStream(_EXAMPLES[2])
158 cfg = PYF.Config(inStream)
159 self.assertEqual(str(cfg), "{'aa': 111, 'bb': $aa + 222}")
160 self.assertEqual(cfg.aa, 111)
161 self.assertEqual(cfg.bb, 333)
164 inStream = DBG.InStream(_EXAMPLES[3])
165 cfg = PYF.Config(inStream)
166 self.assertEqual(cfg.aa, "Yves")
167 self.assertEqual(cfg.bb, "Herve")
168 self.assertEqual(type(cfg.bb), str)
169 cfg.bb = "Hervé" # try this
170 self.assertEqual(type(cfg.bb), str)
171 self.assertEqual(cfg.bb, "Hervé")
174 # make Hervé valid only with pyconf.py as 0.3.9
175 inStream = DBG.InStream(_EXAMPLES[4])
176 outStream = DBG.OutStream()
177 with self.assertRaises(Exception):
178 cfg = PYF.Config(inStream)
180 return # TODO only with pyconf.py as 0.3.9
181 cfg.save(outStream) # OK
182 # TODO: cfg = PYF.Config(inStream)
183 # cfg.__save__(outStream) # KO and sat renamed save() in __save__()
184 res = outStream.value
185 DBG.write("test_045 cfg", res)
186 self.assertTrue("aa : 'Yves'" in res)
187 self.assertTrue(r"bb : 'Herv\xc3\xa9'" in res)
188 self.assertEqual(cfg.bb, "Hervé")
191 inStream = DBG.InStream(_EXAMPLES[5])
192 outStream = DBG.OutStream()
193 cfg = PYF.Config(inStream) # KO
194 cfg.__save__(outStream) # sat renamed save() in __save__()
195 res = outStream.value
196 DBG.write("test_100 cfg save", res)
197 DBG.write("test_100 cfg debug", cfg)
198 DBG.write("test_100 cfg.cc debug", cfg.cc)
201 # DBG.write("test_100 type cc[3]", dir(cc), True)
202 DBG.write("test_100 cc", [cc.data[i] for i in range(len(cc))])
205 inStream = DBG.InStream(_EXAMPLES[5])
206 outStream = DBG.OutStream()
207 cfg = PYF.Config(inStream) # KO
208 cfg.__save__(outStream) # sat renamed save() in __save__()
209 res = outStream.value
210 DBG.write("test_100 cfg save", res)
211 DBG.write("test_100 cfg debug", cfg)
212 DBG.write("test_100 cfg.cc debug", cfg.cc)
215 # DBG.write("test_100 type cc[3]", dir(cc), True)
216 DBG.write("test_100 cc", [cc.data[i] for i in range(len(cc))])
219 inStream = DBG.InStream(_EXAMPLES[6])
220 outStream = DBG.OutStream()
221 cfg = PYF.Config(inStream)
222 cfg.__save__(outStream)
224 res = outStream.value
225 DBG.write("test_110 cfg save", res)
226 self.assertNotIn("ERROR", res)
228 res = DBG.getStrConfigDbg(cfg)
229 DBG.write("test_110 cfg debug", res)
230 self.assertIn("ERROR", res)
231 self.assertIn("unable to evaluate $cc", res)
232 self.assertIn("unable to evaluate $bb", res)
236 inStream = DBG.InStream(_EXAMPLES[ii])
237 outStream = DBG.OutStream()
238 cfg = PYF.Config(inStream)
239 cfg.__save__(outStream)
241 res = outStream.value
242 DBG.write("test_120 cfg save", res)
243 self.assertNotIn("ERROR", res)
245 res = DBG.getStrConfigDbg(cfg)
247 DBG.write("test_120 cfg debug", res)
248 # no error circular !!!
249 # self.assertIn("ERROR", res) # no error circular !!!
250 # self.assertIn("unable to evaluate $cc", res)
251 # self.assertIn("unable to evaluate $bb", res)
253 DBG.write("test_120 cfg.bb debug", res)
256 DBG.write("test_120 cfg.cc debug", res)
259 # one shot tearDown() for this TestCase
260 # SAT.setLocale() # end test english
264 if __name__ == '__main__':
265 unittest.main(exit=False)