BsTestSuite.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. namespace bs
  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), __FUNCTION__, __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 SPtr<TestSuite>& suite);
  36. /** Creates a new suite of a particular type. */
  37. template <class T>
  38. static SPtr<TestSuite> create()
  39. {
  40. static_assert((std::is_base_of<TestSuite, T>::value), "Invalid test suite type. It needs to derive from bs::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] desc Message describing the nature of the failure.
  61. * @param[in] file Name of the source code file the assertment originates from.
  62. * @param[in] line Line number at which the assertment was triggered at.
  63. */
  64. void assertment(bool success, const String& desc, const String& file, long line);
  65. Vector<TestEntry> mTests;
  66. Vector<SPtr<TestSuite>> mSuites;
  67. // Transient
  68. TestOutput* mOutput;
  69. String mActiveTestName;
  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. /** @} */
  74. }