TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_unit_test.hpp
1 
8 #ifndef TurtleBrains_UnitTest_hpp
9 #define TurtleBrains_UnitTest_hpp
10 
11 #include <turtle_brains/core/tb_configuration.hpp>
12 #include <turtle_brains/core/tb_defines.hpp>
13 #include <turtle_brains/core/tb_string.hpp>
14 #include <turtle_brains/core/tb_noncopyable.hpp>
15 
16 #include <functional>
17 #include <string>
18 #include <cstdarg>
19 
23 #define tbFailTestIf(test, failMessage, ...) { TurtleBrains::Core::UnitTest::FailTestIf((test) ? true : false, __FILE__, __LINE__, failMessage, ##__VA_ARGS__); }
24 
28 #define tbFailTestIfNot(test, failMessage, ...) { TurtleBrains::Core::UnitTest::FailTestIf((test) ? false : true, __FILE__, __LINE__, failMessage, ##__VA_ARGS__); }
29 
30 namespace TurtleBrains
31 {
32  namespace Core
33  {
34  namespace UnitTest
35  {
36 
37  void RunAllTests(const tbCore::tbString& testResultFile = "test_results.txt");
38 
39  void FailTestIf(bool testResult, const char* const fromFile, int onLineNumber, const char* const failMessage, ...);
40 
41  void Log(const char* const failMessage, ...);
42  void Log(const char* const fromFile, int onLineNumber, const char* const failMessage, ...);
43  void Log(const char* const fromFile, int onLineNumber, const char* const failMessage, va_list argumentList);
44 
46  {
47  public:
48  TestCaseInterface(const std::string& testName);
49  virtual ~TestCaseInterface(void) = 0;
50 
51  const std::string& GetTestName(void) const { return mTestName; }
52  const std::string& GetMessageBuffer(void) const { return mMessageBuffer; }
53 
54  bool RunTest(void);
55  bool IsWeakTest(void) const { return mIsWeakTest; }
56 
57  protected:
62  void SetWeakTest(void) { mIsWeakTest = true; }
63 
64  template <typename Type> bool ExpectedValue(const Type& value, const Type& expectedValue, const tbCore::tbString formattedMessage, ...)
65  {
66  if (value == expectedValue)
67  {
68  return true;
69  }
70 
71  va_list argumentsList;
72  va_start(argumentsList, formattedMessage);
73  FailedExpectation(formattedMessage, argumentsList);
74  va_end(argumentsList);
75  return false;
76  }
77 
78  void FailTestIf(bool testResult, tbCore::tbString formattedMessage, ...);
79  bool ExpectedValue(float value, float expectedValue, tbCore::tbString formattedMessage, ...);
80  bool ExpectedValueWithin(float value, float expectedValue, float epsilon, tbCore::tbString formattedMessage, ...);
81  bool ExpectedValue(double value, double expectedValue, tbCore::tbString formattedMessage, ...);
82  bool ExpectedValueWithin(double value, double expectedValue, double epsilon, tbCore::tbString formattedMessage, ...);
83 
84  bool ExpectedValues(size_t numberOfValues, const float* values, const float* expectedValues, tbCore::tbString formattedMessage, ...);
85  bool ExpectedValuesWithin(size_t numberOfValues, const float* values, const float* expectedValues, float epsilon, tbCore::tbString formattedMessage, ...);
86  bool ExpectedValues(size_t numberOfValues, const double* values, const double* expectedValues, tbCore::tbString formattedMessage, ...);
87  bool ExpectedValuesWithin(size_t numberOfValues, const double* values, const double* expectedValues, double epsilon, tbCore::tbString formattedMessage, ...);
88 
94  bool ExpectedException(std::function<void(void)> testCode, const tbCore::tbString formattedMessage, ...);
95 
96  virtual bool OnRunTest(void) = 0;
97 
98  void FailedExpectation(tbCore::tbString formattedMessage, va_list argumentList);
99 
100  private:
101  const tbCore::tbString mTestName;
102  tbCore::tbString mMessageBuffer;
103  bool mAllExpectationPassed;
104  bool mIsWeakTest;
105  };
106 
107  }; /* namespace UnitTest */
108  }; /* namespace Core */
109 }; /* namespace TurtleBrains */
110 
111 namespace tbCore = TurtleBrains::Core;
112 
113 #endif /* TurtleBrains_UnitTest_hpp */
void SetWeakTest(void)
Definition: tb_unit_test.hpp:62
Definition: tb_noncopyable.hpp:22
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
void Log(const char *formattedMessage,...)
bool ExpectedException(std::function< void(void)> testCode, const tbCore::tbString formattedMessage,...)
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:88
std::string tbString
Definition: tb_string.hpp:335