2
0

consoleTest.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include "console/stringStack.h"
  11. TEST(Con, executef)
  12. {
  13. char buffer[128];
  14. 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");
  15. SimObject *testObject = NULL;
  16. Sim::findObject("TestConExec", testObject);
  17. EXPECT_TRUE(testObject != NULL)
  18. << "TestConExec object should exist";
  19. // Check basic calls with SimObject. We'll do this for every single possible call just to make sure.
  20. const char *returnValue = NULL;
  21. returnValue = Con::executef(testObject, "testThisFunction");
  22. EXPECT_TRUE(dStricmp(returnValue, " ") == 0) <<
  23. "All values should be printed in the correct order";
  24. returnValue = Con::executef(testObject, "testThisFunction", "a");
  25. EXPECT_TRUE(dStricmp(returnValue, "a ") == 0) <<
  26. "All values should be printed in the correct order";
  27. returnValue = Con::executef(testObject, "testThisFunction", "a", "b");
  28. EXPECT_TRUE(dStricmp(returnValue, "a b ") == 0) <<
  29. "All values should be printed in the correct order";
  30. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c");
  31. EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) <<
  32. "All values should be printed in the correct order";
  33. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d");
  34. EXPECT_TRUE(dStricmp(returnValue, "a b c d ") == 0) <<
  35. "All values should be printed in the correct order";
  36. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e");
  37. EXPECT_TRUE(dStricmp(returnValue, "a b c d e ") == 0) <<
  38. "All values should be printed in the correct order";
  39. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f");
  40. EXPECT_TRUE(dStricmp(returnValue, "a b c d e f ") == 0) <<
  41. "All values should be printed in the correct order";
  42. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f", "g");
  43. EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g ") == 0) <<
  44. "All values should be printed in the correct order";
  45. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f", "g", "h");
  46. EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h ") == 0) <<
  47. "All values should be printed in the correct order";
  48. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f", "g", "h", "i");
  49. EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h i ") == 0) <<
  50. "All values should be printed in the correct order";
  51. // Now test without the object
  52. returnValue = Con::executef("testExecutef");
  53. EXPECT_TRUE(dStricmp(returnValue, " ") == 0) <<
  54. "All values should be printed in the correct order";
  55. returnValue = Con::executef("testExecutef", "a");
  56. EXPECT_TRUE(dStricmp(returnValue, "a ") == 0) <<
  57. "All values should be printed in the correct order";
  58. returnValue = Con::executef("testExecutef", "a", "b");
  59. EXPECT_TRUE(dStricmp(returnValue, "a b ") == 0) <<
  60. "All values should be printed in the correct order";
  61. returnValue = Con::executef("testExecutef", "a", "b", "c");
  62. EXPECT_TRUE(dStricmp(returnValue, "a b c ") == 0) <<
  63. "All values should be printed in the correct order";
  64. returnValue = Con::executef("testExecutef", "a", "b", "c", "d");
  65. EXPECT_TRUE(dStricmp(returnValue, "a b c d ") == 0) <<
  66. "All values should be printed in the correct order";
  67. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e");
  68. EXPECT_TRUE(dStricmp(returnValue, "a b c d e ") == 0) <<
  69. "All values should be printed in the correct order";
  70. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f");
  71. EXPECT_TRUE(dStricmp(returnValue, "a b c d e f ") == 0) <<
  72. "All values should be printed in the correct order";
  73. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g");
  74. EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g ") == 0) <<
  75. "All values should be printed in the correct order";
  76. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h");
  77. EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h ") == 0) <<
  78. "All values should be printed in the correct order";
  79. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h", "i");
  80. EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h i ") == 0) <<
  81. "All values should be printed in the correct order";
  82. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
  83. EXPECT_TRUE(dStricmp(returnValue, "a b c d e f g h i j ") == 0) <<
  84. "All values should be printed in the correct order";
  85. // Test type conversions with and without SimObject...
  86. // Integer
  87. returnValue = Con::executef(testObject, "testThisFunction", 123);
  88. EXPECT_TRUE(dStricmp(returnValue, "123 ") == 0) <<
  89. "Integer should be converted";
  90. returnValue = Con::executef("testExecutef", 123);
  91. EXPECT_TRUE(dStricmp(returnValue, "123 ") == 0) <<
  92. "Integer should be converted";
  93. // Float
  94. returnValue = Con::executef(testObject, "testThisFunction", (F32)123.4);
  95. EXPECT_TRUE(dStricmp(returnValue, "123.4 ") == 0) <<
  96. "Float should be converted";
  97. returnValue = Con::executef("testExecutef", (F32)123.4);
  98. EXPECT_TRUE(dStricmp(returnValue, "123.4 ") == 0) <<
  99. "Float should be converted";
  100. // SimObject
  101. dSprintf(buffer, sizeof(buffer), "%i ", testObject->getId());
  102. returnValue = Con::executef(testObject, "testThisFunction", testObject);
  103. EXPECT_TRUE(dStricmp(returnValue, buffer) == 0) <<
  104. "SimObject should be converted";
  105. dSprintf(buffer, sizeof(buffer), "%i ", testObject->getId());
  106. returnValue = Con::executef("testExecutef", testObject);
  107. EXPECT_TRUE(dStricmp(returnValue, buffer) == 0) <<
  108. "SimObject should be converted";
  109. // Point3F
  110. Point3F point(1,2,3);
  111. returnValue = Con::executef(testObject, "testThisFunction", point);
  112. EXPECT_TRUE(dStricmp(returnValue, "1 2 3 ") == 0) <<
  113. "Point3F should be converted";
  114. returnValue = Con::executef("testExecutef", point);
  115. EXPECT_TRUE(dStricmp(returnValue, "1 2 3 ") == 0) <<
  116. "Point3F should be converted";
  117. // Finally test the function arg offset. This should be consistently 0 after each call
  118. EXPECT_TRUE(STR.mFunctionOffset == 0) <<
  119. "Function offset should be 0";
  120. const char *floatArg = Con::getFloatArg(1.23);
  121. EXPECT_TRUE(STR.mFunctionOffset > 0) <<
  122. "Function offset should not be 0";
  123. Con::executef("testExecutef", floatArg);
  124. EXPECT_TRUE(STR.mFunctionOffset == 0) <<
  125. "Function offset should be 0";
  126. floatArg = Con::getFloatArg(1.23);
  127. EXPECT_TRUE(STR.mFunctionOffset > 0) <<
  128. "Function offset should not be 0";
  129. Con::executef("testImaginaryFunction_", floatArg);
  130. EXPECT_TRUE(STR.mFunctionOffset == 0) <<
  131. "Function offset should be 0";
  132. }
  133. TEST(Con, execute)
  134. {
  135. Con::evaluate("if (isObject(TestConExec)) {\r\nTestConExec.delete();\r\n}\r\nfunction testScriptExecuteFunction(%a,%b) {return %a SPC %b;}\nfunction TestConExec::testScriptExecuteFunction(%this, %a,%b) {return %a SPC %b;}new ScriptObject(TestConExec);\r\n", false, "testExecute");
  136. U32 startStackPos = CSTK.mStackPos;
  137. U32 startStringStackPos = STR.mStart;
  138. U32 startStackFrame = CSTK.mFrame;
  139. SimObject *testObject = NULL;
  140. Sim::findObject("TestConExec", testObject);
  141. EXPECT_TRUE(testObject != NULL)
  142. << "TestConExec object should exist";
  143. // const char* versions of execute should maintain stack
  144. const char *argv[] = {"testScriptExecuteFunction", "1", "2"};
  145. const char *argvObject[] = {"testScriptExecuteFunction", "", "1", "2"};
  146. const char *returnValue = Con::execute(3, argv);
  147. EXPECT_TRUE(dStricmp(returnValue, "1 2") == 0) <<
  148. "execute should return 1 2";
  149. EXPECT_TRUE(CSTK.mStackPos == startStackPos) <<
  150. "execute should restore stack";
  151. returnValue = Con::execute(testObject, 4, argvObject);
  152. EXPECT_TRUE(dStricmp(returnValue, "1 2") == 0) <<
  153. "execute should return 1 2";
  154. EXPECT_TRUE(CSTK.mStackPos == startStackPos) <<
  155. "execute should restore stack";
  156. // ConsoleValueRef versions of execute should not restore stack
  157. CSTK.pushFrame();
  158. STR.pushFrame();
  159. ConsoleValue valueArg[4];
  160. ConsoleValueRef refArg[4];
  161. ConsoleValue valueArgObject[4];
  162. ConsoleValueRef refArgObject[4];
  163. for (U32 i=0; i<4; i++)
  164. {
  165. refArg[i].value = &valueArg[i];
  166. refArgObject[i].value = &valueArgObject[i];
  167. valueArgObject[i].init();
  168. valueArg[i].init();
  169. }
  170. refArg[0] = "testScriptExecuteFunction";
  171. refArg[1] = "1";
  172. refArg[2] = "2";
  173. refArgObject[0] = "testScriptExecuteFunction";
  174. refArgObject[2] = "1";
  175. refArgObject[3] = "2";
  176. returnValue = Con::execute(3, refArg);
  177. EXPECT_TRUE(dStricmp(returnValue, "1 2") == 0) <<
  178. "execute should return 1 2";
  179. EXPECT_TRUE(CSTK.mStackPos == startStackPos) <<
  180. "execute should restore stack";
  181. CSTK.popFrame();
  182. STR.popFrame();
  183. CSTK.pushFrame();
  184. STR.pushFrame();
  185. returnValue = Con::execute(testObject, 4, refArgObject);
  186. EXPECT_TRUE(dStricmp(returnValue, "1 2") == 0) <<
  187. "execute should return 1 2";
  188. EXPECT_TRUE(CSTK.mStackPos == startStackPos) <<
  189. "execute should restore stack";
  190. CSTK.popFrame();
  191. STR.popFrame();
  192. }
  193. #endif
  194. #endif