GameEventsTest.cc 1.5 KB

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