3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
25 #include "inventory.h"
27 class TestInventory : public TestBase {
29 TestInventory() { TestManager::registerTestModule(this); }
30 const char *getName() { return "TestInventory"; }
32 void runTests(IGameDef *gamedef);
34 void testSerializeDeserialize(IItemDefManager *idef);
36 static const char *serialized_inventory_in;
37 static const char *serialized_inventory_out;
38 static const char *serialized_inventory_inc;
41 static TestInventory g_test_instance;
43 void TestInventory::runTests(IGameDef *gamedef)
45 TEST(testSerializeDeserialize, gamedef->getItemDefManager());
48 ////////////////////////////////////////////////////////////////////////////////
50 void TestInventory::testSerializeDeserialize(IItemDefManager *idef)
53 std::istringstream is(serialized_inventory_in, std::ios::binary);
56 UASSERT(inv.getList("0"));
57 UASSERT(!inv.getList("main"));
59 inv.getList("0")->setName("main");
60 UASSERT(!inv.getList("0"));
61 UASSERT(inv.getList("main"));
62 UASSERTEQ(u32, inv.getList("main")->getWidth(), 3);
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);
69 inv.setModified(false);
72 inv.serialize(inv_os, true);
73 UASSERTEQ(std::string, inv_os.str(), serialized_inventory_inc);
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);
80 UASSERT(leftover == wanted);
83 const char *TestInventory::serialized_inventory_in =
88 "Item default:cobble 61\n"
91 "Item default:dirt 71\n"
93 "Item default:dirt 99\n"
94 "Item default:cobble 38\n"
98 "Item default:stick 3\n"
103 const char *TestInventory::serialized_inventory_out =
108 "Item default:cobble 61\n"
111 "Item default:dirt 71\n"
113 "Item default:dirt 99\n"
114 "Item default:cobble 38\n"
119 "Item default:stick 3\n"
123 const char *TestInventory::serialized_inventory_inc =