GameEventsTest.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <fcntl.h>
  2. #include <fstream>
  3. #include <gtest/gtest.h>
  4. #include <gflags/gflags.h>
  5. #include <glog/logging.h>
  6. #include <google/protobuf/text_format.h>
  7. #include "BehaviorTree/GameEvents.h"
  8. #include "BehaviorTree/NodeFactories.h"
  9. #include "BehaviorTree/BehaviorTreeConf.pb.h"
  10. #include "BehaviorTree/SpellBuff.h"
  11. #include "BehaviorTree/CombatContex.h"
  12. #include "BehaviorTree/EventDefine.h"
  13. namespace Egametang {
  14. class GameEventsTest: public testing::Test
  15. {
  16. protected:
  17. NodeFactories factories;
  18. GameEvents game_events;
  19. public:
  20. GameEventsTest():factories(), game_events(factories)
  21. {
  22. }
  23. virtual ~GameEventsTest()
  24. {
  25. }
  26. };
  27. static void FileToString(const std::string& file, std::string& string)
  28. {
  29. std::ifstream in(file.c_str());
  30. std::ostringstream os;
  31. os << in.rdbuf();
  32. string = os.str();
  33. in.close();
  34. }
  35. TEST_F(GameEventsTest, Vampire)
  36. {
  37. std::string file = "../../../Cpp/Game/BehaviorTree/Vampire.txt";
  38. std::string string;
  39. FileToString(file, string);
  40. BehaviorTreeConf conf;
  41. google::protobuf::TextFormat::ParseFromString(string, &conf);
  42. game_events.AddEvent(conf);
  43. Unit caster;
  44. Unit victim;
  45. caster.health = 2000;
  46. victim.health = 2000;
  47. Spell spell;
  48. Buff buff;
  49. spell.caster = &caster;
  50. spell.victim = &victim;
  51. CombatContex contex(&spell, &buff);
  52. game_events.Excute(ON_HIT, &contex);
  53. ASSERT_EQ(2000, caster.health);
  54. ASSERT_EQ(2000, victim.health);
  55. buff.buffType = 2;
  56. game_events.Excute(ON_HIT, &contex);
  57. ASSERT_EQ(2100, caster.health);
  58. ASSERT_EQ(1900, victim.health);
  59. }
  60. } // namespace Egametang
  61. int main(int argc, char* argv[])
  62. {
  63. testing::InitGoogleTest(&argc, argv);
  64. google::ParseCommandLineFlags(&argc, &argv, true);
  65. google::InitGoogleLogging(argv[0]);
  66. return RUN_ALL_TESTS();
  67. }