Salome HOME
Merge multi-study removal branch.
[modules/yacs.git] / src / py2yacs / Test / Py2yacsTest.cxx
1 // Copyright (C) 2006-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include <Python.h>
20 #include <fstream>
21 #include <string>
22 #include <sstream>
23
24 #include "Py2yacsTest.hxx"
25 #include "py2yacs.hxx"
26 #include "Proc.hxx"
27 #include "Executor.hxx"
28
29 #include "RuntimeSALOME.hxx"
30 #include "PythonPorts.hxx"
31 #include "InputPort.hxx"
32 //#include "parsers.hxx"
33
34 void Py2yacsTest::setUp()
35 {
36   YACS::ENGINE::RuntimeSALOME::setRuntime();
37   //YACS::ENGINE::getRuntime()->init();
38 }
39
40 void Py2yacsTest::tearDown()
41 {
42   //YACS::ENGINE::getRuntime()->fini();
43 }
44
45 static
46 void verifyCorrectPycode(const char * code_py, const char * function_name,
47                          int nbInputPorts, const char** input_ports,
48                          int nbOutputPorts, const char** output_ports)
49 {
50   std::cerr << std::endl;
51   std::cerr << "-----------------------------------------------" << std::endl;
52   std::cerr << code_py << std::endl;
53   std::cerr << "-----------------------------------------------" << std::endl;
54   std::cerr << "Function :" << function_name << std::endl;
55   Py2yacs parser;
56   try
57   {
58     parser.load(code_py);
59   }
60   catch(Py2yacsException& e)
61   {
62     CPPUNIT_FAIL(e.what());
63   }
64   catch(...)
65   {
66     CPPUNIT_FAIL("Unknown exception");
67   }
68   YACS::ENGINE::Proc* p = parser.createProc(function_name);
69   CPPUNIT_ASSERT( p != NULL);
70   YACS::ENGINE::Node* n = p->getChildByShortName("default_name");
71   CPPUNIT_ASSERT( n != NULL);
72   CPPUNIT_ASSERT( n->getNumberOfInputPorts() == nbInputPorts);
73   for(int i = 0; i < nbInputPorts; i++)
74     CPPUNIT_ASSERT( n->getInputPort(input_ports[i]) != NULL);
75   CPPUNIT_ASSERT( n->getNumberOfOutputPorts() == nbOutputPorts);
76   for(int i = 0; i < nbOutputPorts; i++)
77     CPPUNIT_ASSERT( n->getOutputPort(output_ports[i]) != NULL);
78   delete p;
79 }
80
81 static
82 void verifyWrongPycode(const char* code_py, const char* function_name,
83                        const char* error_message)
84 {
85   Py2yacs parser;
86   try
87   {
88     parser.load(code_py);
89     YACS::ENGINE::Proc* p = parser.createProc(function_name);
90     CPPUNIT_FAIL("Exception expected and no exception occured.");
91   }
92   catch(Py2yacsException& e)
93   {
94     std::string what = e.what();
95     std::cerr << std::endl;
96     std::cerr << "-----------------------------------------------" << std::endl;
97     std::cerr << code_py << std::endl;
98     std::cerr << "-----------------------------------------------" << std::endl;
99     std::cerr << "Function :" << function_name << std::endl;
100     std::cerr << "==========EXCEPTION TEXT=======================" << std::endl;
101     std::cerr << what << std::endl;
102     std::cerr << "===============================================" << std::endl;
103     CPPUNIT_ASSERT(what.find(error_message) != std::string::npos);
104   }
105 }
106
107 static
108 void verifyWrongParser(const char* parser_module, const char* parser_function,
109                        const char* error_message)
110 {
111   const char* code_py = "def f():\n"
112                         "  return\n";
113   Py2yacs parser(parser_module, parser_function);
114   try
115   {
116     parser.load(code_py);
117     CPPUNIT_FAIL("Exception expected and no exception occured.");
118   }
119   catch(Py2yacsException& e)
120   {
121     std::string what = e.what();
122     std::cerr << std::endl;
123     std::cerr << "==========EXCEPTION TEXT=======================" << std::endl;
124     std::cerr << what << std::endl;
125     std::cerr << "===============================================" << std::endl;
126     CPPUNIT_ASSERT(what.find(error_message) != std::string::npos);
127   }
128 }
129
130 void Py2yacsTest::t1()
131 {
132   const char * code_py = "def f1(a, b, c):\n"
133                          "  y = a*b + c\n"
134                          "  return y\n";
135   const char* input_ports[] = {"a", "b", "c"};
136   const char* output_ports[] = {"y"};
137   verifyCorrectPycode(code_py, "f1", 3, input_ports, 1, output_ports);
138 }
139
140 void Py2yacsTest::t2()
141 {
142   const char * code_py = "def f1(a, b, c):\n"
143                          "  x = a + b + c\n"
144                          "  y = a*b + c\n"
145                          "  z = a*b*c\n"
146                          "  return x, y, z\n";
147   const char* input_ports[] = {"a", "b", "c"};
148   const char* output_ports[] = {"x", "y", "z"};
149   verifyCorrectPycode(code_py, "f1", 3, input_ports, 3, output_ports);
150 }
151
152 void Py2yacsTest::t3()
153 {
154   const char * code_py = "def f1(a, b, c):\n"
155                          "  print a\n"
156                          "  print b\n"
157                          "  print c\n";
158   const char* input_ports[] = {"a", "b", "c"};
159   const char** output_ports;
160   verifyCorrectPycode(code_py, "f1", 3, input_ports, 0, output_ports);
161 }
162
163 void Py2yacsTest::t4()
164 {
165   const char * code_py = "def f1():\n"
166                          "  print 'toto'\n"
167                          "  return\n";
168   const char* input_ports[] = {"a", "b", "c"};
169   const char** output_ports;
170   verifyCorrectPycode(code_py, "f1", 0, input_ports, 0, output_ports);
171 }
172
173 void Py2yacsTest::t5()
174 {
175   const char * code_py = "def f1(a):\n"
176                          "  x = -a\n"
177                          "  if a > 0:\n"
178                          "    return a\n"
179                          "  else:\n"
180                          "    return x\n"
181                          "class ignoremoi:\n"
182                          "  def ignoreF(t):\n"
183                          "    return t+1\n"
184                          "def f2(v):\n"
185                          "  ret = f1(v)\n"
186                          "  return ret\n";
187   const char* input_ports[] = {"v"};
188   const char* output_ports[] = {"ret"};
189   verifyCorrectPycode(code_py, "f2", 1, input_ports, 1, output_ports);
190 }
191
192 void Py2yacsTest::no_multiple_returns()
193 {
194   const char * code_py = "def f(a):\n"
195                          "  x = -a\n"
196                          "  if a > 0:\n"
197                          "    return a\n"
198                          "  else:\n"
199                          "    return x\n";
200   verifyWrongPycode(code_py, "f", "multiple returns.");
201 }
202
203 void Py2yacsTest::unaccepted_statement()
204 {
205   const char * code_py = "def f(a):\n"
206                          "  return a\n"
207                          "x=5\n";
208   verifyWrongPycode(code_py, "f", "not accepted statement");
209 }
210
211 void Py2yacsTest::unaccepted_statement2()
212 {
213   const char * code_py = "def f(a):\n"
214                          "  return a\n"
215                          "if __name__ == '__main__':"
216                          "  print 'toto'\n";
217   verifyWrongPycode(code_py, "f", "not accepted statement");
218 }
219
220 void Py2yacsTest::unaccepted_return()
221 {
222   const char * code_py = "def f(a):\n"
223                          "  return 7\n";
224   verifyWrongPycode(code_py, "f", "invalid type returned");
225 }
226
227 void Py2yacsTest::unaccepted_return2()
228 {
229   const char * code_py = "def f1(a):\n"
230                          "  return 7\n"
231                          "def f2(x):\n"
232                          "  return f1(x)\n";
233   verifyWrongPycode(code_py, "f2", "invalid type returned");
234 }
235
236 void Py2yacsTest::syntaxError()
237 {
238   const char * code_py = "bla bla bla\n"
239                          "  return f1(x)\n";
240   verifyWrongPycode(code_py, "f2", "SyntaxError");
241 }
242
243 void Py2yacsTest::badFunctionName()
244 {
245   const char * code_py = "def f1(a):\n"
246                          "  x=7\n"
247                          "  return x\n"
248                          "def f2(x):\n"
249                          "  y=8\n"
250                          "  return y\n";
251   verifyWrongPycode(code_py, "nonexistant", "Function not found:");
252 }
253
254 void Py2yacsTest::schemaExec()
255 {
256   const char * code_py = "def f(a):\n"
257                          "  x = a\n"
258                          "  return x\n";
259   Py2yacs parser;
260   try
261   {
262     parser.load(code_py);
263   }
264   catch(Py2yacsException& e)
265   {
266     CPPUNIT_FAIL(e.what());
267   }
268   catch(...)
269   {
270     CPPUNIT_FAIL("Unknown exception");
271   }
272   YACS::ENGINE::Proc* p = parser.createProc("f");
273   CPPUNIT_ASSERT( p != NULL);
274   YACS::ENGINE::Node* n = p->getChildByShortName("default_name");
275   CPPUNIT_ASSERT( n != NULL);
276   CPPUNIT_ASSERT( n->getInputPort("a") != NULL);
277   CPPUNIT_ASSERT( n->getOutputPort("x") != NULL);
278   // run the schema
279   n->getInputPort("a")->edInit(42.);
280   YACS::ENGINE::Executor executor;
281   executor.RunW(p, 0);
282   // verify the output port value
283   YACS::ENGINE::OutputPyPort* var = dynamic_cast<YACS::ENGINE::OutputPyPort*>(n->getOutputPort("x"));
284   CPPUNIT_ASSERT(var);
285   PyObject* pyVal = var->get();
286   CPPUNIT_ASSERT(pyVal);
287   CPPUNIT_ASSERT(PyFloat_Check(pyVal));
288   CPPUNIT_ASSERT_DOUBLES_EQUAL(42., PyFloat_AsDouble(pyVal), 1.E-12);
289 }
290
291 // Test the behaviour when there is an error in the python parser
292 void Py2yacsTest::parserErrors()
293 {
294   verifyWrongParser("bad_parsers", "p1", "argument");
295   verifyWrongParser("bad_parsers", "p2", "Attribute 'name' not found");
296   verifyWrongParser("bad_parsers", "p3", "should be a python list");
297   verifyWrongParser("bad_parsers", "p4", "Traceback");
298   verifyWrongParser("bad_parsers", "f", "Cannot find the parsing function");
299   verifyWrongParser("err_py2yacs_invalid", "get_properties", "invalid syntax");
300   verifyWrongParser("no_file", "f", "Failed to load");
301   verifyWrongParser("bad_parsers", "p5", " ");
302   verifyWrongParser("bad_parsers", "p6", " ");
303   verifyWrongParser("bad_parsers", "p7", " ");
304   verifyWrongParser("bad_parsers", "p8", "Attribute 'name' should be a string.");
305   verifyWrongParser("bad_parsers", "p9", " ");
306   verifyWrongParser("bad_parsers", "p10", " ");
307 }
308
309 void Py2yacsTest::globalVerification()
310 {
311   std::ifstream file_stream("exemple_py2yacs.py");
312   if(!file_stream)
313     return;
314
315   std::stringstream buffer;
316   buffer << file_stream.rdbuf();
317   Py2yacs parser;
318   parser.load(buffer.str());
319   std::list<FunctionProperties>::const_iterator it_fp;
320   const std::list<FunctionProperties>& functions = parser.getFunctionProperties();
321   for(it_fp=functions.begin();it_fp!=functions.end();it_fp++)
322   {
323     std::cerr << "Function :" << it_fp->_name << std::endl;
324     std::list<std::string>::const_iterator it;
325     std::cerr << "Input ports :" ;
326     for(it=it_fp->_input_ports.begin();it!=it_fp->_input_ports.end();it++)
327       std::cerr << *it << ",";
328     std::cerr << std::endl;
329     std::cerr << "Output ports :" ;
330     for(it=it_fp->_output_ports.begin();it!=it_fp->_output_ports.end();it++)
331       std::cerr << *it << ",";
332     std::cerr << std::endl;
333     std::cerr << "Imports :" ;
334     for(it=it_fp->_imports.begin();it!=it_fp->_imports.end();it++)
335       std::cerr << *it << ",";
336     std::cerr << std::endl;
337     std::cerr << "Errors :" ;
338     for(it=it_fp->_errors.begin();it!=it_fp->_errors.end();it++)
339       std::cerr << *it << std::endl;
340     std::cerr << std::endl;
341     std::cerr << "-----------------------------------------" << std::endl;
342   }
343
344 }