2023-07-24 11:38:36 +00:00
|
|
|
|
2015-02-08 00:05:45 +00:00
|
|
|
#include "testing/unitTesting.h"
|
|
|
|
|
#include "platform/platform.h"
|
|
|
|
|
#include "console/simBase.h"
|
|
|
|
|
#include "console/consoleTypes.h"
|
2023-07-24 11:38:36 +00:00
|
|
|
#include "console/scriptObjects.h"
|
2015-02-08 00:05:45 +00:00
|
|
|
#include "console/simBase.h"
|
|
|
|
|
#include "console/engineAPI.h"
|
|
|
|
|
#include "math/mMath.h"
|
2023-04-23 08:39:54 +00:00
|
|
|
#include "console/script.h"
|
2015-02-08 00:05:45 +00:00
|
|
|
|
2023-07-24 11:38:36 +00:00
|
|
|
using ::testing::Matcher;
|
|
|
|
|
using ::testing::TypedEq;
|
|
|
|
|
|
|
|
|
|
class EngineAPITest : public ::testing::Test
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
EngineAPITest()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetUp() override
|
|
|
|
|
{
|
|
|
|
|
ScriptObject* test = new ScriptObject();
|
|
|
|
|
test->assignName("TestConExec");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
2015-02-08 00:05:45 +00:00
|
|
|
|
2023-07-24 11:38:36 +00:00
|
|
|
TEST_F(EngineAPITest, EngineMarshallData)
|
2015-02-08 00:05:45 +00:00
|
|
|
{
|
|
|
|
|
// 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())
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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")
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "Integer should be converted to 123";
|
|
|
|
|
value = EngineMarshallData(signedNumber);
|
2023-04-06 13:28:09 +00:00
|
|
|
EXPECT_STREQ(value, "-123")
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "Integer should be converted to -123";
|
|
|
|
|
|
|
|
|
|
bool boolValue = true;
|
|
|
|
|
value = EngineMarshallData(boolValue);
|
2023-04-06 13:28:09 +00:00
|
|
|
EXPECT_STREQ(value, "1")
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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")
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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")
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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)
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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())
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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())
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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")
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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)
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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)
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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)
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "7th arg should be 1.23";
|
|
|
|
|
|
|
|
|
|
foo->deleteObject();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 11:38:36 +00:00
|
|
|
TEST_F(EngineAPITest, EngineUnMarshallData)
|
2015-02-08 00:05:45 +00:00
|
|
|
{
|
|
|
|
|
SimObject *foo = new SimObject();
|
|
|
|
|
foo->registerObject();
|
|
|
|
|
|
|
|
|
|
SimObject *testFoo = EngineUnmarshallData<SimObject*>()(foo->getIdString());
|
|
|
|
|
|
2023-04-06 13:28:09 +00:00
|
|
|
EXPECT_EQ(foo, testFoo)
|
2015-02-08 00:05:45 +00:00
|
|
|
<< "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();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 11:38:36 +00:00
|
|
|
TEST_F(EngineAPITest, _EngineConsoleCallbackHelper)
|
2015-02-08 00:05:45 +00:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
2015-02-08 00:05:45 +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");
|
2015-02-08 00:05:45 +00:00
|
|
|
|
2023-04-06 13:28:09 +00:00
|
|
|
EXPECT_STREQ(returnValue, "a b c ") <<
|
2015-02-08 00:05:45 +00:00
|
|
|
"All values should be printed in the correct order";
|
2023-04-06 13:28:09 +00:00
|
|
|
|
2015-02-08 00:05:45 +00:00
|
|
|
_EngineConsoleCallbackHelper objectHelper("testThisFunction", testObject);
|
2023-04-06 13:28:09 +00:00
|
|
|
returnValue = helper.call<ConsoleValue>("a", "b", "c");
|
2015-02-08 00:05:45 +00:00
|
|
|
|
2023-04-06 13:28:09 +00:00
|
|
|
EXPECT_STREQ(returnValue, "a b c ") <<
|
2015-02-08 00:05:45 +00:00
|
|
|
"All values should be printed in the correct order";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: this is also indirectly tested by the executef tests
|
2023-07-24 11:38:36 +00:00
|
|
|
TEST_F(EngineAPITest, _EngineConsoleExecCallbackHelper)
|
2015-02-08 00:05:45 +00:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
2015-02-08 00:05:45 +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");
|
2015-02-08 00:05:45 +00:00
|
|
|
|
2023-04-06 13:28:09 +00:00
|
|
|
EXPECT_STREQ(returnValue, "a b c ") <<
|
2015-02-08 00:05:45 +00:00
|
|
|
"All values should be printed in the correct order";
|
2023-04-06 13:28:09 +00:00
|
|
|
|
2015-02-08 00:05:45 +00:00
|
|
|
_EngineConsoleExecCallbackHelper<SimObject*> objectHelper(testObject);
|
2023-04-06 13:28:09 +00:00
|
|
|
returnValue = objectHelper.call<ConsoleValue>("testThisFunction", "a", "b", "c");
|
2015-02-08 00:05:45 +00:00
|
|
|
|
2023-04-06 13:28:09 +00:00
|
|
|
EXPECT_STREQ(returnValue, "a b c ") <<
|
2015-02-08 00:05:45 +00:00
|
|
|
"All values should be printed in the correct order";
|
|
|
|
|
}
|
|
|
|
|
|