Implement a convienence class for accessing python interpreter
[mussa.git] / py / test / test_python.cpp
diff --git a/py/test/test_python.cpp b/py/test/test_python.cpp
new file mode 100644 (file)
index 0000000..c33b7ad
--- /dev/null
@@ -0,0 +1,29 @@
+#define BOOST_AUTO_TEST_MAIN
+#include <boost/test/auto_unit_test.hpp>
+
+#include "py/python.hpp"
+#include <string>
+
+namespace py = boost::python;
+
+BOOST_AUTO_TEST_CASE( execute_python )
+{
+  get_py().run("x = 3");
+  int x = py::extract<int>(get_py().eval("x"));
+  BOOST_CHECK_EQUAL(x, 3);
+}
+
+BOOST_AUTO_TEST_CASE( lookup_python )
+{
+  get_py().run("import os");
+  py::object splitext = get_py()["os.path.splitext"];
+  py::object result = splitext("/home/diane/foo.txt");
+  std::string ext = py::extract<std::string>(result[1]);
+  BOOST_CHECK_EQUAL(ext, ".txt");
+
+  get_py().run("from os.path import splitext");
+  py::object splitext2 = get_py()["splitext"];
+  py::object result2 = splitext("/home/diane/bar.txt");
+  std::string ext2 = py::extract<std::string>(result2[1]);
+  BOOST_CHECK_EQUAL(ext, ext2);
+}