Torque3D/Engine/source/testing/consoleTest.cpp

201 lines
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"
#include "console/stringStack.h"
2023-04-06 13:28:09 +00:00
#include "console/consoleInternal.h"
// Stupid globals not declared in a header
extern ExprEvalState gEvalState;
using ::testing::Matcher;
using ::testing::TypedEq;
class ConsoleTest : public ::testing::Test
{
protected:
ConsoleTest()
{
}
void SetUp() override
{
}
};
TEST_F(ConsoleTest, executef)
{
char buffer[128];
Con::evaluate("function 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 testThisFunction(%a,%b,%c,%d,%e,%f,%g,%h,%i,%j, %this){ return %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j;}\r\n", false, "test");
// Check basic calls with SimObject. We'll do this for every single possible call just to make sure.
2023-04-06 13:28:09 +00:00
ConsoleValue returnValue;
returnValue = Con::executef("testThisFunction");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, " ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testThisFunction", "a");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testThisFunction", "a", "b");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("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";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testThisFunction", "a", "b", "c", "d");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testThisFunction", "a", "b", "c", "d", "e");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d e ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testThisFunction", "a", "b", "c", "d", "e", "f");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d e f ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testThisFunction", "a", "b", "c", "d", "e", "f", "g");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d e f g ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testThisFunction", "a", "b", "c", "d", "e", "f", "g", "h");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d e f g h ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testThisFunction", "a", "b", "c", "d", "e", "f", "g", "h", "i");
2023-04-06 13:28:09 +00:00
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
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, " ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", "a");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", "a", "b");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", "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
returnValue = Con::executef("testExecutef", "a", "b", "c", "d");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d e ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d e f ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d e f g ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d e f g h ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h", "i");
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "a b c d e f g h i ") <<
"All values should be printed in the correct order";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
2023-04-06 13:28:09 +00:00
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("testThisFunction", 123);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "123 ") <<
"Integer should be converted";
returnValue = Con::executef("testExecutef", 123);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "123 ") <<
"Integer should be converted";
// Float
returnValue = Con::executef("testThisFunction", (F32)123.0);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "123 ") <<
"Float should be converted";
2023-04-06 13:28:09 +00:00
returnValue = Con::executef("testExecutef", (F32)123.0);
EXPECT_STREQ(returnValue, "123 ") <<
"Float should be converted";
// Point3F
Point3F point(1,2,3);
returnValue = Con::executef("testThisFunction", point);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "1 2 3 ") <<
"Point3F should be converted";
returnValue = Con::executef("testExecutef", point);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "1 2 3 ") <<
"Point3F should be converted";
// Finally test the function arg offset. This should be consistently 0 after each call
2023-04-06 13:28:09 +00:00
EXPECT_EQ(STR.mFunctionOffset, 0) <<
"Function offset should be 0";
const char *floatArg = Con::getFloatArg(1.23);
2023-04-06 13:28:09 +00:00
EXPECT_GT(STR.mFunctionOffset, 0) <<
"Function offset should not be 0";
Con::executef("testExecutef", floatArg);
2023-04-06 13:28:09 +00:00
EXPECT_EQ(STR.mFunctionOffset, 0) <<
"Function offset should be 0";
floatArg = Con::getFloatArg(1.23);
2023-04-06 13:28:09 +00:00
EXPECT_GT(STR.mFunctionOffset, 0) <<
"Function offset should not be 0";
}
TEST_F(ConsoleTest, execute)
{
Con::evaluate("function testScriptExecuteFunction(%a,%b) {return %a SPC %b;}\nfunction testScriptExecuteFunction(%a,%b,%this) {return %a SPC %b;}\r\n", false, "testExecute");
2023-04-06 13:28:09 +00:00
U32 startStackPos = gEvalState.getStackDepth();
U32 startStringStackPos = STR.mStart;
2023-04-06 13:28:09 +00:00
// const char* versions of execute should maintain stack
const char *argv[] = {"testScriptExecuteFunction", "1", "2"};
const char *argvObject[] = {"testScriptExecuteFunction", "1", "2", ""};
2023-04-06 13:28:09 +00:00
ConsoleValue returnValue = Con::execute(3, argv);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "1 2") <<
"execute should return 1 2";
2023-04-06 13:28:09 +00:00
EXPECT_EQ(gEvalState.getStackDepth(), startStackPos) <<
"execute should restore stack";
returnValue = Con::execute(4, argvObject);
2023-04-06 13:28:09 +00:00
EXPECT_STREQ(returnValue, "1 2") <<
"execute should return 1 2";
2023-04-06 13:28:09 +00:00
EXPECT_EQ(gEvalState.getStackDepth(), startStackPos) <<
"execute should restore stack";
}