consoleTest.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include "console/consoleInternal.h"
  11. // Stupid globals not declared in a header
  12. extern ExprEvalState gEvalState;
  13. TEST(Con, executef)
  14. {
  15. char buffer[128];
  16. 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");
  17. SimObject *testObject = NULL;
  18. Sim::findObject("TestConExec", testObject);
  19. EXPECT_TRUE(testObject != NULL)
  20. << "TestConExec object should exist";
  21. // Check basic calls with SimObject. We'll do this for every single possible call just to make sure.
  22. ConsoleValue returnValue;
  23. returnValue = Con::executef(testObject, "testThisFunction");
  24. EXPECT_STREQ(returnValue, " ") <<
  25. "All values should be printed in the correct order";
  26. returnValue = Con::executef(testObject, "testThisFunction", "a");
  27. EXPECT_STREQ(returnValue, "a ") <<
  28. "All values should be printed in the correct order";
  29. returnValue = Con::executef(testObject, "testThisFunction", "a", "b");
  30. EXPECT_STREQ(returnValue, "a b ") <<
  31. "All values should be printed in the correct order";
  32. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c");
  33. EXPECT_STREQ(returnValue, "a b c ") <<
  34. "All values should be printed in the correct order";
  35. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d");
  36. EXPECT_STREQ(returnValue, "a b c d ") <<
  37. "All values should be printed in the correct order";
  38. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e");
  39. EXPECT_STREQ(returnValue, "a b c d e ") <<
  40. "All values should be printed in the correct order";
  41. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f");
  42. EXPECT_STREQ(returnValue, "a b c d e f ") <<
  43. "All values should be printed in the correct order";
  44. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f", "g");
  45. EXPECT_STREQ(returnValue, "a b c d e f g ") <<
  46. "All values should be printed in the correct order";
  47. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f", "g", "h");
  48. EXPECT_STREQ(returnValue, "a b c d e f g h ") <<
  49. "All values should be printed in the correct order";
  50. returnValue = Con::executef(testObject, "testThisFunction", "a", "b", "c", "d", "e", "f", "g", "h", "i");
  51. EXPECT_STREQ(returnValue, "a b c d e f g h i ") <<
  52. "All values should be printed in the correct order";
  53. // Now test without the object
  54. returnValue = Con::executef("testExecutef");
  55. EXPECT_STREQ(returnValue, " ") <<
  56. "All values should be printed in the correct order";
  57. returnValue = Con::executef("testExecutef", "a");
  58. EXPECT_STREQ(returnValue, "a ") <<
  59. "All values should be printed in the correct order";
  60. returnValue = Con::executef("testExecutef", "a", "b");
  61. EXPECT_STREQ(returnValue, "a b ") <<
  62. "All values should be printed in the correct order";
  63. returnValue = Con::executef("testExecutef", "a", "b", "c");
  64. EXPECT_STREQ(returnValue, "a b c ") <<
  65. "All values should be printed in the correct order";
  66. returnValue = Con::executef("testExecutef", "a", "b", "c", "d");
  67. EXPECT_STREQ(returnValue, "a b c d ") <<
  68. "All values should be printed in the correct order";
  69. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e");
  70. EXPECT_STREQ(returnValue, "a b c d e ") <<
  71. "All values should be printed in the correct order";
  72. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f");
  73. EXPECT_STREQ(returnValue, "a b c d e f ") <<
  74. "All values should be printed in the correct order";
  75. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g");
  76. EXPECT_STREQ(returnValue, "a b c d e f g ") <<
  77. "All values should be printed in the correct order";
  78. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h");
  79. EXPECT_STREQ(returnValue, "a b c d e f g h ") <<
  80. "All values should be printed in the correct order";
  81. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h", "i");
  82. EXPECT_STREQ(returnValue, "a b c d e f g h i ") <<
  83. "All values should be printed in the correct order";
  84. returnValue = Con::executef("testExecutef", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
  85. EXPECT_STREQ(returnValue, "a b c d e f g h i j ") <<
  86. "All values should be printed in the correct order";
  87. // Test type conversions with and without SimObject...
  88. // Integer
  89. returnValue = Con::executef(testObject, "testThisFunction", 123);
  90. EXPECT_STREQ(returnValue, "123 ") <<
  91. "Integer should be converted";
  92. returnValue = Con::executef("testExecutef", 123);
  93. EXPECT_STREQ(returnValue, "123 ") <<
  94. "Integer should be converted";
  95. // Float
  96. returnValue = Con::executef(testObject, "testThisFunction", (F32)123.0);
  97. EXPECT_STREQ(returnValue, "123 ") <<
  98. "Float should be converted";
  99. returnValue = Con::executef("testExecutef", (F32)123.0);
  100. EXPECT_STREQ(returnValue, "123 ") <<
  101. "Float should be converted";
  102. // SimObject
  103. dSprintf(buffer, sizeof(buffer), "%i ", testObject->getId());
  104. returnValue = Con::executef(testObject, "testThisFunction", testObject);
  105. EXPECT_STREQ(returnValue, buffer) <<
  106. "SimObject should be converted";
  107. dSprintf(buffer, sizeof(buffer), "%i ", testObject->getId());
  108. returnValue = Con::executef("testExecutef", testObject);
  109. EXPECT_STREQ(returnValue, buffer) <<
  110. "SimObject should be converted";
  111. // Point3F
  112. Point3F point(1,2,3);
  113. returnValue = Con::executef(testObject, "testThisFunction", point);
  114. EXPECT_STREQ(returnValue, "1 2 3 ") <<
  115. "Point3F should be converted";
  116. returnValue = Con::executef("testExecutef", point);
  117. EXPECT_STREQ(returnValue, "1 2 3 ") <<
  118. "Point3F should be converted";
  119. // Finally test the function arg offset. This should be consistently 0 after each call
  120. EXPECT_EQ(STR.mFunctionOffset, 0) <<
  121. "Function offset should be 0";
  122. const char *floatArg = Con::getFloatArg(1.23);
  123. EXPECT_GT(STR.mFunctionOffset, 0) <<
  124. "Function offset should not be 0";
  125. Con::executef("testExecutef", floatArg);
  126. EXPECT_EQ(STR.mFunctionOffset, 0) <<
  127. "Function offset should be 0";
  128. floatArg = Con::getFloatArg(1.23);
  129. EXPECT_GT(STR.mFunctionOffset, 0) <<
  130. "Function offset should not be 0";
  131. Con::executef("testImaginaryFunction_", floatArg);
  132. EXPECT_EQ(STR.mFunctionOffset, 0) <<
  133. "Function offset should be 0";
  134. }
  135. TEST(Con, execute)
  136. {
  137. 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");
  138. U32 startStackPos = gEvalState.getStackDepth();
  139. U32 startStringStackPos = STR.mStart;
  140. SimObject *testObject = NULL;
  141. Sim::findObject("TestConExec", testObject);
  142. EXPECT_TRUE(testObject != NULL)
  143. << "TestConExec object should exist";
  144. // const char* versions of execute should maintain stack
  145. const char *argv[] = {"testScriptExecuteFunction", "1", "2"};
  146. const char *argvObject[] = {"testScriptExecuteFunction", "", "1", "2"};
  147. ConsoleValue returnValue = Con::execute(3, argv);
  148. EXPECT_STREQ(returnValue, "1 2") <<
  149. "execute should return 1 2";
  150. EXPECT_EQ(gEvalState.getStackDepth(), startStackPos) <<
  151. "execute should restore stack";
  152. returnValue = Con::execute(testObject, 4, argvObject);
  153. EXPECT_STREQ(returnValue, "1 2") <<
  154. "execute should return 1 2";
  155. EXPECT_EQ(gEvalState.getStackDepth(), startStackPos) <<
  156. "execute should restore stack";
  157. }
  158. #endif