Minetest  5.4.0
test.h
Go to the documentation of this file.
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 #pragma once
21 
22 #include <exception>
23 #include <vector>
24 
26 #include "porting.h"
27 #include "filesys.h"
28 #include "mapnode.h"
29 
30 class TestFailedException : public std::exception {
31 };
32 
33 // Runs a unit test and reports results
34 #define TEST(fxn, ...) { \
35  u64 t1 = porting::getTimeMs(); \
36  try { \
37  fxn(__VA_ARGS__); \
38  rawstream << "[PASS] "; \
39  } catch (TestFailedException &e) { \
40  rawstream << "[FAIL] "; \
41  num_tests_failed++; \
42  } catch (std::exception &e) { \
43  rawstream << "Caught unhandled exception: " << e.what() << std::endl; \
44  rawstream << "[FAIL] "; \
45  num_tests_failed++; \
46  } \
47  num_tests_run++; \
48  u64 tdiff = porting::getTimeMs() - t1; \
49  rawstream << #fxn << " - " << tdiff << "ms" << std::endl; \
50 }
51 
52 // Asserts the specified condition is true, or fails the current unit test
53 #define UASSERT(x) \
54  if (!(x)) { \
55  rawstream << "Test assertion failed: " #x << std::endl \
56  << " at " << fs::GetFilenameFromPath(__FILE__) \
57  << ":" << __LINE__ << std::endl; \
58  throw TestFailedException(); \
59  }
60 
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, ...) \
64  if (!(x)) { \
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(); \
71  }
72 
73 // Asserts the comparison specified by CMP is true, or fails the current unit test
74 #define UASSERTCMP(T, CMP, actual, expected) { \
75  T a = (actual); \
76  T e = (expected); \
77  if (!(a CMP e)) { \
78  rawstream \
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: " \
84  << e << std::endl; \
85  throw TestFailedException(); \
86  } \
87 }
88 
89 #define UASSERTEQ(T, actual, expected) UASSERTCMP(T, ==, actual, expected)
90 
91 // UASSERTs that the specified exception occurs
92 #define EXCEPTION_CHECK(EType, code) { \
93  bool exception_thrown = false; \
94  try { \
95  code; \
96  } catch (EType &e) { \
97  exception_thrown = true; \
98  } \
99  UASSERT(exception_thrown); \
100 }
101 
102 class IGameDef;
103 
104 class TestBase {
105 public:
106  bool testModule(IGameDef *gamedef);
107  std::string getTestTempDirectory();
108  std::string getTestTempFile();
109 
110  virtual void runTests(IGameDef *gamedef) = 0;
111  virtual const char *getName() = 0;
112 
115 
116 private:
117  std::string m_test_dir;
118 };
119 
120 class TestManager {
121 public:
122  static std::vector<TestBase *> &getTestModules()
123  {
124  static std::vector<TestBase *> m_modules_to_test;
125  return m_modules_to_test;
126  }
127 
128  static void registerTestModule(TestBase *module)
129  {
130  getTestModules().push_back(module);
131  }
132 };
133 
134 // A few item and node definitions for those tests that need them
141 
142 bool run_tests();
Definition: gamedef.h:49
Definition: test.h:104
u32 num_tests_run
Definition: test.h:114
std::string m_test_dir
Definition: test.h:117
std::string getTestTempDirectory()
Definition: test.cpp:321
bool testModule(IGameDef *gamedef)
Definition: test.cpp:301
virtual const char * getName()=0
std::string getTestTempFile()
Definition: test.cpp:336
virtual void runTests(IGameDef *gamedef)=0
u32 num_tests_failed
Definition: test.h:113
Definition: test.h:30
Definition: test.h:120
static void registerTestModule(TestBase *module)
Definition: test.h:128
static std::vector< TestBase * > & getTestModules()
Definition: test.h:122
u16 content_t
Definition: mapnode.h:28
content_t t_CONTENT_TORCH
Definition: test.cpp:33
content_t t_CONTENT_STONE
Definition: test.cpp:31
content_t t_CONTENT_BRICK
Definition: test.cpp:36
content_t t_CONTENT_WATER
Definition: test.cpp:34
content_t t_CONTENT_LAVA
Definition: test.cpp:35
bool run_tests()
Definition: test.cpp:258
content_t t_CONTENT_GRASS
Definition: test.cpp:32