PythonInitTest.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. } // namespace Egametang
  51. int main(int argc, char* argv[])
  52. {
  53. FLAGS_logtostderr = true;
  54. testing::InitGoogleTest(&argc, argv);
  55. google::ParseCommandLineFlags(&argc, &argv, true);
  56. google::InitGoogleLogging(argv[0]);
  57. return RUN_ALL_TESTS();
  58. }