PythonInitTest.cc 1.4 KB

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