nx_metadata_sdk 1.0 Metadata 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 <string>
13
14 #include <nx/kit/utils.h>
15
16 #if !defined(NX_KIT_API)
17 #define NX_KIT_API
18 #endif
19
20 namespace nx {
21 namespace kit {
22 namespace test {
23
24 extern bool NX_KIT_API verbose; //< Use to control additional output of the unit test framework.
25
40 #define TEST(TEST_CASE, TEST_NAME) ENABLED_TEST(TEST_CASE, TEST_NAME)
41
42 #define ENABLED_TEST(TEST_CASE, TEST_NAME) \
43 static void test_##TEST_CASE##_##TEST_NAME(); \
44 int unused_##TEST_CASE##_##TEST_NAME /* Not `static const` to suppress "unused" warning. */ = \
45 ::nx::kit::test::detail::regTest( \
46 {#TEST_CASE, #TEST_NAME, #TEST_CASE "." #TEST_NAME, test_##TEST_CASE##_##TEST_NAME, \
47 /*tempDir*/ ""}); \
48 static void test_##TEST_CASE##_##TEST_NAME()
49 // Function body follows the DEFINE_TEST macro.
50
51 #define DISABLED_TEST(TEST_CASE, TEST_NAME) \
52 static void disabled_test_##TEST_CASE##_##TEST_NAME() /* The function will be unused. */
53 // Function body follows the DISABLED_TEST macro.
54
55 #define ASSERT_TRUE(CONDITION) \
56 ::nx::kit::test::detail::assertBool(true, !!(CONDITION), #CONDITION, __FILE__, __LINE__)
57
58 #define ASSERT_TRUE_AT_LINE(LINE, CONDITION) \
59 ::nx::kit::test::detail::assertBool(true, !!(CONDITION), #CONDITION, __FILE__, LINE, __LINE__)
60
61 #define ASSERT_FALSE(CONDITION) \
62 ::nx::kit::test::detail::assertBool(false, !!(CONDITION), #CONDITION, __FILE__, __LINE__)
63
64 #define ASSERT_FALSE_AT_LINE(LINE, CONDITION) \
65 ::nx::kit::test::detail::assertBool(false, !!(CONDITION), #CONDITION, __FILE__, LINE, __LINE__)
66
67 #define ASSERT_EQ(EXPECTED, ACTUAL) \
68 ::nx::kit::test::detail::assertEq( \
69 (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, __LINE__)
70
71 #define ASSERT_EQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
72 ::nx::kit::test::detail::assertEq( \
73 (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, LINE, __LINE__)
74
75 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
76 ::nx::kit::test::detail::assertStrEq( \
77 ::nx::kit::test::detail::toCStr(EXPECTED), #EXPECTED, \
78 ::nx::kit::test::detail::toCStr(ACTUAL), #ACTUAL, __FILE__, __LINE__)
79
80 #define ASSERT_STREQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
81 ::nx::kit::test::detail::assertStrEq( \
82 ::nx::kit::test::detail::toCStr(EXPECTED), #EXPECTED, \
83 ::nx::kit::test::detail::toCStr(ACTUAL), #ACTUAL, __FILE__, LINE, __LINE__)
84
92 NX_KIT_API const char* tempDir();
93
101 NX_KIT_API const char* staticTempDir();
102
115 NX_KIT_API int runAllTests(const char *testSuiteName);
116
117 NX_KIT_API void createFile(const char* filename, const char* content);
118
119 //-------------------------------------------------------------------------------------------------
120 // Implementation
121
122 #if defined(NX_KIT_TEST_KEEP_TEMP_FILES)
123 namespace
124 {
125 static const nx::kit::test::TempFile::KeepFilesInitializer tempFileKeepFilesInitializer;
126 }
127 #endif
128
129 namespace detail {
130
131 NX_KIT_API void failEq(
132 const char* expectedValue, const char* expectedExpr,
133 const char* actualValue, const char* actualExpr,
134 const char* file, int line, int actualLine = -1);
135
136 typedef std::function<void()> TestFunc;
137
138 struct Test
139 {
140 const char* const testCase;
141 const char* const testName;
142 const char* const testCaseDotName;
143 const TestFunc testFunc;
144 std::string tempDir;
145 };
146
147 NX_KIT_API int regTest(const Test& test);
148
149 NX_KIT_API void assertBool(
150 bool expected, bool condition, const char* conditionStr,
151 const char* file, int line, int actualLine = -1);
152
153 template<typename Expected, typename Actual>
154 void assertEq(
155 const Expected& expected, const char* expectedExpr,
156 const Actual& actual, const char* actualExpr,
157 const char* file, int line, int actualLine = -1)
158 {
159 if (!(expected == actual)) //< Require only operator==().
160 {
161 detail::failEq(
162 utils::toString(expected).c_str(), expectedExpr,
163 utils::toString(actual).c_str(), actualExpr,
164 file, line, actualLine);
165 }
166 }
167
168 NX_KIT_API void assertStrEq(
169 const char* expectedValue, const char* expectedExpr,
170 const char* actualValue, const char* actualExpr,
171 const char* file, int line, int actualLine = -1);
172
173 inline const char* toCStr(const std::string& s)
174 {
175 return s.c_str();
176 }
177
178 inline const char* toCStr(const char* s)
179 {
180 return s;
181 }
182
183 } // namespace detail
184
185 } // namespace test
186 } // namespace kit
187 } // namespace nx
utils.h
nx
Definition: apple_utils.h:6
nx::kit::test::detail::Test
Definition: test.h:138