PythonInitTest.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. protected:
  11. PythonInit python_init;
  12. public:
  13. PythonInitTest(): python_init()
  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. FLAGS_logtostderr = true;
  59. testing::InitGoogleTest(&argc, argv);
  60. google::ParseCommandLineFlags(&argc, &argv, true);
  61. google::InitGoogleLogging(argv[0]);
  62. return RUN_ALL_TESTS();
  63. }