PythonInitTest.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <gtest/gtest.h>
  2. #include <gflags/gflags.h>
  3. #include <glog/logging.h>
  4. #include "Python/PythonInit.h"
  5. namespace Egametang {
  6. using namespace boost::python;
  7. class PythonInitTest: public testing::Test
  8. {
  9. public:
  10. PythonInitTest(): python_init()
  11. {}
  12. protected:
  13. PythonInit python_init;
  14. };
  15. TEST_F(PythonInitTest, Int)
  16. {
  17. boost::python::object i(10);
  18. i = 10 * i;
  19. ASSERT_EQ(100, boost::python::extract<int>(i));
  20. }
  21. TEST_F(PythonInitTest, String)
  22. {
  23. boost::python::object i("ab");
  24. std::string str = boost::python::extract<std::string>(i * 5);
  25. ASSERT_EQ("ababababab", str);
  26. }
  27. TEST_F(PythonInitTest, List)
  28. {
  29. boost::python::list list;
  30. list.append("zelda");
  31. list.append(2.236);
  32. ASSERT_EQ(2, boost::python::len(list));
  33. ASSERT_EQ(1, list.count("zelda"));
  34. ASSERT_FLOAT_EQ(2.236, boost::python::extract<double>(list[1]));
  35. }
  36. TEST_F(PythonInitTest, Tuple)
  37. {
  38. boost::python::tuple tuple = boost::python::make_tuple("metroid", "samus", "ridley");
  39. ASSERT_EQ(3, boost::python::len(tuple));
  40. ASSERT_EQ("samus", std::string(boost::python::extract<std::string>(tuple[-2])));
  41. }
  42. TEST_F(PythonInitTest, Dict)
  43. {
  44. boost::python::dict dict;
  45. dict["mario"] = "peach";
  46. dict[0] = "killer7";
  47. ASSERT_TRUE(dict.has_key(0));
  48. ASSERT_EQ(2, boost::python::len(dict));
  49. }
  50. class Person
  51. {
  52. private:
  53. int guid_;
  54. std::string name_;
  55. public:
  56. Person(): guid_(0)
  57. {
  58. }
  59. void SetGuid(int guid)
  60. {
  61. guid_ = guid;
  62. }
  63. int Guid() const
  64. {
  65. return guid_;
  66. }
  67. void SetName(const std::string& name)
  68. {
  69. name_ = name;
  70. }
  71. std::string Name() const
  72. {
  73. return name_;
  74. }
  75. };
  76. typedef boost::shared_ptr<Person> PersonPtr;
  77. BOOST_PYTHON_MODULE(Person)
  78. {
  79. boost::python::class_<Person>("Person")
  80. .def("SetGuid", &Person::SetGuid)
  81. .def("Guid", &Person::Guid)
  82. ;
  83. boost::python::register_ptr_to_python<PersonPtr>();
  84. }
  85. TEST_F(PythonInitTest, EnterPythonScript)
  86. {
  87. try
  88. {
  89. initPerson();
  90. boost::python::object main_module = boost::python::import("__main__");
  91. boost::python::object main_namespace = main_module.attr("__dict__");
  92. PersonPtr person(new Person);
  93. main_namespace["person"] = person;
  94. std::string str = "import sys\n"
  95. "sys.path.append('../../../Src/Egametang/Python/')\n"
  96. "import Person\n"
  97. "import PythonInitTest\n"
  98. "PythonInitTest.fun(person)\n";
  99. ASSERT_EQ(0, person->Guid());
  100. // 进到python脚本层设置person的值为2
  101. boost::python::exec(str.c_str(), main_namespace);
  102. ASSERT_EQ(2, person->Guid());
  103. }
  104. catch (boost::python::error_already_set& err)
  105. {
  106. python_init.PrintError();
  107. throw err;
  108. }
  109. }
  110. } // namespace Egametang
  111. int main(int argc, char* argv[])
  112. {
  113. FLAGS_logtostderr = true;
  114. testing::InitGoogleTest(&argc, argv);
  115. google::ParseCommandLineFlags(&argc, &argv, true);
  116. google::InitGoogleLogging(argv[0]);
  117. return RUN_ALL_TESTS();
  118. }