X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2Fpy2yacs%2FTest%2FPy2yacsTest.cxx;h=ffd74f9f1d6b4f02ad927ffa2bd47783474a71cc;hb=cc51b9ff75f0e73d5424e421b2c698798177c8cc;hp=8572bd3ffe19dcba9910b550212e5d6b94fcec8b;hpb=11040dd712ae61aecc2cedfc555e0415e9be4984;p=modules%2Fyacs.git diff --git a/src/py2yacs/Test/Py2yacsTest.cxx b/src/py2yacs/Test/Py2yacsTest.cxx index 8572bd3ff..ffd74f9f1 100644 --- a/src/py2yacs/Test/Py2yacsTest.cxx +++ b/src/py2yacs/Test/Py2yacsTest.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2006-2016 CEA/DEN, EDF R&D +// Copyright (C) 2006-2023 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -25,6 +25,7 @@ #include "py2yacs.hxx" #include "Proc.hxx" #include "Executor.hxx" +#include "PythonCppUtils.hxx" #include "RuntimeSALOME.hxx" #include "PythonPorts.hxx" @@ -61,6 +62,10 @@ void verifyCorrectPycode(const char * code_py, const char * function_name, { CPPUNIT_FAIL(e.what()); } + catch(std::exception& e) + { + CPPUNIT_FAIL(e.what()); + } catch(...) { CPPUNIT_FAIL("Unknown exception"); @@ -102,6 +107,10 @@ void verifyWrongPycode(const char* code_py, const char* function_name, std::cerr << "===============================================" << std::endl; CPPUNIT_ASSERT(what.find(error_message) != std::string::npos); } + catch(std::exception& e) + { + CPPUNIT_FAIL(e.what()); + } } static @@ -125,6 +134,10 @@ void verifyWrongParser(const char* parser_module, const char* parser_function, std::cerr << "===============================================" << std::endl; CPPUNIT_ASSERT(what.find(error_message) != std::string::npos); } + catch(std::exception& e) + { + CPPUNIT_FAIL(e.what()); + } } void Py2yacsTest::t1() @@ -152,21 +165,21 @@ void Py2yacsTest::t2() void Py2yacsTest::t3() { const char * code_py = "def f1(a, b, c):\n" - " print a\n" - " print b\n" - " print c\n"; + " print(a)\n" + " print(b)\n" + " print(c)\n"; const char* input_ports[] = {"a", "b", "c"}; - const char* output_ports[] = {}; + const char** output_ports; verifyCorrectPycode(code_py, "f1", 3, input_ports, 0, output_ports); } void Py2yacsTest::t4() { const char * code_py = "def f1():\n" - " print 'toto'\n" + " print('toto')\n" " return\n"; const char* input_ports[] = {"a", "b", "c"}; - const char* output_ports[] = {}; + const char** output_ports; verifyCorrectPycode(code_py, "f1", 0, input_ports, 0, output_ports); } @@ -208,14 +221,15 @@ void Py2yacsTest::unaccepted_statement() verifyWrongPycode(code_py, "f", "not accepted statement"); } +/* // This is now accepted void Py2yacsTest::unaccepted_statement2() { const char * code_py = "def f(a):\n" " return a\n" "if __name__ == '__main__':" - " print 'toto'\n"; + " print('toto')\n"; verifyWrongPycode(code_py, "f", "not accepted statement"); -} +}*/ void Py2yacsTest::unaccepted_return() { @@ -237,7 +251,7 @@ void Py2yacsTest::syntaxError() { const char * code_py = "bla bla bla\n" " return f1(x)\n"; - verifyWrongPycode(code_py, "f2", "SyntaxError"); + verifyWrongPycode(code_py, "f2", "invalid syntax"); } void Py2yacsTest::badFunctionName() @@ -282,28 +296,34 @@ void Py2yacsTest::schemaExec() // verify the output port value YACS::ENGINE::OutputPyPort* var = dynamic_cast(n->getOutputPort("x")); CPPUNIT_ASSERT(var); + double x = -2.0; PyObject* pyVal = var->get(); - CPPUNIT_ASSERT(pyVal); - CPPUNIT_ASSERT(PyFloat_Check(pyVal)); - CPPUNIT_ASSERT_DOUBLES_EQUAL(42., PyFloat_AsDouble(pyVal), 1.E-12); + { + AutoGIL agil; + CPPUNIT_ASSERT(pyVal); + x = PyFloat_AsDouble(pyVal); + CPPUNIT_ASSERT( PyErr_Occurred()==nullptr );// check pyVal is interpretable as float + } + CPPUNIT_ASSERT_DOUBLES_EQUAL(42., x, 1.E-12); + p->shutdown(10); // kill all the containers } // Test the behaviour when there is an error in the python parser void Py2yacsTest::parserErrors() { - verifyWrongParser("bad_parsers", "p1", "argument"); - verifyWrongParser("bad_parsers", "p2", "Attribute 'name' not found"); + verifyWrongParser("bad_parsers", "p1", "0 positional arguments"); + verifyWrongParser("bad_parsers", "p2", "'str' object has no attribute 'name'"); verifyWrongParser("bad_parsers", "p3", "should be a python list"); - verifyWrongParser("bad_parsers", "p4", "Traceback"); + verifyWrongParser("bad_parsers", "p4", "unsupported operand type"); verifyWrongParser("bad_parsers", "f", "Cannot find the parsing function"); verifyWrongParser("err_py2yacs_invalid", "get_properties", "invalid syntax"); verifyWrongParser("no_file", "f", "Failed to load"); - verifyWrongParser("bad_parsers", "p5", " "); - verifyWrongParser("bad_parsers", "p6", " "); - verifyWrongParser("bad_parsers", "p7", " "); + verifyWrongParser("bad_parsers", "p5", "is not a string"); + verifyWrongParser("bad_parsers", "p6", "is not a string"); + verifyWrongParser("bad_parsers", "p7", "is not a string"); verifyWrongParser("bad_parsers", "p8", "Attribute 'name' should be a string."); - verifyWrongParser("bad_parsers", "p9", " "); - verifyWrongParser("bad_parsers", "p10", " "); + verifyWrongParser("bad_parsers", "p9", "Not a python list"); + verifyWrongParser("bad_parsers", "p10", "is not a string"); } void Py2yacsTest::globalVerification() @@ -341,4 +361,4 @@ void Py2yacsTest::globalVerification() std::cerr << "-----------------------------------------" << std::endl; } -} \ No newline at end of file +}