PythonEntryTest.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <gtest/gtest.h>
  2. #include <gflags/gflags.h>
  3. #include <glog/logging.h>
  4. #include <boost/python.hpp>
  5. #include "Python/PythonEntry.h"
  6. namespace Egametang {
  7. class PythonEntryTest: public testing::Test
  8. {
  9. protected:
  10. PythonEntry python_entry_;
  11. public:
  12. PythonEntryTest(): python_entry_()
  13. {}
  14. };
  15. class PersonTest
  16. {
  17. private:
  18. int guid_;
  19. std::string name_;
  20. public:
  21. PersonTest(): guid_(0)
  22. {
  23. }
  24. void SetGuid(int guid)
  25. {
  26. guid_ = guid;
  27. }
  28. int Guid() const
  29. {
  30. return guid_;
  31. }
  32. void SetName(const std::string& name)
  33. {
  34. name_ = name;
  35. }
  36. std::string Name() const
  37. {
  38. return name_;
  39. }
  40. };
  41. typedef boost::shared_ptr<PersonTest> PersonTestPtr;
  42. BOOST_PYTHON_MODULE(PersonTest)
  43. {
  44. boost::python::class_<PersonTest>("Person")
  45. .def("SetGuid", &PersonTest::SetGuid)
  46. .def("Guid", &PersonTest::Guid)
  47. .def("SetName", &PersonTest::SetName)
  48. .def("Name", &PersonTest::Name)
  49. ;
  50. boost::python::register_ptr_to_python<PersonTestPtr>();
  51. }
  52. TEST_F(PythonEntryTest, EnterPythonScript)
  53. {
  54. initPersonTest();
  55. python_entry_.ImportPath("../../../Src/Egametang/Python/");
  56. python_entry_.ImportModule("PythonEntryTest");
  57. PersonTestPtr person(new PersonTest);
  58. python_entry_.RegisterObjectPtr("person", person);
  59. ASSERT_EQ(0, person->Guid());
  60. // 进到python脚本层设置person的值为2
  61. python_entry_.Execute("PythonEntryTest.fun(person)");
  62. ASSERT_EQ(2, person->Guid());
  63. ASSERT_EQ(std::string("tanghai"), person->Name());
  64. }
  65. } // namespace Egametang
  66. int main(int argc, char* argv[])
  67. {
  68. FLAGS_logtostderr = true;
  69. testing::InitGoogleTest(&argc, argv);
  70. google::ParseCommandLineFlags(&argc, &argv, true);
  71. google::InitGoogleLogging(argv[0]);
  72. return RUN_ALL_TESTS();
  73. }