1 // Copyright (C) 2006-2016 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 void Py2yacsTest::unaccepted_statement2()
225 const char * code_py = "def f(a):\n"
227 "if __name__ == '__main__':"
229 verifyWrongPycode(code_py, "f", "not accepted statement");
232 void Py2yacsTest::unaccepted_return()
234 const char * code_py = "def f(a):\n"
236 verifyWrongPycode(code_py, "f", "invalid type returned");
239 void Py2yacsTest::unaccepted_return2()
241 const char * code_py = "def f1(a):\n"
245 verifyWrongPycode(code_py, "f2", "invalid type returned");
248 void Py2yacsTest::syntaxError()
250 const char * code_py = "bla bla bla\n"
252 verifyWrongPycode(code_py, "f2", "invalid syntax");
255 void Py2yacsTest::badFunctionName()
257 const char * code_py = "def f1(a):\n"
263 verifyWrongPycode(code_py, "nonexistant", "Function not found:");
266 void Py2yacsTest::schemaExec()
268 const char * code_py = "def f(a):\n"
274 parser.load(code_py);
276 catch(Py2yacsException& e)
278 CPPUNIT_FAIL(e.what());
282 CPPUNIT_FAIL("Unknown exception");
284 YACS::ENGINE::Proc* p = parser.createProc("f");
285 CPPUNIT_ASSERT( p != NULL);
286 YACS::ENGINE::Node* n = p->getChildByShortName("default_name");
287 CPPUNIT_ASSERT( n != NULL);
288 CPPUNIT_ASSERT( n->getInputPort("a") != NULL);
289 CPPUNIT_ASSERT( n->getOutputPort("x") != NULL);
291 n->getInputPort("a")->edInit(42.);
292 YACS::ENGINE::Executor executor;
294 // verify the output port value
295 YACS::ENGINE::OutputPyPort* var = dynamic_cast<YACS::ENGINE::OutputPyPort*>(n->getOutputPort("x"));
297 PyObject* pyVal = var->get();
298 CPPUNIT_ASSERT(pyVal);
299 CPPUNIT_ASSERT(PyFloat_Check(pyVal));
300 CPPUNIT_ASSERT_DOUBLES_EQUAL(42., PyFloat_AsDouble(pyVal), 1.E-12);
303 // Test the behaviour when there is an error in the python parser
304 void Py2yacsTest::parserErrors()
306 verifyWrongParser("bad_parsers", "p1", "0 positional arguments");
307 verifyWrongParser("bad_parsers", "p2", "'str' object has no attribute 'name'");
308 verifyWrongParser("bad_parsers", "p3", "should be a python list");
309 verifyWrongParser("bad_parsers", "p4", "unsupported operand type");
310 verifyWrongParser("bad_parsers", "f", "Cannot find the parsing function");
311 verifyWrongParser("err_py2yacs_invalid", "get_properties", "invalid syntax");
312 verifyWrongParser("no_file", "f", "Failed to load");
313 verifyWrongParser("bad_parsers", "p5", "is not a string");
314 verifyWrongParser("bad_parsers", "p6", "is not a string");
315 verifyWrongParser("bad_parsers", "p7", "is not a string");
316 verifyWrongParser("bad_parsers", "p8", "Attribute 'name' should be a string.");
317 verifyWrongParser("bad_parsers", "p9", "Not a python list");
318 verifyWrongParser("bad_parsers", "p10", "is not a string");
321 void Py2yacsTest::globalVerification()
323 std::ifstream file_stream("exemple_py2yacs.py");
327 std::stringstream buffer;
328 buffer << file_stream.rdbuf();
330 parser.load(buffer.str());
331 std::list<FunctionProperties>::const_iterator it_fp;
332 const std::list<FunctionProperties>& functions = parser.getFunctionProperties();
333 for(it_fp=functions.begin();it_fp!=functions.end();it_fp++)
335 std::cerr << "Function :" << it_fp->_name << std::endl;
336 std::list<std::string>::const_iterator it;
337 std::cerr << "Input ports :" ;
338 for(it=it_fp->_input_ports.begin();it!=it_fp->_input_ports.end();it++)
339 std::cerr << *it << ",";
340 std::cerr << std::endl;
341 std::cerr << "Output ports :" ;
342 for(it=it_fp->_output_ports.begin();it!=it_fp->_output_ports.end();it++)
343 std::cerr << *it << ",";
344 std::cerr << std::endl;
345 std::cerr << "Imports :" ;
346 for(it=it_fp->_imports.begin();it!=it_fp->_imports.end();it++)
347 std::cerr << *it << ",";
348 std::cerr << std::endl;
349 std::cerr << "Errors :" ;
350 for(it=it_fp->_errors.begin();it!=it_fp->_errors.end();it++)
351 std::cerr << *it << std::endl;
352 std::cerr << std::endl;
353 std::cerr << "-----------------------------------------" << std::endl;