forked from Distributive-Network/PythonMonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python_evaluator.cc
More file actions
58 lines (45 loc) · 1.43 KB
/
test_python_evaluator.cc
File metadata and controls
58 lines (45 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @file test_python_evaluator.cc
* @author Giovanni Tedesco
* @brief Tests for the Python Evaluator Class
* @version 0.1
* @date 2022-08-19
*
* @copyright Copyright (c) 2022
*
*/
#include "include/PyEvaluator.hh"
#include "include/TupleType.hh"
#include <gtest/gtest.h>
#include <Python.h>
#include <string>
#include <iostream>
class PyEvaluatorTests : public ::testing::Test {
protected:
PyObject *i_type;
virtual void SetUp() {
Py_Initialize();
}
virtual void TearDown() {}
};
// TODO: Figure out how to test that it outputs to stdout correctly.
TEST_F(PyEvaluatorTests, test_can_evaluate_and_run_a_function) {
PyEvaluator p;
PyObject *arg_tuple = Py_BuildValue("(i)", 10);
TupleType *args = new TupleType(arg_tuple);
std::string expected = "50";
testing::internal::CaptureStdout();
std::cout << *p.eval("def f(x):\n\treturn x * 5\n", "f", args);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(expected, output);
}
TEST_F(PyEvaluatorTests, test_evaluates_pfactor_correctly) {
PyEvaluator p;
PyObject *arg_tuple = Py_BuildValue("(i)", 10);
TupleType *args = new TupleType(arg_tuple);
std::string expected = "[\n 1,\n 2,\n 5,\n 10\n]";
testing::internal::CaptureStdout();
std::cout << *p.eval("import math\ndef f(n):\n\treturn [x for x in range(1, n + 1) if n % x == 0]\n", "f", args);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(expected, output);
}