Cleanup and improve

This commit is contained in:
Lukas Aldershaab 2023-04-06 15:28:09 +02:00
parent 7c67c2c326
commit 1d28ddf734
49 changed files with 26013 additions and 30304 deletions

View file

@ -177,7 +177,7 @@ TEST(Script, Basic_Global_Variable_Tests)
TEST(Script, Variable_Chaining_And_Usage)
{
ConsoleValue value = RunScript(R"(
function t()
function t()
{
%a = %b = 2;
return %a;
@ -188,7 +188,7 @@ TEST(Script, Variable_Chaining_And_Usage)
ASSERT_EQ(value.getInt(), 2);
ConsoleValue valueGlobal = RunScript(R"(
function t()
function t()
{
$a = %b = 2;
}
@ -199,7 +199,7 @@ TEST(Script, Variable_Chaining_And_Usage)
ASSERT_EQ(valueGlobal.getInt(), 2);
ConsoleValue value2 = RunScript(R"(
function t(%a)
function t(%a)
{
if ((%b = 2 * %a) != 5)
return %b;
@ -281,8 +281,8 @@ TEST(Script, Basic_Loop_Statements)
ASSERT_EQ(whileValue.getInt(), 5);
ConsoleValue forValue = RunScript(R"(
function t(%times)
{
function t(%times)
{
%result = "";
for (%i = 0; %i < %times; %i++)
%result = %result @ "a";
@ -295,8 +295,8 @@ TEST(Script, Basic_Loop_Statements)
ASSERT_STREQ(forValue.getString(), "aaa");
ConsoleValue forReverseLoop = RunScript(R"(
function t(%times)
{
function t(%times)
{
%result = "";
for (%i = %times - 1; %i >= 0; %i--)
%result = %result @ "b";
@ -317,9 +317,9 @@ TEST(Script, Basic_Loop_Statements)
%loopValue = %i;
if (%str $= "")
if (%str $= "")
%str = %loopValue;
else
else
%str = %str @ "," SPC %loopValue;
}
return %str;
@ -547,7 +547,7 @@ TEST(Script, SimObject_Tests)
ConsoleValue object = RunScript(R"(
return new SimObject(FudgeCollector)
{
fudge = "Chocolate";
fudge = "Chocolate";
};
)");
@ -688,7 +688,7 @@ TEST(Script, SimObject_Tests)
function Bar::doTheAddition(%this)
{
return %this.testClass() + %this.test() + %this.doSuperTest();
return %this.testClass() + %this.test() + %this.doSuperTest();
}
return Bar.doTheAddition();
@ -757,7 +757,7 @@ TEST(Script, Internal_Name)
return %val;
}
return a();
return a();
)");
ASSERT_EQ(recursiveValue.getInt(), 12);
@ -983,7 +983,7 @@ TEST(Script, MiscRegressions)
ASSERT_EQ(regression1.getInt(), 200);
ConsoleValue regression2 = RunScript(R"(
new SimObject(TheRegressionObject2)
new SimObject(TheRegressionObject2)
{
extent = "100 200";
};
@ -1001,6 +1001,10 @@ TEST(Script, MiscRegressions)
ASSERT_EQ(regression2.getInt(), 400);
ConsoleValue regression3 = RunScript(R"(
if(!isObject(GuiDefaultProfile))
new GuiControlProfile (GuiDefaultProfile) {};
if(!isObject(GuiTooltipProfile))
new GuiControlProfile (GuiTooltipProfile) {};
function doTest()
{
%button = new GuiIconButtonCtrl()
@ -1022,7 +1026,7 @@ TEST(Script, MiscRegressions)
)");
ASSERT_STREQ(regression3.getString(), "120 20");
ConsoleValue regression4 = RunScript(R"(
function doTest()
{
@ -1038,7 +1042,7 @@ TEST(Script, MiscRegressions)
}
return doTest();
)");
ASSERT_EQ(regression4.getFloat(), 0.5);
ConsoleValue regression5 = RunScript(R"(
@ -1068,7 +1072,7 @@ TEST(Script, MiscRegressions)
if (%obj.isMethod(%function))
{
%line = "abcdefg";
%output = %obj.call(%function, %line);
%output = %obj.call(%function, %line);
}
}

View file

@ -1,6 +1,5 @@
#if 0
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/platform.h"
#include "console/simBase.h"
@ -9,12 +8,16 @@
#include "console/engineAPI.h"
#include "math/mMath.h"
#include "console/stringStack.h"
#include "console/consoleInternal.h"
// Stupid globals not declared in a header
extern ExprEvalState gEvalState;
TEST(Con, executef)
{
char buffer[128];
Con::evaluate("if (isObject(TestConExec)) {\r\nTestConExec.delete();\r\n}\r\nfunction testExecutef(%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k){return %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k;}\r\nfunction TestConExec::testThisFunction(%this,%a,%b,%c,%d,%e,%f,%g,%h,%i,%j){ return %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j;}\r\nnew ScriptObject(TestConExec);\r\n", false, "test");
SimObject *testObject = NULL;
Sim::findObject("TestConExec", testObject);
@ -22,163 +25,162 @@ TEST(Con, executef)
<< "TestConExec object should exist";
// Check basic calls with SimObject. We'll do this for every single possible call just to make sure.
const char *returnValue = NULL;
ConsoleValue returnValue;
returnValue = Con::executef(testObject, "testThisFunction");
EXPECT_TRUE(dStricmp(returnValue, " ") == 0) <<
EXPECT_STREQ(returnValue, " ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a");
EXPECT_TRUE(dStricmp(returnValue, "a ") == 0) <<
EXPECT_STREQ(returnValue, "a ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b");
EXPECT_TRUE(dStricmp(returnValue, "a b ") == 0) <<
EXPECT_STREQ(returnValue, "a b ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c");
EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) <<
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d");
EXPECT_TRUE(dStricmp(returnValue, "a b c d ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e f ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e f ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f", "g");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e f g ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f", "g", "h");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e f g h ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f", "g", "h", "i");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h i ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e f g h i ") <<
"All values should be printed in the correct order";
// Now test without the object
returnValue = Con::executef("testExecutef");
EXPECT_TRUE(dStricmp(returnValue, " ") == 0) <<
EXPECT_STREQ(returnValue, " ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a");
EXPECT_TRUE(dStricmp(returnValue, "a ") == 0) <<
EXPECT_STREQ(returnValue, "a ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b");
EXPECT_TRUE(dStricmp(returnValue, "a b ") == 0) <<
EXPECT_STREQ(returnValue, "a b ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c");
EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) <<
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c", "d");
EXPECT_TRUE(dStricmp(returnValue, "a b c d ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e f ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e f ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e f g ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e f g h ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h", "i");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h i ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e f g h i ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h i j ") == 0) <<
EXPECT_STREQ(returnValue, "a b c d e f g h i j ") <<
"All values should be printed in the correct order";
// Test type conversions with and without SimObject...
// Integer
returnValue = Con::executef(testObject, "testThisFunction", 123);
EXPECT_TRUE(dStricmp(returnValue, "123 ") == 0) <<
EXPECT_STREQ(returnValue, "123 ") <<
"Integer should be converted";
returnValue = Con::executef("testExecutef", 123);
EXPECT_TRUE(dStricmp(returnValue, "123 ") == 0) <<
EXPECT_STREQ(returnValue, "123 ") <<
"Integer should be converted";
// Float
returnValue = Con::executef(testObject, "testThisFunction", (F32)123.4);
EXPECT_TRUE(dStricmp(returnValue, "123.4 ") == 0) <<
returnValue = Con::executef(testObject, "testThisFunction", (F32)123.0);
EXPECT_STREQ(returnValue, "123 ") <<
"Float should be converted";
returnValue = Con::executef("testExecutef", (F32)123.4);
EXPECT_TRUE(dStricmp(returnValue, "123.4 ") == 0) <<
returnValue = Con::executef("testExecutef", (F32)123.0);
EXPECT_STREQ(returnValue, "123 ") <<
"Float should be converted";
// SimObject
dSprintf(buffer, sizeof(buffer), "%i ", testObject->getId());
returnValue = Con::executef(testObject, "testThisFunction", testObject);
EXPECT_TRUE(dStricmp(returnValue, buffer) == 0) <<
EXPECT_STREQ(returnValue, buffer) <<
"SimObject should be converted";
dSprintf(buffer, sizeof(buffer), "%i ", testObject->getId());
returnValue = Con::executef("testExecutef", testObject);
EXPECT_TRUE(dStricmp(returnValue, buffer) == 0) <<
EXPECT_STREQ(returnValue, buffer) <<
"SimObject should be converted";
// Point3F
Point3F point(1,2,3);
returnValue = Con::executef(testObject, "testThisFunction", point);
EXPECT_TRUE(dStricmp(returnValue, "1 2 3 ") == 0) <<
EXPECT_STREQ(returnValue, "1 2 3 ") <<
"Point3F should be converted";
returnValue = Con::executef("testExecutef", point);
EXPECT_TRUE(dStricmp(returnValue, "1 2 3 ") == 0) <<
EXPECT_STREQ(returnValue, "1 2 3 ") <<
"Point3F should be converted";
// Finally test the function arg offset. This should be consistently 0 after each call
EXPECT_TRUE(STR.mFunctionOffset == 0) <<
EXPECT_EQ(STR.mFunctionOffset, 0) <<
"Function offset should be 0";
const char *floatArg = Con::getFloatArg(1.23);
EXPECT_TRUE(STR.mFunctionOffset > 0) <<
EXPECT_GT(STR.mFunctionOffset, 0) <<
"Function offset should not be 0";
Con::executef("testExecutef", floatArg);
EXPECT_TRUE(STR.mFunctionOffset == 0) <<
EXPECT_EQ(STR.mFunctionOffset, 0) <<
"Function offset should be 0";
floatArg = Con::getFloatArg(1.23);
EXPECT_TRUE(STR.mFunctionOffset > 0) <<
EXPECT_GT(STR.mFunctionOffset, 0) <<
"Function offset should not be 0";
Con::executef("testImaginaryFunction_", floatArg);
EXPECT_TRUE(STR.mFunctionOffset == 0) <<
EXPECT_EQ(STR.mFunctionOffset, 0) <<
"Function offset should be 0";
}
TEST(Con, execute)
{
Con::evaluate("if (isObject(TestConExec)) {\r\nTestConExec.delete();\r\n}\r\nfunction testScriptExecuteFunction(%a,%b) {return %a SPC %b;}\nfunction TestConExec::testScriptExecuteFunction(%this, %a,%b) {return %a SPC %b;}new ScriptObject(TestConExec);\r\n", false, "testExecute");
U32 startStackPos = CSTK.mStackPos;
U32 startStackPos = gEvalState.getStackDepth();
U32 startStringStackPos = STR.mStart;
U32 startStackFrame = CSTK.mFrame;
SimObject *testObject = NULL;
Sim::findObject("TestConExec", testObject);
@ -188,71 +190,21 @@ TEST(Con, execute)
// const char* versions of execute should maintain stack
const char *argv[] = {"testScriptExecuteFunction", "1", "2"};
const char *argvObject[] = {"testScriptExecuteFunction", "", "1", "2"};
const char *returnValue = Con::execute(3, argv);
ConsoleValue returnValue = Con::execute(3, argv);
EXPECT_TRUE(dStricmp(returnValue, "1 2") == 0) <<
EXPECT_STREQ(returnValue, "1 2") <<
"execute should return 1 2";
EXPECT_TRUE(CSTK.mStackPos == startStackPos) <<
EXPECT_EQ(gEvalState.getStackDepth(), startStackPos) <<
"execute should restore stack";
returnValue = Con::execute(testObject, 4, argvObject);
EXPECT_TRUE(dStricmp(returnValue, "1 2") == 0) <<
EXPECT_STREQ(returnValue, "1 2") <<
"execute should return 1 2";
EXPECT_TRUE(CSTK.mStackPos == startStackPos) <<
EXPECT_EQ(gEvalState.getStackDepth(), startStackPos) <<
"execute should restore stack";
// ConsoleValueRef versions of execute should not restore stack
CSTK.pushFrame();
STR.pushFrame();
ConsoleValue valueArg[4];
ConsoleValueRef refArg[4];
ConsoleValue valueArgObject[4];
ConsoleValueRef refArgObject[4];
for (U32 i=0; i<4; i++)
{
refArg[i].value = &valueArg[i];
refArgObject[i].value = &valueArgObject[i];
valueArgObject[i].init();
valueArg[i].init();
}
refArg[0] = "testScriptExecuteFunction";
refArg[1] = "1";
refArg[2] = "2";
refArgObject[0] = "testScriptExecuteFunction";
refArgObject[2] = "1";
refArgObject[3] = "2";
returnValue = Con::execute(3, refArg);
EXPECT_TRUE(dStricmp(returnValue, "1 2") == 0) <<
"execute should return 1 2";
EXPECT_TRUE(CSTK.mStackPos == startStackPos) <<
"execute should restore stack";
CSTK.popFrame();
STR.popFrame();
CSTK.pushFrame();
STR.pushFrame();
returnValue = Con::execute(testObject, 4, refArgObject);
EXPECT_TRUE(dStricmp(returnValue, "1 2") == 0) <<
"execute should return 1 2";
EXPECT_TRUE(CSTK.mStackPos == startStackPos) <<
"execute should restore stack";
CSTK.popFrame();
STR.popFrame();
}
#endif
#endif

View file

@ -1,4 +1,3 @@
#if 0
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/platform.h"
@ -13,78 +12,86 @@ TEST(EngineAPI, EngineMarshallData)
{
// Reserve some values
ConsoleValue values[16];
ConsoleValueRef refValues[16];
for (U32 i=0; i<16; i++)
{
values[i].init();
refValues[i].value = &values[i];
}
// Basic string casting...
SimObject *foo = new SimObject();
foo->registerObject();
const char *value = EngineMarshallData(foo);
EXPECT_TRUE(dStricmp(value, foo->getIdString()) == 0)
EXPECT_STREQ(value, foo->getIdString())
<< "SimObject should be casted to its ID";
U32 unsignedNumber = 123;
S32 signedNumber = -123;
value = EngineMarshallData(unsignedNumber);
EXPECT_TRUE(dStricmp(value, "123") == 0)
EXPECT_STREQ(value, "123")
<< "Integer should be converted to 123";
value = EngineMarshallData(signedNumber);
EXPECT_TRUE(dStricmp(value, "-123") == 0)
EXPECT_STREQ(value, "-123")
<< "Integer should be converted to -123";
bool boolValue = true;
value = EngineMarshallData(boolValue);
EXPECT_TRUE(dStricmp(value, "1") == 0)
EXPECT_STREQ(value, "1")
<< "Bool should be converted to 1";
Point3F point(1,2,3);
value = EngineMarshallData(point);
EXPECT_TRUE(dStricmp(value, "1 2 3") == 0)
EXPECT_STREQ(value, "1 2 3")
<< "Point3F should be converted to 1 2 3";
F32 floatValue = 1.23f;
value = EngineMarshallData(floatValue);
EXPECT_TRUE(dStricmp(value, "1.23") == 0)
EXPECT_STREQ(value, "1.23")
<< "F32 should be converted to 1.23";
// Argv based casting
S32 argc = 0;
EngineMarshallData(foo, argc, refValues);
EngineMarshallData((const SimObject*)foo, argc, refValues);
EngineMarshallData(point, argc, refValues);
EngineMarshallData(unsignedNumber, argc, refValues);
EngineMarshallData(signedNumber, argc, refValues);
EngineMarshallData(boolValue, argc, refValues);
EngineMarshallData(floatValue, argc, refValues);
EngineMarshallData(foo, argc, values);
EngineMarshallData((const SimObject*)foo, argc, values);
EngineMarshallData(point, argc, values);
EngineMarshallData(unsignedNumber, argc, values);
EngineMarshallData(signedNumber, argc, values);
EngineMarshallData(boolValue, argc, values);
EngineMarshallData(floatValue, argc, values);
EXPECT_TRUE(argc == 7)
EXPECT_EQ(argc, 7)
<< "7 args should have been set";
EXPECT_TRUE(values[0].type == ConsoleValue::TypeInternalInt && values[0].getSignedIntValue() == foo->getId())
EXPECT_EQ(values[0].getType(), ConsoleValueType::cvInteger)
<< "1st arg should be an int";
EXPECT_EQ(values[0].getInt(), foo->getId())
<< "1st arg should be foo's id";
EXPECT_TRUE(values[1].type == ConsoleValue::TypeInternalInt && values[1].getSignedIntValue() == foo->getId())
EXPECT_EQ(values[1].getType(), ConsoleValueType::cvInteger)
<< "2nd arg should be an int";
EXPECT_EQ(values[1].getInt(), foo->getId())
<< "2nd arg should be foo's id";
EXPECT_TRUE(values[2].type == ConsoleValue::TypeInternalString && dStricmp(values[2].getStringValue(), "1 2 3") == 0)
EXPECT_EQ(values[2].getType(), ConsoleValueType::cvString)
<< "3rd arg should be a string";
EXPECT_STREQ(values[2].getString(), "1 2 3")
<< "3rd arg should be 1 2 3";
EXPECT_TRUE(values[3].type == ConsoleValue::TypeInternalFloat && values[3].getSignedIntValue() == 123)
EXPECT_EQ(values[3].getType(), ConsoleValueType::cvInteger)
<< "4th arg should be a float";
EXPECT_EQ(values[3].getInt(), 123)
<< "4th arg should be 123";
EXPECT_TRUE(values[4].type == ConsoleValue::TypeInternalFloat && values[4].getSignedIntValue() == -123)
EXPECT_EQ(values[4].getType(), ConsoleValueType::cvInteger)
<< "5th arg should be a float";
EXPECT_EQ(values[4].getInt(), -123)
<< "5th arg should be -123";
EXPECT_TRUE(values[5].type == ConsoleValue::TypeInternalFloat && values[5].getBoolValue() == true)
<< "6th arg should be -123";
EXPECT_TRUE(values[6].type == ConsoleValue::TypeInternalFloat && mRound(values[6].getFloatValue() * 100) == 123)
EXPECT_EQ(values[5].getType(), ConsoleValueType::cvInteger)
<< "6th arg should be a float";
EXPECT_TRUE(values[5].getBool())
<< "6th arg should be true";
EXPECT_EQ(values[6].getType(), ConsoleValueType::cvFloat)
<< "7th arg should be a float";
EXPECT_FLOAT_EQ(values[6].getFloat(), 1.23)
<< "7th arg should be 1.23";
foo->deleteObject();
@ -97,7 +104,7 @@ TEST(EngineAPI, EngineUnMarshallData)
SimObject *testFoo = EngineUnmarshallData<SimObject*>()(foo->getIdString());
EXPECT_TRUE(foo == testFoo)
EXPECT_EQ(foo, testFoo)
<< "Unmarshalling foo's id should return foo";
testFoo = EngineUnmarshallData<SimObject*>()("ShouldNotExist_Really123");
@ -110,20 +117,20 @@ TEST(EngineAPI, EngineUnMarshallData)
TEST(EngineAPI, _EngineConsoleCallbackHelper)
{
Con::evaluate("if (isObject(TestConExec)) {\r\nTestConExec.delete();\r\n}\r\nfunction testExecutef(%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k){return %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k;}\r\nfunction TestConExec::testThisFunction(%this,%a,%b,%c,%d,%e,%f,%g,%h,%i,%j){ return %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j;}\r\nnew ScriptObject(TestConExec);\r\n", false, "test");
SimObject *testObject = NULL;
Sim::findObject("TestConExec", testObject);
_EngineConsoleCallbackHelper helper("testExecutef", NULL);
const char *returnValue = helper.call<const char*>("a", "b", "c");
ConsoleValue returnValue = helper.call<ConsoleValue>("a", "b", "c");
EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) <<
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
_EngineConsoleCallbackHelper objectHelper("testThisFunction", testObject);
returnValue = helper.call<const char*>("a", "b", "c");
EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) <<
_EngineConsoleCallbackHelper objectHelper("testThisFunction", testObject);
returnValue = helper.call<ConsoleValue>("a", "b", "c");
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
}
@ -131,22 +138,21 @@ TEST(EngineAPI, _EngineConsoleCallbackHelper)
TEST(EngineAPI, _EngineConsoleExecCallbackHelper)
{
Con::evaluate("if (isObject(TestConExec)) {\r\nTestConExec.delete();\r\n}\r\nfunction testExecutef(%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k){return %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k;}\r\nfunction TestConExec::testThisFunction(%this,%a,%b,%c,%d,%e,%f,%g,%h,%i,%j){ return %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j;}\r\nnew ScriptObject(TestConExec);\r\n", false, "test");
SimObject *testObject = NULL;
Sim::findObject("TestConExec", testObject);
_EngineConsoleExecCallbackHelper<const char*> helper("testExecutef");
const char *returnValue = helper.call<const char*>("a", "b", "c");
ConsoleValue returnValue = helper.call<ConsoleValue>("a", "b", "c");
EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) <<
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
_EngineConsoleExecCallbackHelper<SimObject*> objectHelper(testObject);
returnValue = objectHelper.call<const char*>("testThisFunction", "a", "b", "c");
EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) <<
_EngineConsoleExecCallbackHelper<SimObject*> objectHelper(testObject);
returnValue = objectHelper.call<ConsoleValue>("testThisFunction", "a", "b", "c");
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
}
#endif
#endif

View file

@ -30,7 +30,7 @@ struct TcpHandle
NetSocket mSocket;
S32 mDataReceived;
void notify(NetSocket sock, U32 state)
void notify(NetSocket sock, U32 state)
{
// Only consider our own socket.
if(mSocket != sock)
@ -79,8 +79,8 @@ TEST(Net, TCPRequest)
Net::smConnectionNotify ->notify(&handler, &TcpHandle::notify);
Net::smConnectionReceive->notify(&handler, &TcpHandle::receive);
// Open a TCP connection to garagegames.com
handler.mSocket = Net::openConnectTo("72.246.107.193:80");
// Open a TCP connection to torque3d.org
handler.mSocket = Net::openConnectTo("108.61.193.195:80");
const U32 limit = Platform::getRealMilliseconds() + (5*1000);
while(Process::processEvents() && (Platform::getRealMilliseconds() < limit) ) {}
@ -142,8 +142,8 @@ struct JournalHandle
Net::smConnectionNotify ->notify(this, &JournalHandle::notify);
Net::smConnectionReceive->notify(this, &JournalHandle::receive);
// Open a TCP connection to garagegames.com
mSocket = Net::openConnectTo("72.246.107.193:80");
// Open a TCP connection to torque3d.org
mSocket = Net::openConnectTo("108.61.193.195:80");
// Let the callbacks enable things to process.
while(Process::processEvents()) {}

View file

@ -22,12 +22,14 @@
#ifdef TORQUE_TESTS_ENABLED
#include "console/console.h"
#include "console/codeBlock.h"
#include "console/engineAPI.h"
#include "console/consoleInternal.h"
#include "unitTesting.h"
#include "memoryTester.h"
#include <gtest/gtest-all.cc>
#include <gtest/src/gtest-all.cc>
//-----------------------------------------------------------------------------
@ -76,7 +78,67 @@ public:
TorqueUnitTestListener( bool verbose ) : mVerbose( verbose ) {}
};
DefineEngineFunction( runAllUnitTests, int, (const char* testSpecs), (""),
class TorqueScriptFixture : public testing::Test {};
class TorqueScriptTest : public TorqueScriptFixture {
public:
explicit TorqueScriptTest(const char* pFunctionName) : mFunctionName(pFunctionName) {}
void TestBody() override
{
Con::executef(mFunctionName);
}
private:
const char* mFunctionName;
};
DefineEngineFunction( addUnitTest, void, (const char* function),,
"Add a TorqueScript function as a GTest unit test.\n"
"@note This is only implemented rudimentarily to open the door for future development in unit-testing the engine.\n"
"@tsexample\n"
"function MyTest() {\n"
" expectTrue(2+2 == 4, \"basic math should work\");\n"
"}\n"
"addUnitTest(MyTest);\n"
"@endtsexample\n"
"@see expectTrue")
{
Namespace::Entry* entry = Namespace::global()->lookup(StringTable->insert(function));
const char* file = __FILE__;
U32 ln = __LINE__;
if (entry != NULL)
{
file = entry->mCode->name;
U32 inst;
entry->mCode->findBreakLine(entry->mFunctionOffset, ln, inst);
}
else
{
Con::warnf("failed to register unit test %s, could not find the function", function);
}
testing::RegisterTest("TorqueScriptFixture", function, NULL, NULL, file, ln,
[=]() -> TorqueScriptFixture* { return new TorqueScriptTest(function); });
}
String scriptFileMessage(const char* message)
{
Dictionary* frame = &gEvalState.getCurrentFrame();
CodeBlock* code = frame->code;
const char* scriptLine = code->getFileLine(frame->ip);
return String::ToString("at %s: %s", scriptLine, message);
}
DefineEngineFunction( expectTrue, void, (bool test, const char* message),(""),
"TorqueScript wrapper around the EXPECT_TRUE assertion in GTest.\n"
"@tsexample\n"
"expectTrue(2+2 == 4, \"basic math should work\");\n"
"@endtsexample")
{
EXPECT_TRUE(test) << scriptFileMessage(message).c_str();
}
DefineEngineFunction( runAllUnitTests, int, (const char* testSpecs, const char* reportFormat), (""),
"Runs engine unit tests. Some tests are marked as 'stress' tests which do not "
"necessarily check correctness, just performance or possible nondeterministic "
"glitches. There may also be interactive or networking tests which may be "
@ -89,22 +151,27 @@ DefineEngineFunction( runAllUnitTests, int, (const char* testSpecs), (""),
"and http://stackoverflow.com/a/14021997/945863 "
"for a description of the flag format.")
{
S32 testArgc = 0;
char** testArgv = NULL;
Vector<char*> args;
args.push_back(NULL); // Program name is unused by googletest.
String specsArg;
if ( dStrlen( testSpecs ) > 0 )
{
String specs(testSpecs);
specs.replace(' ', ':');
specs.insert(0, "--gtest_filter=");
testArgc = 2;
testArgv = new char*[2];
testArgv[0] = NULL; // Program name is unused by googletest.
testArgv[1] = new char[specs.size()];
dStrcpy(testArgv[1], specs, specs.size());
specsArg = testSpecs;
specsArg.replace(' ', ':');
specsArg.insert(0, "--gtest_filter=");
args.push_back(const_cast<char*>(specsArg.c_str()));
}
String reportFormatArg;
if ( dStrlen(reportFormat) > 0 )
{
reportFormatArg = String::ToString("--gtest_output=%s", reportFormat);
args.push_back(const_cast<char*>(reportFormatArg.c_str()));
}
S32 argc = args.size();
// Initialize Google Test.
testing::InitGoogleTest( &testArgc, testArgv );
testing::InitGoogleTest( &argc, args.address() );
// Fetch the unit test instance.
testing::UnitTest& unitTest = *testing::UnitTest::GetInstance();