Rewrite eval tests to allow for simple asserts

Most of the test code is left in C++ since this is the
intended use case for the eval functions.
This commit is contained in:
Dean Moldovan 2016-08-18 16:55:26 +02:00
parent ec0d38ef25
commit 3b44daedf6
3 changed files with 71 additions and 96 deletions

View File

@ -11,92 +11,70 @@
#include <pybind11/eval.h> #include <pybind11/eval.h>
#include "pybind11_tests.h" #include "pybind11_tests.h"
void example_eval() { void init_ex_eval(py::module & m) {
py::module main_module = py::module::import("__main__"); auto global = py::dict(py::module::import("__main__").attr("__dict__"));
py::object main_namespace = main_module.attr("__dict__");
bool ok = false; m.def("test_eval_statements", [global]() {
auto local = py::dict();
main_module.def("call_test", [&]() -> int { local["call_test"] = py::cpp_function([&]() -> int {
ok = true;
return 42; return 42;
}); });
cout << "eval_statements test" << endl;
auto result = py::eval<py::eval_statements>( auto result = py::eval<py::eval_statements>(
"print('Hello World!');\n" "print('Hello World!');\n"
"x = call_test();", main_namespace); "x = call_test();",
global, local
);
auto x = local["x"].cast<int>();
if (ok && result == py::none()) return result == py::none() && x == 42;
cout << "eval_statements passed" << endl; });
else
cout << "eval_statements failed" << endl;
cout << "eval test" << endl; m.def("test_eval", [global]() {
auto local = py::dict();
local["x"] = py::int_(42);
auto x = py::eval("x", global, local);
return x.cast<int>() == 42;
});
py::object val = py::eval("x", main_namespace); m.def("test_eval_single_statement", []() {
auto local = py::dict();
local["call_test"] = py::cpp_function([&]() -> int {
return 42;
});
if (val.cast<int>() == 42) auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local);
cout << "eval passed" << endl; auto x = local["x"].cast<int>();
else return result == py::none() && x == 42;
cout << "eval failed" << endl; });
ok = false; m.def("test_eval_file", [global](py::str filename) {
cout << "eval_single_statement test" << endl; auto local = py::dict();
local["y"] = py::int_(43);
py::eval<py::eval_single_statement>(
"y = call_test();", main_namespace);
if (ok)
cout << "eval_single_statement passed" << endl;
else
cout << "eval_single_statement failed" << endl;
cout << "eval_file test" << endl;
int val_out; int val_out;
main_module.def("call_test2", [&](int value) {val_out = value;}); local["call_test2"] = py::cpp_function([&](int value) { val_out = value; });
try { auto result = py::eval_file(filename, global, local);
result = py::eval_file("test_eval_call.py", main_namespace); return val_out == 43 && result == py::none();
} catch (...) { });
result = py::eval_file("tests/test_eval_call.py", main_namespace);
}
if (val_out == 42 && result == py::none()) m.def("test_eval_failure", []() {
cout << "eval_file passed" << endl;
else
cout << "eval_file failed" << endl;
ok = false;
cout << "eval failure test" << endl;
try { try {
py::eval("nonsense code ..."); py::eval("nonsense code ...");
} catch (py::error_already_set &) { } catch (py::error_already_set &) {
PyErr_Clear(); PyErr_Clear();
ok = true; return true;
} }
return false;
});
if (ok) m.def("test_eval_file_failure", []() {
cout << "eval failure test passed" << endl;
else
cout << "eval failure test failed" << endl;
ok = false;
cout << "eval_file failure test" << endl;
try { try {
py::eval_file("nonexisting file"); py::eval_file("non-existing file");
} catch (std::exception &) { } catch (std::exception &) {
ok = true; return true;
} }
return false;
if (ok) });
cout << "eval_file failure test passed" << endl;
else
cout << "eval_file failure test failed" << endl;
}
void init_ex_eval(py::module & m) {
m.def("example_eval", &example_eval);
} }

View File

@ -1,22 +1,19 @@
import os
def test_eval(capture): def test_evals(capture):
from pybind11_tests import example_eval from pybind11_tests import (test_eval_statements, test_eval, test_eval_single_statement,
test_eval_file, test_eval_failure, test_eval_file_failure)
with capture: with capture:
example_eval() assert test_eval_statements()
assert capture == """ assert capture == "Hello World!"
eval_statements test
Hello World! assert test_eval()
eval_statements passed assert test_eval_single_statement()
eval test
eval passed filename = os.path.join(os.path.dirname(__file__), "test_eval_call.py")
eval_single_statement test assert test_eval_file(filename)
eval_single_statement passed
eval_file test assert test_eval_failure()
eval_file passed assert test_eval_file_failure()
eval failure test
eval failure test passed
eval_file failure test
eval_file failure test passed
"""

View File

@ -1,4 +1,4 @@
# This file is called from 'test_eval.py' # This file is called from 'test_eval.py'
if 'call_test2' in globals(): if 'call_test2' in locals():
call_test2(y) call_test2(y)