]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_server_shutdown_state.cpp
Drop wide/narrow conversion functions
[minetest.git] / src / unittest / test_server_shutdown_state.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 <server.h>
21 #include "test.h"
22
23 #include "util/string.h"
24 #include "util/serialize.h"
25
26 class FakeServer : public Server
27 {
28 public:
29         // clang-format off
30         FakeServer() : Server("fakeworld", SubgameSpec("fakespec", "fakespec"), true,
31                                         Address(), true, nullptr)
32         {
33         }
34         // clang-format on
35
36 private:
37         void SendChatMessage(session_t peer_id, const ChatMessage &message)
38         {
39                 // NOOP
40         }
41 };
42
43 class TestServerShutdownState : public TestBase
44 {
45 public:
46         TestServerShutdownState() { TestManager::registerTestModule(this); }
47         const char *getName() { return "TestServerShutdownState"; }
48
49         void runTests(IGameDef *gamedef);
50
51         void testInit();
52         void testReset();
53         void testTrigger();
54         void testTick();
55 };
56
57 static TestServerShutdownState g_test_instance;
58
59 void TestServerShutdownState::runTests(IGameDef *gamedef)
60 {
61         TEST(testInit);
62         TEST(testReset);
63         TEST(testTrigger);
64         TEST(testTick);
65 }
66
67 void TestServerShutdownState::testInit()
68 {
69         Server::ShutdownState ss;
70         UASSERT(!ss.is_requested);
71         UASSERT(!ss.should_reconnect);
72         UASSERT(ss.message.empty());
73         UASSERT(ss.m_timer == 0.0f);
74 }
75
76 void TestServerShutdownState::testReset()
77 {
78         Server::ShutdownState ss;
79         ss.reset();
80         UASSERT(!ss.is_requested);
81         UASSERT(!ss.should_reconnect);
82         UASSERT(ss.message.empty());
83         UASSERT(ss.m_timer == 0.0f);
84 }
85
86 void TestServerShutdownState::testTrigger()
87 {
88         Server::ShutdownState ss;
89         ss.trigger(3.0f, "testtrigger", true);
90         UASSERT(!ss.is_requested);
91         UASSERT(ss.should_reconnect);
92         UASSERT(ss.message == "testtrigger");
93         UASSERT(ss.m_timer == 3.0f);
94 }
95
96 void TestServerShutdownState::testTick()
97 {
98         std::unique_ptr<FakeServer> fakeServer(new FakeServer());
99         Server::ShutdownState ss;
100         ss.trigger(28.0f, "testtrigger", true);
101         ss.tick(0.0f, fakeServer.get());
102
103         // Tick with no time should not change anything
104         UASSERT(!ss.is_requested);
105         UASSERT(ss.should_reconnect);
106         UASSERT(ss.message == "testtrigger");
107         UASSERT(ss.m_timer == 28.0f);
108
109         // Tick 2 seconds
110         ss.tick(2.0f, fakeServer.get());
111         UASSERT(!ss.is_requested);
112         UASSERT(ss.should_reconnect);
113         UASSERT(ss.message == "testtrigger");
114         UASSERT(ss.m_timer == 26.0f);
115
116         // Tick remaining seconds + additional expire
117         ss.tick(26.1f, fakeServer.get());
118         UASSERT(ss.is_requested);
119         UASSERT(ss.should_reconnect);
120         UASSERT(ss.message == "testtrigger");
121         UASSERT(ss.m_timer == 0.0f);
122 }