test_eventmanager.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Minetest
  3. Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <unordered_map>
  17. #include "test.h"
  18. #include "client/event_manager.h"
  19. class TestEventManager : public TestBase
  20. {
  21. public:
  22. TestEventManager() { TestManager::registerTestModule(this); }
  23. const char *getName() override { return "TestEventManager"; }
  24. void runTests(IGameDef *gamedef) override;
  25. void testRegister();
  26. void testDeregister();
  27. void testRealEvent();
  28. void testRealEventAfterDereg();
  29. };
  30. // EventManager test class
  31. class EventManagerTest : public EventManager
  32. {
  33. public:
  34. static void eventTest(MtEvent *e, void *data)
  35. {
  36. UASSERT(e->getType() >= 0);
  37. UASSERT(e->getType() < MtEvent::TYPE_MAX);
  38. EventManagerTest *emt = (EventManagerTest *)data;
  39. emt->m_test_value = e->getType();
  40. }
  41. u64 getTestValue() const { return m_test_value; }
  42. void resetValue() { m_test_value = 0; }
  43. private:
  44. u64 m_test_value = 0;
  45. };
  46. static TestEventManager g_test_instance;
  47. void TestEventManager::runTests(IGameDef *gamedef)
  48. {
  49. TEST(testRegister);
  50. TEST(testDeregister);
  51. TEST(testRealEvent);
  52. TEST(testRealEventAfterDereg);
  53. }
  54. void TestEventManager::testRegister()
  55. {
  56. EventManager ev;
  57. ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
  58. ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
  59. }
  60. void TestEventManager::testDeregister()
  61. {
  62. EventManager ev;
  63. ev.dereg(MtEvent::NODE_DUG, nullptr, nullptr);
  64. ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
  65. ev.dereg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
  66. }
  67. void TestEventManager::testRealEvent()
  68. {
  69. EventManager ev;
  70. auto emt = std::make_unique<EventManagerTest>();
  71. ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
  72. // Put event & verify event value
  73. ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
  74. UASSERT(emt->getTestValue() == MtEvent::PLAYER_REGAIN_GROUND);
  75. }
  76. void TestEventManager::testRealEventAfterDereg()
  77. {
  78. EventManager ev;
  79. auto emt = std::make_unique<EventManagerTest>();
  80. ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
  81. // Put event & verify event value
  82. ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
  83. UASSERT(emt->getTestValue() == MtEvent::PLAYER_REGAIN_GROUND);
  84. // Reset internal value
  85. emt->resetValue();
  86. // Remove the registered event
  87. ev.dereg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
  88. // Push the new event & ensure we target the default value
  89. ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
  90. UASSERT(emt->getTestValue() == 0);
  91. }