1 // Copyright (C) 2006-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 #include "Py2yacsTest.hxx"
25 #include "py2yacs.hxx"
27 #include "Executor.hxx"
29 #include "RuntimeSALOME.hxx"
30 #include "PythonPorts.hxx"
31 #include "InputPort.hxx"
32 //#include "parsers.hxx"
34 void Py2yacsTest::setUp()
36 YACS::ENGINE::RuntimeSALOME::setRuntime();
37 //YACS::ENGINE::getRuntime()->init();
40 void Py2yacsTest::tearDown()
42 //YACS::ENGINE::getRuntime()->fini();
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)
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;
60 catch(Py2yacsException& e)
62 CPPUNIT_FAIL(e.what());
64 catch(std::exception& e)
66 CPPUNIT_FAIL(e.what());
70 CPPUNIT_FAIL("Unknown exception");
72 YACS::ENGINE::Proc* p = parser.createProc(function_name);
73 CPPUNIT_ASSERT( p != NULL);
74 YACS::ENGINE::Node* n = p->getChildByShortName("default_name");
75 CPPUNIT_ASSERT( n != NULL);
76 CPPUNIT_ASSERT( n->getNumberOfInputPorts() == nbInputPorts);
77 for(int i = 0; i < nbInputPorts; i++)
78 CPPUNIT_ASSERT( n->getInputPort(input_ports[i]) != NULL);
79 CPPUNIT_ASSERT( n->getNumberOfOutputPorts() == nbOutputPorts);
80 for(int i = 0; i < nbOutputPorts; i++)
81 CPPUNIT_ASSERT( n->getOutputPort(output_ports[i]) != NULL);
86 void verifyWrongPycode(const char* code_py, const char* function_name,
87 const char* error_message)
93 YACS::ENGINE::Proc* p = parser.createProc(function_name);
94 CPPUNIT_FAIL("Exception expected and no exception occured.");
96 catch(Py2yacsException& e)
98 std::string what = e.what();
99 std::cerr << std::endl;
100 std::cerr << "-----------------------------------------------" << std::endl;
101 std::cerr << code_py << std::endl;
102 std::cerr << "-----------------------------------------------" << std::endl;
103 std::cerr << "Function :" << function_name << std::endl;
104 std::cerr << "==========EXCEPTION TEXT=======================" << std::endl;
105 std::cerr << what << std::endl;
106 std::cerr << "===============================================" << std::endl;
107 CPPUNIT_ASSERT(what.find(error_message) != std::string::npos);
109 catch(std::exception& e)
111 CPPUNIT_FAIL(e.what());
116 void verifyWrongParser(const char* parser_module, const char* parser_function,
117 const char* error_message)
119 const char* code_py = "def f():\n"
121 Py2yacs parser(parser_module, parser_function);
124 parser.load(code_py);
125 CPPUNIT_FAIL("Exception expected and no exception occured.");
127 catch(Py2yacsException& e)
129 std::string what = e.what();
130 std::cerr << std::endl;
131 std::cerr << "==========EXCEPTION TEXT=======================" << std::endl;
132 std::cerr << what << std::endl;
133 std::cerr << "===============================================" << std::endl;
134 CPPUNIT_ASSERT(what.find(error_message) != std::string::npos);
136 catch(std::exception& e)
138 CPPUNIT_FAIL(e.what());
142 void Py2yacsTest::t1()
144 const char * code_py = "def f1(a, b, c):\n"
147 const char* input_ports[] = {"a", "b", "c"};
148 const char* output_ports[] = {"y"};
149 verifyCorrectPycode(code_py, "f1", 3, input_ports, 1, output_ports);
152 void Py2yacsTest::t2()
154 const char * code_py = "def f1(a, b, c):\n"
159 const char* input_ports[] = {"a", "b", "c"};
160 const char* output_ports[] = {"x", "y", "z"};
161 verifyCorrectPycode(code_py, "f1", 3, input_ports, 3, output_ports);
164 void Py2yacsTest::t3()
166 const char * code_py = "def f1(a, b, c):\n"
170 const char* input_ports[] = {"a", "b", "c"};
171 const char** output_ports;
172 verifyCorrectPycode(code_py, "f1", 3, input_ports, 0, output_ports);
175 void Py2yacsTest::t4()
177 const char * code_py = "def f1():\n"
180 const char* input_ports[] = {"a", "b", "c"};
181 const char** output_ports;
182 verifyCorrectPycode(code_py, "f1", 0, input_ports, 0, output_ports);
185 void Py2yacsTest::t5()
187 const char * code_py = "def f1(a):\n"
199 const char* input_ports[] = {"v"};
200 const char* output_ports[] = {"ret"};
201 verifyCorrectPycode(code_py, "f2", 1, input_ports, 1, output_ports);
204 void Py2yacsTest::no_multiple_returns()
206 const char * code_py = "def f(a):\n"
212 verifyWrongPycode(code_py, "f", "multiple returns.");
215 void Py2yacsTest::unaccepted_statement()
217 const char * code_py = "def f(a):\n"
220 verifyWrongPycode(code_py, "f", "not accepted statement");
223 /* // This is now accepted
224 void Py2yacsTest::unaccepted_statement2()
226 const char * code_py = "def f(a):\n"
228 "if __name__ == '__main__':"
230 verifyWrongPycode(code_py, "f", "not accepted statement");
233 void Py2yacsTest::unaccepted_return()
235 const char * code_py = "def f(a):\n"
237 verifyWrongPycode(code_py, "f", "invalid type returned");
240 void Py2yacsTest::unaccepted_return2()
242 const char * code_py = "def f1(a):\n"
246 verifyWrongPycode(code_py, "f2", "invalid type returned");
249 void Py2yacsTest::syntaxError()
251 const char * code_py = "bla bla bla\n"
253 verifyWrongPycode(code_py, "f2", "invalid syntax");
256 void Py2yacsTest::badFunctionName()
258 const char * code_py = "def f1(a):\n"
264 verifyWrongPycode(code_py, "nonexistant", "Function not found:");
267 void Py2yacsTest::schemaExec()
269 const char * code_py = "def f(a):\n"
275 parser.load(code_py);
277 catch(Py2yacsException& e)
279 CPPUNIT_FAIL(e.what());
283 CPPUNIT_FAIL("Unknown exception");
285 YACS::ENGINE::Proc* p = parser.createProc("f");
286 CPPUNIT_ASSERT( p != NULL);
287 YACS::ENGINE::Node* n = p->getChildByShortName("default_name");
288 CPPUNIT_ASSERT( n != NULL);
289 CPPUNIT_ASSERT( n->getInputPort("a") != NULL);
290 CPPUNIT_ASSERT( n->getOutputPort("x") != NULL);
292 n->getInputPort("a")->edInit(42.);
293 YACS::ENGINE::Executor executor;
295 // verify the output port value
296 YACS::ENGINE::OutputPyPort* var = dynamic_cast<YACS::ENGINE::OutputPyPort*>(n->getOutputPort("x"));
298 PyObject* pyVal = var->get();
299 CPPUNIT_ASSERT(pyVal);
300 CPPUNIT_ASSERT(PyFloat_Check(pyVal));
301 CPPUNIT_ASSERT_DOUBLES_EQUAL(42., PyFloat_AsDouble(pyVal), 1.E-12);
302 p->shutdown(10); // kill all the containers
305 // Test the behaviour when there is an error in the python parser
306 void Py2yacsTest::parserErrors()
308 verifyWrongParser("bad_parsers", "p1", "0 positional arguments");
309 verifyWrongParser("bad_parsers", "p2", "'str' object has no attribute 'name'");
310 verifyWrongParser("bad_parsers", "p3", "should be a python list");
311 verifyWrongParser("bad_parsers", "p4", "unsupported operand type");
312 verifyWrongParser("bad_parsers", "f", "Cannot find the parsing function");
313 verifyWrongParser("err_py2yacs_invalid", "get_properties", "invalid syntax");
314 verifyWrongParser("no_file", "f", "Failed to load");
315 verifyWrongParser("bad_parsers", "p5", "is not a string");
316 verifyWrongParser("bad_parsers", "p6", "is not a string");
317 verifyWrongParser("bad_parsers", "p7", "is not a string");
318 verifyWrongParser("bad_parsers", "p8", "Attribute 'name' should be a string.");
319 verifyWrongParser("bad_parsers", "p9", "Not a python list");
320 verifyWrongParser("bad_parsers", "p10", "is not a string");
323 void Py2yacsTest::globalVerification()
325 std::ifstream file_stream("exemple_py2yacs.py");
329 std::stringstream buffer;
330 buffer << file_stream.rdbuf();
332 parser.load(buffer.str());
333 std::list<FunctionProperties>::const_iterator it_fp;
334 const std::list<FunctionProperties>& functions = parser.getFunctionProperties();
335 for(it_fp=functions.begin();it_fp!=functions.end();it_fp++)
337 std::cerr << "Function :" << it_fp->_name << std::endl;
338 std::list<std::string>::const_iterator it;
339 std::cerr << "Input ports :" ;
340 for(it=it_fp->_input_ports.begin();it!=it_fp->_input_ports.end();it++)
341 std::cerr << *it << ",";
342 std::cerr << std::endl;
343 std::cerr << "Output ports :" ;
344 for(it=it_fp->_output_ports.begin();it!=it_fp->_output_ports.end();it++)
345 std::cerr << *it << ",";
346 std::cerr << std::endl;
347 std::cerr << "Imports :" ;
348 for(it=it_fp->_imports.begin();it!=it_fp->_imports.end();it++)
349 std::cerr << *it << ",";
350 std::cerr << std::endl;
351 std::cerr << "Errors :" ;
352 for(it=it_fp->_errors.begin();it!=it_fp->_errors.end();it++)
353 std::cerr << *it << std::endl;
354 std::cerr << std::endl;
355 std::cerr << "-----------------------------------------" << std::endl;