nx_video_source_sdk  1.0
Video Source 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  EXPECTED, #EXPECTED, ACTUAL, #ACTUAL, __FILE__, __LINE__)
78 
79 #define ASSERT_STREQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
80  ::nx::kit::test::detail::assertStrEq( \
81  EXPECTED, #EXPECTED, ACTUAL, #ACTUAL, __FILE__, LINE, __LINE__)
82 
97 NX_KIT_API void assertMultilineTextEquals(
98  const char* file, int line, const std::string& testCaseTag,
99  const std::string& expected, const std::string& actual,
100  const std::string actualSubstrToReplace = "", const std::string& actualSubstrReplacement = "");
101 
109 NX_KIT_API const char* tempDir();
110 
118 NX_KIT_API const char* staticTempDir();
119 
132 NX_KIT_API int runAllTests(const char *testSuiteName);
133 
135 NX_KIT_API void createFile(const std::string& filename, const std::string& content);
136 
137 //-------------------------------------------------------------------------------------------------
138 // Implementation
139 
140 namespace detail {
141 
142 #if defined(NX_KIT_TEST_KEEP_TEMP_FILES)
143  static const nx::kit::test::TempFile::KeepFilesInitializer tempFileKeepFilesInitializer;
144 #endif
145 
146 typedef std::function<void()> TestFunc;
147 
148 struct Test
149 {
150  const char* const testCase;
151  const char* const testName;
152  const char* const testCaseDotName;
153  const TestFunc testFunc;
154  std::string tempDir;
155 };
156 
157 NX_KIT_API int regTest(const Test& test);
158 
159 NX_KIT_API void failEq(
160  const std::string& expectedValue, const char* expectedExpr,
161  const std::string& actualValue, const char* actualExpr,
162  const char* file, int line, int actualLine = -1);
163 
164 NX_KIT_API void assertBool(
165  bool expected, bool condition, const char* conditionStr,
166  const char* file, int line, int actualLine = -1);
167 
168 template<typename Expected, typename Actual>
169 void assertEq(
170  const Expected& expected, const char* expectedExpr,
171  const Actual& actual, const char* actualExpr,
172  const char* file, int line, int actualLine = -1)
173 {
174  if (!(expected == actual)) //< Require only operator==().
175  {
176  detail::failEq(
177  nx::kit::utils::toString(expected), expectedExpr,
178  nx::kit::utils::toString(actual), actualExpr,
179  file, line, actualLine);
180  }
181 }
182 
183 // The overloads below are needed to support null `char*`, as well as std::string with '\0' inside.
184 
185 NX_KIT_API void assertStrEq(
186  const std::string& expectedValue, const char* expectedExpr,
187  const std::string& actualValue, const char* actualExpr,
188  const char* file, int line, int actualLine = -1);
189 
190 NX_KIT_API void assertStrEq(
191  const char* expectedValue, const char* expectedExpr,
192  const char* actualValue, const char* actualExpr,
193  const char* file, int line, int actualLine = -1);
194 
195 NX_KIT_API void assertStrEq(
196  const char* expectedValue, const char* expectedExpr,
197  const std::string& actualValue, const char* actualExpr,
198  const char* file, int line, int actualLine = -1);
199 
200 NX_KIT_API void assertStrEq(
201  const std::string& expectedValue, const char* expectedExpr,
202  const char* actualValue, const char* actualExpr,
203  const char* file, int line, int actualLine = -1);
204 
205 } // namespace detail
206 
207 } // namespace test
208 } // namespace kit
209 } // namespace nx
Definition: json_ut.cpp:14
Definition: apple_utils.h:6
Definition: test.h:148