make compiled in python extension initialization more flexable
[mussa.git] / py / test / test_python.cpp
1 #define BOOST_AUTO_TEST_MAIN
2 #include <boost/test/auto_unit_test.hpp>
3
4 #include "py/python.hpp"
5 #include <string>
6
7 namespace py = boost::python;
8
9 BOOST_AUTO_TEST_CASE( execute_python )
10 {
11   get_py()->run("x = 3");
12   int x = py::extract<int>(get_py()->eval("x"));
13   BOOST_CHECK_EQUAL(x, 3);
14 }
15
16 BOOST_AUTO_TEST_CASE( lookup_python )
17 {
18   get_py()->run("import os");
19   py::object splitext = (*get_py())["os.path.splitext"];
20   py::object result = splitext("/home/diane/foo.txt");
21   std::string ext = py::extract<std::string>(result[1]);
22   BOOST_CHECK_EQUAL(ext, ".txt");
23
24   get_py()->run("from os.path import splitext");
25   py::object splitext2 = (*get_py())["splitext"];
26   py::object result2 = splitext("/home/diane/bar.txt");
27   std::string ext2 = py::extract<std::string>(result2[1]);
28   BOOST_CHECK_EQUAL(ext, ext2);
29 }