Line data Source code
1 : /* Copyright (C) 2026 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 "lib/self_test.h" 28 : 29 : #include "lib/code_generation.h" 30 : #include "lib/debug.h" 31 : #include "lib/os_path.h" 32 : #include "lib/path.h" 33 : #include "lib/sysdep/compiler.h" 34 : #include "lib/sysdep/os.h" 35 : #include "lib/sysdep/sysdep.h" 36 : #include "lib/timer.h" 37 : #include "lib/utf8.h" 38 : #include "ps/Profiler2.h" 39 : #include "ps/TaskManager.h" 40 : #include "ps/ThreadUtil.h" 41 : #include "scriptinterface/FunctionWrapper.h" 42 : #include "scriptinterface/Context.h" 43 : #include "scriptinterface/Engine.h" 44 : #include "scriptinterface/Interface.h" 45 : #include "scriptinterface/Request.h" 46 : 47 : #include <fstream> 48 : #include <iterator> 49 : #include <memory> 50 : #include <optional> 51 : #include <string> 52 : 53 : class LeakReporter : public CxxTest::GlobalFixture 54 : { 55 1 : virtual bool tearDownWorld() 56 : { 57 1 : return true; 58 : } 59 : 60 1 : virtual bool setUpWorld() 61 : { 62 : #if MSC_VERSION 63 : // (Warning: the allocation numbers seem to differ by 3 when you 64 : // run in the build process vs the debugger) 65 : // _CrtSetBreakAlloc(1952); 66 : #endif 67 1 : return true; 68 : } 69 : 70 : }; 71 : 72 : class MiscSetup : public CxxTest::GlobalFixture 73 : { 74 1 : virtual bool setUpWorld() 75 : { 76 : // Timer must be initialised, else things will break when tests do IO 77 1 : timer_Init(); 78 : 79 : #if OS_MACOSX || OS_BSD 80 : // See comment in GameSetup.cpp FixLocales 81 : setlocale(LC_CTYPE, "UTF-8"); 82 : #endif 83 : 84 1 : Threading::SetMainThread(); 85 : 86 1 : g_Profiler2.Initialise(); 87 1 : m_ScriptEngine = new Script::Engine; 88 1 : g_ScriptContext = std::make_shared<Script::Context>(); 89 : 90 1 : taskManager.emplace(); 91 : 92 1 : return true; 93 : } 94 : 95 1 : virtual bool tearDownWorld() 96 : { 97 1 : taskManager.reset(); 98 1 : g_ScriptContext.reset(); 99 1 : SAFE_DELETE(m_ScriptEngine); 100 1 : g_Profiler2.Shutdown(); 101 : 102 1 : return true; 103 : } 104 : 105 487 : virtual bool setUp() 106 : { 107 : // Clean up any JS leftover between tests. 108 487 : g_ScriptContext->ShrinkingGC(); 109 : 110 487 : return true; 111 : } 112 : 113 : private: 114 : 115 : // We're doing the initialization and shutdown of the Script::Engine explicitly here 116 : // to make sure it's only initialized when setUpWorld is called. 117 : Script::Engine* m_ScriptEngine; 118 : std::optional<Threading::TaskManager> taskManager; 119 : }; 120 : 121 : static LeakReporter leakReporter; 122 : static MiscSetup miscSetup; 123 : 124 : // Definition of functions from lib/self_test.h 125 : 126 40 : bool ts_str_contains(const std::string& str1, const std::string& str2) 127 : { 128 40 : return str1.find(str2) != str1.npos; 129 : } 130 : 131 0 : bool ts_str_contains(const std::wstring& str1, const std::wstring& str2) 132 : { 133 0 : return str1.find(str2) != str1.npos; 134 : } 135 : 136 : // we need the (version-controlled) binaries/data directory because it 137 : // contains input files (it is assumed that developer's machines have 138 : // write access to those directories). note that argv0 isn't 139 : // available, so we use sys_ExecutablePathname. 140 396 : OsPath DataDir() 141 : { 142 1980 : return sys_ExecutablePathname().Parent().Parent()/"data"; 143 : } 144 : 145 : // Script-based testing setup: 146 : 147 : namespace 148 : { 149 0 : void script_TS_FAIL(const std::wstring& msg) 150 : { 151 0 : TS_FAIL(utf8_from_wstring(msg).c_str()); 152 0 : } 153 : } 154 : 155 88 : void ScriptTestSetup(const Script::Interface& scriptInterface) 156 : { 157 88 : Script::Request rq(scriptInterface); 158 88 : Script::Function::Register<script_TS_FAIL>(rq, "TS_FAIL"); 159 : 160 : // Load the TS_* function definitions 161 : // (We don't use VFS because tests might not have the normal VFS paths loaded) 162 352 : OsPath path = DataDir()/"tests"/"test_setup.js"; 163 88 : std::ifstream ifs(OsString(path)); 164 88 : ENSURE(ifs.good()); 165 176 : std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); 166 176 : ENSURE(scriptInterface.LoadScript(L"test_setup.js", content)); 167 176 : }