unitTestComponentInterface.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _UNITTESTCOMPONENTINTERFACE_H_
  23. #define _UNITTESTCOMPONENTINTERFACE_H_
  24. #include "unit/test.h"
  25. #include "component/simComponent.h"
  26. #include "component/componentInterface.h"
  27. // This is commented out because I want to keep the explicit namespace referencing
  28. // so that the multiple inheritances from UnitTest doesn't screw anyone up. It will
  29. // also make for more readable code in the derived test-interfaces. -patw
  30. //using namespace UnitTesting;
  31. /// This is a class that will make it very easy for a component author to provide
  32. /// unit testing functionality from within an instantiated component.
  33. class UnitTestComponentInterface : public ComponentInterface, UnitTesting::UnitTest
  34. {
  35. typedef ComponentInterface Parent;
  36. private:
  37. StringTableEntry mName;
  38. UnitTesting::DynamicTestRegistration *mTestReg;
  39. // Constructors/Destructors
  40. public:
  41. UnitTestComponentInterface( const char *name )
  42. {
  43. mName = StringTable->insert( name );
  44. mTestReg = new UnitTesting::DynamicTestRegistration( name, this );
  45. }
  46. virtual ~UnitTestComponentInterface()
  47. {
  48. delete mTestReg;
  49. }
  50. // ComponentInterface overrides
  51. public:
  52. virtual bool isValid() const
  53. {
  54. return Parent::isValid() && ( mTestReg != NULL );
  55. }
  56. // UnitTest overrides
  57. public:
  58. /// This is the only function you need to overwrite to add a unit test interface
  59. /// your component.
  60. virtual void run() = 0;
  61. };
  62. // Macros
  63. #ifndef TORQUE_DEBUG
  64. # define DECLARE_UNITTEST_INTERFACE(x)
  65. # define CACHE_UNITTEST_INTERFACE(x)
  66. #else
  67. //-----------------------------------------------------------------------------
  68. # define DECLARE_UNITTEST_INTERFACE(x) \
  69. class x##_UnitTestInterface : public UnitTestComponentInterface \
  70. {\
  71. typedef UnitTestComponentInterface Parent; \
  72. public: \
  73. x##_UnitTestInterface() : UnitTestComponentInterface( #x ) {}; \
  74. virtual void run(); \
  75. } _##x##UnitTestInterface
  76. //-----------------------------------------------------------------------------
  77. # define CACHE_UNITTEST_INTERFACE(x) registerCachedInterface( "unittest", #x, this, &_##x##UnitTestInterface )
  78. #endif
  79. #endif