1a783afaebd0461cd1d67c9ec91171506d576ef2
[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;
37         static const char *serialized_inventory_2;
38 };
39
40 static TestInventory g_test_instance;
41
42 void TestInventory::runTests(IGameDef *gamedef)
43 {
44         TEST(testSerializeDeserialize, gamedef->getItemDefManager());
45 }
46
47 ////////////////////////////////////////////////////////////////////////////////
48
49 void TestInventory::testSerializeDeserialize(IItemDefManager *idef)
50 {
51         Inventory inv(idef);
52         std::istringstream is(serialized_inventory, std::ios::binary);
53
54         inv.deSerialize(is);
55         UASSERT(inv.getList("0"));
56         UASSERT(!inv.getList("main"));
57
58         inv.getList("0")->setName("main");
59         UASSERT(!inv.getList("0"));
60         UASSERT(inv.getList("main"));
61         UASSERTEQ(u32, inv.getList("main")->getWidth(), 3);
62
63         inv.getList("main")->setWidth(5);
64         std::ostringstream inv_os(std::ios::binary);
65         inv.serialize(inv_os);
66         UASSERTEQ(std::string, inv_os.str(), serialized_inventory_2);
67 }
68
69 const char *TestInventory::serialized_inventory =
70         "List 0 32\n"
71         "Width 3\n"
72         "Empty\n"
73         "Empty\n"
74         "Empty\n"
75         "Empty\n"
76         "Empty\n"
77         "Empty\n"
78         "Empty\n"
79         "Empty\n"
80         "Empty\n"
81         "Item default:cobble 61\n"
82         "Empty\n"
83         "Empty\n"
84         "Empty\n"
85         "Empty\n"
86         "Empty\n"
87         "Empty\n"
88         "Item default:dirt 71\n"
89         "Empty\n"
90         "Empty\n"
91         "Empty\n"
92         "Empty\n"
93         "Empty\n"
94         "Empty\n"
95         "Empty\n"
96         "Item default:dirt 99\n"
97         "Item default:cobble 38\n"
98         "Empty\n"
99         "Empty\n"
100         "Empty\n"
101         "Empty\n"
102         "Empty\n"
103         "Empty\n"
104         "EndInventoryList\n"
105         "EndInventory\n";
106
107 const char *TestInventory::serialized_inventory_2 =
108         "List main 32\n"
109         "Width 5\n"
110         "Empty\n"
111         "Empty\n"
112         "Empty\n"
113         "Empty\n"
114         "Empty\n"
115         "Empty\n"
116         "Empty\n"
117         "Empty\n"
118         "Empty\n"
119         "Item default:cobble 61\n"
120         "Empty\n"
121         "Empty\n"
122         "Empty\n"
123         "Empty\n"
124         "Empty\n"
125         "Empty\n"
126         "Item default:dirt 71\n"
127         "Empty\n"
128         "Empty\n"
129         "Empty\n"
130         "Empty\n"
131         "Empty\n"
132         "Empty\n"
133         "Empty\n"
134         "Item default:dirt 99\n"
135         "Item default:cobble 38\n"
136         "Empty\n"
137         "Empty\n"
138         "Empty\n"
139         "Empty\n"
140         "Empty\n"
141         "Empty\n"
142         "EndInventoryList\n"
143         "EndInventory\n";