ScriptTest.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/platform.h"
  25. #include "console/simBase.h"
  26. #include "console/consoleTypes.h"
  27. #include "console/simBase.h"
  28. #include "console/engineAPI.h"
  29. #include "math/mMath.h"
  30. #include "console/stringStack.h"
  31. inline ConsoleValue RunScript(const char* str)
  32. {
  33. return std::move(Con::evaluate(str, false, NULL));
  34. }
  35. TEST(Script, Basic_Arithmetic)
  36. {
  37. ConsoleValue add = RunScript(R"(
  38. return 1.0 + 1;
  39. )");
  40. ASSERT_EQ(add.getInt(), 2);
  41. ConsoleValue sub = RunScript(R"(
  42. return 10 - 1.0;
  43. )");
  44. ASSERT_EQ(sub.getInt(), 9);
  45. ConsoleValue mult = RunScript(R"(
  46. return 10 * 2.5;
  47. )");
  48. ASSERT_EQ(mult.getInt(), 25);
  49. ConsoleValue div = RunScript(R"(
  50. return 10.0 / 2;
  51. )");
  52. ASSERT_EQ(div.getInt(), 5);
  53. }
  54. TEST(Script, Complex_Arithmetic)
  55. {
  56. ConsoleValue result = RunScript(R"(
  57. return 1 * 2 - (0.5 * 2);
  58. )");
  59. ASSERT_EQ(result.getInt(), 1);
  60. ConsoleValue result2 = RunScript(R"(
  61. return 1 * 2 * 3 % 2;
  62. )");
  63. ASSERT_EQ(result2.getInt(), 0);
  64. }
  65. TEST(Script, Basic_Concatination)
  66. {
  67. ConsoleValue result1 = RunScript(R"(
  68. return "a" @ "b";
  69. )");
  70. ASSERT_STREQ(result1.getString(), "ab");
  71. ConsoleValue result2 = RunScript(R"(
  72. return "a" SPC "b";
  73. )");
  74. ASSERT_STREQ(result2.getString(), "a b");
  75. ConsoleValue result3 = RunScript(R"(
  76. return "a" TAB "b";
  77. )");
  78. ASSERT_STREQ(result3.getString(), "a\tb");
  79. ConsoleValue result4 = RunScript(R"(
  80. return "a" NL "b";
  81. )");
  82. ASSERT_STREQ(result4.getString(), "a\nb");
  83. ConsoleValue complex = RunScript(R"(
  84. return "a" @ "b" @ "c" @ "d";
  85. )");
  86. ASSERT_STREQ(complex.getString(), "abcd");
  87. }
  88. TEST(Script, Basic_Global_Variable_Tests)
  89. {
  90. ConsoleValue value = RunScript(R"(
  91. $a = 1;
  92. return $a;
  93. )");
  94. ASSERT_EQ(value.getInt(), 1);
  95. }
  96. TEST(Script, Variable_Chaining_And_Usage)
  97. {
  98. ConsoleValue value = RunScript(R"(
  99. function t()
  100. {
  101. %a = %b = 2;
  102. return %a;
  103. }
  104. return t();
  105. )");
  106. ASSERT_EQ(value.getInt(), 2);
  107. ConsoleValue valueGlobal = RunScript(R"(
  108. function t()
  109. {
  110. $a = %b = 2;
  111. }
  112. t();
  113. return $a;
  114. )");
  115. ASSERT_EQ(valueGlobal.getInt(), 2);
  116. ConsoleValue value2 = RunScript(R"(
  117. function t(%a)
  118. {
  119. if ((%b = 2 * %a) != 5)
  120. return %b;
  121. return 5;
  122. }
  123. return t(2);
  124. )");
  125. ASSERT_EQ(value2.getInt(), 4);
  126. }
  127. TEST(Script, Basic_Function_Call_And_Local_Variable_Testing)
  128. {
  129. ConsoleValue value = RunScript(R"(
  130. function t() { %a = 2; return %a; }
  131. return t();
  132. )");
  133. ASSERT_EQ(value.getInt(), 2);
  134. ConsoleValue value2 = RunScript(R"(
  135. function add(%a, %b) { return %a + %b; }
  136. return add(2, 4);
  137. )");
  138. ASSERT_EQ(value2.getInt(), 6);
  139. ConsoleValue value3 = RunScript(R"(
  140. function fib(%a) {
  141. if (%a == 0)
  142. return 0;
  143. if (%a == 1)
  144. return 1;
  145. return fib(%a - 1) + fib(%a - 2);
  146. }
  147. return fib(15);
  148. )");
  149. ASSERT_EQ(value3.getInt(), 610);
  150. ConsoleValue staticCall = RunScript(R"(
  151. function SimObject::bar(%a, %b) {
  152. return %a + %b;
  153. }
  154. return SimObject::bar(1, 2);
  155. )");
  156. ASSERT_EQ(staticCall.getInt(), 3);
  157. }
  158. TEST(Script, Basic_Conditional_Statements)
  159. {
  160. ConsoleValue value = RunScript(R"(
  161. $a = "hello";
  162. if ($a $= "hello")
  163. return 1;
  164. return 2;
  165. )");
  166. ASSERT_EQ(value.getInt(), 1);
  167. ConsoleValue ternaryValue = RunScript(R"(
  168. return $a $= "hello" ? "World" : "No U";
  169. )");
  170. ASSERT_STREQ(ternaryValue.getString(), "World");
  171. }
  172. TEST(Script, Basic_Loop_Statements)
  173. {
  174. ConsoleValue whileValue = RunScript(R"(
  175. $count = 0;
  176. while ($count < 5)
  177. $count++;
  178. return $count;
  179. )");
  180. ASSERT_EQ(whileValue.getInt(), 5);
  181. ConsoleValue forValue = RunScript(R"(
  182. function t(%times)
  183. {
  184. %result = "";
  185. for (%i = 0; %i < %times; %i++)
  186. %result = %result @ "a";
  187. return %result;
  188. }
  189. return t(3);
  190. )");
  191. ASSERT_STREQ(forValue.getString(), "aaa");
  192. ConsoleValue forIfValue = RunScript(R"(
  193. function t() {
  194. %str = "";
  195. for (%i = 0; %i < 5; %i++) {
  196. %loopValue = %i;
  197. if (%str $= "")
  198. %str = %loopValue;
  199. else
  200. %str = %str @ "," SPC %loopValue;
  201. }
  202. return %str;
  203. }
  204. return t();
  205. )");
  206. ASSERT_STREQ(forIfValue.getString(), "0, 1, 2, 3, 4");
  207. }
  208. TEST(Script, TorqueScript_Array_Testing)
  209. {
  210. ConsoleValue value = RunScript(R"(
  211. function t(%idx) { %a[idx] = 2; return %a[idx]; }
  212. return t(5);
  213. )");
  214. ASSERT_EQ(value.getInt(), 2);
  215. ConsoleValue value2 = RunScript(R"(
  216. function t(%idx) { %a[idx, 0] = 2; return %a[idx, 0]; }
  217. return t(5);
  218. )");
  219. ASSERT_EQ(value2.getInt(), 2);
  220. }
  221. TEST(Script, Basic_SimObject)
  222. {
  223. ConsoleValue object = RunScript(R"(
  224. return new SimObject(FudgeCollector) {
  225. fudge = "Chocolate";
  226. };
  227. )");
  228. ASSERT_NE(Sim::findObject(object), (SimObject*)NULL);
  229. ConsoleValue propertyValue = RunScript(R"(
  230. return FudgeCollector.fudge;
  231. )");
  232. ASSERT_STREQ(propertyValue.getString(), "Chocolate");
  233. ConsoleValue funcReturn = RunScript(R"(
  234. function SimObject::fooFunc(%this)
  235. {
  236. return "Bar";
  237. }
  238. return FudgeCollector.fooFunc();
  239. )");
  240. ASSERT_STREQ(funcReturn.getString(), "Bar");
  241. ConsoleValue parentFn = RunScript(R"(
  242. new SimObject(Hello);
  243. function SimObject::fooFunc2(%this)
  244. {
  245. return "Bar";
  246. }
  247. function Hello::fooFunc2(%this)
  248. {
  249. %bar = Parent::fooFunc2(%this);
  250. return "Foo" @ %bar;
  251. }
  252. return Hello.fooFunc2();
  253. )");
  254. ASSERT_STREQ(parentFn.getString(), "FooBar");
  255. }
  256. TEST(Script, Basic_Package)
  257. {
  258. ConsoleValue value = RunScript(R"(
  259. function a() { return 3; }
  260. package overrides {
  261. function a() { return 5; }
  262. };
  263. return a();
  264. )");
  265. ASSERT_EQ(value.getInt(), 3);
  266. ConsoleValue overrideValue = RunScript(R"(
  267. activatePackage(overrides);
  268. return a();
  269. )");
  270. ASSERT_EQ(overrideValue.getInt(), 5);
  271. ConsoleValue deactivatedValue = RunScript(R"(
  272. deactivatePackage(overrides);
  273. return a();
  274. )");
  275. ASSERT_EQ(deactivatedValue.getInt(), 3);
  276. }
  277. #endif