2023-06-16 09:44:21 -07:00
|
|
|
/* Copyright (C) 2023 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2007-01-03 19:22:23 -08:00
|
|
|
// Got to be consistent with what the rest of the source files do before
|
|
|
|
|
// including precompiled.h, so that the PCH works correctly
|
|
|
|
|
#ifndef CXXTEST_RUNNING
|
|
|
|
|
#define CXXTEST_RUNNING
|
|
|
|
|
#endif
|
|
|
|
|
#define _CXXTEST_HAVE_STD
|
|
|
|
|
|
2006-12-19 19:09:21 -08:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
#include <fstream>
|
|
|
|
|
|
2010-01-10 03:31:31 -08:00
|
|
|
#include "lib/self_test.h"
|
2006-12-19 19:09:21 -08:00
|
|
|
#include <cxxtest/GlobalFixture.h>
|
|
|
|
|
|
2011-02-23 16:32:38 -08:00
|
|
|
#include "lib/timer.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2011-11-05 16:09:31 -07:00
|
|
|
#include "ps/Profiler2.h"
|
2021-06-03 07:48:38 -07:00
|
|
|
#include "ps/TaskManager.h"
|
2021-03-01 12:52:24 -08:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2016-08-06 08:41:59 -07:00
|
|
|
#include "scriptinterface/ScriptEngine.h"
|
2020-11-14 02:57:50 -08:00
|
|
|
#include "scriptinterface/ScriptContext.h"
|
2020-11-18 03:35:36 -08:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2006-12-19 19:09:21 -08:00
|
|
|
class LeakReporter : public CxxTest::GlobalFixture
|
|
|
|
|
{
|
|
|
|
|
virtual bool tearDownWorld()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool setUpWorld()
|
|
|
|
|
{
|
2008-01-07 16:09:52 -08:00
|
|
|
#if MSC_VERSION
|
2006-12-19 19:09:21 -08:00
|
|
|
// (Warning: the allocation numbers seem to differ by 3 when you
|
|
|
|
|
// run in the build process vs the debugger)
|
|
|
|
|
// _CrtSetBreakAlloc(1952);
|
|
|
|
|
#endif
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2011-07-06 14:19:59 -07:00
|
|
|
class MiscSetup : public CxxTest::GlobalFixture
|
2011-02-23 16:32:38 -08:00
|
|
|
{
|
|
|
|
|
virtual bool setUpWorld()
|
|
|
|
|
{
|
|
|
|
|
// Timer must be initialised, else things will break when tests do IO
|
2020-12-12 00:18:24 -08:00
|
|
|
timer_Init();
|
2011-02-23 16:32:38 -08:00
|
|
|
|
2014-12-21 16:23:07 -08:00
|
|
|
#if OS_MACOSX || OS_BSD
|
2011-07-06 14:19:59 -07:00
|
|
|
// See comment in GameSetup.cpp FixLocales
|
|
|
|
|
setlocale(LC_CTYPE, "UTF-8");
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-01-10 00:39:54 -08:00
|
|
|
Threading::SetMainThread();
|
2012-04-03 11:44:46 -07:00
|
|
|
|
2011-11-05 16:09:31 -07:00
|
|
|
g_Profiler2.Initialise();
|
2016-08-06 08:41:59 -07:00
|
|
|
m_ScriptEngine = new ScriptEngine;
|
2020-11-14 02:57:50 -08:00
|
|
|
g_ScriptContext = ScriptContext::CreateContext();
|
2011-11-05 16:09:31 -07:00
|
|
|
|
2021-06-03 07:48:38 -07:00
|
|
|
Threading::TaskManager::Initialise();
|
|
|
|
|
|
2011-02-23 16:32:38 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-05 16:09:31 -07:00
|
|
|
virtual bool tearDownWorld()
|
|
|
|
|
{
|
2021-06-03 07:48:38 -07:00
|
|
|
Threading::TaskManager::Instance().ClearQueue();
|
2020-11-14 02:57:50 -08:00
|
|
|
g_ScriptContext.reset();
|
2016-08-06 08:41:59 -07:00
|
|
|
SAFE_DELETE(m_ScriptEngine);
|
2011-11-05 16:09:31 -07:00
|
|
|
g_Profiler2.Shutdown();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-08-06 08:41:59 -07:00
|
|
|
|
2020-11-14 07:13:56 -08:00
|
|
|
virtual bool setUp()
|
|
|
|
|
{
|
|
|
|
|
// Clean up any JS leftover between tests.
|
|
|
|
|
g_ScriptContext->ShrinkingGC();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 08:41:59 -07:00
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
// We're doing the initialization and shutdown of the ScriptEngine explicitly here
|
|
|
|
|
// to make sure it's only initialized when setUpWorld is called.
|
|
|
|
|
ScriptEngine* m_ScriptEngine;
|
2011-02-23 16:32:38 -08:00
|
|
|
};
|
|
|
|
|
|
2006-12-19 19:09:21 -08:00
|
|
|
static LeakReporter leakReporter;
|
2011-07-06 14:19:59 -07:00
|
|
|
static MiscSetup miscSetup;
|
2007-03-19 13:58:46 -07:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
// Definition of functions from lib/self_test.h
|
|
|
|
|
|
2015-01-22 12:30:05 -08:00
|
|
|
bool ts_str_contains(const std::string& str1, const std::string& str2)
|
|
|
|
|
{
|
|
|
|
|
return str1.find(str2) != str1.npos;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
bool ts_str_contains(const std::wstring& str1, const std::wstring& str2)
|
2007-03-19 13:58:46 -07:00
|
|
|
{
|
|
|
|
|
return str1.find(str2) != str1.npos;
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// we need the (version-controlled) binaries/data directory because it
|
|
|
|
|
// contains input files (it is assumed that developer's machines have
|
|
|
|
|
// write access to those directories). note that argv0 isn't
|
2011-03-23 09:14:47 -07:00
|
|
|
// available, so we use sys_ExecutablePathname.
|
2011-03-23 06:36:20 -07:00
|
|
|
OsPath DataDir()
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2011-05-06 11:45:30 -07:00
|
|
|
return sys_ExecutablePathname().Parent()/".."/"data";
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Script-based testing setup:
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
void script_TS_FAIL(const std::wstring& msg)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2017-10-13 14:56:45 -07:00
|
|
|
TS_FAIL(utf8_from_wstring(msg).c_str());
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void ScriptTestSetup(const ScriptInterface& scriptInterface)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
ScriptRequest rq(scriptInterface);
|
|
|
|
|
ScriptFunction::Register<script_TS_FAIL>(rq, "TS_FAIL");
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// Load the TS_* function definitions
|
|
|
|
|
// (We don't use VFS because tests might not have the normal VFS paths loaded)
|
2011-05-06 11:45:30 -07:00
|
|
|
OsPath path = DataDir()/"tests"/"test_setup.js";
|
2011-03-23 06:36:20 -07:00
|
|
|
std::ifstream ifs(OsString(path).c_str());
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(ifs.good());
|
2010-01-09 11:20:14 -08:00
|
|
|
std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
2021-03-01 12:52:24 -08:00
|
|
|
ENSURE(scriptInterface.LoadScript(L"test_setup.js", content));
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|