]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_moveaction.cpp
Disable Prometheus in singleplayer mode
[minetest.git] / src / unittest / test_moveaction.cpp
1 /*
2 Minetest
3 Copyright (C) 2022 Minetest core developers & community
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 "test.h"
21 #include "test_config.h"
22
23 #include "mock_inventorymanager.h"
24 #include "mock_server.h"
25 #include "mock_serveractiveobject.h"
26
27 #include <scripting_server.h>
28
29
30 class TestMoveAction : public TestBase
31 {
32 public:
33         TestMoveAction() { TestManager::registerTestModule(this); }
34         const char *getName() { return "TestMoveAction"; }
35
36         void runTests(IGameDef *gamedef);
37
38         void testMove(ServerActiveObject *obj, IGameDef *gamedef);
39         void testMoveSomewhere(ServerActiveObject *obj, IGameDef *gamedef);
40         void testMoveUnallowed(ServerActiveObject *obj, IGameDef *gamedef);
41         void testMovePartial(ServerActiveObject *obj, IGameDef *gamedef);
42         void testSwap(ServerActiveObject *obj, IGameDef *gamedef);
43         void testSwapFromUnallowed(ServerActiveObject *obj, IGameDef *gamedef);
44         void testSwapToUnallowed(ServerActiveObject *obj, IGameDef *gamedef);
45 };
46
47 static TestMoveAction g_test_instance;
48
49 void TestMoveAction::runTests(IGameDef *gamedef)
50 {
51         MockServer server;
52
53         ServerScripting server_scripting(&server);
54         server_scripting.loadMod(Server::getBuiltinLuaPath() + DIR_DELIM "init.lua", BUILTIN_MOD_NAME);
55         server_scripting.loadMod(std::string(HELPERS_PATH) + DIR_DELIM "helper_moveaction.lua", BUILTIN_MOD_NAME);
56
57         MetricsBackend mb;
58         ServerEnvironment server_env(nullptr, &server_scripting, &server, "", &mb);
59         MockServerActiveObject obj(&server_env);
60
61         TEST(testMove, &obj, gamedef);
62         TEST(testMoveSomewhere, &obj, gamedef);
63         TEST(testMoveUnallowed, &obj, gamedef);
64         TEST(testMovePartial, &obj, gamedef);
65         TEST(testSwap, &obj, gamedef);
66         TEST(testSwapFromUnallowed, &obj, gamedef);
67         TEST(testSwapToUnallowed, &obj, gamedef);
68 }
69
70 static ItemStack parse_itemstack(const char *s)
71 {
72         ItemStack item;
73         item.deSerialize(s, nullptr);
74         return item;
75 }
76
77 static void apply_action(const char *s, InventoryManager *inv, ServerActiveObject *obj, IGameDef *gamedef)
78 {
79         std::istringstream str(s);
80         InventoryAction *action = InventoryAction::deSerialize(str);
81         action->apply(inv, obj, gamedef);
82         delete action;
83 }
84
85 void TestMoveAction::testMove(ServerActiveObject *obj, IGameDef *gamedef)
86 {
87         MockInventoryManager inv(gamedef);
88
89         inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:stone 50"));
90         inv.p2.addList("main", 10);
91
92         apply_action("Move 20 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
93
94         UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:stone 30");
95         UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:stone 20");
96 }
97
98 void TestMoveAction::testMoveSomewhere(ServerActiveObject *obj, IGameDef *gamedef)
99 {
100         MockInventoryManager inv(gamedef);
101
102         inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:stone 50"));
103         InventoryList *list = inv.p2.addList("main", 10);
104         list->addItem(0, parse_itemstack("default:brick 10"));
105         list->addItem(2, parse_itemstack("default:stone 85"));
106
107         apply_action("MoveSomewhere 50 player:p1 main 0 player:p2 main", &inv, obj, gamedef);
108
109         UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:brick 10");
110         UASSERT(inv.p2.getList("main")->getItem(1).getItemString() == "default:stone 36");
111         UASSERT(inv.p2.getList("main")->getItem(2).getItemString() == "default:stone 99");
112 }
113
114 void TestMoveAction::testMoveUnallowed(ServerActiveObject *obj, IGameDef *gamedef)
115 {
116         MockInventoryManager inv(gamedef);
117
118         inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:water 50"));
119         inv.p2.addList("main", 10);
120
121         apply_action("Move 20 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
122
123         UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:water 50");
124         UASSERT(inv.p2.getList("main")->getItem(0).empty())
125 }
126
127 void TestMoveAction::testMovePartial(ServerActiveObject *obj, IGameDef *gamedef)
128 {
129         MockInventoryManager inv(gamedef);
130
131         inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:lava 50"));
132         inv.p2.addList("main", 10);
133
134         apply_action("Move 20 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
135
136         UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:lava 45");
137         UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:lava 5");
138 }
139
140 void TestMoveAction::testSwap(ServerActiveObject *obj, IGameDef *gamedef)
141 {
142         MockInventoryManager inv(gamedef);
143
144         inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:stone 50"));
145         inv.p2.addList("main", 10)->addItem(0, parse_itemstack("default:brick 60"));
146
147         apply_action("Move 50 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
148
149         UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:brick 60");
150         UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:stone 50");
151 }
152
153 void TestMoveAction::testSwapFromUnallowed(ServerActiveObject *obj, IGameDef *gamedef)
154 {
155         MockInventoryManager inv(gamedef);
156
157         inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:water 50"));
158         inv.p2.addList("main", 10)->addItem(0, parse_itemstack("default:brick 60"));
159
160         apply_action("Move 50 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
161
162         UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:water 50");
163         UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:brick 60");
164 }
165
166 void TestMoveAction::testSwapToUnallowed(ServerActiveObject *obj, IGameDef *gamedef)
167 {
168         MockInventoryManager inv(gamedef);
169
170         inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:stone 50"));
171         inv.p2.addList("main", 10)->addItem(0, parse_itemstack("default:water 60"));
172
173         apply_action("Move 50 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
174
175         UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:stone 50");
176         UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:water 60");
177 }