Torque3D/Engine/source/testing/engineAPITest.cpp

178 lines
5.7 KiB
C++
Raw Normal View History

#include "testing/unitTesting.h"
#include "platform/platform.h"
#include "console/simBase.h"
#include "console/consoleTypes.h"
#include "console/scriptObjects.h"
#include "console/simBase.h"
#include "console/engineAPI.h"
#include "math/mMath.h"
2023-04-23 08:39:54 +00:00
#include "console/script.h"
using ::testing::Matcher;
using ::testing::TypedEq;
class EngineAPITest : public ::testing::Test
{
protected:
EngineAPITest()
{
}
void SetUp() override
{
ScriptObject* test = new ScriptObject();
test->assignName("TestConExec");
}
};
TEST_F(EngineAPITest, EngineMarshallData)
{
// Reserve some values
ConsoleValue values[16];
// Basic string casting...
SimObject *foo = new SimObject();
foo->registerObject();
const char *value = EngineMarshallData(foo);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(value, foo->getIdString())
<< "SimObject should be casted to its ID";
U32 unsignedNumber = 123;
S32 signedNumber = -123;
value = EngineMarshallData(unsignedNumber);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(value, "123")
<< "Integer should be converted to 123";
value = EngineMarshallData(signedNumber);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(value, "-123")
<< "Integer should be converted to -123";
bool boolValue = true;
value = EngineMarshallData(boolValue);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(value, "1")
<< "Bool should be converted to 1";
Point3F point(1,2,3);
value = EngineMarshallData(point);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(value, "1 2 3")
<< "Point3F should be converted to 1 2 3";
F32 floatValue = 1.23f;
value = EngineMarshallData(floatValue);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(value, "1.23")
<< "F32 should be converted to 1.23";
// Argv based casting
S32 argc = 0;
2023-04-06 13:28:09 +00:00
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";
2023-04-06 13:28:09 +00:00
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";
2023-04-06 13:28:09 +00:00
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";
2023-04-06 13:28:09 +00:00
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";
2023-04-06 13:28:09 +00:00
EXPECT_EQ(values[3].getType(), ConsoleValueType::cvInteger)
<< "4th arg should be a float";
EXPECT_EQ(values[3].getInt(), 123)
<< "4th arg should be 123";
2023-04-06 13:28:09 +00:00
EXPECT_EQ(values[4].getType(), ConsoleValueType::cvInteger)
<< "5th arg should be a float";
EXPECT_EQ(values[4].getInt(), -123)
<< "5th arg should be -123";
2023-04-06 13:28:09 +00:00
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_F(EngineAPITest, EngineUnMarshallData)
{
SimObject *foo = new SimObject();
foo->registerObject();
SimObject *testFoo = EngineUnmarshallData<SimObject*>()(foo->getIdString());
2023-04-06 13:28:09 +00:00
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_F(EngineAPITest, _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");
2023-04-06 13:28:09 +00:00
SimObject *testObject = NULL;
Sim::findObject("TestConExec", testObject);
_EngineConsoleCallbackHelper helper("testExecutef", NULL);
2023-04-06 13:28:09 +00:00
ConsoleValue returnValue = helper.call<ConsoleValue>("a", "b", "c");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
_EngineConsoleCallbackHelper objectHelper("testThisFunction", testObject);
2023-04-06 13:28:09 +00:00
returnValue = helper.call<ConsoleValue>("a", "b", "c");
2023-04-06 13:28:09 +00:00
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_F(EngineAPITest, _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");
2023-04-06 13:28:09 +00:00
SimObject *testObject = NULL;
Sim::findObject("TestConExec", testObject);
_EngineConsoleExecCallbackHelper<const char*> helper("testExecutef");
2023-04-06 13:28:09 +00:00
ConsoleValue returnValue = helper.call<ConsoleValue>("a", "b", "c");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
_EngineConsoleExecCallbackHelper<SimObject*> objectHelper(testObject);
2023-04-06 13:28:09 +00:00
returnValue = objectHelper.call<ConsoleValue>("testThisFunction", "a", "b", "c");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c ") <<
"All values should be printed in the correct order";
}