Use @file JSDoc tag for the rmgen library, so that these comments are distinguished...
[0ad.git] / source / test_setup.cpp
blob8b8c40b581ea0ace02cf92c6d6ce5e8e65ef2f6f
1 /* Copyright (C) 2017 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/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.)
49 #if OS_WIN
50 wdbg_heap_Enable(true);
51 #endif
52 return true;
55 virtual bool setUpWorld()
57 #if MSC_VERSION
58 // (Warning: the allocation numbers seem to differ by 3 when you
59 // run in the build process vs the debugger)
60 // _CrtSetBreakAlloc(1952);
61 #endif
62 return true;
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");
77 #endif
79 ThreadUtil::SetMainThread();
81 g_Profiler2.Initialise();
82 m_ScriptEngine = new ScriptEngine;
83 g_ScriptRuntime = ScriptInterface::CreateRuntime();
85 return true;
88 virtual bool tearDownWorld()
90 g_ScriptRuntime.reset();
91 SAFE_DELETE(m_ScriptEngine);
92 g_Profiler2.Shutdown();
94 return true;
97 private:
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.
123 OsPath DataDir()
125 return sys_ExecutablePathname().Parent()/".."/"data";
128 // Script-based testing setup:
130 namespace
132 void script_TS_FAIL(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& msg)
134 TS_FAIL(utf8_from_wstring(msg).c_str());
138 void ScriptTestSetup(const ScriptInterface& scriptinterface)
140 scriptinterface.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());
146 ENSURE(ifs.good());
147 std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
148 bool ok = scriptinterface.LoadScript(L"test_setup.js", content);
149 ENSURE(ok);