|
|
@@ -10,11 +10,11 @@ using namespace boost::python;
|
|
|
class PythonInitTest: public testing::Test
|
|
|
{
|
|
|
public:
|
|
|
- PythonInitTest(): python_init()
|
|
|
+ PythonInitTest(): python_init_()
|
|
|
{}
|
|
|
|
|
|
protected:
|
|
|
- PythonInit python_init;
|
|
|
+ PythonInit python_init_;
|
|
|
};
|
|
|
|
|
|
TEST_F(PythonInitTest, Int)
|
|
|
@@ -57,77 +57,6 @@ TEST_F(PythonInitTest, Dict)
|
|
|
ASSERT_EQ(2, boost::python::len(dict));
|
|
|
}
|
|
|
|
|
|
-class PersonTest
|
|
|
-{
|
|
|
-private:
|
|
|
- int guid_;
|
|
|
- std::string name_;
|
|
|
-
|
|
|
-public:
|
|
|
- PersonTest(): guid_(0)
|
|
|
- {
|
|
|
- }
|
|
|
- void SetGuid(int guid)
|
|
|
- {
|
|
|
- guid_ = guid;
|
|
|
- }
|
|
|
-
|
|
|
- int Guid() const
|
|
|
- {
|
|
|
- return guid_;
|
|
|
- }
|
|
|
-
|
|
|
- void SetName(const std::string& name)
|
|
|
- {
|
|
|
- name_ = name;
|
|
|
- }
|
|
|
-
|
|
|
- std::string Name() const
|
|
|
- {
|
|
|
- return name_;
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-typedef boost::shared_ptr<PersonTest> PersonTestPtr;
|
|
|
-
|
|
|
-BOOST_PYTHON_MODULE(PersonTest)
|
|
|
-{
|
|
|
- boost::python::class_<PersonTest>("Person")
|
|
|
- .def("SetGuid", &PersonTest::SetGuid)
|
|
|
- .def("Guid", &PersonTest::Guid)
|
|
|
- .def("SetName", &PersonTest::SetName)
|
|
|
- .def("Name", &PersonTest::Name)
|
|
|
- ;
|
|
|
- boost::python::register_ptr_to_python<PersonTestPtr>();
|
|
|
-}
|
|
|
-
|
|
|
-TEST_F(PythonInitTest, EnterPythonScript)
|
|
|
-{
|
|
|
- try
|
|
|
- {
|
|
|
- initPersonTest();
|
|
|
- boost::python::object main_module = boost::python::import("__main__");
|
|
|
- boost::python::object main_namespace = main_module.attr("__dict__");
|
|
|
- PersonTestPtr person(new PersonTest);
|
|
|
- main_namespace["person"] = person;
|
|
|
- std::string str = "import sys\n"
|
|
|
- "sys.path.append('../../../Src/Egametang/Python/')\n"
|
|
|
- "import PythonInitTest\n"
|
|
|
- "PythonInitTest.fun(person)\n";
|
|
|
- ASSERT_EQ(0, person->Guid());
|
|
|
-
|
|
|
- // 进到python脚本层设置person的值为2
|
|
|
- boost::python::exec(str.c_str(), main_namespace);
|
|
|
- ASSERT_EQ(2, person->Guid());
|
|
|
- ASSERT_EQ(std::string("tanghai"), person->Name());
|
|
|
- }
|
|
|
- catch (boost::python::error_already_set& err)
|
|
|
- {
|
|
|
- python_init.PrintError();
|
|
|
- throw err;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
} // namespace Egametang
|
|
|
|
|
|
int main(int argc, char* argv[])
|