PythonInitTest.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <boost/python.hpp>
  2. #include <gtest/gtest.h>
  3. #include <gflags/gflags.h>
  4. #include <glog/logging.h>
  5. #include "Python/PythonInit.h"
  6. namespace Egametang {
  7. using namespace boost::python;
  8. class PythonInitTest: public testing::Test
  9. {
  10. public:
  11. PythonInitTest(): python_init_()
  12. {}
  13. protected:
  14. PythonInit python_init_;
  15. };
  16. TEST_F(PythonInitTest, Int)
  17. {
  18. boost::python::object i(10);
  19. i = 10 * i;
  20. ASSERT_EQ(100, boost::python::extract<int>(i));
  21. }
  22. TEST_F(PythonInitTest, String)
  23. {
  24. boost::python::object i("ab");
  25. std::string str = boost::python::extract<std::string>(i * 5);
  26. ASSERT_EQ("ababababab", str);
  27. }
  28. TEST_F(PythonInitTest, List)
  29. {
  30. boost::python::list list;
  31. list.append("zelda");
  32. list.append(2.236);
  33. ASSERT_EQ(2, boost::python::len(list));
  34. ASSERT_EQ(1, list.count("zelda"));
  35. ASSERT_FLOAT_EQ(2.236, boost::python::extract<double>(list[1]));
  36. }
  37. TEST_F(PythonInitTest, Tuple)
  38. {
  39. boost::python::tuple tuple = boost::python::make_tuple("metroid", "samus", "ridley");
  40. ASSERT_EQ(3, boost::python::len(tuple));
  41. ASSERT_EQ("samus", std::string(boost::python::extract<std::string>(tuple[-2])));
  42. }
  43. TEST_F(PythonInitTest, Dict)
  44. {
  45. boost::python::dict dict;
  46. dict["mario"] = "peach";
  47. dict[0] = "killer7";
  48. ASSERT_TRUE(dict.has_key(0));
  49. ASSERT_EQ(2, boost::python::len(dict));
  50. }
  51. } // namespace Egametang
  52. int main(int argc, char* argv[])
  53. {
  54. FLAGS_logtostderr = true;
  55. testing::InitGoogleTest(&argc, argv);
  56. google::ParseCommandLineFlags(&argc, &argv, true);
  57. google::InitGoogleLogging(argv[0]);
  58. return RUN_ALL_TESTS();
  59. }