engineAPITest.cpp 5.7 KB

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