]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_eventmanager.cpp
Merge pull request #35 from arydevy/patch-1
[dragonfireclient.git] / src / unittest / test_eventmanager.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
4
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.
9
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.
14
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.
18 */
19
20 #include <unordered_map>
21 #include "test.h"
22 #include "client/event_manager.h"
23
24 class TestEventManager : public TestBase
25 {
26 public:
27         TestEventManager() { TestManager::registerTestModule(this); }
28         const char *getName() override { return "TestEventManager"; }
29
30         void runTests(IGameDef *gamedef) override;
31
32         void testRegister();
33         void testDeregister();
34         void testRealEvent();
35         void testRealEventAfterDereg();
36 };
37
38 // EventManager test class
39 class EventManagerTest : public EventManager
40 {
41 public:
42         static void eventTest(MtEvent *e, void *data)
43         {
44                 UASSERT(e->getType() >= 0);
45                 UASSERT(e->getType() < MtEvent::TYPE_MAX);
46                 EventManagerTest *emt = (EventManagerTest *)data;
47                 emt->m_test_value = e->getType();
48         }
49
50         u64 getTestValue() const { return m_test_value; }
51         void resetValue() { m_test_value = 0; }
52
53 private:
54         u64 m_test_value = 0;
55 };
56
57 static TestEventManager g_test_instance;
58
59 void TestEventManager::runTests(IGameDef *gamedef)
60 {
61         TEST(testRegister);
62         TEST(testDeregister);
63         TEST(testRealEvent);
64         TEST(testRealEventAfterDereg);
65 }
66
67 void TestEventManager::testRegister()
68 {
69         EventManager ev;
70         ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
71         ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
72 }
73
74 void TestEventManager::testDeregister()
75 {
76         EventManager ev;
77         ev.dereg(MtEvent::NODE_DUG, nullptr, nullptr);
78         ev.reg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
79         ev.dereg(MtEvent::PLAYER_DAMAGE, nullptr, nullptr);
80 }
81
82 void TestEventManager::testRealEvent()
83 {
84         EventManager ev;
85         std::unique_ptr<EventManagerTest> emt(new EventManagerTest());
86         ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
87
88         // Put event & verify event value
89         ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
90         UASSERT(emt->getTestValue() == MtEvent::PLAYER_REGAIN_GROUND);
91 }
92
93 void TestEventManager::testRealEventAfterDereg()
94 {
95         EventManager ev;
96         std::unique_ptr<EventManagerTest> emt(new EventManagerTest());
97         ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
98
99         // Put event & verify event value
100         ev.put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
101         UASSERT(emt->getTestValue() == MtEvent::PLAYER_REGAIN_GROUND);
102
103         // Reset internal value
104         emt->resetValue();
105
106         // Remove the registered event
107         ev.dereg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
108
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);
112 }