BsTestSuite.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. /** @addtogroup Testing
  4. * @{
  5. */
  6. /** Tests if condition is true, and reports unit test failure if it fails. */
  7. #define BS_TEST_ASSERT(expr) assertment((expr), "", __FILE__, __LINE__);
  8. /** Tests if condition is true, and reports unit test failure with a message if it fails. */
  9. #define BS_TEST_ASSERT_MSG(expr, msg) assertment((expr), msg, __FILE__, __LINE__);
  10. namespace BansheeEngine
  11. {
  12. /**
  13. * Primary class for unit testing. Override and register unit tests in constructor then run the tests using the
  14. * desired method of output.
  15. */
  16. class BS_UTILITY_EXPORT TestSuite
  17. {
  18. public:
  19. typedef void(TestSuite::*Func)();
  20. private:
  21. /** Contains data about a single unit test. */
  22. struct TestEntry
  23. {
  24. TestEntry(Func test, const String& name);
  25. Func test;
  26. String name;
  27. };
  28. public:
  29. virtual ~TestSuite() {}
  30. /** Runs all the tests in the suite (and sub-suites). Tests results are reported to the provided output class. */
  31. void run(TestOutput& output);
  32. /** Adds a new child suite to this suite. This method allows you to group suites and execute them all at once. */
  33. void add(const TestSuitePtr& suite);
  34. /** Creates a new suite of a particular type. */
  35. template <class T>
  36. static TestSuitePtr create()
  37. {
  38. static_assert((std::is_base_of<TestSuite, T>::value), "Invalid test suite type. It needs to derive from BansheeEngine::TestSuite.");
  39. return std::static_pointer_cast<TestSuite>(bs_shared_ptr_new<T>());
  40. }
  41. protected:
  42. TestSuite();
  43. /** Called right before any tests are ran. */
  44. virtual void startUp() {}
  45. /**
  46. * @brief Called after all tests and child suite's tests are ran.
  47. */
  48. virtual void shutDown() {}
  49. /**
  50. * Register a new unit test.
  51. *
  52. * @param[in] test Function to call in order to execute the test.
  53. * @param[in] name Name of the test we can use for referencing it later.
  54. */
  55. void addTest(Func test, const String& name);
  56. /**
  57. * Reports success or failure depending on the result of an expression.
  58. *
  59. * @param[in] success If true success is reported, otherwise failure.
  60. * @param[in] file Name of the source code file the assertment originates from.
  61. * @param[in] line Line number at which the assertment was triggered at.
  62. */
  63. void assertment(bool success, const String& desc, const String& file, long line);
  64. Vector<TestEntry> mTests;
  65. Vector<TestSuitePtr> mSuites;
  66. // Transient
  67. TestOutput* mOutput;
  68. String mActiveTestName;
  69. };
  70. }
  71. /** Registers a new unit test within an implementation of TestSuite. */
  72. #define BS_ADD_TEST(func) addTest(static_cast<Func>(&func), #func);
  73. /** @} */