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