BsTestSuite.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Testing
  8. * @{
  9. */
  10. /** Tests if condition is true, and reports unit test failure if it fails. */
  11. #define BS_TEST_ASSERT(expr) assertment((expr), "", __FILE__, __LINE__);
  12. /** Tests if condition is true, and reports unit test failure with a message if it fails. */
  13. #define BS_TEST_ASSERT_MSG(expr, msg) assertment((expr), msg, __FILE__, __LINE__);
  14. /**
  15. * Primary class for unit testing. Override and register unit tests in constructor then run the tests using the
  16. * desired method of output.
  17. */
  18. class BS_UTILITY_EXPORT TestSuite
  19. {
  20. public:
  21. typedef void(TestSuite::*Func)();
  22. private:
  23. /** Contains data about a single unit test. */
  24. struct TestEntry
  25. {
  26. TestEntry(Func test, const String& name);
  27. Func test;
  28. String name;
  29. };
  30. public:
  31. virtual ~TestSuite() {}
  32. /** Runs all the tests in the suite (and sub-suites). Tests results are reported to the provided output class. */
  33. void run(TestOutput& output);
  34. /** Adds a new child suite to this suite. This method allows you to group suites and execute them all at once. */
  35. void add(const TestSuitePtr& suite);
  36. /** Creates a new suite of a particular type. */
  37. template <class T>
  38. static TestSuitePtr create()
  39. {
  40. static_assert((std::is_base_of<TestSuite, T>::value), "Invalid test suite type. It needs to derive from BansheeEngine::TestSuite.");
  41. return std::static_pointer_cast<TestSuite>(bs_shared_ptr_new<T>());
  42. }
  43. protected:
  44. TestSuite();
  45. /** Called right before any tests are ran. */
  46. virtual void startUp() {}
  47. /** Called after all tests and child suite's tests are ran. */
  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. /** Registers a new unit test within an implementation of TestSuite. */
  71. #define BS_ADD_TEST(func) addTest(static_cast<Func>(&func), #func);
  72. /** @} */
  73. }