PythonInitTest.cc 1.4 KB

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