Merge 'remotes/trunk'
[0ad.git] / source / test_setup.cpp
blob71fd465d6ea22c53f40bc1e0b840026bbe73cd48
1 /* Copyright (C) 2023 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
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
25 #include "precompiled.h"
27 #include <fstream>
29 #include "lib/self_test.h"
30 #include <cxxtest/GlobalFixture.h>
32 #include "lib/timer.h"
33 #include "lib/sysdep/sysdep.h"
34 #include "ps/Profiler2.h"
35 #include "ps/TaskManager.h"
36 #include "scriptinterface/FunctionWrapper.h"
37 #include "scriptinterface/ScriptEngine.h"
38 #include "scriptinterface/ScriptContext.h"
39 #include "scriptinterface/ScriptInterface.h"
41 class LeakReporter : public CxxTest::GlobalFixture
43 virtual bool tearDownWorld()
45 return true;
48 virtual bool setUpWorld()
50 #if MSC_VERSION
51 // (Warning: the allocation numbers seem to differ by 3 when you
52 // run in the build process vs the debugger)
53 // _CrtSetBreakAlloc(1952);
54 #endif
55 return true;
60 class MiscSetup : public CxxTest::GlobalFixture
62 virtual bool setUpWorld()
64 // Timer must be initialised, else things will break when tests do IO
65 timer_Init();
67 #if OS_MACOSX || OS_BSD
68 // See comment in GameSetup.cpp FixLocales
69 setlocale(LC_CTYPE, "UTF-8");
70 #endif
72 Threading::SetMainThread();
74 g_Profiler2.Initialise();
75 m_ScriptEngine = new ScriptEngine;
76 g_ScriptContext = ScriptContext::CreateContext();
78 Threading::TaskManager::Initialise();
80 return true;
83 virtual bool tearDownWorld()
85 Threading::TaskManager::Instance().ClearQueue();
86 g_ScriptContext.reset();
87 SAFE_DELETE(m_ScriptEngine);
88 g_Profiler2.Shutdown();
90 return true;
93 virtual bool setUp()
95 // Clean up any JS leftover between tests.
96 g_ScriptContext->ShrinkingGC();
98 return true;
101 private:
103 // We're doing the initialization and shutdown of the ScriptEngine explicitly here
104 // to make sure it's only initialized when setUpWorld is called.
105 ScriptEngine* m_ScriptEngine;
108 static LeakReporter leakReporter;
109 static MiscSetup miscSetup;
111 // Definition of functions from lib/self_test.h
113 bool ts_str_contains(const std::string& str1, const std::string& str2)
115 return str1.find(str2) != str1.npos;
118 bool ts_str_contains(const std::wstring& str1, const std::wstring& str2)
120 return str1.find(str2) != str1.npos;
123 // we need the (version-controlled) binaries/data directory because it
124 // contains input files (it is assumed that developer's machines have
125 // write access to those directories). note that argv0 isn't
126 // available, so we use sys_ExecutablePathname.
127 OsPath DataDir()
129 return sys_ExecutablePathname().Parent()/".."/"data";
132 // Script-based testing setup:
134 namespace
136 void script_TS_FAIL(const std::wstring& msg)
138 TS_FAIL(utf8_from_wstring(msg).c_str());
142 void ScriptTestSetup(const ScriptInterface& scriptInterface)
144 ScriptRequest rq(scriptInterface);
145 ScriptFunction::Register<script_TS_FAIL>(rq, "TS_FAIL");
147 // Load the TS_* function definitions
148 // (We don't use VFS because tests might not have the normal VFS paths loaded)
149 OsPath path = DataDir()/"tests"/"test_setup.js";
150 std::ifstream ifs(OsString(path).c_str());
151 ENSURE(ifs.good());
152 std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
153 ENSURE(scriptInterface.LoadScript(L"test_setup.js", content));