3 Copyright (C) 2022 Minetest core developers & community
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.
21 #include "test_config.h"
23 #include "mock_inventorymanager.h"
24 #include "mock_server.h"
25 #include "mock_serveractiveobject.h"
27 #include <scripting_server.h>
30 class TestMoveAction : public TestBase
33 TestMoveAction() { TestManager::registerTestModule(this); }
34 const char *getName() { return "TestMoveAction"; }
36 void runTests(IGameDef *gamedef);
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);
47 static TestMoveAction g_test_instance;
49 void TestMoveAction::runTests(IGameDef *gamedef)
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);
58 ServerEnvironment server_env(nullptr, &server_scripting, &server, "", &mb);
59 MockServerActiveObject obj(&server_env);
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);
70 static ItemStack parse_itemstack(const char *s)
73 item.deSerialize(s, nullptr);
77 static void apply_action(const char *s, InventoryManager *inv, ServerActiveObject *obj, IGameDef *gamedef)
79 std::istringstream str(s);
80 InventoryAction *action = InventoryAction::deSerialize(str);
81 action->apply(inv, obj, gamedef);
85 void TestMoveAction::testMove(ServerActiveObject *obj, IGameDef *gamedef)
87 MockInventoryManager inv(gamedef);
89 inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:stone 50"));
90 inv.p2.addList("main", 10);
92 apply_action("Move 20 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
94 UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:stone 30");
95 UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:stone 20");
98 void TestMoveAction::testMoveSomewhere(ServerActiveObject *obj, IGameDef *gamedef)
100 MockInventoryManager inv(gamedef);
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"));
107 apply_action("MoveSomewhere 50 player:p1 main 0 player:p2 main", &inv, obj, gamedef);
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");
114 void TestMoveAction::testMoveUnallowed(ServerActiveObject *obj, IGameDef *gamedef)
116 MockInventoryManager inv(gamedef);
118 inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:water 50"));
119 inv.p2.addList("main", 10);
121 apply_action("Move 20 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
123 UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:water 50");
124 UASSERT(inv.p2.getList("main")->getItem(0).empty())
127 void TestMoveAction::testMovePartial(ServerActiveObject *obj, IGameDef *gamedef)
129 MockInventoryManager inv(gamedef);
131 inv.p1.addList("main", 10)->addItem(0, parse_itemstack("default:lava 50"));
132 inv.p2.addList("main", 10);
134 apply_action("Move 20 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
136 UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:lava 45");
137 UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:lava 5");
140 void TestMoveAction::testSwap(ServerActiveObject *obj, IGameDef *gamedef)
142 MockInventoryManager inv(gamedef);
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"));
147 apply_action("Move 50 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
149 UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:brick 60");
150 UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:stone 50");
153 void TestMoveAction::testSwapFromUnallowed(ServerActiveObject *obj, IGameDef *gamedef)
155 MockInventoryManager inv(gamedef);
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"));
160 apply_action("Move 50 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
162 UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:water 50");
163 UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:brick 60");
166 void TestMoveAction::testSwapToUnallowed(ServerActiveObject *obj, IGameDef *gamedef)
168 MockInventoryManager inv(gamedef);
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"));
173 apply_action("Move 50 player:p1 main 0 player:p2 main 0", &inv, obj, gamedef);
175 UASSERT(inv.p1.getList("main")->getItem(0).getItemString() == "default:stone 50");
176 UASSERT(inv.p2.getList("main")->getItem(0).getItemString() == "default:water 60");