Merge 'remotes/trunk'
[0ad.git] / source / test_setup.cpp
blob8ad8fd09eab2437045e0d933e2f180326a43092f
1 /* Copyright (C) 2014 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 #if OS_WIN
33 #include "lib/sysdep/os/win/wdbg_heap.h"
34 #endif
36 #include "lib/timer.h"
37 #include "lib/sysdep/sysdep.h"
38 #include "ps/Profiler2.h"
39 #include "scriptinterface/ScriptInterface.h"
41 class LeakReporter : public CxxTest::GlobalFixture
43 virtual bool tearDownWorld()
45 // Shut down JS to prevent leak reports from it
46 ScriptInterface::ShutDown();
48 // Enable leak reporting on exit.
49 // (This is done in tearDownWorld so that it doesn't report 'leaks'
50 // if the program is aborted before finishing cleanly.)
51 #if OS_WIN
52 wdbg_heap_Enable(true);
53 #endif
54 return true;
57 virtual bool setUpWorld()
59 #if MSC_VERSION
60 // (Warning: the allocation numbers seem to differ by 3 when you
61 // run in the build process vs the debugger)
62 // _CrtSetBreakAlloc(1952);
63 #endif
64 return true;
69 class MiscSetup : public CxxTest::GlobalFixture
71 virtual bool setUpWorld()
73 // Timer must be initialised, else things will break when tests do IO
74 timer_LatchStartTime();
76 #if OS_MACOSX || OS_BSD
77 // See comment in GameSetup.cpp FixLocales
78 setlocale(LC_CTYPE, "UTF-8");
79 #endif
81 ThreadUtil::SetMainThread();
83 g_Profiler2.Initialise();
84 g_ScriptRuntime = ScriptInterface::CreateRuntime();
86 return true;
89 virtual bool tearDownWorld()
91 g_ScriptRuntime.reset();
92 g_Profiler2.Shutdown();
94 return true;
98 static LeakReporter leakReporter;
99 static MiscSetup miscSetup;
101 // Definition of functions from lib/self_test.h
103 bool ts_str_contains(const std::string& str1, const std::string& str2)
105 return str1.find(str2) != str1.npos;
108 bool ts_str_contains(const std::wstring& str1, const std::wstring& str2)
110 return str1.find(str2) != str1.npos;
113 // we need the (version-controlled) binaries/data directory because it
114 // contains input files (it is assumed that developer's machines have
115 // write access to those directories). note that argv0 isn't
116 // available, so we use sys_ExecutablePathname.
117 OsPath DataDir()
119 return sys_ExecutablePathname().Parent()/".."/"data";
122 // Script-based testing setup:
124 namespace
126 void script_TS_FAIL(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& msg)
128 TS_FAIL(msg);
132 void ScriptTestSetup(ScriptInterface& ifc)
134 ifc.RegisterFunction<void, std::wstring, script_TS_FAIL>("TS_FAIL");
136 // Load the TS_* function definitions
137 // (We don't use VFS because tests might not have the normal VFS paths loaded)
138 OsPath path = DataDir()/"tests"/"test_setup.js";
139 std::ifstream ifs(OsString(path).c_str());
140 ENSURE(ifs.good());
141 std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
142 bool ok = ifc.LoadScript(L"test_setup.js", content);
143 ENSURE(ok);