separate testing environment

-Separate main for running unit tests
-Move unit tests into testing folder
This commit is contained in:
marauder2k7 2023-07-24 12:38:36 +01:00
parent 2e8f5795fa
commit c09f79d199
265 changed files with 84537 additions and 334 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,210 +0,0 @@
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/platform.h"
#include "console/simBase.h"
#include "console/consoleTypes.h"
#include "console/simBase.h"
#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);
EXPECT_TRUE(testObject != NULL)
<< "TestConExec object should exist";
// Check basic calls with SimObject. We'll do this for every single possible call just to make sure.
ConsoleValue returnValue;
returnValue = Con::executef(testObject, "testThisFunction");
EXPECT_STREQ(returnValue, " ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a");
EXPECT_STREQ(returnValue, "a ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b");
EXPECT_STREQ(returnValue, "a b ") <<
"All values should be printed in the correct order";
returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c");
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_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_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_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_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_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_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_STREQ(returnValue, " ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a");
EXPECT_STREQ(returnValue, "a ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b");
EXPECT_STREQ(returnValue, "a b ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c");
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
returnValue = Con::executef("testExecutef", "a", "b", "c", "d");
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_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_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_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_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_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_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_STREQ(returnValue, "123 ") <<
"Integer should be converted";
returnValue = Con::executef("testExecutef", 123);
EXPECT_STREQ(returnValue, "123 ") <<
"Integer should be converted";
// Float
returnValue = Con::executef(testObject, "testThisFunction", (F32)123.0);
EXPECT_STREQ(returnValue, "123 ") <<
"Float should be converted";
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_STREQ(returnValue, buffer) <<
"SimObject should be converted";
dSprintf(buffer, sizeof(buffer), "%i ", testObject->getId());
returnValue = Con::executef("testExecutef", testObject);
EXPECT_STREQ(returnValue, buffer) <<
"SimObject should be converted";
// Point3F
Point3F point(1,2,3);
returnValue = Con::executef(testObject, "testThisFunction", point);
EXPECT_STREQ(returnValue, "1 2 3 ") <<
"Point3F should be converted";
returnValue = Con::executef("testExecutef", point);
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_EQ(STR.mFunctionOffset, 0) <<
"Function offset should be 0";
const char *floatArg = Con::getFloatArg(1.23);
EXPECT_GT(STR.mFunctionOffset, 0) <<
"Function offset should not be 0";
Con::executef("testExecutef", floatArg);
EXPECT_EQ(STR.mFunctionOffset, 0) <<
"Function offset should be 0";
floatArg = Con::getFloatArg(1.23);
EXPECT_GT(STR.mFunctionOffset, 0) <<
"Function offset should not be 0";
Con::executef("testImaginaryFunction_", floatArg);
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 = gEvalState.getStackDepth();
U32 startStringStackPos = STR.mStart;
SimObject *testObject = NULL;
Sim::findObject("TestConExec", testObject);
EXPECT_TRUE(testObject != NULL)
<< "TestConExec object should exist";
// const char* versions of execute should maintain stack
const char *argv[] = {"testScriptExecuteFunction", "1", "2"};
const char *argvObject[] = {"testScriptExecuteFunction", "", "1", "2"};
ConsoleValue returnValue = Con::execute(3, argv);
EXPECT_STREQ(returnValue, "1 2") <<
"execute should return 1 2";
EXPECT_EQ(gEvalState.getStackDepth(), startStackPos) <<
"execute should restore stack";
returnValue = Con::execute(testObject, 4, argvObject);
EXPECT_STREQ(returnValue, "1 2") <<
"execute should return 1 2";
EXPECT_EQ(gEvalState.getStackDepth(), startStackPos) <<
"execute should restore stack";
}
#endif

View file

@ -1,158 +0,0 @@
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/platform.h"
#include "console/simBase.h"
#include "console/consoleTypes.h"
#include "console/simBase.h"
#include "console/engineAPI.h"
#include "math/mMath.h"
TEST(EngineAPI, EngineMarshallData)
{
// Reserve some values
ConsoleValue values[16];
// Basic string casting...
SimObject *foo = new SimObject();
foo->registerObject();
const char *value = EngineMarshallData(foo);
EXPECT_STREQ(value, foo->getIdString())
<< "SimObject should be casted to its ID";
U32 unsignedNumber = 123;
S32 signedNumber = -123;
value = EngineMarshallData(unsignedNumber);
EXPECT_STREQ(value, "123")
<< "Integer should be converted to 123";
value = EngineMarshallData(signedNumber);
EXPECT_STREQ(value, "-123")
<< "Integer should be converted to -123";
bool boolValue = true;
value = EngineMarshallData(boolValue);
EXPECT_STREQ(value, "1")
<< "Bool should be converted to 1";
Point3F point(1,2,3);
value = EngineMarshallData(point);
EXPECT_STREQ(value, "1 2 3")
<< "Point3F should be converted to 1 2 3";
F32 floatValue = 1.23f;
value = EngineMarshallData(floatValue);
EXPECT_STREQ(value, "1.23")
<< "F32 should be converted to 1.23";
// Argv based casting
S32 argc = 0;
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_EQ(argc, 7)
<< "7 args should have been set";
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_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_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_EQ(values[3].getType(), ConsoleValueType::cvInteger)
<< "4th arg should be a float";
EXPECT_EQ(values[3].getInt(), 123)
<< "4th arg should be 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_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();
}
TEST(EngineAPI, EngineUnMarshallData)
{
SimObject *foo = new SimObject();
foo->registerObject();
SimObject *testFoo = EngineUnmarshallData<SimObject*>()(foo->getIdString());
EXPECT_EQ(foo, testFoo)
<< "Unmarshalling foo's id should return foo";
testFoo = EngineUnmarshallData<SimObject*>()("ShouldNotExist_Really123");
EXPECT_TRUE(testFoo == NULL)
<< "Unmarshalling a bad object should return NULL";
foo->deleteObject();
}
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);
ConsoleValue returnValue = helper.call<ConsoleValue>("a", "b", "c");
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
_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";
}
// NOTE: this is also indirectly tested by the executef tests
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");
ConsoleValue returnValue = helper.call<ConsoleValue>("a", "b", "c");
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
_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

View file

@ -1,108 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/platform.h"
#include "console/simBase.h"
#include "console/consoleTypes.h"
#include "console/runtimeClassRep.h"
class RuntimeRegisteredSimObject : public SimObject
{
typedef SimObject Parent;
protected:
bool mFoo;
public:
RuntimeRegisteredSimObject() : mFoo(false) {};
DECLARE_RUNTIME_CONOBJECT(RuntimeRegisteredSimObject);
static void initPersistFields();
};
IMPLEMENT_RUNTIME_CONOBJECT(RuntimeRegisteredSimObject);
void RuntimeRegisteredSimObject::initPersistFields()
{
addField("fooField", TypeBool, Offset(mFoo, RuntimeRegisteredSimObject));
}
TEST(Console, RuntimeClassRep)
{
// First test to make sure that the test class is not registered (don't
// know how it could be, but that's programming for you). Stop the test if
// it is.
ASSERT_TRUE(!RuntimeRegisteredSimObject::dynRTClassRep.isRegistered())
<< "RuntimeRegisteredSimObject class was already registered with the console";
// This should not be able to find the class, and return null (this may
// AssertWarn as well).
ConsoleObject *conobj = ConsoleObject::create("RuntimeRegisteredSimObject");
EXPECT_TRUE(conobj == NULL)
<< "Unregistered AbstractClassRep returned non-NULL value! That is really bad!";
// Register with console system.
RuntimeRegisteredSimObject::dynRTClassRep.consoleRegister();
// Make sure that the object knows it's registered.
EXPECT_TRUE(RuntimeRegisteredSimObject::dynRTClassRep.isRegistered())
<< "RuntimeRegisteredSimObject class failed console registration";
// Now try again to create the instance.
conobj = ConsoleObject::create("RuntimeRegisteredSimObject");
EXPECT_TRUE(conobj != NULL)
<< "AbstractClassRep::create method failed!";
// Cast the instance, and test it.
RuntimeRegisteredSimObject *rtinst =
dynamic_cast<RuntimeRegisteredSimObject *>(conobj);
EXPECT_TRUE(rtinst != NULL)
<< "Casting failed for some reason";
// Register it with a name.
rtinst->registerObject("_utRRTestObject");
EXPECT_TRUE(rtinst->isProperlyAdded())
<< "registerObject failed on test object";
// Now execute some script on it.
Con::evaluate("_utRRTestObject.fooField = true;");
// Test to make sure field worked.
EXPECT_TRUE(dAtob(rtinst->getDataField(StringTable->insert("fooField"), NULL)))
<< "Set property failed on instance.";
// BALETED
rtinst->deleteObject();
// Unregister the type.
RuntimeRegisteredSimObject::dynRTClassRep.consoleUnRegister();
// And make sure we can't create another one.
conobj = ConsoleObject::create("RuntimeRegisteredSimObject");
EXPECT_TRUE(conobj == NULL)
<< "Unregistration of type failed";
}
#endif