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  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 
188 {
189  const std::string s;
190  const bool isNull;
191 
192  UniversalString(const UniversalString& u, std::false_type): s(u.s), isNull(u.isNull) {}
193 
195  template<size_t len>
196  UniversalString(const char (&a)[len], std::true_type): s(a, len - 1), isNull(false) {}
197 
199  template<size_t len>
200  UniversalString(char (&a)[len], std::true_type): s(a, len - 1), isNull(false) {}
201 
202  UniversalString(const char* p, std::false_type): s(p ? p : ""), isNull(!p) {}
203  UniversalString(std::string s, std::false_type): s(std::move(s)), isNull(false) {}
204 
206  template<typename T>
208  std::forward<T>(arg), std::is_array<typename std::remove_reference<T>::type>())
209  {
210  }
211 
212  bool operator==(const UniversalString& u) const { return isNull == u.isNull && s == u.s; }
213  bool operator!=(const UniversalString& u) const { return !(*this == u); }
214 
215  std::string toString() const
216  {
217  if (isNull)
218  return "null";
219  return nx::kit::utils::toString(s);
220  }
221 };
222 
223 inline std::ostream& operator<<(std::ostream& stream, const UniversalString& str)
224 {
225  return stream << str.toString();
226 }
227 
228 NX_KIT_API void assertStrEq(
229  const UniversalString& expectedValue, const char* expectedExpr,
230  const UniversalString& actualValue, const char* actualExpr,
231  const char* file, int line, int actualLine = -1);
232 
233 } // namespace detail
234 
235 } // namespace test
236 } // namespace kit
237 } // namespace nx
UniversalString(char(&a)[len], std::true_type)
Definition: test.h:200
Definition: json_ut.cpp:14
Definition: json.h:39
UniversalString(const char(&a)[len], std::true_type)
Definition: test.h:196
UniversalString(T &&arg)
Definition: test.h:207
Definition: apple_utils.h:6
Definition: test.h:148