Salome HOME
sat #32302 pip option --build obsolète : integration du patch fourni par Nabil
[tools/sat.git] / test / test_035_pyconf.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 #  Copyright (C) 2010-2018  CEA/DEN
5 #
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.
10 #
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.
15 #
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
19
20 import os
21 import sys
22 import unittest
23
24 import initializeTest # set PATH etc for test
25
26 import src.debug as DBG # Easy print stderr (for DEBUG only)
27 import src.pyconf as PYF # 0.3.7
28
29 _EXAMPLES = {
30 1 : """\
31   messages:
32   [
33     {
34       stream : "sys.stderr" # modified
35       message: 'Welcome'
36       name: 'Harry'
37     }
38     {
39       stream : "sys.stdout" # modified
40       message: 'Welkom'
41       name: 'Ruud'
42     }
43     {
44       stream : $messages[0].stream
45       message: 'Bienvenue'
46       name: "Yves"
47     }
48   ]
49 """,
50
51 2 : """\
52   aa: 111
53   bb: $aa + 222
54 """,
55
56 3 : """\
57   aa: Yves
58   bb: "Herve" # avoid Hervé -> 'utf8' codec can't decode byte
59 """,
60
61 4 : """\
62   aa: Yves
63   bb: "Hervé" # avoid Hervé -> 'utf8' codec can't decode byte
64 """,
65
66 5 : """\
67   aa: Yves
68   bb: "Herve"
69   cc: [ 
70     cc1
71     cc2
72     cc3
73     $bb + " hello"
74     ]
75   dd: { 
76    d1 : dd11 
77    d2 : dd22
78    d3 : dd33 
79    d4 : $bb + " bye"
80    }   
81 """,
82
83 # error circular
84 6 : """\
85   aa: Yves
86   bb: $cc
87   cc: $bb
88 """,
89
90 7 : """\
91   aa: Yves
92   bb: $cc
93   cc: [ 
94     cc1
95     $bb
96     ]
97 """,
98
99 8 : """\
100   aa: Yves
101   bb: $cc
102   cc: { 
103     cc1: cc11
104     cc2: $bb
105     }
106 """,
107
108 }
109
110
111 class TestCase(unittest.TestCase):
112   "Test the pyconf.py"""
113   
114   def test_000(self):
115     # one shot setUp() for this TestCase
116     # DBG.push_debug(True)
117     # SAT.setNotLocale() # test english
118     return
119
120   def test_010(self):
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:
124
125     expected = """\
126 Welcome, Harry
127 Welkom, Ruud
128 Bienvenue, Yves
129 """
130     inStream = DBG.InStream(_EXAMPLES[1])
131     cfg = PYF.Config(inStream)
132     res = ''
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)
143         
144   def test_020(self):
145     cfg = PYF.Config()
146     self.assertEqual(str(cfg), '{}')
147     self.assertEqual(cfg.__repr__(), '{}')
148     cfg.aa = "1111"
149     self.assertEqual(str(cfg), "{'aa': '1111'}")
150     cfg.bb = 2222
151     self.assertTrue("'bb': 2222" in str(cfg))
152     self.assertTrue("'aa': '1111'" in str(cfg))
153     cfg.cc = 3333.
154     self.assertTrue("'cc': 3333." in str(cfg))
155     
156   def test_030(self):
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)
162       
163   def test_040(self):
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é")
172     
173   def test_045(self):
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)
179
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é")
189     
190   def test_100(self):
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)
199     
200     cc = 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))])
203       
204   def test_100(self):
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)
213     
214     cc = 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))])
217       
218   def test_110(self):
219     inStream = DBG.InStream(_EXAMPLES[6])
220     outStream = DBG.OutStream()
221     cfg = PYF.Config(inStream)
222     cfg.__save__(outStream)
223     
224     res = outStream.value
225     DBG.write("test_110 cfg save", res)
226     self.assertNotIn("ERROR", res)
227     
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)
233     
234   def test_120(self):
235    for ii in [7, 8]:
236     inStream = DBG.InStream(_EXAMPLES[ii])
237     outStream = DBG.OutStream()
238     cfg = PYF.Config(inStream)
239     cfg.__save__(outStream)
240     
241     res = outStream.value
242     DBG.write("test_120 cfg save", res)
243     self.assertNotIn("ERROR", res)
244     
245     res = DBG.getStrConfigDbg(cfg)
246
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)
252     res = cfg.bb
253     DBG.write("test_120 cfg.bb debug", res)
254
255     res = cfg.cc
256     DBG.write("test_120 cfg.cc debug", res)
257     
258   def test_999(self):
259     # one shot tearDown() for this TestCase
260     # SAT.setLocale() # end test english
261     # DBG.pop_debug()
262     return
263     
264 if __name__ == '__main__':
265     unittest.main(exit=False)
266     pass