mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-13 09:03:54 +00:00
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:
parent
ec0d38ef25
commit
3b44daedf6
@ -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();
|
||||||
|
local["call_test"] = py::cpp_function([&]() -> int {
|
||||||
|
return 42;
|
||||||
|
});
|
||||||
|
|
||||||
main_module.def("call_test", [&]() -> int {
|
auto result = py::eval<py::eval_statements>(
|
||||||
ok = true;
|
"print('Hello World!');\n"
|
||||||
return 42;
|
"x = call_test();",
|
||||||
|
global, local
|
||||||
|
);
|
||||||
|
auto x = local["x"].cast<int>();
|
||||||
|
|
||||||
|
return result == py::none() && x == 42;
|
||||||
});
|
});
|
||||||
|
|
||||||
cout << "eval_statements 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;
|
||||||
|
});
|
||||||
|
|
||||||
auto result = py::eval<py::eval_statements>(
|
m.def("test_eval_single_statement", []() {
|
||||||
"print('Hello World!');\n"
|
auto local = py::dict();
|
||||||
"x = call_test();", main_namespace);
|
local["call_test"] = py::cpp_function([&]() -> int {
|
||||||
|
return 42;
|
||||||
|
});
|
||||||
|
|
||||||
if (ok && result == py::none())
|
auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local);
|
||||||
cout << "eval_statements passed" << endl;
|
auto x = local["x"].cast<int>();
|
||||||
else
|
return result == py::none() && x == 42;
|
||||||
cout << "eval_statements failed" << endl;
|
});
|
||||||
|
|
||||||
cout << "eval test" << endl;
|
m.def("test_eval_file", [global](py::str filename) {
|
||||||
|
auto local = py::dict();
|
||||||
|
local["y"] = py::int_(43);
|
||||||
|
|
||||||
py::object val = py::eval("x", main_namespace);
|
int val_out;
|
||||||
|
local["call_test2"] = py::cpp_function([&](int value) { val_out = value; });
|
||||||
|
|
||||||
if (val.cast<int>() == 42)
|
auto result = py::eval_file(filename, global, local);
|
||||||
cout << "eval passed" << endl;
|
return val_out == 43 && result == py::none();
|
||||||
else
|
});
|
||||||
cout << "eval failed" << endl;
|
|
||||||
|
|
||||||
ok = false;
|
m.def("test_eval_failure", []() {
|
||||||
cout << "eval_single_statement test" << endl;
|
try {
|
||||||
|
py::eval("nonsense code ...");
|
||||||
|
} catch (py::error_already_set &) {
|
||||||
|
PyErr_Clear();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
py::eval<py::eval_single_statement>(
|
m.def("test_eval_file_failure", []() {
|
||||||
"y = call_test();", main_namespace);
|
try {
|
||||||
|
py::eval_file("non-existing file");
|
||||||
if (ok)
|
} catch (std::exception &) {
|
||||||
cout << "eval_single_statement passed" << endl;
|
return true;
|
||||||
else
|
}
|
||||||
cout << "eval_single_statement failed" << endl;
|
return false;
|
||||||
|
});
|
||||||
cout << "eval_file test" << endl;
|
|
||||||
|
|
||||||
int val_out;
|
|
||||||
main_module.def("call_test2", [&](int value) {val_out = value;});
|
|
||||||
|
|
||||||
try {
|
|
||||||
result = py::eval_file("test_eval_call.py", main_namespace);
|
|
||||||
} catch (...) {
|
|
||||||
result = py::eval_file("tests/test_eval_call.py", main_namespace);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (val_out == 42 && result == py::none())
|
|
||||||
cout << "eval_file passed" << endl;
|
|
||||||
else
|
|
||||||
cout << "eval_file failed" << endl;
|
|
||||||
|
|
||||||
ok = false;
|
|
||||||
cout << "eval failure test" << endl;
|
|
||||||
try {
|
|
||||||
py::eval("nonsense code ...");
|
|
||||||
} catch (py::error_already_set &) {
|
|
||||||
PyErr_Clear();
|
|
||||||
ok = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ok)
|
|
||||||
cout << "eval failure test passed" << endl;
|
|
||||||
else
|
|
||||||
cout << "eval failure test failed" << endl;
|
|
||||||
|
|
||||||
ok = false;
|
|
||||||
cout << "eval_file failure test" << endl;
|
|
||||||
try {
|
|
||||||
py::eval_file("nonexisting file");
|
|
||||||
} catch (std::exception &) {
|
|
||||||
ok = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
|
||||||
"""
|
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user