engineAPITest.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "testing/unitTesting.h"
  2. #include "platform/platform.h"
  3. #include "console/simBase.h"
  4. #include "console/consoleTypes.h"
  5. #include "console/scriptObjects.h"
  6. #include "console/simBase.h"
  7. #include "console/engineAPI.h"
  8. #include "math/mMath.h"
  9. #include "console/script.h"
  10. using ::testing::Matcher;
  11. using ::testing::TypedEq;
  12. class EngineAPITest : public ::testing::Test
  13. {
  14. protected:
  15. EngineAPITest()
  16. {
  17. }
  18. void SetUp() override
  19. {
  20. ScriptObject* test = new ScriptObject();
  21. test->assignName("TestConExec");
  22. }
  23. };
  24. TEST_F(EngineAPITest, EngineMarshallData)
  25. {
  26. // Reserve some values
  27. ConsoleValue values[16];
  28. // Basic string casting...
  29. SimObject *foo = new SimObject();
  30. foo->registerObject();
  31. const char *value = EngineMarshallData(foo);
  32. EXPECT_STREQ(value, foo->getIdString())
  33. << "SimObject should be casted to its ID";
  34. U32 unsignedNumber = 123;
  35. S32 signedNumber = -123;
  36. value = EngineMarshallData(unsignedNumber);
  37. EXPECT_STREQ(value, "123")
  38. << "Integer should be converted to 123";
  39. value = EngineMarshallData(signedNumber);
  40. EXPECT_STREQ(value, "-123")
  41. << "Integer should be converted to -123";
  42. bool boolValue = true;
  43. value = EngineMarshallData(boolValue);
  44. EXPECT_STREQ(value, "1")
  45. << "Bool should be converted to 1";
  46. Point3F point(1,2,3);
  47. value = EngineMarshallData(point);
  48. EXPECT_STREQ(value, "1 2 3")
  49. << "Point3F should be converted to 1 2 3";
  50. F32 floatValue = 1.23f;
  51. value = EngineMarshallData(floatValue);
  52. EXPECT_STREQ(value, "1.23")
  53. << "F32 should be converted to 1.23";
  54. // Argv based casting
  55. S32 argc = 0;
  56. EngineMarshallData(foo, argc, values);
  57. EngineMarshallData((const SimObject*)foo, argc, values);
  58. EngineMarshallData(point, argc, values);
  59. EngineMarshallData(unsignedNumber, argc, values);
  60. EngineMarshallData(signedNumber, argc, values);
  61. EngineMarshallData(boolValue, argc, values);
  62. EngineMarshallData(floatValue, argc, values);
  63. EXPECT_EQ(argc, 7)
  64. << "7 args should have been set";
  65. EXPECT_EQ(values[0].getType(), ConsoleValueType::cvInteger)
  66. << "1st arg should be an int";
  67. EXPECT_EQ(values[0].getInt(), foo->getId())
  68. << "1st arg should be foo's id";
  69. EXPECT_EQ(values[1].getType(), ConsoleValueType::cvInteger)
  70. << "2nd arg should be an int";
  71. EXPECT_EQ(values[1].getInt(), foo->getId())
  72. << "2nd arg should be foo's id";
  73. EXPECT_EQ(values[2].getType(), ConsoleValueType::cvString)
  74. << "3rd arg should be a string";
  75. EXPECT_STREQ(values[2].getString(), "1 2 3")
  76. << "3rd arg should be 1 2 3";
  77. EXPECT_EQ(values[3].getType(), ConsoleValueType::cvInteger)
  78. << "4th arg should be a float";
  79. EXPECT_EQ(values[3].getInt(), 123)
  80. << "4th arg should be 123";
  81. EXPECT_EQ(values[4].getType(), ConsoleValueType::cvInteger)
  82. << "5th arg should be a float";
  83. EXPECT_EQ(values[4].getInt(), -123)
  84. << "5th arg should be -123";
  85. EXPECT_EQ(values[5].getType(), ConsoleValueType::cvInteger)
  86. << "6th arg should be a float";
  87. EXPECT_TRUE(values[5].getBool())
  88. << "6th arg should be true";
  89. EXPECT_EQ(values[6].getType(), ConsoleValueType::cvFloat)
  90. << "7th arg should be a float";
  91. EXPECT_FLOAT_EQ(values[6].getFloat(), 1.23)
  92. << "7th arg should be 1.23";
  93. foo->deleteObject();
  94. }
  95. TEST_F(EngineAPITest, EngineUnMarshallData)
  96. {
  97. SimObject *foo = new SimObject();
  98. foo->registerObject();
  99. SimObject *testFoo = EngineUnmarshallData<SimObject*>()(foo->getIdString());
  100. EXPECT_EQ(foo, testFoo)
  101. << "Unmarshalling foo's id should return foo";
  102. testFoo = EngineUnmarshallData<SimObject*>()("ShouldNotExist_Really123");
  103. EXPECT_TRUE(testFoo == NULL)
  104. << "Unmarshalling a bad object should return NULL";
  105. foo->deleteObject();
  106. }
  107. TEST_F(EngineAPITest, _EngineConsoleCallbackHelper)
  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. _EngineConsoleCallbackHelper helper("testExecutef", NULL);
  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. _EngineConsoleCallbackHelper objectHelper("testThisFunction", testObject);
  117. returnValue = helper.call<ConsoleValue>("a", "b", "c");
  118. EXPECT_STREQ(returnValue, "a b c ") <<
  119. "All values should be printed in the correct order";
  120. }
  121. // NOTE: this is also indirectly tested by the executef tests
  122. TEST_F(EngineAPITest, _EngineConsoleExecCallbackHelper)
  123. {
  124. 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");
  125. SimObject *testObject = NULL;
  126. Sim::findObject("TestConExec", testObject);
  127. _EngineConsoleExecCallbackHelper<const char*> helper("testExecutef");
  128. ConsoleValue returnValue = helper.call<ConsoleValue>("a", "b", "c");
  129. EXPECT_STREQ(returnValue, "a b c ") <<
  130. "All values should be printed in the correct order";
  131. _EngineConsoleExecCallbackHelper<SimObject*> objectHelper(testObject);
  132. returnValue = objectHelper.call<ConsoleValue>("testThisFunction", "a", "b", "c");
  133. EXPECT_STREQ(returnValue, "a b c ") <<
  134. "All values should be printed in the correct order";
  135. }