consoleTest.cpp 7.0 KB

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