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 "irrlichttypes_extrabloated.h"
30 class TestFailedException : public std::exception {
33 // Runs a unit test and reports results
34 #define TEST(fxn, ...) { \
35 u64 t1 = porting::getTimeMs(); \
38 rawstream << "[PASS] "; \
39 } catch (TestFailedException &e) { \
40 rawstream << "[FAIL] "; \
42 } catch (std::exception &e) { \
43 rawstream << "Caught unhandled exception: " << e.what() << std::endl; \
44 rawstream << "[FAIL] "; \
48 u64 tdiff = porting::getTimeMs() - t1; \
49 rawstream << #fxn << " - " << tdiff << "ms" << std::endl; \
52 // Asserts the specified condition is true, or fails the current unit test
55 rawstream << "Test assertion failed: " #x << std::endl \
56 << " at " << fs::GetFilenameFromPath(__FILE__) \
57 << ":" << __LINE__ << std::endl; \
58 throw TestFailedException(); \
61 // Asserts the specified condition is true, or fails the current unit test
62 // and prints the format specifier fmt
63 #define UTEST(x, fmt, ...) \
65 char utest_buf[1024]; \
66 snprintf(utest_buf, sizeof(utest_buf), fmt, __VA_ARGS__); \
67 rawstream << "Test assertion failed: " << utest_buf << std::endl \
68 << " at " << fs::GetFilenameFromPath(__FILE__) \
69 << ":" << __LINE__ << std::endl; \
70 throw TestFailedException(); \
73 // Asserts the comparison specified by CMP is true, or fails the current unit test
74 #define UASSERTCMP(T, CMP, actual, expected) { \
79 << "Test assertion failed: " << #actual << " " << #CMP << " " \
80 << #expected << std::endl \
81 << " at " << fs::GetFilenameFromPath(__FILE__) << ":" \
82 << __LINE__ << std::endl \
83 << " actual: " << a << std::endl << " expected: " \
85 throw TestFailedException(); \
89 #define UASSERTEQ(T, actual, expected) UASSERTCMP(T, ==, actual, expected)
91 // UASSERTs that the specified exception occurs
92 #define EXCEPTION_CHECK(EType, code) { \
93 bool exception_thrown = false; \
96 } catch (EType &e) { \
97 exception_thrown = true; \
99 UASSERT(exception_thrown); \
106 bool testModule(IGameDef *gamedef);
107 std::string getTestTempDirectory();
108 std::string getTestTempFile();
110 virtual void runTests(IGameDef *gamedef) = 0;
111 virtual const char *getName() = 0;
113 u32 num_tests_failed;
117 std::string m_test_dir;
122 static std::vector<TestBase *> &getTestModules()
124 static std::vector<TestBase *> m_modules_to_test;
125 return m_modules_to_test;
128 static void registerTestModule(TestBase *module)
130 getTestModules().push_back(module);
134 // A few item and node definitions for those tests that need them
135 extern content_t t_CONTENT_STONE;
136 extern content_t t_CONTENT_GRASS;
137 extern content_t t_CONTENT_TORCH;
138 extern content_t t_CONTENT_WATER;
139 extern content_t t_CONTENT_LAVA;
140 extern content_t t_CONTENT_BRICK;