blob: ed4c226fe4f54c1a9d53a14d6216cde8e9ed21bb [file] [log] [blame]
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +02001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_eval.cpp -- Usage of eval() and eval_file()
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +02003
4 Copyright (c) 2016 Klemens D. Morgenstern
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10
Wenzel Jakob0d3fc352016-07-08 10:52:10 +020011#include <pybind11/eval.h>
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020012#include "pybind11_tests.h"
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020013
Jason Rhinelander52f4be82016-09-03 14:54:22 -040014test_initializer eval([](py::module &m) {
Dean Moldovan3b44dae2016-08-18 16:55:26 +020015 auto global = py::dict(py::module::import("__main__").attr("__dict__"));
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020016
Dean Moldovan3b44dae2016-08-18 16:55:26 +020017 m.def("test_eval_statements", [global]() {
18 auto local = py::dict();
19 local["call_test"] = py::cpp_function([&]() -> int {
20 return 42;
21 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020022
Dean Moldovan3b44dae2016-08-18 16:55:26 +020023 auto result = py::eval<py::eval_statements>(
24 "print('Hello World!');\n"
25 "x = call_test();",
26 global, local
27 );
28 auto x = local["x"].cast<int>();
29
30 return result == py::none() && x == 42;
Wenzel Jakob0d3fc352016-07-08 10:52:10 +020031 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020032
Dean Moldovan3b44dae2016-08-18 16:55:26 +020033 m.def("test_eval", [global]() {
34 auto local = py::dict();
35 local["x"] = py::int_(42);
36 auto x = py::eval("x", global, local);
37 return x.cast<int>() == 42;
38 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020039
Dean Moldovan3b44dae2016-08-18 16:55:26 +020040 m.def("test_eval_single_statement", []() {
41 auto local = py::dict();
42 local["call_test"] = py::cpp_function([&]() -> int {
43 return 42;
44 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020045
Dean Moldovan3b44dae2016-08-18 16:55:26 +020046 auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local);
47 auto x = local["x"].cast<int>();
48 return result == py::none() && x == 42;
49 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020050
Dean Moldovan3b44dae2016-08-18 16:55:26 +020051 m.def("test_eval_file", [global](py::str filename) {
52 auto local = py::dict();
53 local["y"] = py::int_(43);
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020054
Dean Moldovan3b44dae2016-08-18 16:55:26 +020055 int val_out;
56 local["call_test2"] = py::cpp_function([&](int value) { val_out = value; });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020057
Dean Moldovan3b44dae2016-08-18 16:55:26 +020058 auto result = py::eval_file(filename, global, local);
59 return val_out == 43 && result == py::none();
60 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020061
Dean Moldovan3b44dae2016-08-18 16:55:26 +020062 m.def("test_eval_failure", []() {
63 try {
64 py::eval("nonsense code ...");
65 } catch (py::error_already_set &) {
Dean Moldovan3b44dae2016-08-18 16:55:26 +020066 return true;
67 }
68 return false;
69 });
Klemens Morgensternc6ad2c42016-06-09 16:10:26 +020070
Dean Moldovan3b44dae2016-08-18 16:55:26 +020071 m.def("test_eval_file_failure", []() {
72 try {
73 py::eval_file("non-existing file");
74 } catch (std::exception &) {
75 return true;
76 }
77 return false;
78 });
Jason Rhinelander52f4be82016-09-03 14:54:22 -040079});