GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/interpreter-test-runfile.cc Lines: 31 40 77.5 %
Date: 2022-09-30 08:22:45 Branches: 62 198 31.3 %

Line Branch Exec Source
1
// The purpose of this unit test is to check the interpreter::runPythonFile
2
// method
3
#include <cstring>
4
#include <iostream>
5
6
#include "dynamic-graph/python/interpreter.hh"
7
8
4
bool testFile(const std::string& filename, const std::string& expectedOutput,
9
              int numTest) {
10
8
  std::string err = "";
11
8
  dynamicgraph::python::Interpreter interp;
12
4104
  for (int i = 0; i < numTest; ++i) {
13

4100
    interp.runPythonFile(filename, err);
14
4100
    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
4
  return true;
23
}
24
25
1
bool testInterpreterDestructor(const std::string& filename,
26
                               const std::string& expectedOutput) {
27
2
  std::string err = "";
28
  {
29
1
    dynamicgraph::python::Interpreter interp;
30

1
    interp.runPythonFile(filename, err);
31
  }
32
  {
33
1
    dynamicgraph::python::Interpreter interp;
34

1
    interp.runPythonFile(filename, err);
35
1
    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
1
  return true;
43
}
44
45
1
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
1
  int numTest = 1025;
50
1
  if (argc > 1) numTest = atoi(argv[1]);
51
52
1
  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


2
  res = testFile(PATH "test_python-name_error.py",
57


2
                 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

3
                 numTest) &&
64
        res;
65
66






1
  res = testFile(PATH "test_python-ok.py", "", numTest) && res;
67





3
  res = testFile(PATH "unexistant_file.py",
68

3
                 PATH "unexistant_file.py cannot be open", numTest) &&
69
        res;
70


2
  res = testFile(PATH "test_python-syntax_error.py",
71


2
                 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

3
                 numTest) &&
83
        res;
84





3
  res = testInterpreterDestructor(PATH "test_python-restart_interpreter.py",
85

3
                                  "") &&
86
        res;
87
1
  return (res ? 0 : 1);
88
}