unitTesting.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. if( mVerbose )
  35. Con::printf("> Starting Test '%s.%s'",
  36. testInfo.test_case_name(), testInfo.name());
  37. }
  38. // Called after a failed assertion or a SUCCEED() invocation.
  39. virtual void OnTestPartResult( const ::testing::TestPartResult& testPartResult )
  40. {
  41. if ( testPartResult.failed() )
  42. {
  43. Con::warnf(">> Failed with '%s' in '%s' at (line:%d)\n",
  44. testPartResult.summary(),
  45. testPartResult.file_name(),
  46. testPartResult.line_number()
  47. );
  48. }
  49. else if( mVerbose )
  50. {
  51. Con::printf(">> Passed with '%s' in '%s' at (line:%d)",
  52. testPartResult.summary(),
  53. testPartResult.file_name(),
  54. testPartResult.line_number()
  55. );
  56. }
  57. }
  58. // Called after a test ends.
  59. virtual void OnTestEnd( const ::testing::TestInfo& testInfo )
  60. {
  61. if( mVerbose )
  62. Con::printf("> Ending Test '%s.%s'\n",
  63. testInfo.test_case_name(), testInfo.name());
  64. }
  65. bool mVerbose;
  66. public:
  67. TorqueUnitTestListener( bool verbose ) : mVerbose( verbose ) {}
  68. };
  69. DefineEngineFunction( runAllUnitTests, int, (const char* testSpecs), (""),
  70. "Runs engine unit tests. Some tests are marked as 'stress' tests which do not "
  71. "necessarily check correctness, just performance or possible nondeterministic "
  72. "glitches. There may also be interactive or networking tests which may be "
  73. "excluded by using the testSpecs argument.\n"
  74. "This function should only be called once per executable run, because of "
  75. "googletest's design.\n\n"
  76. "@param testSpecs A space-sepatated list of filters for test cases. "
  77. "See https://code.google.com/p/googletest/wiki/AdvancedGuide#Running_a_Subset_of_the_Tests "
  78. "and http://stackoverflow.com/a/14021997/945863 "
  79. "for a description of the flag format.")
  80. {
  81. S32 testArgc = 0;
  82. char** testArgv = NULL;
  83. if ( dStrlen( testSpecs ) > 0 )
  84. {
  85. String specs(testSpecs);
  86. specs.replace(' ', ':');
  87. specs.insert(0, "--gtest_filter=");
  88. testArgc = 2;
  89. testArgv = new char*[2];
  90. testArgv[0] = NULL; // Program name is unused by googletest.
  91. testArgv[1] = new char[specs.size()];
  92. dStrcpy(testArgv[1], specs, specs.size());
  93. }
  94. // Initialize Google Test.
  95. testing::InitGoogleTest( &testArgc, testArgv );
  96. // Fetch the unit test instance.
  97. testing::UnitTest& unitTest = *testing::UnitTest::GetInstance();
  98. // Fetch the unit test event listeners.
  99. testing::TestEventListeners& listeners = unitTest.listeners();
  100. // Release the default listener.
  101. delete listeners.Release( listeners.default_result_printer() );
  102. if ( Con::getBoolVariable( "$Testing::CheckMemoryLeaks", false ) ) {
  103. // Add the memory leak tester.
  104. listeners.Append( new testing::MemoryLeakDetector );
  105. }
  106. // Add the Torque unit test listener.
  107. listeners.Append( new TorqueUnitTestListener(false) );
  108. // Perform googletest run.
  109. Con::printf( "\nUnit Tests Starting...\n" );
  110. const S32 result = RUN_ALL_TESTS();
  111. Con::printf( "... Unit Tests Ended.\n" );
  112. return result;
  113. }
  114. #endif // TORQUE_TESTS_ENABLED