LCOV - code coverage report
Current view: top level - source - test_setup.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 38 43 88.4 %
Date: 2023-01-19 00:18:29 Functions: 14 19 73.7 %

          Line data    Source code
       1             : /* Copyright (C) 2021 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : // Got to be consistent with what the rest of the source files do before
      19             : // including precompiled.h, so that the PCH works correctly
      20             : #ifndef CXXTEST_RUNNING
      21             : #define CXXTEST_RUNNING
      22             : #endif
      23             : #define _CXXTEST_HAVE_STD
      24             : 
      25             : #include "precompiled.h"
      26             : 
      27             : #include <fstream>
      28             : 
      29             : #include "lib/self_test.h"
      30             : #include <cxxtest/GlobalFixture.h>
      31             : 
      32             : #if OS_WIN
      33             : #include "lib/sysdep/os/win/wdbg_heap.h"
      34             : #endif
      35             : 
      36             : #include "lib/timer.h"
      37             : #include "lib/sysdep/sysdep.h"
      38             : #include "ps/Profiler2.h"
      39             : #include "ps/TaskManager.h"
      40             : #include "scriptinterface/FunctionWrapper.h"
      41             : #include "scriptinterface/ScriptEngine.h"
      42             : #include "scriptinterface/ScriptContext.h"
      43             : #include "scriptinterface/ScriptInterface.h"
      44             : 
      45           2 : class LeakReporter : public CxxTest::GlobalFixture
      46             : {
      47           1 :     virtual bool tearDownWorld()
      48             :     {
      49             :         // Enable leak reporting on exit.
      50             :         // (This is done in tearDownWorld so that it doesn't report 'leaks'
      51             :         // if the program is aborted before finishing cleanly.)
      52             : #if OS_WIN
      53             :         wdbg_heap_Enable(true);
      54             : #endif
      55           1 :         return true;
      56             :     }
      57             : 
      58           1 :     virtual bool setUpWorld()
      59             :     {
      60             : #if MSC_VERSION
      61             :         // (Warning: the allocation numbers seem to differ by 3 when you
      62             :         // run in the build process vs the debugger)
      63             :         // _CrtSetBreakAlloc(1952);
      64             : #endif
      65           1 :         return true;
      66             :     }
      67             : 
      68             : };
      69             : 
      70           2 : class MiscSetup : public CxxTest::GlobalFixture
      71             : {
      72           1 :     virtual bool setUpWorld()
      73             :     {
      74             :         // Timer must be initialised, else things will break when tests do IO
      75           1 :         timer_Init();
      76             : 
      77             : #if OS_MACOSX || OS_BSD
      78             :         // See comment in GameSetup.cpp FixLocales
      79             :         setlocale(LC_CTYPE, "UTF-8");
      80             : #endif
      81             : 
      82           1 :         Threading::SetMainThread();
      83             : 
      84           1 :         g_Profiler2.Initialise();
      85           1 :         m_ScriptEngine = new ScriptEngine;
      86           1 :         g_ScriptContext = ScriptContext::CreateContext();
      87             : 
      88           1 :         Threading::TaskManager::Initialise();
      89             : 
      90           1 :         return true;
      91             :     }
      92             : 
      93           1 :     virtual bool tearDownWorld()
      94             :     {
      95           1 :         Threading::TaskManager::Instance().ClearQueue();
      96           1 :         g_ScriptContext.reset();
      97           1 :         SAFE_DELETE(m_ScriptEngine);
      98           1 :         g_Profiler2.Shutdown();
      99             : 
     100           1 :         return true;
     101             :     }
     102             : 
     103         397 :     virtual bool setUp()
     104             :     {
     105             :         // Clean up any JS leftover between tests.
     106         397 :         g_ScriptContext->ShrinkingGC();
     107             : 
     108         397 :         return true;
     109             :     }
     110             : 
     111             : private:
     112             : 
     113             :     // We're doing the initialization and shutdown of the ScriptEngine explicitly here
     114             :     // to make sure it's only initialized when setUpWorld is called.
     115             :     ScriptEngine* m_ScriptEngine;
     116             : };
     117             : 
     118           1 : static LeakReporter leakReporter;
     119           1 : static MiscSetup miscSetup;
     120             : 
     121             : // Definition of functions from lib/self_test.h
     122             : 
     123          18 : bool ts_str_contains(const std::string& str1, const std::string& str2)
     124             : {
     125          18 :     return str1.find(str2) != str1.npos;
     126             : }
     127             : 
     128           0 : bool ts_str_contains(const std::wstring& str1, const std::wstring& str2)
     129             : {
     130           0 :     return str1.find(str2) != str1.npos;
     131             : }
     132             : 
     133             : // we need the (version-controlled) binaries/data directory because it
     134             : // contains input files (it is assumed that developer's machines have
     135             : // write access to those directories). note that argv0 isn't
     136             : // available, so we use sys_ExecutablePathname.
     137         281 : OsPath DataDir()
     138             : {
     139         281 :     return sys_ExecutablePathname().Parent()/".."/"data";
     140             : }
     141             : 
     142             : // Script-based testing setup:
     143             : 
     144             : namespace
     145             : {
     146           0 :     void script_TS_FAIL(const std::wstring& msg)
     147             :     {
     148           0 :         TS_FAIL(utf8_from_wstring(msg).c_str());
     149           0 :     }
     150             : }
     151             : 
     152          76 : void ScriptTestSetup(const ScriptInterface& scriptInterface)
     153             : {
     154         152 :     ScriptRequest rq(scriptInterface);
     155          76 :     ScriptFunction::Register<script_TS_FAIL>(rq, "TS_FAIL");
     156             : 
     157             :     // Load the TS_* function definitions
     158             :     // (We don't use VFS because tests might not have the normal VFS paths loaded)
     159         152 :     OsPath path = DataDir()/"tests"/"test_setup.js";
     160         152 :     std::ifstream ifs(OsString(path).c_str());
     161          76 :     ENSURE(ifs.good());
     162         152 :     std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
     163          76 :     ENSURE(scriptInterface.LoadScript(L"test_setup.js", content));
     164          79 : }

Generated by: LCOV version 1.13