Cleanup sound manager class (#7158)
[oweals/minetest.git] / src / unittest / test_servermodmanager.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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 <algorithm>
22 #include "server/mods.h"
23 #include "test_config.h"
24
25 class TestServerModManager : public TestBase
26 {
27 public:
28         TestServerModManager() { TestManager::registerTestModule(this); }
29         const char *getName() { return "TestServerModManager"; }
30
31         void runTests(IGameDef *gamedef);
32
33         void testCreation();
34         void testIsConsistent();
35         void testUnsatisfiedMods();
36         void testGetMods();
37         void testGetModsWrongDir();
38         void testGetModspec();
39         void testGetModNamesWrongDir();
40         void testGetModNames();
41         void testGetModMediaPathsWrongDir();
42         void testGetModMediaPaths();
43 };
44
45 static TestServerModManager g_test_instance;
46
47 void TestServerModManager::runTests(IGameDef *gamedef)
48 {
49         const char *saved_env_mt_subgame_path = getenv("MINETEST_SUBGAME_PATH");
50 #ifdef WIN32
51         {
52                 std::string subgame_path("MINETEST_SUBGAME_PATH=");
53                 subgame_path.append(TEST_SUBGAME_PATH);
54                 _putenv(subgame_path.c_str());
55         }
56 #else
57         setenv("MINETEST_SUBGAME_PATH", TEST_SUBGAME_PATH, 1);
58 #endif
59
60         TEST(testCreation);
61         TEST(testIsConsistent);
62         TEST(testGetModsWrongDir);
63         TEST(testUnsatisfiedMods);
64         TEST(testGetMods);
65         TEST(testGetModspec);
66         TEST(testGetModNamesWrongDir);
67         TEST(testGetModNames);
68         TEST(testGetModMediaPathsWrongDir);
69         TEST(testGetModMediaPaths);
70
71 #ifdef WIN32
72         {
73                 std::string subgame_path("MINETEST_SUBGAME_PATH=");
74                 if (saved_env_mt_subgame_path)
75                         subgame_path.append(saved_env_mt_subgame_path);
76                 _putenv(subgame_path.c_str());
77         }
78 #else
79         if (saved_env_mt_subgame_path)
80                 setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
81         else
82                 unsetenv("MINETEST_SUBGAME_PATH");
83 #endif
84 }
85
86 void TestServerModManager::testCreation()
87 {
88         ServerModManager sm(TEST_WORLDDIR);
89 }
90
91 void TestServerModManager::testGetModsWrongDir()
92 {
93         // Test in non worlddir to ensure no mods are found
94         ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
95         UASSERTEQ(bool, sm.getMods().empty(), true);
96 }
97
98 void TestServerModManager::testUnsatisfiedMods()
99 {
100         ServerModManager sm(std::string(TEST_WORLDDIR));
101         UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
102 }
103
104 void TestServerModManager::testIsConsistent()
105 {
106         ServerModManager sm(std::string(TEST_WORLDDIR));
107         UASSERTEQ(bool, sm.isConsistent(), true);
108 }
109
110 void TestServerModManager::testGetMods()
111 {
112         ServerModManager sm(std::string(TEST_WORLDDIR));
113         const auto &mods = sm.getMods();
114         UASSERTEQ(bool, mods.empty(), false);
115
116         // Ensure we found default mod inside the test folder
117         bool default_found = false;
118         for (const auto &m : mods) {
119                 if (m.name == "default")
120                         default_found = true;
121
122                 // Verify if paths are not empty
123                 UASSERTEQ(bool, m.path.empty(), false);
124         }
125
126         UASSERTEQ(bool, default_found, true);
127 }
128
129 void TestServerModManager::testGetModspec()
130 {
131         ServerModManager sm(std::string(TEST_WORLDDIR));
132         UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
133         UASSERT(sm.getModSpec("default") != NULL);
134 }
135
136 void TestServerModManager::testGetModNamesWrongDir()
137 {
138         ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
139         std::vector<std::string> result;
140         sm.getModNames(result);
141         UASSERTEQ(bool, result.empty(), true);
142 }
143
144 void TestServerModManager::testGetModNames()
145 {
146         ServerModManager sm(std::string(TEST_WORLDDIR));
147         std::vector<std::string> result;
148         sm.getModNames(result);
149         UASSERTEQ(bool, result.empty(), false);
150         UASSERT(std::find(result.begin(), result.end(), "default") != result.end());
151 }
152
153 void TestServerModManager::testGetModMediaPathsWrongDir()
154 {
155         ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
156         std::vector<std::string> result;
157         sm.getModsMediaPaths(result);
158         UASSERTEQ(bool, result.empty(), true);
159 }
160
161 void TestServerModManager::testGetModMediaPaths()
162 {
163         ServerModManager sm(std::string(TEST_WORLDDIR));
164         std::vector<std::string> result;
165         sm.getModsMediaPaths(result);
166         UASSERTEQ(bool, result.empty(), false);
167         // We should have 5 folders for each mod (textures, media, locale, model, sounds)
168         UASSERTEQ(unsigned long, result.size() % 5, 0);
169 }