PythonInitTest.cc 1.5 KB

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