2
0

engineAPITest.cpp 5.5 KB

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