unitTesting.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 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. #ifdef TORQUE_TESTS_ENABLED
  23. #include "console/engineAPI.h"
  24. #include "console/consoleInternal.h"
  25. #include "unitTesting.h"
  26. #include "memoryTester.h"
  27. #include <gtest/gtest-all.cc>
  28. //-----------------------------------------------------------------------------
  29. class TorqueUnitTestListener : public ::testing::EmptyTestEventListener
  30. {
  31. // Called before a test starts.
  32. virtual void OnTestStart( const ::testing::TestInfo& testInfo )
  33. {
  34. Con::printf("> Starting Test '%s.%s'",
  35. testInfo.test_case_name(), testInfo.name());
  36. }
  37. // Called after a failed assertion or a SUCCEED() invocation.
  38. virtual void OnTestPartResult( const ::testing::TestPartResult& testPartResult )
  39. {
  40. if ( testPartResult.failed() )
  41. {
  42. Con::warnf(">> Failed with '%s' in '%s' at (line:%d)",
  43. testPartResult.summary(),
  44. testPartResult.file_name(),
  45. testPartResult.line_number()
  46. );
  47. }
  48. else
  49. {
  50. Con::printf(">> Passed with '%s' in '%s' at (line:%d)",
  51. testPartResult.summary(),
  52. testPartResult.file_name(),
  53. testPartResult.line_number()
  54. );
  55. }
  56. }
  57. // Called after a test ends.
  58. virtual void OnTestEnd( const ::testing::TestInfo& testInfo )
  59. {
  60. Con::printf("> Ending Test '%s.%s'\n",
  61. testInfo.test_case_name(), testInfo.name());
  62. }
  63. };
  64. DefineConsoleFunction( runAllUnitTests, int, (),,
  65. "" )
  66. {
  67. // Set-up some empty arguments.
  68. S32 testArgc = 0;
  69. char** testArgv = NULL;
  70. // Initialize Google Test.
  71. testing::InitGoogleTest( &testArgc, testArgv );
  72. // Fetch the unit test instance.
  73. testing::UnitTest& unitTest = *testing::UnitTest::GetInstance();
  74. // Fetch the unit test event listeners.
  75. testing::TestEventListeners& listeners = unitTest.listeners();
  76. // Release the default listener.
  77. delete listeners.Release( listeners.default_result_printer() );
  78. if ( Con::getBoolVariable( "$testing::checkMemoryLeaks", false ) ) {
  79. // Add the memory leak tester.
  80. listeners.Append( new testing::MemoryLeakDetector );
  81. }
  82. // Add the Torque unit test listener.
  83. listeners.Append( new TorqueUnitTestListener );
  84. Con::printf( "\nUnit Tests Starting...\n" );
  85. const S32 result = RUN_ALL_TESTS();
  86. Con::printf( "\n... Unit Tests Ended.\n" );
  87. return result;
  88. }
  89. #endif // TORQUE_TESTS_ENABLED