BsTestSuite.h 2.4 KB

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