"...composable_kernel_rocm.git" did not exist on "fa9da1a42a2c21403633b958e28da7cb27dfebe9"
Commit 3b44daed authored by Dean Moldovan's avatar Dean Moldovan
Browse files

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.
parent ec0d38ef
...@@ -11,92 +11,70 @@ ...@@ -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__");
m.def("test_eval_statements", [global]() {
auto local = py::dict();
local["call_test"] = py::cpp_function([&]() -> int {
return 42;
});
bool ok = false; auto result = py::eval<py::eval_statements>(
"print('Hello World!');\n"
"x = call_test();",
global, local
);
auto x = local["x"].cast<int>();
main_module.def("call_test", [&]() -> int { return result == py::none() && x == 42;
ok = true;
return 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()) });
cout << "eval_statements passed" << endl;
else
cout << "eval_statements failed" << endl;
cout << "eval test" << endl;
py::object val = py::eval("x", main_namespace);
if (val.cast<int>() == 42)
cout << "eval passed" << endl;
else
cout << "eval failed" << endl;
ok = false;
cout << "eval_single_statement test" << endl;
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;
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) { auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local);
m.def("example_eval", &example_eval); auto x = local["x"].cast<int>();
return result == py::none() && x == 42;
});
m.def("test_eval_file", [global](py::str filename) {
auto local = py::dict();
local["y"] = py::int_(43);
int val_out;
local["call_test2"] = py::cpp_function([&](int value) { val_out = value; });
auto result = py::eval_file(filename, global, local);
return val_out == 43 && result == py::none();
});
m.def("test_eval_failure", []() {
try {
py::eval("nonsense code ...");
} catch (py::error_already_set &) {
PyErr_Clear();
return true;
}
return false;
});
m.def("test_eval_file_failure", []() {
try {
py::eval_file("non-existing file");
} catch (std::exception &) {
return true;
}
return false;
});
} }
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
"""
# 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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment