engineAPITest.cpp 5.4 KB

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