Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_setup.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 <fstream>
28 
29 #include "lib/self_test.h"
30 #include <cxxtest/GlobalFixture.h>
31 
32 #if OS_WIN
34 #endif
35 
36 #include "lib/timer.h"
37 #include "lib/sysdep/sysdep.h"
38 #include "ps/Profiler2.h"
40 
41 class LeakReporter : public CxxTest::GlobalFixture
42 {
43  virtual bool tearDownWorld()
44  {
45  // Shut down JS to prevent leak reports from it
47 
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;
55  }
56 
57  virtual bool setUpWorld()
58  {
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;
65  }
66 
67 };
68 
69 class MiscSetup : public CxxTest::GlobalFixture
70 {
71  virtual bool setUpWorld()
72  {
73  // Timer must be initialised, else things will break when tests do IO
75 
76 #if OS_MACOSX
77  // See comment in GameSetup.cpp FixLocales
78  setlocale(LC_CTYPE, "UTF-8");
79 #endif
80 
82 
84 
85  return true;
86  }
87 
88  virtual bool tearDownWorld()
89  {
91 
92  return true;
93  }
94 };
95 
98 
99 // Definition of functions from lib/self_test.h
100 
101 bool ts_str_contains(const std::wstring& str1, const std::wstring& str2)
102 {
103  return str1.find(str2) != str1.npos;
104 }
105 
106 // we need the (version-controlled) binaries/data directory because it
107 // contains input files (it is assumed that developer's machines have
108 // write access to those directories). note that argv0 isn't
109 // available, so we use sys_ExecutablePathname.
111 {
112  return sys_ExecutablePathname().Parent()/".."/"data";
113 }
114 
115 // Script-based testing setup:
116 
117 namespace
118 {
119  void script_TS_FAIL(void*, std::wstring msg)
120  {
121  TS_FAIL(msg);
122  }
123 }
124 
126 {
127  ifc.RegisterFunction<void, std::wstring, script_TS_FAIL>("TS_FAIL");
128 
129  // Load the TS_* function definitions
130  // (We don't use VFS because tests might not have the normal VFS paths loaded)
131  OsPath path = DataDir()/"tests"/"test_setup.js";
132  std::ifstream ifs(OsString(path).c_str());
133  ENSURE(ifs.good());
134  std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
135  bool ok = ifc.LoadScript(L"test_setup.js", content);
136  ENSURE(ok);
137 }
OsPath DataDir()
Definition: test_setup.cpp:110
void ScriptTestSetup(ScriptInterface &)
Definition: test_setup.cpp:125
void script_TS_FAIL(void *, std::wstring msg)
Definition: test_setup.cpp:119
Path Parent() const
Definition: path.h:150
static MiscSetup miscSetup
Definition: test_setup.cpp:97
static void ShutDown()
Shut down the JS system to clean up memory.
bool LoadScript(const VfsPath &filename, const std::string &code)
Load and execute the given script in a new function scope.
virtual bool setUpWorld()
Definition: test_setup.cpp:57
virtual bool tearDownWorld()
Definition: test_setup.cpp:43
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
void wdbg_heap_Enable(bool enable)
enable or disable manual and automatic heap validity checking.
Definition: wdbg_heap.cpp:41
CProfiler2 g_Profiler2
Definition: Profiler2.cpp:35
Definition: path.h:75
New profiler (complementing the older CProfileManager)
void timer_LatchStartTime()
timer_Time will subsequently return values relative to the current time.
Definition: timer.cpp:74
void SetMainThread()
Set the current thread as the &#39;main&#39; thread.
Definition: ThreadUtil.cpp:35
void Initialise()
Call in main thread to set up the profiler, before calling any other profiler functions.
Definition: Profiler2.cpp:141
bool ts_str_contains(const std::wstring &str1, const std::wstring &str2)
Definition: test_setup.cpp:101
void Shutdown()
Call in main thread to shut everything down.
Definition: Profiler2.cpp:186
OsPath sys_ExecutablePathname()
Definition: bsd.cpp:33
static LeakReporter leakReporter
Definition: test_setup.cpp:96
virtual bool tearDownWorld()
Definition: test_setup.cpp:88
Abstraction around a SpiderMonkey JSContext.
virtual bool setUpWorld()
Definition: test_setup.cpp:71
static std::string OsString(const OsPath &path)
Definition: os_path.h:42