ScriptTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. ConsoleValue mod = RunScript(R"(
  54. return 4 % 5;
  55. )");
  56. ASSERT_EQ(mod.getInt(), 4);
  57. ConsoleValue add2 = RunScript(R"(
  58. $a = 0;
  59. $a += 2;
  60. return $a;
  61. )");
  62. ASSERT_EQ(add2.getInt(), 2);
  63. ConsoleValue sub2 = RunScript(R"(
  64. $a = 0;
  65. $a -= 2;
  66. return $a;
  67. )");
  68. ASSERT_EQ(sub2.getInt(), -2);
  69. ConsoleValue mult2 = RunScript(R"(
  70. $a = 2;
  71. $a *= 3;
  72. return $a;
  73. )");
  74. ASSERT_EQ(mult2.getInt(), 6);
  75. ConsoleValue div2 = RunScript(R"(
  76. $a = 10;
  77. $a /= 2;
  78. return $a;
  79. )");
  80. ASSERT_EQ(div2.getInt(), 5);
  81. ConsoleValue pp = RunScript(R"(
  82. $a = 0;
  83. $a++;
  84. return $a;
  85. )");
  86. ASSERT_EQ(pp.getInt(), 1);
  87. ConsoleValue mm = RunScript(R"(
  88. $a = 2;
  89. $a--;
  90. return $a;
  91. )");
  92. ASSERT_EQ(mm.getInt(), 1);
  93. }
  94. TEST(Script, Complex_Arithmetic)
  95. {
  96. ConsoleValue result = RunScript(R"(
  97. return 1 * 2 - (0.5 * 2);
  98. )");
  99. ASSERT_EQ(result.getInt(), 1);
  100. ConsoleValue result2 = RunScript(R"(
  101. return 1 * 2 * 3 % 2;
  102. )");
  103. ASSERT_EQ(result2.getInt(), 0);
  104. }
  105. TEST(Script, Basic_Concatination)
  106. {
  107. ConsoleValue result1 = RunScript(R"(
  108. return "a" @ "b";
  109. )");
  110. ASSERT_STREQ(result1.getString(), "ab");
  111. ConsoleValue result2 = RunScript(R"(
  112. return "a" SPC "b";
  113. )");
  114. ASSERT_STREQ(result2.getString(), "a b");
  115. ConsoleValue result3 = RunScript(R"(
  116. return "a" TAB "b";
  117. )");
  118. ASSERT_STREQ(result3.getString(), "a\tb");
  119. ConsoleValue result4 = RunScript(R"(
  120. return "a" NL "b";
  121. )");
  122. ASSERT_STREQ(result4.getString(), "a\nb");
  123. ConsoleValue complex = RunScript(R"(
  124. return "a" @ "b" @ "c" @ "d";
  125. )");
  126. ASSERT_STREQ(complex.getString(), "abcd");
  127. }
  128. TEST(Script, Basic_Global_Variable_Tests)
  129. {
  130. ConsoleValue value = RunScript(R"(
  131. $a = 1;
  132. return $a;
  133. )");
  134. ASSERT_EQ(value.getInt(), 1);
  135. }
  136. TEST(Script, Variable_Chaining_And_Usage)
  137. {
  138. ConsoleValue value = RunScript(R"(
  139. function t()
  140. {
  141. %a = %b = 2;
  142. return %a;
  143. }
  144. return t();
  145. )");
  146. ASSERT_EQ(value.getInt(), 2);
  147. ConsoleValue valueGlobal = RunScript(R"(
  148. function t()
  149. {
  150. $a = %b = 2;
  151. }
  152. t();
  153. return $a;
  154. )");
  155. ASSERT_EQ(valueGlobal.getInt(), 2);
  156. ConsoleValue value2 = RunScript(R"(
  157. function t(%a)
  158. {
  159. if ((%b = 2 * %a) != 5)
  160. return %b;
  161. return 5;
  162. }
  163. return t(2);
  164. )");
  165. ASSERT_EQ(value2.getInt(), 4);
  166. }
  167. TEST(Script, Basic_Function_Call_And_Local_Variable_Testing)
  168. {
  169. ConsoleValue value = RunScript(R"(
  170. function t() { %a = 2; return %a; }
  171. return t();
  172. )");
  173. ASSERT_EQ(value.getInt(), 2);
  174. ConsoleValue value2 = RunScript(R"(
  175. function add(%a, %b) { return %a + %b; }
  176. return add(2, 4);
  177. )");
  178. ASSERT_EQ(value2.getInt(), 6);
  179. ConsoleValue value3 = RunScript(R"(
  180. function fib(%a) {
  181. if (%a == 0)
  182. return 0;
  183. if (%a == 1)
  184. return 1;
  185. return fib(%a - 1) + fib(%a - 2);
  186. }
  187. return fib(15);
  188. )");
  189. ASSERT_EQ(value3.getInt(), 610);
  190. ConsoleValue staticCall = RunScript(R"(
  191. function SimObject::bar(%a, %b) {
  192. return %a + %b;
  193. }
  194. return SimObject::bar(1, 2);
  195. )");
  196. ASSERT_EQ(staticCall.getInt(), 3);
  197. }
  198. TEST(Script, Basic_Conditional_Statements)
  199. {
  200. ConsoleValue value = RunScript(R"(
  201. $a = "hello";
  202. if ($a $= "hello")
  203. return 1;
  204. return 2;
  205. )");
  206. ASSERT_EQ(value.getInt(), 1);
  207. ConsoleValue ternaryValue = RunScript(R"(
  208. return $a $= "hello" ? "World" : "No U";
  209. )");
  210. ASSERT_STREQ(ternaryValue.getString(), "World");
  211. }
  212. TEST(Script, Basic_Loop_Statements)
  213. {
  214. ConsoleValue whileValue = RunScript(R"(
  215. $count = 0;
  216. while ($count < 5)
  217. $count++;
  218. return $count;
  219. )");
  220. ASSERT_EQ(whileValue.getInt(), 5);
  221. ConsoleValue forValue = RunScript(R"(
  222. function t(%times)
  223. {
  224. %result = "";
  225. for (%i = 0; %i < %times; %i++)
  226. %result = %result @ "a";
  227. return %result;
  228. }
  229. return t(3);
  230. )");
  231. ASSERT_STREQ(forValue.getString(), "aaa");
  232. ConsoleValue forReverseLoop = RunScript(R"(
  233. function t(%times)
  234. {
  235. %result = "";
  236. for (%i = %times - 1; %i >= 0; %i--)
  237. %result = %result @ "b";
  238. return %result;
  239. }
  240. return t(3);
  241. )");
  242. ASSERT_STREQ(forReverseLoop.getString(), "bbb");
  243. ConsoleValue forIfValue = RunScript(R"(
  244. function t()
  245. {
  246. %str = "";
  247. for (%i = 0; %i < 5; %i++)
  248. {
  249. %loopValue = %i;
  250. if (%str $= "")
  251. %str = %loopValue;
  252. else
  253. %str = %str @ "," SPC %loopValue;
  254. }
  255. return %str;
  256. }
  257. return t();
  258. )");
  259. ASSERT_STREQ(forIfValue.getString(), "0, 1, 2, 3, 4");
  260. }
  261. TEST(Script, ForEachLoop)
  262. {
  263. ConsoleValue forEach1 = RunScript(R"(
  264. $theSimSet = new SimSet();
  265. $theSimSet.add(new SimObject());
  266. $theSimSet.add(new SimObject());
  267. $counter = 0;
  268. foreach ($obj in $theSimSet)
  269. {
  270. $counter++;
  271. }
  272. $theSimSet.delete();
  273. return $counter;
  274. )");
  275. ASSERT_EQ(forEach1.getInt(), 2);
  276. ConsoleValue forEach2 = RunScript(R"(
  277. $counter = 0;
  278. foreach$ ($word in "a b c d")
  279. {
  280. $counter++;
  281. }
  282. return $counter;
  283. )");
  284. ASSERT_EQ(forEach2.getInt(), 4);
  285. ConsoleValue forEach3 = RunScript(R"(
  286. function SimObject::addOne(%this)
  287. {
  288. return 1;
  289. }
  290. function test()
  291. {
  292. %set = new SimSet();
  293. %set.add(new SimObject());
  294. %set.add(new SimObject());
  295. %count = 0;
  296. foreach (%obj in %set)
  297. %count += %obj.addOne();
  298. %set.delete();
  299. return %count;
  300. }
  301. return test();
  302. )");
  303. ASSERT_EQ(forEach3.getInt(), 2);
  304. ConsoleValue forEach4 = RunScript(R"(
  305. function test()
  306. {
  307. %string = "a b c d e";
  308. %count = 0;
  309. foreach$ (%word in %string)
  310. %count++;
  311. return %count;
  312. }
  313. return test();
  314. )");
  315. ASSERT_EQ(forEach4.getInt(), 5);
  316. }
  317. TEST(Script, TorqueScript_Array_Testing)
  318. {
  319. ConsoleValue value = RunScript(R"(
  320. function t(%idx) { %a[%idx] = 2; return %a[%idx]; }
  321. return t(5);
  322. )");
  323. ASSERT_EQ(value.getInt(), 2);
  324. ConsoleValue value2 = RunScript(R"(
  325. function t(%idx) { %a[%idx, 0] = 2; return %a[%idx, 0]; }
  326. return t(5);
  327. )");
  328. ASSERT_EQ(value2.getInt(), 2);
  329. }
  330. TEST(Script, Basic_SimObject)
  331. {
  332. ConsoleValue object = RunScript(R"(
  333. return new SimObject(FudgeCollector)
  334. {
  335. fudge = "Chocolate";
  336. };
  337. )");
  338. ASSERT_NE(Sim::findObject(object), (SimObject*)NULL);
  339. ConsoleValue propertyValue = RunScript(R"(
  340. return FudgeCollector.fudge;
  341. )");
  342. ASSERT_STREQ(propertyValue.getString(), "Chocolate");
  343. ConsoleValue funcReturn = RunScript(R"(
  344. function SimObject::fooFunc(%this)
  345. {
  346. return "Bar";
  347. }
  348. return FudgeCollector.fooFunc();
  349. )");
  350. ASSERT_STREQ(funcReturn.getString(), "Bar");
  351. ConsoleValue parentFn = RunScript(R"(
  352. new SimObject(Hello);
  353. function SimObject::fooFunc2(%this)
  354. {
  355. return "Bar";
  356. }
  357. function Hello::fooFunc2(%this)
  358. {
  359. %bar = Parent::fooFunc2(%this);
  360. return "Foo" @ %bar;
  361. }
  362. return Hello.fooFunc2();
  363. )");
  364. ASSERT_STREQ(parentFn.getString(), "FooBar");
  365. ConsoleValue grp = RunScript(R"(
  366. new SimGroup(FudgeCollectorGroup)
  367. {
  368. theName = "fudge";
  369. new SimObject(ChocolateFudge)
  370. {
  371. type = "Chocolate";
  372. };
  373. new SimObject(PeanutButterFudge)
  374. {
  375. type = "Peanut Butter";
  376. field["a"] = "Yes";
  377. };
  378. };
  379. return FudgeCollectorGroup.getId();
  380. )");
  381. SimGroup* simGroup = dynamic_cast<SimGroup*>(Sim::findObject(grp));
  382. ASSERT_NE(simGroup, (SimGroup*)NULL);
  383. ASSERT_EQ(simGroup->size(), 2);
  384. simGroup->deleteObject();
  385. ConsoleValue fieldTest = RunScript(R"(
  386. function a()
  387. {
  388. %obj = new SimObject();
  389. %obj.field = "A";
  390. %obj.val[%obj.field] = "B";
  391. %value = %obj.val["A"];
  392. %obj.delete();
  393. return %value;
  394. }
  395. return a();
  396. )");
  397. ASSERT_STREQ(fieldTest.getString(), "B");
  398. ConsoleValue fieldOpTest = RunScript(R"(
  399. function a()
  400. {
  401. %obj = new SimObject();
  402. %obj.field = 1;
  403. %obj.field += 2;
  404. %value = %obj.field;
  405. %obj.delete();
  406. return %value;
  407. }
  408. return a();
  409. )");
  410. ASSERT_EQ(fieldOpTest.getInt(), 3);
  411. }
  412. TEST(Script, Internal_Name)
  413. {
  414. ConsoleValue value = RunScript(R"(
  415. function SimObject::_internalCall(%this)
  416. {
  417. return 5;
  418. }
  419. function a()
  420. {
  421. %grp = new SimGroup();
  422. %obj = new SimObject()
  423. {
  424. internalName = "Yay";
  425. };
  426. %grp.add(%obj);
  427. %val = %grp->Yay._internalCall();
  428. %grp.delete();
  429. return %val;
  430. }
  431. return a();
  432. )");
  433. ASSERT_EQ(value.getInt(), 5);
  434. }
  435. TEST(Script, Basic_Package)
  436. {
  437. ConsoleValue value = RunScript(R"(
  438. function a() { return 3; }
  439. package overrides
  440. {
  441. function a() { return 5; }
  442. };
  443. return a();
  444. )");
  445. ASSERT_EQ(value.getInt(), 3);
  446. ConsoleValue overrideValue = RunScript(R"(
  447. activatePackage(overrides);
  448. return a();
  449. )");
  450. ASSERT_EQ(overrideValue.getInt(), 5);
  451. ConsoleValue deactivatedValue = RunScript(R"(
  452. deactivatePackage(overrides);
  453. return a();
  454. )");
  455. ASSERT_EQ(deactivatedValue.getInt(), 3);
  456. }
  457. #endif