nx_server_plugin_sdk 1.0 Server Plugin SDK

test.h
Go to the documentation of this file.
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
9 #include <functional>
10 #include <iostream>
11 #include <sstream>
12 #include <stdint.h>
13 #include <string>
14
15 #include <nx/kit/utils.h>
16
17 #if !defined(NX_KIT_API)
18 #define NX_KIT_API
19 #endif
20
21 namespace nx {
22 namespace kit {
23 namespace test {
24
25 extern bool NX_KIT_API verbose; //< Use to control additional output of the unit test framework.
26
41 #define TEST(TEST_CASE, TEST_NAME) ENABLED_TEST(TEST_CASE, TEST_NAME)
42
43 #define ENABLED_TEST(TEST_CASE, TEST_NAME) \
44 static void test_##TEST_CASE##_##TEST_NAME(); \
45 int unused_##TEST_CASE##_##TEST_NAME /* Not `static const` to suppress "unused" warning. */ = \
46 ::nx::kit::test::detail::regTest( \
47 {#TEST_CASE, #TEST_NAME, #TEST_CASE "." #TEST_NAME, test_##TEST_CASE##_##TEST_NAME, \
48 /*tempDir*/ ""}); \
49 static void test_##TEST_CASE##_##TEST_NAME()
50 // Function body follows the DEFINE_TEST macro.
51
52 #define DISABLED_TEST(TEST_CASE, TEST_NAME) \
53 static void disabled_test_##TEST_CASE##_##TEST_NAME() /* The function will be unused. */
54 // Function body follows the DISABLED_TEST macro.
55
56 #define ASSERT_TRUE(CONDITION) \
57 ::nx::kit::test::detail::assertBool(true, !!(CONDITION), #CONDITION, __FILE__, __LINE__)
58
59 #define ASSERT_TRUE_AT_LINE(LINE, CONDITION) \
60 ::nx::kit::test::detail::assertBool(true, !!(CONDITION), #CONDITION, __FILE__, LINE, __LINE__)
61
62 #define ASSERT_FALSE(CONDITION) \
63 ::nx::kit::test::detail::assertBool(false, !!(CONDITION), #CONDITION, __FILE__, __LINE__)
64
65 #define ASSERT_FALSE_AT_LINE(LINE, CONDITION) \
66 ::nx::kit::test::detail::assertBool(false, !!(CONDITION), #CONDITION, __FILE__, LINE, __LINE__)
67
68 #define ASSERT_EQ(EXPECTED, ACTUAL) \
69 ::nx::kit::test::detail::assertEq( \
70 (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, __LINE__)
71
72 #define ASSERT_EQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
73 ::nx::kit::test::detail::assertEq( \
74 (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, LINE, __LINE__)
75
76 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
77 ::nx::kit::test::detail::assertStrEq( \
78 EXPECTED, #EXPECTED, ACTUAL, #ACTUAL, __FILE__, __LINE__)
79
80 #define ASSERT_STREQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
81 ::nx::kit::test::detail::assertStrEq( \
82 EXPECTED, #EXPECTED, ACTUAL, #ACTUAL, __FILE__, LINE, __LINE__)
83
98 NX_KIT_API void assertMultilineTextEquals(
99 const char* file, int line, const std::string& testCaseTag,
100 const std::string& expected, const std::string& actual,
101 const std::string actualSubstrToReplace = "", const std::string& actualSubstrReplacement = "");
102
110 NX_KIT_API const char* tempDir();
111
119 NX_KIT_API const char* staticTempDir();
120
133 NX_KIT_API int runAllTests(const char *testSuiteName, const char* specificArgsHelp = nullptr);
134
136 NX_KIT_API void createFile(const std::string& filename, const std::string& content);
137
138 //-------------------------------------------------------------------------------------------------
139 // Implementation
140
141 namespace detail {
142
143 #if defined(NX_KIT_TEST_KEEP_TEMP_FILES)
144 static const nx::kit::test::TempFile::KeepFilesInitializer tempFileKeepFilesInitializer;
145 #endif
146
147 typedef std::function<void()> TestFunc;
148
149 struct Test
150 {
151 const char* const testCase;
152 const char* const testName;
153 const char* const testCaseDotName;
154 const TestFunc testFunc;
155 std::string tempDir;
156 };
157
158 NX_KIT_API int regTest(const Test& test);
159
160 NX_KIT_API void failEq(
161 const std::string& expectedValue, const char* expectedExpr,
162 const std::string& actualValue, const char* actualExpr,
163 const char* file, int line, int actualLine = -1);
164
165 NX_KIT_API void assertBool(
166 bool expected, bool condition, const char* conditionStr,
167 const char* file, int line, int actualLine = -1);
168
169 template<typename Expected, typename Actual>
170 void assertEq(
171 const Expected& expected, const char* expectedExpr,
172 const Actual& actual, const char* actualExpr,
173 const char* file, int line, int actualLine = -1)
174 {
175 if (!(expected == actual)) //< Require only operator==().
176 {
177 detail::failEq(
178 nx::kit::utils::toString(expected), expectedExpr,
179 nx::kit::utils::toString(actual), actualExpr,
180 file, line, actualLine);
181 }
182 }
183
188 struct UniversalString
189 {
190 const std::string s;
191 const bool isNull;
192
193 UniversalString(const UniversalString& u, std::false_type): s(u.s), isNull(u.isNull) {}
194
196 template<size_t len>
197 UniversalString(const char (&a)[len], std::true_type): s(a, len - 1), isNull(false) {}
198
200 template<size_t len>
201 UniversalString(char (&a)[len], std::true_type): s(a, len - 1), isNull(false) {}
202
203 UniversalString(const char* p, std::false_type): s(p ? p : ""), isNull(!p) {}
204 UniversalString(std::string s, std::false_type): s(std::move(s)), isNull(false) {}
205
207 template<typename T>
208 UniversalString(T&& arg): UniversalString(
209 std::forward<T>(arg), std::is_array<typename std::remove_reference<T>::type>())
210 {
211 }
212
213 bool operator==(const UniversalString& u) const { return isNull == u.isNull && s == u.s; }
214 bool operator!=(const UniversalString& u) const { return !(*this == u); }
215
216 std::string toString() const
217 {
218 if (isNull)
219 return "null";
220 return nx::kit::utils::toString(s);
221 }
222 };
223
224 inline std::ostream& operator<<(std::ostream& stream, const UniversalString& str)
225 {
226 return stream << str.toString();
227 }
228
229 NX_KIT_API void assertStrEq(
230 const UniversalString& expectedValue, const char* expectedExpr,
231 const UniversalString& actualValue, const char* actualExpr,
232 const char* file, int line, int actualLine = -1);
233
234 } // namespace detail
235
236 } // namespace test
237 } // namespace kit
238 } // namespace nx
nx::kit::test::detail::UniversalString::UniversalString
UniversalString(char(&a)[len], std::true_type)
Definition: test.h:201
test
Definition: json_ut.cpp:14
utils.h
std
Definition: to_string.h:49
nx::kit::test::detail::UniversalString::UniversalString
UniversalString(const char(&a)[len], std::true_type)
Definition: test.h:197
nx::kit::test::detail::UniversalString
Definition: test.h:188
nx::kit::test::detail::UniversalString::UniversalString
UniversalString(T &&arg)
Definition: test.h:208
nx
Definition: apple_utils.h:6
nx::kit::test::detail::Test
Definition: test.h:149