openclonk/tests/aul/AulTest.cpp

98 lines
2.3 KiB
C++

/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2015-2016, The OpenClonk Team and contributors
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
*
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
* See accompanying file "TRADEMARK" for details.
*
* To redistribute this file separately, substitute the full license texts
* for the above references.
*/
// Testing C4Aul behaviour.
#include <C4Include.h>
#include "AulTest.h"
#include "script/C4ScriptHost.h"
#include "lib/C4Random.h"
#include "object/C4DefList.h"
C4Value AulTest::RunCode(const char *code, bool wrap)
{
class OnScopeExit
{
public:
~OnScopeExit()
{
GameScript.Clear();
ScriptEngine.Clear();
}
} _cleanup;
InitCoreFunctionMap(&ScriptEngine);
FixedRandom(0x40490fdb);
std::string wrapped;
if (wrap)
{
wrapped = "func Main() {\n";
wrapped += code;
wrapped += "\n}\n";
}
else
{
wrapped = code;
}
std::string src("<");
auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
src += test_info->test_case_name();
src += "::";
src += test_info->name();
src += ">";
GameScript.LoadData(src.c_str(), wrapped.c_str(), NULL);
ScriptEngine.Link(&::Definitions);
ScriptEngine.GlobalNamed.SetNameList(&ScriptEngine.GlobalNamedNames);
return GameScript.Call("Main", nullptr, true);
}
C4Value AulTest::RunExpr(const char *expr)
{
std::string code = "return ";
code += expr;
code += ';';
return RunCode(code.c_str());
}
const C4Value AulTest::C4VINT_MIN = C4VInt(-2147483647 - 1);
const C4Value AulTest::C4VINT_MAX = C4VInt(2147483647);
TEST_F(AulTest, ValueReturn)
{
// Make sure primitive value returns work.
EXPECT_EQ(C4VNull, RunCode("return;"));
EXPECT_EQ(C4VNull, RunExpr("nil"));
EXPECT_EQ(C4VTrue, RunExpr("true"));
EXPECT_EQ(C4VFalse, RunExpr("false"));
EXPECT_EQ(C4VInt(42), RunExpr("42"));
EXPECT_EQ(C4VString("Hello World!"), RunExpr("\"Hello World!\""));
// Make sure array returns work.
EXPECT_EQ(C4VArray(), RunExpr("[]"));
EXPECT_EQ(
C4VArray(C4VInt(0), C4VNull, C4VArray(C4VInt(1)), C4VString("Hi")),
RunExpr("[0, nil, [1], \"Hi\"]"));
// Make sure proplist returns work.
EXPECT_EQ(C4VPropList(), RunExpr("{}"));
EXPECT_EQ(
C4VPropList("a", C4VInt(1), "b", C4VArray()),
RunExpr("{\"a\": 1, \"b\"=[]}"));
}