Conscience Core
unit-test.h
Go to the documentation of this file.
1 #ifndef UNIT_TEST_UTILS
2 #define UNIT_TEST_UTILS
3 
4 #ifndef TEST_RESOURCES_DIR
5 #define TEST_RESOURCES_DIR "../TestResources"
6 #define TEST_TMP_DIR TEST_RESOURCES_DIR "/tmp"
7 #endif
8 
9 #include "CscCommon.h"
10 
11 #include <boost/test/execution_monitor.hpp>
12 #include <boost/test/framework.hpp>
13 #include <boost/test/results_collector.hpp>
14 #include <boost/test/tree/observer.hpp>
15 #include <boost/test/unit_test.hpp>
16 #include "./fakeit.hpp"
17 
18 #define CSCTEST_HAS_ARGS(...) CSCTEST_HAS_ARGS_IMPL(__VA_ARGS__, 1, 0)
19 #define CSCTEST_HAS_ARGS_IMPL(_1, _2, N, ...) N
20 #define CSCTEST_ASSERT(expr, ...) \
21  if (CSCTEST_HAS_ARGS(__VA_ARGS__)) { \
22  if (!(expr)) { \
23  LOG_ERROR("assert failed - 🧾 stacktrace = " + getStackTrace()); \
24  } \
25  } \
26  BOOST_TEST(expr __VA_OPT__(, ) __VA_ARGS__);
27 
28 using std::string, std::function, std::vector, std::to_string, boost::unit_test::assertion_result, boost::test_tools::tt_detail::tolerance_manip_delay, boost::test_tools::tolerance;
29 using namespace conscience_utils;
30 using namespace fakeit;
31 using namespace boost::unit_test::framework;
32 
33 namespace fs = std::filesystem;
34 
35 class ConscienceTestObserver : public boost::unit_test::test_observer {
36 public:
38  void exception_caught(boost::execution_exception const &error) override;
39  void assertion_result(enum assertion_result result) override;
40  bool wasAnExceptionCaught();
41  void resetExceptionTracking();
42  void test_finish() override;
43 
44 private:
45  int errorCount = 0;
46  bool exceptionCaught;
47  std::unique_ptr<CscLogger> logger;
48 };
49 
50 class FixtureBase {
51 public:
53  unique_ptr<CscLogger> logger;
54  FixtureBase();
55  virtual ~FixtureBase();
56 };
57 
60 public:
62  virtual ~FixtureIntegrationTestBase();
63 
67  CscEnvironmentSimulator *createEnvironmentSimulator(bool loadDefaultFloor = true);
68 
69 private:
70  vector<CscEnvironmentSimulator *> createdEnvironmentSimulators;
71 };
72 
73 string getCurrentTestName();
74 fs::path getTestTempPath(const fs::path &relativePath);
75 
76 template <class MockedType>
77 MockedType *mockToPointer(Mock<MockedType> &mock) {
78  Fake(Dtor(mock));
79 
80  MockedType &instanceReference = mock.get();
81  std::shared_ptr<MockedType> sharedPtr(&instanceReference);
82  MockedType *pointer = sharedPtr.get();
83  return pointer;
84 }
85 
86 template <class MockedType>
87 ptr<MockedType> mockToSharedPointer(Mock<MockedType> &mock) {
88  Fake(Dtor(mock));
89 
90  MockedType &instanceReference = mock.get();
91  ptr<MockedType> sharedPtr(&instanceReference);
92 
93  return sharedPtr;
94 }
95 
96 template <class T>
97 void assertVectorsEqual(const vector<T> &left, const vector<T> &right) {
98  CSCTEST_ASSERT(left.size() == right.size());
99  for (int i = 0; i < left.size(); i++) {
100  if constexpr (std::is_same<T, double>::value) {
101  CSCTEST_ASSERT(left[i] == right[i], boost::test_tools::tolerance(0.00001));
102  } else {
103  CSCTEST_ASSERT(left[i] == right[i]);
104  }
105  }
106 }
107 template <class T>
108 void assertTrue(const T &boolValue) {
109  CSCTEST_ASSERT(bool(boolValue));
110 }
111 template <class T>
112 void assertFalse(const T &boolValue) {
113  CSCTEST_ASSERT(!bool(boolValue));
114 }
115 
116 template <typename T>
117 concept IsStringLike = std::is_same_v<T, const string> || std::is_same_v<T, string> || std::is_same_v<T, char *> || std::is_same_v<T, const char *>;
118 template <IsStringLike T1, IsStringLike T2>
119 void assertEquals(T1 left, T2 right) {
120  CSCTEST_ASSERT(string(left) == string(right));
121 }
122 
123 template <class T1, class T2>
124  requires(!IsStringLike<T1>)
125 void assertEquals(T1 left, T2 right, optional<double> decimalTolerance = {}) {
126  if (decimalTolerance.has_value()) {
127  CSCTEST_ASSERT(left == right, tolerance(decimalTolerance.value()));
128  } else {
129  CSCTEST_ASSERT(left == right);
130  }
131 }
132 template <class T1, class T2>
133 void assertNotEquals(T1 left, T2 right) {
134  CSCTEST_ASSERT(left != right);
135 }
136 template <class T>
137 void assertNotNull(T *object) {
138  CSCTEST_ASSERT(object != nullptr);
139 }
140 template <class T>
142  CSCTEST_ASSERT(object != nullptr);
143 }
144 template <class T>
145 void assertNotNull(ptr<T> object) {
146  CSCTEST_ASSERT(object != nullptr);
147 }
148 
149 template <class TException>
150 void assertThrows(function<void()> action) {
151  bool thrown = false;
152  try {
153  action();
154  } catch (const TException &e) {
155  thrown = true;
156  }
157  assertTrue(thrown);
158 }
159 
160 void assertFileContent(const fs::path &path, const string &expectedContent);
161 
162 #endif
IsStringLike
concept IsStringLike
Definition: unit-test.h:117
nlohmann::to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:26470
assertNotNull
void assertNotNull(T *object)
Definition: unit-test.h:137
CSCTEST_ASSERT
#define CSCTEST_ASSERT(expr,...)
Definition: unit-test.h:20
FixtureBase::logger
unique_ptr< CscLogger > logger
Definition: unit-test.h:53
mockToSharedPointer
ptr< MockedType > mockToSharedPointer(Mock< MockedType > &mock)
Definition: unit-test.h:87
decimalTolerance
T2 optional< double > decimalTolerance
Definition: unit-test.h:125
getCurrentTestName
string getCurrentTestName()
Definition: unit-test.cpp:60
right
T2 right
Definition: unit-test.h:125
FixtureIntegrationTestBase
Definition: unit-test.h:59
assertNotEquals
T2 optional< double > class T2 void assertNotEquals(T1 left, T2 right)
Definition: unit-test.h:133
FixtureBase::observer
static ConscienceTestObserver * observer
Definition: unit-test.h:52
ConscienceTestObserver
Definition: unit-test.h:35
mockToPointer
MockedType * mockToPointer(Mock< MockedType > &mock)
Definition: unit-test.h:77
assertThrows
void assertThrows(function< void()> action)
Definition: unit-test.h:150
logger
static std::unique_ptr< CscLogger > logger
Definition: gltfHelpers.cpp:6
getTestTempPath
fs::path getTestTempPath(const fs::path &relativePath)
Definition: unit-test.cpp:63
nlohmann::detail::void
j template void())
Definition: json.hpp:4189
CscCommon.h
FixtureBase
Definition: unit-test.h:50
assertTrue
void assertTrue(const T &boolValue)
Definition: unit-test.h:108
conscience_utils
Definition: CscEntityReflexion.h:50
assertVectorsEqual
void assertVectorsEqual(const vector< T > &left, const vector< T > &right)
Definition: unit-test.h:97
CscEnvironmentSimulator
Definition: CscEnvironmentSimulator.h:31
requires
requires(!IsStringLike< T1 >) void assertEquals(T1 left
assertEquals
void assertEquals(T1 left, T2 right)
Definition: unit-test.h:119
assertFileContent
void assertFileContent(const fs::path &path, const string &expectedContent)
Definition: unit-test.cpp:95
ptr
std::shared_ptr< T > ptr
Definition: CscCommon.h:29
assertFalse
void assertFalse(const T &boolValue)
Definition: unit-test.h:112
i
int i
Definition: HybridAStar.cpp:191