BeTest.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 #include "Bentley.h"
11 #include "bvector.h"
12 #include "bset.h"
13 #include "BeFile.h"
14 #include "WString.h"
15 #include "BeFileName.h"
16 #include "BeFileListIterator.h"
17 #include "BeStringUtilities.h"
18 #include "BeAssert.h"
19 #include <Bentley/bvector.h>
20 
21 #ifdef BETEST_GENERATE_UNITTESTS_LIST_H
22  #undef USE_GTEST
23 #endif
24  #if !defined (GTEST_INCLUDE_GTEST_GTEST_H_)
25  #include <math.h>
26 
27  namespace testing
28  {
29  class Test
30  {
31  bool m_anyError;
32  void RunTest();
33  protected:
34  virtual void SetUp() {;}
35  virtual void TearDown() {;}
36  virtual void TestBody() {;}
37  public:
38  Test() : m_anyError(false) {;}
39  virtual ~Test() {;}
40  BENTLEYDLL_EXPORT void Run();
41  virtual CharCP GetTestCaseNameA () const = 0;
42  virtual CharCP GetTestNameA () const = 0;
43  };
44  }
45 
46  #define TEST_CLASS_NAME(testCaseName, testName) TEST_##testCaseName##_##testName
47  #define TEST_RUNNER_FUNC(testCaseName, testName) run_TEST_##testCaseName##_##testName
48 
49  #define DEFINE_BETEST_INTERNAL(superTestName, testCaseName, testName)\
50  struct TEST_CLASS_NAME(testCaseName,testName) : superTestName \
51  { \
52  CharCP GetTestCaseNameA () const { return #testCaseName; } \
53  CharCP GetTestNameA () const { return #testName; } \
54  virtual void TestBody () override; \
55  }; \
56  extern "C" EXPORT_ATTRIBUTE int TEST_RUNNER_FUNC(testCaseName,testName) () {size_t e = BeTest::GetErrorCount(); TEST_CLASS_NAME(testCaseName,testName) t; t.Run(); return BeTest::GetErrorCount() > e;} \
57  void TEST_CLASS_NAME(testCaseName,testName) :: TestBody ()
58 
59 
60  #if !defined(BETEST_GENERATE_UNITTESTS_LIST_H)
61  #undef TEST_F
62  #define TEST_F(testCaseName, testName) DEFINE_BETEST_INTERNAL(testCaseName,testCaseName,testName)
63 
64  #undef TEST
65  #define TEST(testCaseName, testName) DEFINE_BETEST_INTERNAL(testing::Test,testCaseName,testName)
66  #endif
67 
68  #if !defined(RUN_ALL_TESTS)
69  #define RUN_ALL_TESTS BeTest::RunAllTests
70  #endif
71  #endif
72 
74 
75 /*=================================================================================**/
79 struct BeTest
80 {
85 BENTLEYDLL_EXPORT static void Initialize ();
86 
89 
91 
94 BENTLEYDLL_EXPORT static void SetThrowOnAssert (bool doThrow, BeAssertFunctions::AssertType atype = BeAssertFunctions::AssertType::All);
95 
97 typedef void T_BeAssertListener (WCharCP _Message, WCharCP _File, unsigned _Line, BeAssertFunctions::AssertType atype);
98 
100 BENTLEYDLL_EXPORT static void SetBeAssertListener (T_BeAssertListener*);
101 
103 
106 
109  {
111  virtual void GetHomeRoot (BeFileName& path) {BeAssert(false);}
113  virtual void GetDocumentsRoot (BeFileName& path) {BeAssert(false);}
115  virtual void GetAppSupportRoot (BeFileName& path) {BeAssert(false);}
117  virtual void GetOutputRoot (BeFileName& path) {BeAssert(false);}
118  };
119 
121 BENTLEYDLL_EXPORT static void SetIGetDataRoots (IGetDataRoots&);
122 
124 BENTLEYDLL_EXPORT static IGetDataRoots& GetIGetDataRoots();
125 
127 
130 
134 BENTLEYDLL_EXPORT static void ReadIgnoreListAsFilters (bastring& filters, FILE* fp);
135 
138 BENTLEYDLL_EXPORT static void LoadIgnoreList (bastring& filters, BeFileName const& ignoreDir);
139 
142 BENTLEYDLL_EXPORT static void LoadIgnoreList (bastring& filters);
143 
145 
147 BENTLEYDLL_EXPORT static void BreakInDebugger (CharCP msg1="BeTest", CharCP msg2="Break!");
148 
149 #if !defined (GTEST_INCLUDE_GTEST_GTEST_H_)
150 
151 #if defined (EXPERIMENT_COMMENT_OFF)
152  #define BE_TEST_EXPECTED_RESULT_EQ(expected,actual,fatal) BeTest::CheckResultEQ (BeTest::PrimitiveValueUnion(actual), BeTest::PrimitiveValueUnion(expected), true, #expected, #actual, __FILE__ , __LINE__,fatal)
153  #define BE_TEST_EXPECTED_RESULT_NE(expected,actual,fatal) BeTest::CheckResultEQ (BeTest::PrimitiveValueUnion(actual), BeTest::PrimitiveValueUnion(expected), false, #expected, #actual, __FILE__ , __LINE__,fatal)
154  #define BE_TEST_EXPECTED_RESULT_STREQ(expected,actual,fatal) BeTest::CheckResultEQ (BeTest::PrimitiveValueUnion(actual), BeTest::PrimitiveValueUnion(expected), true, #expected, #actual, __FILE__ , __LINE__,fatal)
155  #define BE_TEST_EXPECTED_RESULT_STRNE(expected,actual,fatal) BeTest::CheckResultEQ (BeTest::PrimitiveValueUnion(actual), BeTest::PrimitiveValueUnion(expected), false, #expected, #actual, __FILE__ , __LINE__,fatal)
156  #define BE_TEST_EXPECTED_RESULT_ASTREQ(expected,actual,fatal) BeTest::CheckResultEQ (BeTest::PrimitiveValueUnion(actual), BeTest::PrimitiveValueUnion(expected), true, #expected, #actual, __FILE__ , __LINE__,fatal)
157 #else
158  #define BE_TEST_EXPECTED_RESULT_EQ(expected,actual,fatal) BeTest::ExpectedResult ((expected) == (actual), #expected, #actual, __FILE__ , __LINE__,fatal)
159  #define BE_TEST_EXPECTED_RESULT_NE(val1,val2,fatal) BeTest::ExpectedResult ((val1) != (val2), #val1, #val2, __FILE__ , __LINE__,fatal)
160  #define BE_TEST_EXPECTED_RESULT_STREQ(val1,val2,fatal) BeTest::ExpectedResult (0==wcscmp(val1, val2), #val1, #val2, __FILE__ , __LINE__,fatal)
161  #define BE_TEST_EXPECTED_RESULT_STRNE(val1,val2,fatal) BeTest::ExpectedResult (0!=wcscmp(val1, val2), #val1, #val2, __FILE__ , __LINE__,fatal)
162  #define BE_TEST_EXPECTED_RESULT_ASTREQ(val1,val2,fatal) BeTest::ExpectedResult (0==strcmp(val1, val2), #val1, #val2, __FILE__ , __LINE__,fatal)
163 #endif //defined (EXPERIMENT_COMMENT_OFF)
164  #define BE_TEST_EXPECTED_RESULT_TRUE(expression,fatal) BeTest::ExpectedResult ((expression) != 0, "TRUE", #expression, __FILE__ , __LINE__,fatal)
165  #define BE_TEST_EXPECTED_RESULT_FALSE(expression,fatal) BeTest::ExpectedResult ((expression) == 0, "FALSE", #expression, __FILE__ , __LINE__,fatal)
166  #define BE_TEST_EXPECTED_RESULT_LE(val1,val2,fatal) BeTest::ExpectedResult ((val1) <= (val2), #val1, #val2, __FILE__ , __LINE__,fatal)
167  #define BE_TEST_EXPECTED_RESULT_LT(val1,val2,fatal) BeTest::ExpectedResult ((val1) < (val2), #val1, #val2, __FILE__ , __LINE__,fatal)
168  #define BE_TEST_EXPECTED_RESULT_GE(val1,val2,fatal) BeTest::ExpectedResult ((val1) >= (val2), #val1, #val2, __FILE__ , __LINE__,fatal)
169  #define BE_TEST_EXPECTED_RESULT_GT(val1,val2,fatal) BeTest::ExpectedResult ((val1) > (val2), #val1, #val2, __FILE__ , __LINE__,fatal)
170  #define BE_TEST_EXPECTED_RESULT_NEAR(val1, val2, tol,fatal) BeTest::ExpectedResult (BeTest::EqTol(val1,val2,tol), #val1, #val2, __FILE__ , __LINE__,fatal)
171  #define BE_TEST_EXPECTED_RESULT_FAIL() BeTest::ExpectedResult (false, "SUCCESS", "FAIL", __FILE__ , __LINE__,true)
172 
173  // These macro names match those defined in gtest.h
174  // These are not as good as the functions used in gtest.h but
175  // we put these here so we don't have to publish gtest.h
176  #if !defined (ASSERT_EQ)
177  #define ASSERT_EQ(expected,actual) BE_TEST_EXPECTED_RESULT_EQ(expected,actual,true)
178  #endif
179  #if !defined (EXPECT_EQ)
180  #define EXPECT_EQ(expected,actual) BE_TEST_EXPECTED_RESULT_EQ(expected,actual,false)
181  #endif
182  #if !defined (ASSERT_TRUE)
183  #define ASSERT_TRUE(expression) BE_TEST_EXPECTED_RESULT_TRUE(expression,true)
184  #endif
185  #if !defined (EXPECT_TRUE)
186  #define EXPECT_TRUE(expression) BE_TEST_EXPECTED_RESULT_TRUE(expression,false)
187  #endif
188  #if !defined (ASSERT_FALSE)
189  #define ASSERT_FALSE(expression) BE_TEST_EXPECTED_RESULT_FALSE(expression,true)
190  #endif
191  #if !defined (EXPECT_FALSE)
192  #define EXPECT_FALSE(expression) BE_TEST_EXPECTED_RESULT_FALSE(expression,false)
193  #endif
194  #if !defined (ASSERT_NE)
195  #define ASSERT_NE(val1,val2) BE_TEST_EXPECTED_RESULT_NE(val1,val2,true)
196  #endif
197  #if !defined (EXPECT_NE)
198  #define EXPECT_NE(val1,val2) BE_TEST_EXPECTED_RESULT_NE(val1,val2,false)
199  #endif
200  #if !defined (ASSERT_LE)
201  #define ASSERT_LE(val1,val2) BE_TEST_EXPECTED_RESULT_LE(val1,val2,true)
202  #endif
203  #if !defined (EXPECT_LE)
204  #define EXPECT_LE(val1,val2) BE_TEST_EXPECTED_RESULT_LE(val1,val2,false)
205  #endif
206  #if !defined (ASSERT_LT)
207  #define ASSERT_LT(val1,val2) BE_TEST_EXPECTED_RESULT_LT(val1,val2,true)
208  #endif
209  #if !defined (EXPECT_LT)
210  #define EXPECT_LT(val1,val2) BE_TEST_EXPECTED_RESULT_LT(val1,val2,false)
211  #endif
212  #if !defined (ASSERT_GE)
213  #define ASSERT_GE(val1,val2) BE_TEST_EXPECTED_RESULT_GE(val1,val2,true)
214  #endif
215  #if !defined (EXPECT_GE)
216  #define EXPECT_GE(val1,val2) BE_TEST_EXPECTED_RESULT_GE(val1,val2,false)
217  #endif
218  #if !defined (ASSERT_GT)
219  #define ASSERT_GT(val1,val2) BE_TEST_EXPECTED_RESULT_GT(val1,val2,true)
220  #endif
221  #if !defined (EXPECT_GT)
222  #define EXPECT_GT(val1,val2) BE_TEST_EXPECTED_RESULT_GT(val1,val2,false)
223  #endif
224  #if !defined (ASSERT_STREQ)
225  #define ASSERT_STREQ(val1,val2) BE_TEST_EXPECTED_RESULT_STREQ(val1,val2,true)
226  #endif
227  #if !defined (EXPECT_STREQ)
228  #define EXPECT_STREQ(val1,val2) BE_TEST_EXPECTED_RESULT_STREQ(val1,val2,false)
229  #endif
230  #if !defined (ASSERT_ASTREQ)
231  #define ASSERT_ASTREQ(val1,val2) BE_TEST_EXPECTED_RESULT_ASTREQ(val1,val2,true)
232  #endif
233  #if !defined (EXPECT_ASTREQ)
234  #define EXPECT_ASTREQ(val1,val2) BE_TEST_EXPECTED_RESULT_ASTREQ(val1,val2,false)
235  #endif
236  #if !defined (ASSERT_STRNE)
237  #define ASSERT_STRNE(val1,val2) BE_TEST_EXPECTED_RESULT_STRNE(val1,val2,true)
238  #endif
239  #if !defined (EXPECT_STRNE)
240  #define EXPECT_STRNE(val1,val2) BE_TEST_EXPECTED_RESULT_STRNE(val1,val2,false)
241  #endif
242  #if !defined (ASSERT_NEAR)
243  #define ASSERT_NEAR(val1, val2, tol) BE_TEST_EXPECTED_RESULT_NEAR(val1, val2, tol,true)
244  #endif
245  #if !defined (EXPECT_NEAR)
246  #define EXPECT_NEAR(val1, val2, tol) BE_TEST_EXPECTED_RESULT_NEAR(val1, val2, tol,false)
247  #endif
248  #if !defined (FAIL)
249  #define FAIL() BE_TEST_EXPECTED_RESULT_FAIL()
250  #endif
251  #if !defined (SUCCEED)
252  #define SUCCEED()
253  #endif
254  #if !defined (EXPECT_DOUBLE_EQ)
255  #define EXPECT_DOUBLE_EQ(expected,actual) EXPECT_EQ(expected,actual)
256  #endif
257 
260 
261 #if defined (EXPERIMENT_COMMENT_OFF)
262  struct PrimitiveValueUnion
264  {
265  enum Type {INTEGRAL=0, TYPE_BOOL, UINTEGRAL, VOIDSTAR, CHARCP, WCHARCP, DOUBLE};
266 
267  union
268  {
269  UInt64 m_ivalue;
270  double m_dvalue;
271  void* m_pvalue;
272  };
273  byte m_type;
274 
275  PrimitiveValueUnion PromoteTo (Type) const;
276 
277  BENTLEYDLL_EXPORT PrimitiveValueUnion ( bool v);
278  BENTLEYDLL_EXPORT PrimitiveValueUnion ( Int8 v);
279  BENTLEYDLL_EXPORT PrimitiveValueUnion (UInt8 v);
280  BENTLEYDLL_EXPORT PrimitiveValueUnion ( Int16 v);
281  BENTLEYDLL_EXPORT PrimitiveValueUnion (UInt16 v);
282  BENTLEYDLL_EXPORT PrimitiveValueUnion ( Int32 v);
283  BENTLEYDLL_EXPORT PrimitiveValueUnion (UInt32 v);
284  BENTLEYDLL_EXPORT PrimitiveValueUnion ( Long32 v);
285 #if !defined (__APPLE__)
286  BENTLEYDLL_EXPORT PrimitiveValueUnion (ULong32 v);
287 #endif
288  BENTLEYDLL_EXPORT PrimitiveValueUnion ( Int64 v);
289  BENTLEYDLL_EXPORT PrimitiveValueUnion (UInt64 v);
290  BENTLEYDLL_EXPORT PrimitiveValueUnion (double v);
291  BENTLEYDLL_EXPORT PrimitiveValueUnion (void const* v);
292  BENTLEYDLL_EXPORT PrimitiveValueUnion (CharCP v);
293  BENTLEYDLL_EXPORT PrimitiveValueUnion (WCharCP v);
294 
295  BENTLEYDLL_EXPORT bool operator==(PrimitiveValueUnion const& rhs) const;
296  BENTLEYDLL_EXPORT Utf8String ToString () const;
297  };
298 #endif //defined (EXPERIMENT_COMMENT_OFF)
299 
302  {
305  BENTLEYDLL_EXPORT ExpectedResult (bool isAsExpected, CharCP actualExpression, CharCP expectedExpression, CharCP fileName, size_t lineNum, bool abortImmediately);
306  BENTLEYDLL_EXPORT ExpectedResult (bool isAsExpected, CharCP actualValue, CharCP expectedValue, bool expectedEq, CharCP actualExpression, CharCP expectedExpression, CharCP fileName, size_t lineNum, bool abortImmediately);
308 #if defined(_MSC_VER) && (_MSC_VER >= 1900)
309  noexcept(false)
310 #endif
311  ;
312 
314  //BENTLEYDLL_EXPORT ExpectedResult& operator<< (Int64 val);
317  };
318 
320  BENTLEYDLL_EXPORT static bool EqTol (double v1, double v2, double tol);
322  BENTLEYDLL_EXPORT static bool EqTol (int v1, int v2, int tol);
324  BENTLEYDLL_EXPORT static void ClearErrorCount();
326  BENTLEYDLL_EXPORT static void IncrementErrorCount();
328  BENTLEYDLL_EXPORT static size_t GetErrorCount();
330  BENTLEYDLL_EXPORT static void RecordFailedTest (testing::Test const&);
332  BENTLEYDLL_EXPORT static bset<AString> const& GetFailedTests();
334  BENTLEYDLL_EXPORT static void SetBreakOnFailure (bool);
336  BENTLEYDLL_EXPORT static bool GetBreakOnFailure ();
337 
338 #if defined (EXPERIMENT_COMMENT_OFF)
339  BENTLEYDLL_EXPORT static ExpectedResult CheckResultEQ (PrimitiveValueUnion const& a, PrimitiveValueUnion const& x, bool expectEq, CharCP aexp, CharCP xexp, CharCP file, UInt32 ln, bool fatal);
340 #endif
341 
343 
344  private:
345  static bset<AString> s_failedTests;
346  static size_t s_errorCount;
347  static bool s_breakOnFailure;
348 
349 #endif
350 
351 };
352 
354 
Contains a UTF-8 encoded string.
Definition: WString.h:275
Provides Bentley specific functions to iterate over a file path (Bentley/BeFileListIterator.h).
virtual void GetOutputRoot(BeFileName &path)
Where a unit test can write temporary data files.
Definition: BeTest.h:117
#define BENTLEYDLL_EXPORT
Definition: Bentley.h:249
virtual void GetHomeRoot(BeFileName &path)
The "home" directory.
Definition: BeTest.h:111
virtual void SetUp()
Definition: BeTest.h:34
A string class that has many of the same capabilities as std::string, plus additional functions such ...
Definition: WString.h:30
Provides Bentley specific set implementation (Bentley/bset.h).
Test()
Definition: BeTest.h:38
Definition: TextTableHandler.h:216
virtual CharCP GetTestCaseNameA() const =0
bool m_abortImmediately
Definition: BeTest.h:303
virtual void TestBody()
Definition: BeTest.h:36
long Long32
Definition: Bentley.r.h:145
uint16_t UInt16
Definition: Bentley.r.h:118
virtual void GetDocumentsRoot(BeFileName &path)
Where a unit test can find DGN and other data files.
Definition: BeTest.h:113
virtual ~Test()
Definition: BeTest.h:39
unsigned long ULong32
Definition: Bentley.r.h:146
Provides Bentley specific assert functions (Bentley/BeAssert.h).
uint8_t UInt8
Definition: Bentley.r.h:116
wchar_t const * WCharCP
Definition: Bentley.h:224
int16_t Int16
Definition: Bentley.r.h:117
virtual CharCP GetTestNameA() const =0
uint32_t UInt32
Definition: Bentley.r.h:128
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
Provides Bentley specific string utility functions (Bentley/BeStringUtilities.h). ...
Definition: BeTest.h:29
Portable unit test utility functions, not specific to any one test harness.
Definition: BeTest.h:79
int8_t Int8
Definition: Bentley.r.h:115
AssertType
Definition: BeAssert.h:19
int32_t Int32
Definition: Bentley.r.h:119
Captures the result of testing a condition.
Definition: BeTest.h:301
AString m_message
Definition: BeTest.h:304
Interface to be implemented by an object that can tell unit tests where to find files.
Definition: BeTest.h:108
virtual void GetAppSupportRoot(BeFileName &path)
Where a unit test can find input supporting files such as fonts.rsc.
Definition: BeTest.h:115
Defines typedefs and constants that can be used across other namespaces. All Bentley-authored C++ sou...
unsigned char byte
Definition: Bentley.r.h:144
uint64_t UInt64
Definition: Bentley.r.h:131
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const basic_string< _CharT, _Traits, _Allocator > &__str)
Definition: basic_string.h:1488
char const * CharCP
Definition: Bentley.h:226
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
int64_t Int64
Definition: Bentley.r.h:130
#define BeAssert(_Expression)
BeAssert performs the same function as the standard assert, plus it prevents the most common cases of...
Definition: BeAssert.h:56
void T_BeAssertListener(WCharCP _Message, WCharCP _File, unsigned _Line, BeAssertFunctions::AssertType atype)
Signature of an assertion failure listener. There can be many listeners. A listener does not control ...
Definition: BeTest.h:97
virtual void TearDown()
Definition: BeTest.h:35
Provides Bentley implemented file management functions (Bentley/BeFile.h).
Bstdcxx::basic_string< char, std::char_traits< char >, BentleyAllocator< char > > bastring
Definition: WString.h:22
bool operator==(const BentleyAllocator< _Ty > &, const BentleyAllocator< _Other > &)
Definition: BentleyAllocator.h:146

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.