engineAPITest.cpp
Engine/source/console/test/engineAPITest.cpp
Detailed Description
1 2#ifdef TORQUE_TESTS_ENABLED 3#include "testing/unitTesting.h" 4#include "platform/platform.h" 5#include "console/simBase.h" 6#include "console/consoleTypes.h" 7#include "console/simBase.h" 8#include "console/engineAPI.h" 9#include "math/mMath.h" 10 11 12TEST(EngineAPI, EngineMarshallData) 13{ 14 // Reserve some values 15 ConsoleValue values[16]; 16 ConsoleValueRef refValues[16]; 17 18 for (U32 i=0; i<16; i++) 19 { 20 values[i].init(); 21 refValues[i].value = &values[i]; 22 } 23 24 // Basic string casting... 25 SimObject *foo = new SimObject(); 26 foo->registerObject(); 27 28 const char *value = EngineMarshallData(foo); 29 EXPECT_TRUE(dStricmp(value, foo->getIdString()) == 0) 30 << "SimObject should be casted to its ID"; 31 32 U32 unsignedNumber = 123; 33 S32 signedNumber = -123; 34 value = EngineMarshallData(unsignedNumber); 35 EXPECT_TRUE(dStricmp(value, "123") == 0) 36 << "Integer should be converted to 123"; 37 value = EngineMarshallData(signedNumber); 38 EXPECT_TRUE(dStricmp(value, "-123") == 0) 39 << "Integer should be converted to -123"; 40 41 bool boolValue = true; 42 value = EngineMarshallData(boolValue); 43 EXPECT_TRUE(dStricmp(value, "1") == 0) 44 << "Bool should be converted to 1"; 45 46 Point3F point(1,2,3); 47 value = EngineMarshallData(point); 48 EXPECT_TRUE(dStricmp(value, "1 2 3") == 0) 49 << "Point3F should be converted to 1 2 3"; 50 51 F32 floatValue = 1.23f; 52 value = EngineMarshallData(floatValue); 53 EXPECT_TRUE(dStricmp(value, "1.23") == 0) 54 << "F32 should be converted to 1.23"; 55 56 // Argv based casting 57 S32 argc = 0; 58 EngineMarshallData(foo, argc, refValues); 59 EngineMarshallData((const SimObject*)foo, argc, refValues); 60 EngineMarshallData(point, argc, refValues); 61 EngineMarshallData(unsignedNumber, argc, refValues); 62 EngineMarshallData(signedNumber, argc, refValues); 63 EngineMarshallData(boolValue, argc, refValues); 64 EngineMarshallData(floatValue, argc, refValues); 65 66 EXPECT_TRUE(argc == 7) 67 << "7 args should have been set"; 68 69 EXPECT_TRUE(values[0].type == ConsoleValue::TypeInternalInt && values[0].getSignedIntValue() == foo->getId()) 70 << "1st arg should be foo's id"; 71 72 EXPECT_TRUE(values[1].type == ConsoleValue::TypeInternalInt && values[1].getSignedIntValue() == foo->getId()) 73 << "2nd arg should be foo's id"; 74 75 EXPECT_TRUE(values[2].type == ConsoleValue::TypeInternalString && dStricmp(values[2].getStringValue(), "1 2 3") == 0) 76 << "3rd arg should be 1 2 3"; 77 78 EXPECT_TRUE(values[3].type == ConsoleValue::TypeInternalFloat && values[3].getSignedIntValue() == 123) 79 << "4th arg should be 123"; 80 81 EXPECT_TRUE(values[4].type == ConsoleValue::TypeInternalFloat && values[4].getSignedIntValue() == -123) 82 << "5th arg should be -123"; 83 84 EXPECT_TRUE(values[5].type == ConsoleValue::TypeInternalFloat && values[5].getBoolValue() == true) 85 << "6th arg should be -123"; 86 87 EXPECT_TRUE(values[6].type == ConsoleValue::TypeInternalFloat && mRound(values[6].getFloatValue() * 100) == 123) 88 << "7th arg should be 1.23"; 89 90 foo->deleteObject(); 91} 92 93TEST(EngineAPI, EngineUnMarshallData) 94{ 95 SimObject *foo = new SimObject(); 96 foo->registerObject(); 97 98 SimObject *testFoo = EngineUnmarshallData<SimObject*>()(foo->getIdString()); 99 100 EXPECT_TRUE(foo == testFoo) 101 << "Unmarshalling foo's id should return foo"; 102 103 testFoo = EngineUnmarshallData<SimObject*>()("ShouldNotExist_Really123"); 104 EXPECT_TRUE(testFoo == NULL) 105 << "Unmarshalling a bad object should return NULL"; 106 107 foo->deleteObject(); 108} 109 110TEST(EngineAPI, _EngineConsoleCallbackHelper) 111{ 112 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"); 113 114 SimObject *testObject = NULL; 115 Sim::findObject("TestConExec", testObject); 116 117 _EngineConsoleCallbackHelper helper("testExecutef", NULL); 118 const char *returnValue = helper.call<const char*>("a", "b", "c"); 119 120 EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) << 121 "All values should be printed in the correct order"; 122 123 _EngineConsoleCallbackHelper objectHelper("testThisFunction", testObject); 124 returnValue = helper.call<const char*>("a", "b", "c"); 125 126 EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) << 127 "All values should be printed in the correct order"; 128} 129 130// NOTE: this is also indirectly tested by the executef tests 131TEST(EngineAPI, _EngineConsoleExecCallbackHelper) 132{ 133 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"); 134 135 SimObject *testObject = NULL; 136 Sim::findObject("TestConExec", testObject); 137 138 _EngineConsoleExecCallbackHelper<const char*> helper("testExecutef"); 139 const char *returnValue = helper.call<const char*>("a", "b", "c"); 140 141 EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) << 142 "All values should be printed in the correct order"; 143 144 _EngineConsoleExecCallbackHelper<SimObject*> objectHelper(testObject); 145 returnValue = objectHelper.call<const char*>("testThisFunction", "a", "b", "c"); 146 147 EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) << 148 "All values should be printed in the correct order"; 149} 150 151#endif 152