1 /* Copyright (C) 2016 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
23 #define _CXXTEST_HAVE_STD
25 #include "precompiled.h"
29 #include "lib/self_test.h"
30 #include <cxxtest/GlobalFixture.h>
33 #include "lib/sysdep/os/win/wdbg_heap.h"
36 #include "lib/timer.h"
37 #include "lib/sysdep/sysdep.h"
38 #include "ps/Profiler2.h"
39 #include "scriptinterface/ScriptEngine.h"
40 #include "scriptinterface/ScriptInterface.h"
42 class LeakReporter
: public CxxTest::GlobalFixture
44 virtual bool tearDownWorld()
46 // Enable leak reporting on exit.
47 // (This is done in tearDownWorld so that it doesn't report 'leaks'
48 // if the program is aborted before finishing cleanly.)
50 wdbg_heap_Enable(true);
55 virtual bool setUpWorld()
58 // (Warning: the allocation numbers seem to differ by 3 when you
59 // run in the build process vs the debugger)
60 // _CrtSetBreakAlloc(1952);
67 class MiscSetup
: public CxxTest::GlobalFixture
69 virtual bool setUpWorld()
71 // Timer must be initialised, else things will break when tests do IO
72 timer_LatchStartTime();
74 #if OS_MACOSX || OS_BSD
75 // See comment in GameSetup.cpp FixLocales
76 setlocale(LC_CTYPE
, "UTF-8");
79 ThreadUtil::SetMainThread();
81 g_Profiler2
.Initialise();
82 m_ScriptEngine
= new ScriptEngine
;
83 g_ScriptRuntime
= ScriptInterface::CreateRuntime();
88 virtual bool tearDownWorld()
90 g_ScriptRuntime
.reset();
91 SAFE_DELETE(m_ScriptEngine
);
92 g_Profiler2
.Shutdown();
99 // We're doing the initialization and shutdown of the ScriptEngine explicitly here
100 // to make sure it's only initialized when setUpWorld is called.
101 ScriptEngine
* m_ScriptEngine
;
104 static LeakReporter leakReporter
;
105 static MiscSetup miscSetup
;
107 // Definition of functions from lib/self_test.h
109 bool ts_str_contains(const std::string
& str1
, const std::string
& str2
)
111 return str1
.find(str2
) != str1
.npos
;
114 bool ts_str_contains(const std::wstring
& str1
, const std::wstring
& str2
)
116 return str1
.find(str2
) != str1
.npos
;
119 // we need the (version-controlled) binaries/data directory because it
120 // contains input files (it is assumed that developer's machines have
121 // write access to those directories). note that argv0 isn't
122 // available, so we use sys_ExecutablePathname.
125 return sys_ExecutablePathname().Parent()/".."/"data";
128 // Script-based testing setup:
132 void script_TS_FAIL(ScriptInterface::CxPrivate
* UNUSED(pCxPrivate
), const std::wstring
& msg
)
138 void ScriptTestSetup(ScriptInterface
& ifc
)
140 ifc
.RegisterFunction
<void, std::wstring
, script_TS_FAIL
>("TS_FAIL");
142 // Load the TS_* function definitions
143 // (We don't use VFS because tests might not have the normal VFS paths loaded)
144 OsPath path
= DataDir()/"tests"/"test_setup.js";
145 std::ifstream
ifs(OsString(path
).c_str());
147 std::string
content((std::istreambuf_iterator
<char>(ifs
)), std::istreambuf_iterator
<char>());
148 bool ok
= ifc
.LoadScript(L
"test_setup.js", content
);