unitTesting.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, (const char* testSpecs), (""),
  65. "Runs engine unit tests. Some tests are marked as 'stress' tests which do not "
  66. "necessarily check correctness, just performance or possible nondeterministic "
  67. "glitches. There may also be interactive or networking tests which may be "
  68. "excluded by using the testSpecs argument.\n"
  69. "This function should only be called once per executable run, because of "
  70. "googletest's design.\n\n"
  71. "@param testSpecs A space-sepatated list of filters for test cases. "
  72. "See https://code.google.com/p/googletest/wiki/AdvancedGuide#Running_a_Subset_of_the_Tests "
  73. "for a description of the flag format.")
  74. {
  75. S32 testArgc = 0;
  76. char** testArgv = NULL;
  77. if ( dStrlen( testSpecs ) > 0 )
  78. {
  79. String specs(testSpecs);
  80. specs.replace(' ', ':');
  81. specs.insert(0, "--gtest_filter=");
  82. testArgc = 2;
  83. testArgv = new char*[2];
  84. testArgv[0] = NULL; // Program name is unused by googletest.
  85. testArgv[1] = new char[specs.length()+1];
  86. dStrcpy(testArgv[1], specs);
  87. }
  88. // Initialize Google Test.
  89. testing::InitGoogleTest( &testArgc, testArgv );
  90. // Fetch the unit test instance.
  91. testing::UnitTest& unitTest = *testing::UnitTest::GetInstance();
  92. // Fetch the unit test event listeners.
  93. testing::TestEventListeners& listeners = unitTest.listeners();
  94. // Release the default listener.
  95. delete listeners.Release( listeners.default_result_printer() );
  96. if ( Con::getBoolVariable( "$Testing::CheckMemoryLeaks", false ) ) {
  97. // Add the memory leak tester.
  98. listeners.Append( new testing::MemoryLeakDetector );
  99. }
  100. // Add the Torque unit test listener.
  101. listeners.Append( new TorqueUnitTestListener );
  102. // Perform googletest run.
  103. Con::printf( "\nUnit Tests Starting...\n" );
  104. const S32 result = RUN_ALL_TESTS();
  105. Con::printf( "... Unit Tests Ended.\n" );
  106. return result;
  107. }
  108. #endif // TORQUE_TESTS_ENABLED