consoleTest.cpp 10 KB

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