Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <functional>
8#include <exception>
9#include <sstream>
10#include <vector>
11
13#include "porting.h"
14#include "filesys.h"
15#include "mapnode.h"
16
17class TestFailedException { // don’t derive from std::exception to avoid accidental catch
18public:
19 TestFailedException(std::string in_message, const char *in_file, int in_line)
20 : message(std::move(in_message))
21 , file(fs::GetFilenameFromPath(in_file))
22 , line(in_line)
23 {}
24
25 const std::string message;
26 const std::string file;
27 const int line;
28};
29
30// Runs a unit test and reports results
31#define TEST(fxn, ...) runTest(#fxn, [&] () { fxn(__VA_ARGS__); });
32
33// Asserts the specified condition is true, or fails the current unit test
34#define UASSERT(x) \
35 if (!(x)) { \
36 throw TestFailedException(#x, __FILE__, __LINE__); \
37 }
38
39// Asserts the specified condition is true, or fails the current unit test
40// and prints the format specifier fmt
41#define UTEST(x, fmt, ...) \
42 if (!(x)) { \
43 char utest_buf[1024]; \
44 snprintf(utest_buf, sizeof(utest_buf), fmt, __VA_ARGS__); \
45 throw TestFailedException(utest_buf, __FILE__, __LINE__); \
46 }
47
48// Asserts the comparison specified by CMP is true, or fails the current unit test
49#define UASSERTCMP(T, CMP, actual, expected) { \
50 T a = (actual); \
51 T e = (expected); \
52 if (!(a CMP e)) { \
53 std::ostringstream message; \
54 message << #actual " " #CMP " " #expected; \
55 message << std::endl << " actual : " << a; \
56 message << std::endl << " expected: " << e; \
57 throw TestFailedException(message.str(), __FILE__, __LINE__); \
58 } \
59}
60
61#define UASSERTEQ(T, actual, expected) UASSERTCMP(T, ==, actual, expected)
62
63// UASSERTs that the specified exception occurs
64#define EXCEPTION_CHECK(EType, code) { \
65 bool exception_thrown = false; \
66 try { \
67 code; \
68 } catch (EType &e) { \
69 exception_thrown = true; \
70 } \
71 UASSERT(exception_thrown); \
72}
73
74class IGameDef;
75
76class TestBase {
77public:
78 bool testModule(IGameDef *gamedef);
79 std::string getTestTempDirectory();
80 std::string getTestTempFile();
81
82 virtual void runTests(IGameDef *gamedef) = 0;
83 virtual const char *getName() = 0;
84
87
88 void runTest(const char *name, std::function<void()> &&test);
89
90private:
91 std::string m_test_dir;
92};
93
95public:
96 static std::vector<TestBase *> &getTestModules()
97 {
98 static std::vector<TestBase *> m_modules_to_test;
99 return m_modules_to_test;
100 }
101
102 static void registerTestModule(TestBase *module)
103 {
104 getTestModules().push_back(module);
105 }
106};
107
108// A few item and node definitions for those tests that need them
115
116bool run_tests();
117bool run_tests(const std::string &module_name);
Definition gamedef.h:36
Definition test.h:76
u32 num_tests_run
Definition test.h:86
std::string m_test_dir
Definition test.h:91
std::string getTestTempDirectory()
Definition test.cpp:333
bool testModule(IGameDef *gamedef)
Definition test.cpp:313
virtual const char * getName()=0
std::string getTestTempFile()
Definition test.cpp:343
virtual void runTests(IGameDef *gamedef)=0
u32 num_tests_failed
Definition test.h:85
void runTest(const char *name, std::function< void()> &&test)
Definition test.cpp:351
Definition test.h:17
const std::string message
Definition test.h:25
TestFailedException(std::string in_message, const char *in_file, int in_line)
Definition test.h:19
const std::string file
Definition test.h:26
const int line
Definition test.h:27
Definition test.h:94
static std::vector< TestBase * > & getTestModules()
Definition test.h:96
static void registerTestModule(TestBase *module)
Definition test.h:102
u16 content_t
Definition mapnode.h:22
Definition filesys.cpp:53
content_t t_CONTENT_TORCH
Definition test.cpp:24
content_t t_CONTENT_STONE
Definition test.cpp:22
content_t t_CONTENT_BRICK
Definition test.cpp:27
content_t t_CONTENT_WATER
Definition test.cpp:25
content_t t_CONTENT_LAVA
Definition test.cpp:26
bool run_tests()
Definition test.cpp:210
content_t t_CONTENT_GRASS
Definition test.cpp:23