unitTesting.cpp 3.4 KB

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