engineAPITest.cpp 5.5 KB

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