interpreter-test-runfile.cc 2.83 KB
Newer Older
1
// The purpose of this unit test is to check the interpreter::runPythonFile method
2
3
4
#include <cstring>
#include <iostream>

5
6
#include "dynamic-graph/python/interpreter.hh"

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
7
bool testFile(const std::string& filename, const std::string& expectedOutput, int numTest) {
8
9
  std::string err = "";
  dynamicgraph::python::Interpreter interp;
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
10
  for (int i = 0; i < numTest; ++i) {
11
    interp.runPythonFile(filename, err);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
12
    if (err != expectedOutput) {
13
14
15
16
17
      std::cerr << "At iteration " << i << ", the output was not the one expected:" << std::endl;
      std::cerr << " expected: " << expectedOutput << std::endl;
      std::cerr << " err:      " << err << std::endl;
      return false;
    }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
18
  }
19
20
  return true;
}
21

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
22
bool testInterpreterDestructor(const std::string& filename, const std::string& expectedOutput) {
23
24
25
26
27
28
29
30
  std::string err = "";
  {
    dynamicgraph::python::Interpreter interp;
    interp.runPythonFile(filename, err);
  }
  {
    dynamicgraph::python::Interpreter interp;
    interp.runPythonFile(filename, err);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
31
    if (err != expectedOutput) {
32
33
34
35
36
      std::cerr << "The output was not the one expected:" << std::endl;
      std::cerr << " expected: " << expectedOutput << std::endl;
      std::cerr << " err:      " << err << std::endl;
      return false;
    }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
37
  }
38
39
40
  return true;
}

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
41
int main(int argc, char** argv) {
42
43
44
45
  // execute numerous time the same file.
  // While running 1025, we can notice a change in the error.
  // unfortunately, it can not be shown using a redirection of the streams
  int numTest = 1025;
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
46
  if (argc > 1) numTest = atoi(argv[1]);
47

48
  bool res = true;
49
50
51
  // This test succeeds only because it is launched before "test_python-ok.py"
  // because re as been imported in a previous test and it is not
  // safe to delete imported module...
52
  res = testFile(PATH "test_python-name_error.py",
Guilhem Saurel's avatar
Guilhem Saurel committed
53
                 std::string("Traceback (most recent call last):\n"
54
                             "  File \"" PATH "test_python-name_error.py\", line 7, in <module>\n"
Guilhem Saurel's avatar
Guilhem Saurel committed
55
56
                             "    pathList = re.split(':', pkgConfigPath)  # noqa\n"
                             "NameError: name 're' is not defined\n"),
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
57
58
                 numTest) &&
        res;
59

60
61
62
63
  res = testFile(PATH "test_python-ok.py", "", numTest) && res;
  res = testFile(PATH "unexistant_file.py", PATH "unexistant_file.py cannot be open", numTest) && res;
  res = testFile(PATH "test_python-syntax_error.py",
                 std::string("  File \"" PATH "test_python-syntax_error.py\", line 2\n"
Guilhem Saurel's avatar
Guilhem Saurel committed
64
                             "    hello world\n"
Guilhem Saurel's avatar
Guilhem Saurel committed
65
66
67
#if PY_MINOR_VERSION >= 8
                             "          ^\n"
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
68
                             "              ^\n"
Guilhem Saurel's avatar
Guilhem Saurel committed
69
#endif
Guilhem Saurel's avatar
Guilhem Saurel committed
70
                             "SyntaxError: invalid syntax\n"),
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
71
72
                 numTest) &&
        res;
73
  res = testInterpreterDestructor(PATH "test_python-restart_interpreter.py", "") && res;
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
74
  return (res ? 0 : 1);
75
}