ScriptTest.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. {
  195. %str = "";
  196. for (%i = 0; %i < 5; %i++)
  197. {
  198. %loopValue = %i;
  199. if (%str $= "")
  200. %str = %loopValue;
  201. else
  202. %str = %str @ "," SPC %loopValue;
  203. }
  204. return %str;
  205. }
  206. return t();
  207. )");
  208. ASSERT_STREQ(forIfValue.getString(), "0, 1, 2, 3, 4");
  209. }
  210. TEST(Script, ForEachLoop)
  211. {
  212. ConsoleValue forEach1 = RunScript(R"(
  213. $theSimSet = new SimSet();
  214. $theSimSet.add(new SimObject());
  215. $theSimSet.add(new SimObject());
  216. $counter = 0;
  217. foreach ($obj in $theSimSet)
  218. {
  219. $counter++;
  220. }
  221. $theSimSet.delete();
  222. return $counter;
  223. )");
  224. ASSERT_EQ(forEach1.getInt(), 2);
  225. ConsoleValue forEach2 = RunScript(R"(
  226. $counter = 0;
  227. foreach$ ($word in "a b c d")
  228. {
  229. $counter++;
  230. }
  231. return $counter;
  232. )");
  233. ASSERT_EQ(forEach2.getInt(), 4);
  234. ConsoleValue forEach3 = RunScript(R"(
  235. function SimObject::addOne(%this)
  236. {
  237. return 1;
  238. }
  239. function test()
  240. {
  241. %set = new SimSet();
  242. %set.add(new SimObject());
  243. %set.add(new SimObject());
  244. %count = 0;
  245. foreach (%obj in %set)
  246. %count += %obj.addOne();
  247. %set.delete();
  248. return %count;
  249. }
  250. return test();
  251. )");
  252. ASSERT_EQ(forEach3.getInt(), 2);
  253. ConsoleValue forEach4 = RunScript(R"(
  254. function test()
  255. {
  256. %string = "a b c d e";
  257. %count = 0;
  258. foreach$ (%word in %string)
  259. %count++;
  260. return %count;
  261. }
  262. return test();
  263. )");
  264. ASSERT_EQ(forEach4.getInt(), 5);
  265. }
  266. TEST(Script, TorqueScript_Array_Testing)
  267. {
  268. ConsoleValue value = RunScript(R"(
  269. function t(%idx) { %a[idx] = 2; return %a[idx]; }
  270. return t(5);
  271. )");
  272. ASSERT_EQ(value.getInt(), 2);
  273. ConsoleValue value2 = RunScript(R"(
  274. function t(%idx) { %a[idx, 0] = 2; return %a[idx, 0]; }
  275. return t(5);
  276. )");
  277. ASSERT_EQ(value2.getInt(), 2);
  278. }
  279. TEST(Script, Basic_SimObject)
  280. {
  281. ConsoleValue object = RunScript(R"(
  282. return new SimObject(FudgeCollector)
  283. {
  284. fudge = "Chocolate";
  285. };
  286. )");
  287. ASSERT_NE(Sim::findObject(object), (SimObject*)NULL);
  288. ConsoleValue propertyValue = RunScript(R"(
  289. return FudgeCollector.fudge;
  290. )");
  291. ASSERT_STREQ(propertyValue.getString(), "Chocolate");
  292. ConsoleValue funcReturn = RunScript(R"(
  293. function SimObject::fooFunc(%this)
  294. {
  295. return "Bar";
  296. }
  297. return FudgeCollector.fooFunc();
  298. )");
  299. ASSERT_STREQ(funcReturn.getString(), "Bar");
  300. ConsoleValue parentFn = RunScript(R"(
  301. new SimObject(Hello);
  302. function SimObject::fooFunc2(%this)
  303. {
  304. return "Bar";
  305. }
  306. function Hello::fooFunc2(%this)
  307. {
  308. %bar = Parent::fooFunc2(%this);
  309. return "Foo" @ %bar;
  310. }
  311. return Hello.fooFunc2();
  312. )");
  313. ASSERT_STREQ(parentFn.getString(), "FooBar");
  314. }
  315. TEST(Script, Basic_Package)
  316. {
  317. ConsoleValue value = RunScript(R"(
  318. function a() { return 3; }
  319. package overrides
  320. {
  321. function a() { return 5; }
  322. };
  323. return a();
  324. )");
  325. ASSERT_EQ(value.getInt(), 3);
  326. ConsoleValue overrideValue = RunScript(R"(
  327. activatePackage(overrides);
  328. return a();
  329. )");
  330. ASSERT_EQ(overrideValue.getInt(), 5);
  331. ConsoleValue deactivatedValue = RunScript(R"(
  332. deactivatePackage(overrides);
  333. return a();
  334. )");
  335. ASSERT_EQ(deactivatedValue.getInt(), 3);
  336. }
  337. #endif