TurtleBrains  0.3.1
High quality, portable, C++ framework for rapid 2D game development.
tb_unit_test.h
1 
8 #ifndef _TurtleBrains_UnitTest_h_
9 #define _TurtleBrains_UnitTest_h_
10 
11 #include "../tb_configuration.h"
12 #include "../tb_defines.h"
13 #include "../tb_string.h"
14 #include "../tb_noncopyable.h"
15 
16 #include <string>
17 #include <cstdarg>
18 
22 #define tbFailTestIf(test, failMessage, ...) { TurtleBrains::Core::UnitTest::FailTestIf((test) ? true : false, __FILE__, __LINE__, failMessage, ##__VA_ARGS__); }
23 
27 #define tbFailTestIfNot(test, failMessage, ...) { TurtleBrains::Core::UnitTest::FailTestIf((test) ? false : true, __FILE__, __LINE__, failMessage, ##__VA_ARGS__); }
28 
29 namespace TurtleBrains
30 {
31  namespace Core
32  {
33  namespace UnitTest
34  {
35 
36  extern const float kEpsilon;
37 
38  void RunAllTests(void);
39 
40  void FailTestIf(bool testResult, const char* const fromFile, int onLineNumber, const char* const failMessage, ...);
41 
42  void Log(const char* const failMessage, ...);
43  void Log(const char* const fromFile, int onLineNumber, const char* const failMessage, ...);
44  void Log(const char* const fromFile, int onLineNumber, const char* const failMessage, va_list argumentList);
45 
47  {
48  public:
49  TestCaseInterface(const std::string& testName);
50  virtual ~TestCaseInterface(void) = 0;
51 
52  const std::string& GetTestName(void) const { return mTestName; }
53  const std::string& GetMessageBuffer(void) const { return mMessageBuffer; }
54 
55  bool RunTest(void);
56 
57  protected:
58  template <typename Type> bool ExpectedValue(const Type& value, const Type& expectedValue, const tbCore::tbString formattedMessage, ...)
59  {
60  if (value != expectedValue)
61  {
62  va_list argumentsList;
63  va_start(argumentsList, formattedMessage);
64  FailedExpectation(formattedMessage, argumentsList);
65  va_end(argumentsList);
66  return false;
67  }
68  return true;
69  }
70 
71  bool ExpectedValue(float value, float expectedValue, const tbCore::tbString formattedMessage, ...);
72  bool ExpectedValueWithin(float value, float expectedValue, float epsilon, const tbCore::tbString formattedMessage, ...);
73  bool ExpectedValue(double value, double expectedValue, const tbCore::tbString formattedMessage, ...);
74  bool ExpectedValueWithin(double value, double expectedValue, double epsilon, const tbCore::tbString formattedMessage, ...);
75 
76  virtual bool OnRunTest(void) = 0;
77 
78  private:
79  void FailedExpectation(const tbCore::tbString formattedMessage, va_list argumentList);
80 
81  const tbCore::tbString mTestName;
82  tbCore::tbString mMessageBuffer;
83  bool mAllExpectationPassed;
84  };
85 
86  }; /* namespace UnitTest */
87  }; /* namespace Core */
88 }; /* namespace TurtleBrains */
89 
90 namespace tbCore = TurtleBrains::Core;
91 
92 #endif /* _TurtleBrains_UnitTest_h_ */
Definition: tb_noncopyable.h:22
Here is some information about the primary namespace.
Definition: tb_application_dialog.h:21
void Log(const char *formattedMessage,...)
Contains core functionality for each component of the API.
Definition: tb_debug_logger.h:91
std::string tbString
Definition: tb_string.h:335