Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Unit test program
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "unitlib/unitlib.h"
  8. #include "appframework/iappsystemgroup.h"
  9. #include "appframework/appframework.h"
  10. #include "tier0/dbg.h"
  11. #include <stdio.h>
  12. #include <windows.h>
  13. #include "vstdlib/iprocessutils.h"
  14. #include "tier1/interface.h"
  15. #include "vstdlib/cvar.h"
  16. #pragma warning (disable:4100)
  17. SpewRetval_t UnitTestSpew( SpewType_t type, char const *pMsg )
  18. {
  19. switch( type )
  20. {
  21. case SPEW_WARNING:
  22. printf( "UnitTest Warning:\n" );
  23. break;
  24. case SPEW_ASSERT:
  25. printf( "UnitTest Assert:\n" );
  26. break;
  27. case SPEW_ERROR:
  28. printf( "UnitTest Error:\n" );
  29. break;
  30. }
  31. printf( "%s", pMsg );
  32. OutputDebugString( pMsg );
  33. if ( Sys_IsDebuggerPresent() )
  34. return ( type == SPEW_ASSERT || type == SPEW_ERROR ) ? SPEW_DEBUGGER : SPEW_CONTINUE;
  35. return SPEW_CONTINUE;
  36. }
  37. //-----------------------------------------------------------------------------
  38. // The application object
  39. //-----------------------------------------------------------------------------
  40. class CUnitTestApp : public CDefaultAppSystemGroup<CSteamAppSystemGroup>
  41. {
  42. public:
  43. // Methods of IApplication
  44. virtual bool Create();
  45. virtual int Main();
  46. virtual void Destroy();
  47. private:
  48. };
  49. DEFINE_CONSOLE_STEAM_APPLICATION_OBJECT( CUnitTestApp );
  50. //-----------------------------------------------------------------------------
  51. // The application object
  52. //-----------------------------------------------------------------------------
  53. bool CUnitTestApp::Create()
  54. {
  55. // Install a special Spew handler that ignores all assertions and lets us
  56. // run for as long as possible
  57. SpewOutputFunc( UnitTestSpew );
  58. // FIXME: This list of dlls should come from the unittests themselves
  59. AppSystemInfo_t appSystems[] =
  60. {
  61. { "vstdlib.dll", PROCESS_UTILS_INTERFACE_VERSION },
  62. { "", "" } // Required to terminate the list
  63. };
  64. if ( !AddSystems( appSystems ) )
  65. return false;
  66. // Very simple... just iterate over all .DLLs in the current directory
  67. // see if they export UNITTEST_INTERFACE_VERSION. If not, then unload them
  68. // just as quick.
  69. // We may want to make this more sophisticated, giving it a search path,
  70. // or giving test DLLs special extensions, or statically linking the test DLLs
  71. // to this program.
  72. WIN32_FIND_DATA findFileData;
  73. HANDLE hFind= FindFirstFile("*.dll", &findFileData);
  74. while (hFind != INVALID_HANDLE_VALUE)
  75. {
  76. CSysModule* hLib = Sys_LoadModule(findFileData.cFileName);
  77. if ( hLib )
  78. {
  79. CreateInterfaceFn factory = Sys_GetFactory( hLib );
  80. if ( factory && factory( UNITTEST_INTERFACE_VERSION, NULL ) )
  81. {
  82. AppModule_t module = LoadModule( factory );
  83. AddSystem( module, UNITTEST_INTERFACE_VERSION );
  84. }
  85. else
  86. {
  87. Sys_UnloadModule( hLib );
  88. }
  89. }
  90. if (!FindNextFile( hFind, &findFileData ))
  91. break;
  92. }
  93. return true;
  94. }
  95. void CUnitTestApp::Destroy()
  96. {
  97. }
  98. //-----------------------------------------------------------------------------
  99. // The application object
  100. //-----------------------------------------------------------------------------
  101. int CUnitTestApp::Main()
  102. {
  103. printf( "Valve Software - unittest.exe (%s)\n", __DATE__ );
  104. int nTestCount = UnitTestCount();
  105. for ( int i = 0; i < nTestCount; ++i )
  106. {
  107. ITestCase* pTestCase = GetUnitTest(i);
  108. printf("Starting test %s....\n", pTestCase->GetName() );
  109. pTestCase->RunTest();
  110. }
  111. return 0;
  112. }