interpreter-test-runfile.cc
Go to the documentation of this file.
1 // The purpose of this unit test is to check the interpreter::runPythonFile
2 // method
3 #include <cstring>
4 #include <iostream>
5 
7 
8 bool testFile(const std::string& filename, const std::string& expectedOutput,
9  int numTest) {
10  std::string err = "";
12  for (int i = 0; i < numTest; ++i) {
13  interp.runPythonFile(filename, err);
14  if (err != expectedOutput) {
15  std::cerr << "At iteration " << i
16  << ", the output was not the one expected:" << std::endl;
17  std::cerr << " expected: " << expectedOutput << std::endl;
18  std::cerr << " err: " << err << std::endl;
19  return false;
20  }
21  }
22  return true;
23 }
24 
25 bool testInterpreterDestructor(const std::string& filename,
26  const std::string& expectedOutput) {
27  std::string err = "";
28  {
30  interp.runPythonFile(filename, err);
31  }
32  {
34  interp.runPythonFile(filename, err);
35  if (err != expectedOutput) {
36  std::cerr << "The output was not the one expected:" << std::endl;
37  std::cerr << " expected: " << expectedOutput << std::endl;
38  std::cerr << " err: " << err << std::endl;
39  return false;
40  }
41  }
42  return true;
43 }
44 
45 int main(int argc, char** argv) {
46  // execute numerous time the same file.
47  // While running 1025, we can notice a change in the error.
48  // unfortunately, it can not be shown using a redirection of the streams
49  int numTest = 1025;
50  if (argc > 1) numTest = atoi(argv[1]);
51 
52  bool res = true;
53  // This test succeeds only because it is launched before "test_python-ok.py"
54  // because re as been imported in a previous test and it is not
55  // safe to delete imported module...
56  res = testFile(PATH "test_python-name_error.py",
57  std::string(
58  "Traceback (most recent call last):\n"
59  " File \"" PATH
60  "test_python-name_error.py\", line 7, in <module>\n"
61  " pathList = re.split(\":\", pkgConfigPath) # noqa\n"
62  "NameError: name 're' is not defined\n"),
63  numTest) &&
64  res;
65 
66  res = testFile(PATH "test_python-ok.py", "", numTest) && res;
67  res = testFile(PATH "unexistant_file.py",
68  PATH "unexistant_file.py cannot be open", numTest) &&
69  res;
70  res = testFile(PATH "test_python-syntax_error.py",
71  std::string(" File \"" PATH
72  "test_python-syntax_error.py\", line 2\n"
73  " hello world\n"
74 #if PY_MINOR_VERSION >= 10
75  " ^^^^^\n"
76 #elif PY_MINOR_VERSION >= 8
77  " ^\n"
78 #else
79  " ^\n"
80 #endif
81  "SyntaxError: invalid syntax\n"),
82  numTest) &&
83  res;
84  res = testInterpreterDestructor(PATH "test_python-restart_interpreter.py",
85  "") &&
86  res;
87  return (res ? 0 : 1);
88 }
main
int main(int argc, char **argv)
Definition: interpreter-test-runfile.cc:45
dynamicgraph::python::Interpreter::runPythonFile
void runPythonFile(std::string filename)
Method to exectue a python script.
Definition: interpreter.cc:209
testFile
bool testFile(const std::string &filename, const std::string &expectedOutput, int numTest)
Definition: interpreter-test-runfile.cc:8
interpreter.hh
testInterpreterDestructor
bool testInterpreterDestructor(const std::string &filename, const std::string &expectedOutput)
Definition: interpreter-test-runfile.cc:25
res
res
dynamicgraph::python::Interpreter
Definition: interpreter.hh:21


dynamic-graph-python
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Oct 27 2023 02:16:36