summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/test_eval.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ext/pybind11/tests/test_eval.cpp')
-rw-r--r--ext/pybind11/tests/test_eval.cpp28
1 files changed, 20 insertions, 8 deletions
diff --git a/ext/pybind11/tests/test_eval.cpp b/ext/pybind11/tests/test_eval.cpp
index ed4c226fe..e09482191 100644
--- a/ext/pybind11/tests/test_eval.cpp
+++ b/ext/pybind11/tests/test_eval.cpp
@@ -11,7 +11,9 @@
#include <pybind11/eval.h>
#include "pybind11_tests.h"
-test_initializer eval([](py::module &m) {
+TEST_SUBMODULE(eval_, m) {
+ // test_evals
+
auto global = py::dict(py::module::import("__main__").attr("__dict__"));
m.def("test_eval_statements", [global]() {
@@ -20,14 +22,24 @@ test_initializer eval([](py::module &m) {
return 42;
});
- auto result = py::eval<py::eval_statements>(
- "print('Hello World!');\n"
- "x = call_test();",
+ // Regular string literal
+ py::exec(
+ "message = 'Hello World!'\n"
+ "x = call_test()",
global, local
);
+
+ // Multi-line raw string literal
+ py::exec(R"(
+ if x == 42:
+ print(message)
+ else:
+ raise RuntimeError
+ )", global, local
+ );
auto x = local["x"].cast<int>();
- return result == py::none() && x == 42;
+ return x == 42;
});
m.def("test_eval", [global]() {
@@ -45,7 +57,7 @@ test_initializer eval([](py::module &m) {
auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local);
auto x = local["x"].cast<int>();
- return result == py::none() && x == 42;
+ return result.is_none() && x == 42;
});
m.def("test_eval_file", [global](py::str filename) {
@@ -56,7 +68,7 @@ test_initializer eval([](py::module &m) {
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();
+ return val_out == 43 && result.is_none();
});
m.def("test_eval_failure", []() {
@@ -76,4 +88,4 @@ test_initializer eval([](py::module &m) {
}
return false;
});
-});
+}