3 Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <unordered_map>
22 #include "client/event_manager.h"
24 class TestEventManager : public TestBase
27 TestEventManager() { TestManager::registerTestModule(this); }
28 const char *getName() override { return "TestEventManager"; }
30 void runTests(IGameDef *gamedef) override;
33 void testDeregister();
35 void testRealEventAfterDereg();
38 // EventManager test class
39 class EventManagerTest : public EventManager
42 static void eventTest(MtEvent *e, void *data)
44 UASSERT(e->getType() >= 0);
45 UASSERT(e->getType() < MtEvent::TYPE_MAX);
46 EventManagerTest *emt = (EventManagerTest *)data;
47 emt->m_test_value = e->getType();
50 u64 getTestValue() const { return m_test_value; }
51 void resetValue() { m_test_value = 0; }
57 static TestEventManager g_test_instance;
59 void TestEventManager::runTests(IGameDef *gamedef)
64 TEST(testRealEventAfterDereg);
67 void TestEventManager::testRegister()
70 ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
71 ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
74 void TestEventManager::testDeregister()
77 ev.dereg(MtEvent::NODE_DUG, nullptr, nullptr);
78 ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
79 ev.dereg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
82 void TestEventManager::testRealEvent()
85 std::unique_ptr<EventManagerTest> emt(new EventManagerTest());
86 ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
88 // Put event & verify event value
89 ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
90 UASSERT(emt->getTestValue() == MtEvent::PLAYER_REGAIN_GROUND);
93 void TestEventManager::testRealEventAfterDereg()
96 std::unique_ptr<EventManagerTest> emt(new EventManagerTest());
97 ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
99 // Put event & verify event value
100 ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
101 UASSERT(emt->getTestValue() == MtEvent::PLAYER_REGAIN_GROUND);
103 // Reset internal value
106 // Remove the registered event
107 ev.dereg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
109 // Push the new event & ensure we target the default value
110 ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
111 UASSERT(emt->getTestValue() == 0);