Translated using Weblate (Italian)
[oweals/minetest.git] / src / unittest / test_inventory.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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
22 #include <sstream>
23
24 #include "gamedef.h"
25 #include "inventory.h"
26
27 class TestInventory : public TestBase {
28 public:
29         TestInventory() { TestManager::registerTestModule(this); }
30         const char *getName() { return "TestInventory"; }
31
32         void runTests(IGameDef *gamedef);
33
34         void testSerializeDeserialize(IItemDefManager *idef);
35
36         static const char *serialized_inventory_in;
37         static const char *serialized_inventory_out;
38         static const char *serialized_inventory_inc;
39 };
40
41 static TestInventory g_test_instance;
42
43 void TestInventory::runTests(IGameDef *gamedef)
44 {
45         TEST(testSerializeDeserialize, gamedef->getItemDefManager());
46 }
47
48 ////////////////////////////////////////////////////////////////////////////////
49
50 void TestInventory::testSerializeDeserialize(IItemDefManager *idef)
51 {
52         Inventory inv(idef);
53         std::istringstream is(serialized_inventory_in, std::ios::binary);
54
55         inv.deSerialize(is);
56         UASSERT(inv.getList("0"));
57         UASSERT(!inv.getList("main"));
58
59         inv.getList("0")->setName("main");
60         UASSERT(!inv.getList("0"));
61         UASSERT(inv.getList("main"));
62         UASSERTEQ(u32, inv.getList("main")->getWidth(), 3);
63
64         inv.getList("main")->setWidth(5);
65         std::ostringstream inv_os(std::ios::binary);
66         inv.serialize(inv_os, false);
67         UASSERTEQ(std::string, inv_os.str(), serialized_inventory_out);
68
69         inv.setModified(false);
70         inv_os.str("");
71         inv_os.clear();
72         inv.serialize(inv_os, true);
73         UASSERTEQ(std::string, inv_os.str(), serialized_inventory_inc);
74
75         ItemStack leftover = inv.getList("main")->takeItem(7, 99 - 12);
76         ItemStack wanted = ItemStack("default:dirt", 99 - 12, 0, idef);
77         UASSERT(leftover == wanted);
78         leftover = inv.getList("main")->getItem(7);
79         wanted.count = 12;
80         UASSERT(leftover == wanted);
81 }
82
83 const char *TestInventory::serialized_inventory_in =
84         "List 0 10\n"
85         "Width 3\n"
86         "Empty\n"
87         "Empty\n"
88         "Item default:cobble 61\n"
89         "Empty\n"
90         "Empty\n"
91         "Item default:dirt 71\n"
92         "Empty\n"
93         "Item default:dirt 99\n"
94         "Item default:cobble 38\n"
95         "Empty\n"
96         "EndInventoryList\n"
97         "List abc 1\n"
98         "Item default:stick 3\n"
99         "Width 0\n"
100         "EndInventoryList\n"
101         "EndInventory\n";
102
103 const char *TestInventory::serialized_inventory_out =
104         "List main 10\n"
105         "Width 5\n"
106         "Empty\n"
107         "Empty\n"
108         "Item default:cobble 61\n"
109         "Empty\n"
110         "Empty\n"
111         "Item default:dirt 71\n"
112         "Empty\n"
113         "Item default:dirt 99\n"
114         "Item default:cobble 38\n"
115         "Empty\n"
116         "EndInventoryList\n"
117         "List abc 1\n"
118         "Width 0\n"
119         "Item default:stick 3\n"
120         "EndInventoryList\n"
121         "EndInventory\n";
122
123 const char *TestInventory::serialized_inventory_inc =
124         "KeepList main\n"
125         "KeepList abc\n"
126         "EndInventory\n";