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());
66 CPPUNIT_FAIL("Unknown exception");
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);
82 void verifyWrongPycode(const char* code_py, const char* function_name,
83 const char* error_message)
89 YACS::ENGINE::Proc* p = parser.createProc(function_name);
90 CPPUNIT_FAIL("Exception expected and no exception occured.");
92 catch(Py2yacsException& e)
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);
108 void verifyWrongParser(const char* parser_module, const char* parser_function,
109 const char* error_message)
111 const char* code_py = "def f():\n"
113 Py2yacs parser(parser_module, parser_function);
116 parser.load(code_py);
117 CPPUNIT_FAIL("Exception expected and no exception occured.");
119 catch(Py2yacsException& e)
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);
130 void Py2yacsTest::t1()
132 const char * code_py = "def f1(a, b, c):\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);
140 void Py2yacsTest::t2()
142 const char * code_py = "def f1(a, b, c):\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);
152 void Py2yacsTest::t3()
154 const char * code_py = "def f1(a, b, 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);
163 void Py2yacsTest::t4()
165 const char * code_py = "def f1():\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);
173 void Py2yacsTest::t5()
175 const char * code_py = "def f1(a):\n"
187 const char* input_ports[] = {"v"};
188 const char* output_ports[] = {"ret"};
189 verifyCorrectPycode(code_py, "f2", 1, input_ports, 1, output_ports);
192 void Py2yacsTest::no_multiple_returns()
194 const char * code_py = "def f(a):\n"
200 verifyWrongPycode(code_py, "f", "multiple returns.");
203 void Py2yacsTest::unaccepted_statement()
205 const char * code_py = "def f(a):\n"
208 verifyWrongPycode(code_py, "f", "not accepted statement");
211 void Py2yacsTest::unaccepted_statement2()
213 const char * code_py = "def f(a):\n"
215 "if __name__ == '__main__':"
217 verifyWrongPycode(code_py, "f", "not accepted statement");
220 void Py2yacsTest::unaccepted_return()
222 const char * code_py = "def f(a):\n"
224 verifyWrongPycode(code_py, "f", "invalid type returned");
227 void Py2yacsTest::unaccepted_return2()
229 const char * code_py = "def f1(a):\n"
233 verifyWrongPycode(code_py, "f2", "invalid type returned");
236 void Py2yacsTest::syntaxError()
238 const char * code_py = "bla bla bla\n"
240 verifyWrongPycode(code_py, "f2", "SyntaxError");
243 void Py2yacsTest::badFunctionName()
245 const char * code_py = "def f1(a):\n"
251 verifyWrongPycode(code_py, "nonexistant", "Function not found:");
254 void Py2yacsTest::schemaExec()
256 const char * code_py = "def f(a):\n"
262 parser.load(code_py);
264 catch(Py2yacsException& e)
266 CPPUNIT_FAIL(e.what());
270 CPPUNIT_FAIL("Unknown exception");
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);
279 n->getInputPort("a")->edInit(42.);
280 YACS::ENGINE::Executor executor;
282 // verify the output port value
283 YACS::ENGINE::OutputPyPort* var = dynamic_cast<YACS::ENGINE::OutputPyPort*>(n->getOutputPort("x"));
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);
291 // Test the behaviour when there is an error in the python parser
292 void Py2yacsTest::parserErrors()
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", " ");
309 void Py2yacsTest::globalVerification()
311 std::ifstream file_stream("exemple_py2yacs.py");
315 std::stringstream buffer;
316 buffer << file_stream.rdbuf();
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++)
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;