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 <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);
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 
184 // The overloads below are needed to support null `char*`, as well as std::string with '\0' inside.
185 
186 NX_KIT_API void assertStrEq(
187  const std::string& expectedValue, const char* expectedExpr,
188  const std::string& actualValue, const char* actualExpr,
189  const char* file, int line, int actualLine = -1);
190 
191 NX_KIT_API void assertStrEq(
192  const char* expectedValue, const char* expectedExpr,
193  const char* actualValue, const char* actualExpr,
194  const char* file, int line, int actualLine = -1);
195 
196 NX_KIT_API void assertStrEq(
197  const char* expectedValue, const char* expectedExpr,
198  const std::string& actualValue, const char* actualExpr,
199  const char* file, int line, int actualLine = -1);
200 
201 NX_KIT_API void assertStrEq(
202  const std::string& expectedValue, const char* expectedExpr,
203  const char* actualValue, const char* actualExpr,
204  const char* file, int line, int actualLine = -1);
205 
206 } // namespace detail
207 
208 } // namespace test
209 } // namespace kit
210 } // namespace nx
Definition: json_ut.cpp:14
Definition: apple_utils.h:6
Definition: test.h:149