consoleTest.cpp 7.0 KB

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