ScriptTest.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. #include "testing/unitTesting.h"
  23. #include "platform/platform.h"
  24. #include "console/simBase.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/simBase.h"
  27. #include "console/engineAPI.h"
  28. #include "math/mMath.h"
  29. #include "console/script.h"
  30. #include "console/stringStack.h"
  31. #include "gui/buttons/guiIconButtonCtrl.h"
  32. inline ConsoleValue RunScript(const char* str)
  33. {
  34. return std::move(Con::evaluate(str, false, NULL).value);
  35. }
  36. using ::testing::Matcher;
  37. using ::testing::TypedEq;
  38. class ScriptTest : public ::testing::Test
  39. {
  40. protected:
  41. ScriptTest()
  42. {
  43. }
  44. void SetUp() override
  45. {
  46. }
  47. };
  48. TEST_F(ScriptTest, Basic_Arithmetic)
  49. {
  50. ConsoleValue add = RunScript(R"(
  51. return 1.0 + 1;
  52. )");
  53. ASSERT_EQ(add.getInt(), 2);
  54. ConsoleValue sub = RunScript(R"(
  55. return 10 - 1.0;
  56. )");
  57. ASSERT_EQ(sub.getInt(), 9);
  58. ConsoleValue mult = RunScript(R"(
  59. return 10 * 2.5;
  60. )");
  61. ASSERT_EQ(mult.getInt(), 25);
  62. ConsoleValue div = RunScript(R"(
  63. return 10.0 / 2;
  64. )");
  65. ASSERT_EQ(div.getInt(), 5);
  66. ConsoleValue mod = RunScript(R"(
  67. return 4 % 5;
  68. )");
  69. ASSERT_EQ(mod.getInt(), 4);
  70. ConsoleValue add2 = RunScript(R"(
  71. $a = 0;
  72. $a += 2;
  73. return $a;
  74. )");
  75. ASSERT_EQ(add2.getInt(), 2);
  76. ConsoleValue sub2 = RunScript(R"(
  77. $a = 0;
  78. $a -= 2;
  79. return $a;
  80. )");
  81. ASSERT_EQ(sub2.getInt(), -2);
  82. ConsoleValue mult2 = RunScript(R"(
  83. $a = 2;
  84. $a *= 3;
  85. return $a;
  86. )");
  87. ASSERT_EQ(mult2.getInt(), 6);
  88. ConsoleValue div2 = RunScript(R"(
  89. $a = 10;
  90. $a /= 2;
  91. return $a;
  92. )");
  93. ASSERT_EQ(div2.getInt(), 5);
  94. ConsoleValue pp = RunScript(R"(
  95. $a = 0;
  96. $a++;
  97. return $a;
  98. )");
  99. ASSERT_EQ(pp.getInt(), 1);
  100. ConsoleValue mm = RunScript(R"(
  101. $a = 2;
  102. $a--;
  103. return $a;
  104. )");
  105. ASSERT_EQ(mm.getInt(), 1);
  106. }
  107. TEST_F(ScriptTest, Complex_Arithmetic)
  108. {
  109. ConsoleValue result = RunScript(R"(
  110. return 1 * 2 - (0.5 * 2);
  111. )");
  112. ASSERT_EQ(result.getInt(), 1);
  113. ConsoleValue result2 = RunScript(R"(
  114. return 1 * 2 * 3 % 2;
  115. )");
  116. ASSERT_EQ(result2.getInt(), 0);
  117. }
  118. TEST_F(ScriptTest, Basic_Concatination)
  119. {
  120. ConsoleValue result1 = RunScript(R"(
  121. return "a" @ "b";
  122. )");
  123. ASSERT_STREQ(result1.getString(), "ab");
  124. ConsoleValue result2 = RunScript(R"(
  125. return "a" SPC "b";
  126. )");
  127. ASSERT_STREQ(result2.getString(), "a b");
  128. ConsoleValue result3 = RunScript(R"(
  129. return "a" TAB "b";
  130. )");
  131. ASSERT_STREQ(result3.getString(), "a\tb");
  132. ConsoleValue result4 = RunScript(R"(
  133. return "a" NL "b";
  134. )");
  135. ASSERT_STREQ(result4.getString(), "a\nb");
  136. ConsoleValue complex = RunScript(R"(
  137. return "a" @ "b" @ "c" @ "d";
  138. )");
  139. ASSERT_STREQ(complex.getString(), "abcd");
  140. }
  141. TEST_F(ScriptTest, Basic_Global_Variable_Tests)
  142. {
  143. ConsoleValue value = RunScript(R"(
  144. $a = 1;
  145. return $a;
  146. )");
  147. ASSERT_EQ(value.getInt(), 1);
  148. }
  149. TEST_F(ScriptTest, Variable_Chaining_And_Usage)
  150. {
  151. ConsoleValue value = RunScript(R"(
  152. function t()
  153. {
  154. %a = %b = 2;
  155. return %a;
  156. }
  157. return t();
  158. )");
  159. ASSERT_EQ(value.getInt(), 2);
  160. ConsoleValue valueGlobal = RunScript(R"(
  161. function t()
  162. {
  163. $a = %b = 2;
  164. }
  165. t();
  166. return $a;
  167. )");
  168. ASSERT_EQ(valueGlobal.getInt(), 2);
  169. ConsoleValue value2 = RunScript(R"(
  170. function t(%a)
  171. {
  172. if ((%b = 2 * %a) != 5)
  173. return %b;
  174. return 5;
  175. }
  176. return t(2);
  177. )");
  178. ASSERT_EQ(value2.getInt(), 4);
  179. }
  180. TEST_F(ScriptTest, Basic_Function_Call_And_Local_Variable_Testing)
  181. {
  182. ConsoleValue value = RunScript(R"(
  183. function t() { %a = 2; return %a; }
  184. return t();
  185. )");
  186. ASSERT_EQ(value.getInt(), 2);
  187. ConsoleValue value2 = RunScript(R"(
  188. function add(%a, %b) { return %a + %b; }
  189. return add(2, 4);
  190. )");
  191. ASSERT_EQ(value2.getInt(), 6);
  192. ConsoleValue value3 = RunScript(R"(
  193. function fib(%a) {
  194. if (%a == 0)
  195. return 0;
  196. if (%a == 1)
  197. return 1;
  198. return fib(%a - 1) + fib(%a - 2);
  199. }
  200. return fib(15);
  201. )");
  202. ASSERT_EQ(value3.getInt(), 610);
  203. ConsoleValue staticCall = RunScript(R"(
  204. function SimObject::bar(%a, %b) {
  205. return %a + %b;
  206. }
  207. return SimObject::bar(1, 2);
  208. )");
  209. ASSERT_EQ(staticCall.getInt(), 3);
  210. }
  211. TEST_F(ScriptTest, Basic_Conditional_Statements)
  212. {
  213. ConsoleValue value = RunScript(R"(
  214. $a = "hello";
  215. if ($a $= "hello")
  216. return 1;
  217. return 2;
  218. )");
  219. ASSERT_EQ(value.getInt(), 1);
  220. ConsoleValue ternaryValue = RunScript(R"(
  221. return $a $= "hello" ? "World" : "No U";
  222. )");
  223. ASSERT_STREQ(ternaryValue.getString(), "World");
  224. }
  225. TEST_F(ScriptTest, Basic_Loop_Statements)
  226. {
  227. ConsoleValue whileValue = RunScript(R"(
  228. $count = 0;
  229. while ($count < 5)
  230. $count++;
  231. return $count;
  232. )");
  233. ASSERT_EQ(whileValue.getInt(), 5);
  234. ConsoleValue forValue = RunScript(R"(
  235. function t(%times)
  236. {
  237. %result = "";
  238. for (%i = 0; %i < %times; %i++)
  239. %result = %result @ "a";
  240. return %result;
  241. }
  242. return t(3);
  243. )");
  244. ASSERT_STREQ(forValue.getString(), "aaa");
  245. ConsoleValue forReverseLoop = RunScript(R"(
  246. function t(%times)
  247. {
  248. %result = "";
  249. for (%i = %times - 1; %i >= 0; %i--)
  250. %result = %result @ "b";
  251. return %result;
  252. }
  253. return t(3);
  254. )");
  255. ASSERT_STREQ(forReverseLoop.getString(), "bbb");
  256. ConsoleValue forIfValue = RunScript(R"(
  257. function t()
  258. {
  259. %str = "";
  260. for (%i = 0; %i < 5; %i++)
  261. {
  262. %loopValue = %i;
  263. if (%str $= "")
  264. %str = %loopValue;
  265. else
  266. %str = %str @ "," SPC %loopValue;
  267. }
  268. return %str;
  269. }
  270. return t();
  271. )");
  272. ASSERT_STREQ(forIfValue.getString(), "0, 1, 2, 3, 4");
  273. }
  274. TEST_F(ScriptTest, ForEachLoop)
  275. {
  276. ConsoleValue forEach1 = RunScript(R"(
  277. $theSimSet = new SimSet();
  278. $theSimSet.add(new SimObject());
  279. $theSimSet.add(new SimObject());
  280. $counter = 0;
  281. foreach ($obj in $theSimSet)
  282. {
  283. $counter++;
  284. }
  285. $theSimSet.delete();
  286. return $counter;
  287. )");
  288. ASSERT_EQ(forEach1.getInt(), 2);
  289. ConsoleValue forEach2 = RunScript(R"(
  290. $counter = 0;
  291. foreach$ ($word in "a b c d")
  292. {
  293. $counter++;
  294. }
  295. return $counter;
  296. )");
  297. ASSERT_EQ(forEach2.getInt(), 4);
  298. ConsoleValue forEach3 = RunScript(R"(
  299. function SimObject::addOne(%this)
  300. {
  301. return 1;
  302. }
  303. function test()
  304. {
  305. %set = new SimSet();
  306. %set.add(new SimObject());
  307. %set.add(new SimObject());
  308. %count = 0;
  309. foreach (%obj in %set)
  310. %count += %obj.addOne();
  311. %set.delete();
  312. return %count;
  313. }
  314. return test();
  315. )");
  316. ASSERT_EQ(forEach3.getInt(), 2);
  317. ConsoleValue forEach4 = RunScript(R"(
  318. function test()
  319. {
  320. %string = "a b c d e";
  321. %count = 0;
  322. foreach$ (%word in %string)
  323. %count++;
  324. return %count;
  325. }
  326. return test();
  327. )");
  328. ASSERT_EQ(forEach4.getInt(), 5);
  329. ConsoleValue forEach5 = RunScript(R"(
  330. function SimObject::ret1(%this)
  331. {
  332. return 1;
  333. }
  334. function SimSet::doForeach5(%this)
  335. {
  336. %count = 0;
  337. foreach (%obj in %this)
  338. {
  339. %count += %obj.ret1();
  340. }
  341. return %count;
  342. }
  343. function a()
  344. {
  345. %set = new SimSet();
  346. %set.add(new SimObject());
  347. %set.add(new SimObject());
  348. %set.add(new SimObject());
  349. return %set.doForeach5();
  350. }
  351. return a();
  352. )");
  353. ASSERT_EQ(forEach5.getInt(), 3);
  354. ConsoleValue forEachContinue = RunScript(R"(
  355. function SimSet::foreach6(%this)
  356. {
  357. %count = 0;
  358. foreach (%obj in %this)
  359. {
  360. if (%obj.getName() $= "A_FEC")
  361. continue;
  362. %count++;
  363. }
  364. return %count;
  365. }
  366. function a()
  367. {
  368. %set = new SimSet();
  369. %set.add(new SimObject(A_FEC));
  370. %set.add(new SimObject());
  371. %set.add(new SimObject());
  372. return %set.foreach6();
  373. }
  374. return a();
  375. )");
  376. ASSERT_EQ(forEachContinue.getInt(), 2);
  377. ConsoleValue forEachReturn = RunScript(R"(
  378. function SimSet::findA(%this)
  379. {
  380. foreach (%obj in %this)
  381. {
  382. if (%obj.getName() $= "A_FER")
  383. return 76;
  384. }
  385. return 0;
  386. }
  387. function a()
  388. {
  389. %set = new SimSet();
  390. %set.add(new SimObject(A_FER));
  391. %set.add(new SimObject());
  392. %set.add(new SimObject());
  393. return %set.findA();
  394. }
  395. return a();
  396. )");
  397. ASSERT_EQ(forEachReturn.getInt(), 76);
  398. ConsoleValue forEachNestedReturn = RunScript(R"(
  399. function SimSet::findA(%this)
  400. {
  401. foreach (%obj in %this)
  402. {
  403. foreach (%innerObj in %this)
  404. {
  405. if (%innerObj.getName() $= "A_FENR")
  406. return 42;
  407. }
  408. }
  409. return 0;
  410. }
  411. function a()
  412. {
  413. %set = new SimSet();
  414. %set.add(new SimObject(A_FENR));
  415. %set.add(new SimObject());
  416. %set.add(new SimObject());
  417. %group = new SimGroup();
  418. %group.add(%set);
  419. return %set.findA();
  420. }
  421. return a();
  422. )");
  423. ASSERT_EQ(forEachNestedReturn.getInt(), 42);
  424. ConsoleValue forEachNonExistantObject = RunScript(R"(
  425. $counter = 0;
  426. foreach ($obj in NonExistantSimSet)
  427. {
  428. $counter++;
  429. }
  430. return $counter;
  431. )");
  432. ASSERT_EQ(forEachNonExistantObject.getInt(), 0);
  433. ConsoleValue forEachOnZero = RunScript(R"(
  434. $counter = 0;
  435. foreach ($obj in 0)
  436. {
  437. $counter++;
  438. }
  439. return $counter;
  440. )");
  441. ASSERT_EQ(forEachOnZero.getInt(), 0);
  442. ConsoleValue forEachOnEmptyString = RunScript(R"(
  443. $counter = 0;
  444. foreach ($obj in "")
  445. {
  446. $counter++;
  447. }
  448. return $counter;
  449. )");
  450. ASSERT_EQ(forEachOnEmptyString.getInt(), 0);
  451. }
  452. TEST_F(ScriptTest, TorqueScript_Array_Testing)
  453. {
  454. ConsoleValue value = RunScript(R"(
  455. function t(%idx) { %a[%idx] = 2; return %a[%idx]; }
  456. return t(5);
  457. )");
  458. ASSERT_EQ(value.getInt(), 2);
  459. ConsoleValue value2 = RunScript(R"(
  460. function t(%idx) { %a[%idx, 0] = 2; return %a[%idx, 0]; }
  461. return t(5);
  462. )");
  463. ASSERT_EQ(value2.getInt(), 2);
  464. }
  465. TEST_F(ScriptTest, SimObject_Tests)
  466. {
  467. ConsoleValue object = RunScript(R"(
  468. return new SimObject(FudgeCollector)
  469. {
  470. fudge = "Chocolate";
  471. };
  472. )");
  473. ASSERT_NE(Sim::findObject(object), (SimObject*)NULL);
  474. ConsoleValue propertyValue = RunScript(R"(
  475. return FudgeCollector.fudge;
  476. )");
  477. ASSERT_STREQ(propertyValue.getString(), "Chocolate");
  478. ConsoleValue funcReturn = RunScript(R"(
  479. function SimObject::fooFunc(%this)
  480. {
  481. return "Bar";
  482. }
  483. return FudgeCollector.fooFunc();
  484. )");
  485. ASSERT_STREQ(funcReturn.getString(), "Bar");
  486. ConsoleValue parentFn = RunScript(R"(
  487. new SimObject(Hello);
  488. function SimObject::fooFunc2(%this)
  489. {
  490. return "Bar";
  491. }
  492. function Hello::fooFunc2(%this)
  493. {
  494. %bar = Parent::fooFunc2(%this);
  495. return "Foo" @ %bar;
  496. }
  497. return Hello.fooFunc2();
  498. )");
  499. ASSERT_STREQ(parentFn.getString(), "FooBar");
  500. ConsoleValue simpleFieldTest = RunScript(R"(
  501. function a()
  502. {
  503. FudgeCollector.field = "A";
  504. return FudgeCollector.field;
  505. }
  506. return a();
  507. )");
  508. ASSERT_STREQ(simpleFieldTest.getString(), "A");
  509. ConsoleValue grp = RunScript(R"(
  510. new SimGroup(FudgeCollectorGroup)
  511. {
  512. theName = "fudge";
  513. new SimObject(ChocolateFudge)
  514. {
  515. type = "Chocolate";
  516. };
  517. new SimObject(PeanutButterFudge)
  518. {
  519. type = "Peanut Butter";
  520. field["a"] = "Yes";
  521. };
  522. };
  523. return FudgeCollectorGroup.getId();
  524. )");
  525. SimGroup* simGroup = dynamic_cast<SimGroup*>(Sim::findObject(grp));
  526. ASSERT_NE(simGroup, (SimGroup*)NULL);
  527. ASSERT_EQ(simGroup->size(), 2);
  528. simGroup->deleteObject();
  529. ConsoleValue fieldTest = RunScript(R"(
  530. function a()
  531. {
  532. %obj = new SimObject();
  533. %obj.field = "A";
  534. %obj.val[%obj.field] = "B";
  535. %value = %obj.val["A"];
  536. %obj.delete();
  537. return %value;
  538. }
  539. return a();
  540. )");
  541. ASSERT_STREQ(fieldTest.getString(), "B");
  542. ConsoleValue fieldOpTest = RunScript(R"(
  543. function a()
  544. {
  545. %obj = new SimObject();
  546. %obj.field = 1;
  547. %obj.field += 2;
  548. %value = %obj.field;
  549. %obj.delete();
  550. return %value;
  551. }
  552. return a();
  553. )");
  554. ASSERT_EQ(fieldOpTest.getInt(), 3);
  555. ConsoleValue inheritedObjectTest = RunScript(R"(
  556. function SimObject::testClass(%this)
  557. {
  558. return 4;
  559. }
  560. function SuperFooBar::doSuperTest(%this)
  561. {
  562. return 5;
  563. }
  564. function FooBar::test(%this)
  565. {
  566. return 2;
  567. }
  568. new SimObject(GrandFooBar)
  569. {
  570. superClass = "SuperFooBar";
  571. };
  572. new SimObject(Foo : GrandFooBar)
  573. {
  574. class = "FooBar";
  575. };
  576. new SimObject(Bar : Foo);
  577. function Bar::doTheAddition(%this)
  578. {
  579. return %this.testClass() + %this.test() + %this.doSuperTest();
  580. }
  581. return Bar.doTheAddition();
  582. )");
  583. ASSERT_EQ(inheritedObjectTest.getInt(), 11);
  584. }
  585. TEST_F(ScriptTest, Internal_Name)
  586. {
  587. ConsoleValue value = RunScript(R"(
  588. function TheFirstInner::_internalCall(%this)
  589. {
  590. return 5;
  591. }
  592. function a()
  593. {
  594. %grp = new SimGroup();
  595. %obj = new SimObject(TheFirstInner)
  596. {
  597. internalName = "Yay";
  598. };
  599. %grp.add(%obj);
  600. %val = %grp->Yay._internalCall();
  601. %grp.delete();
  602. return %val;
  603. }
  604. return a();
  605. )");
  606. ASSERT_EQ(value.getInt(), 5);
  607. ConsoleValue recursiveValue = RunScript(R"(
  608. function SimGroup::doTheInternalCall(%this)
  609. {
  610. return %this-->Yeah._internalCall2();
  611. }
  612. function TheAnotherObject::_internalCall2(%this)
  613. {
  614. return %this.property;
  615. }
  616. function a()
  617. {
  618. %grp = new SimGroup();
  619. %obj = new SimGroup()
  620. {
  621. internalName = "Yay2";
  622. new SimObject(TheAnotherObject)
  623. {
  624. internalName = "Yeah";
  625. property = 12;
  626. };
  627. };
  628. %grp.add(%obj);
  629. %val = %grp.doTheInternalCall();
  630. %grp.delete();
  631. return %val;
  632. }
  633. return a();
  634. )");
  635. ASSERT_EQ(recursiveValue.getInt(), 12);
  636. }
  637. TEST_F(ScriptTest, Basic_Package)
  638. {
  639. ConsoleValue value = RunScript(R"(
  640. function a() { return 3; }
  641. package overrides
  642. {
  643. function a() { return 5; }
  644. };
  645. return a();
  646. )");
  647. ASSERT_EQ(value.getInt(), 3);
  648. ConsoleValue overrideValue = RunScript(R"(
  649. activatePackage(overrides);
  650. return a();
  651. )");
  652. ASSERT_EQ(overrideValue.getInt(), 5);
  653. ConsoleValue deactivatedValue = RunScript(R"(
  654. deactivatePackage(overrides);
  655. return a();
  656. )");
  657. ASSERT_EQ(deactivatedValue.getInt(), 3);
  658. }
  659. TEST_F(ScriptTest, Sugar_Syntax)
  660. {
  661. ConsoleValue value = RunScript(R"(
  662. function a()
  663. {
  664. %vector = "1 2 3";
  665. return %vector.y;
  666. }
  667. return a();
  668. )");
  669. ASSERT_EQ(value.getInt(), 2);
  670. ConsoleValue setValue = RunScript(R"(
  671. function a()
  672. {
  673. %vector = "1 2 3";
  674. %vector.y = 4;
  675. return %vector.y;
  676. }
  677. return a();
  678. )");
  679. ASSERT_EQ(setValue.getInt(), 4);
  680. ConsoleValue valueArray = RunScript(R"(
  681. function a()
  682. {
  683. %vector[0] = "1 2 3";
  684. return %vector[0].y;
  685. }
  686. return a();
  687. )");
  688. ASSERT_EQ(valueArray.getInt(), 2);
  689. ConsoleValue valueSetArray = RunScript(R"(
  690. function a()
  691. {
  692. %vector[0] = "1 2 3";
  693. %vector[0].z = 5;
  694. return %vector[0].z;
  695. }
  696. return a();
  697. )");
  698. ASSERT_EQ(valueSetArray.getInt(), 5);
  699. ConsoleValue valueStoreCalculated = RunScript(R"(
  700. function a()
  701. {
  702. %extent = 10 SPC 20;
  703. %scaling = 1;
  704. %size = %extent.x * %scaling;
  705. return %size;
  706. }
  707. return a();
  708. )");
  709. ASSERT_EQ(valueStoreCalculated.getInt(), 10);
  710. ConsoleValue globalValueGet = RunScript(R"(
  711. new SimObject(AAAA);
  712. AAAA.doSomething = false;
  713. $vec = "1 2 3";
  714. return $vec.x * 4;
  715. )");
  716. ASSERT_EQ(globalValueGet.getFloat(), 4);
  717. ConsoleValue globalValueSet = RunScript(R"(
  718. new SimObject(AAAAB);
  719. AAAAB.doSomething = false;
  720. $vec2 = "1 2 3";
  721. $vec2.x *= 4;
  722. return $vec2.x;
  723. )");
  724. ASSERT_EQ(globalValueSet.getFloat(), 4);
  725. }
  726. TEST_F(ScriptTest, InnerObjectTests)
  727. {
  728. ConsoleValue theObject = RunScript(R"(
  729. function a()
  730. {
  731. %obj = new SimObject(TheOuterObject)
  732. {
  733. innerObject = new SimObject(TheInnerObject)
  734. {
  735. testField = 123;
  736. position = "1 2 3";
  737. };
  738. };
  739. return %obj;
  740. }
  741. return a();
  742. )");
  743. SimObject* outerObject = Sim::findObject("TheOuterObject");
  744. ASSERT_NE(outerObject, (SimObject*)NULL);
  745. if (outerObject)
  746. {
  747. ASSERT_EQ(theObject.getInt(), Sim::findObject("TheOuterObject")->getId());
  748. }
  749. ASSERT_NE(Sim::findObject("TheInnerObject"), (SimObject*)NULL);
  750. ConsoleValue positionValue = RunScript(R"(
  751. function TheOuterObject::getInnerPosition(%this)
  752. {
  753. return %this.innerObject.position;
  754. }
  755. function a()
  756. {
  757. %position = TheOuterObject.getInnerPosition();
  758. return %position.y;
  759. }
  760. return a();
  761. )");
  762. ASSERT_EQ(positionValue.getInt(), 2);
  763. ConsoleValue nestedFuncCall = RunScript(R"(
  764. function TheInnerObject::test(%this)
  765. {
  766. return %this.testField;
  767. }
  768. return TheOuterObject.innerObject.test();
  769. )");
  770. ASSERT_EQ(nestedFuncCall.getInt(), 123);
  771. }
  772. TEST_F(ScriptTest, SlotOperatorTests)
  773. {
  774. ConsoleValue testSlot1 = RunScript(R"(
  775. new SimObject(testObjectSlot1);
  776. testObjectSlot1.data[1] = 2;
  777. function test(%value)
  778. {
  779. testObjectSlot1.data[%value]++;
  780. return testObjectSlot1.data[%value];
  781. }
  782. return test(1);
  783. )");
  784. ASSERT_EQ(testSlot1.getInt(), 3);
  785. ConsoleValue testSlot2 = RunScript(R"(
  786. new SimObject(testObjectSlot2);
  787. testObjectSlot2.data[1] = 5;
  788. function test(%value)
  789. {
  790. testObjectSlot2.data[%value]--;
  791. return testObjectSlot2.data[%value];
  792. }
  793. return test(1);
  794. )");
  795. ASSERT_EQ(testSlot2.getInt(), 4);
  796. ConsoleValue testSlot3 = RunScript(R"(
  797. new SimObject(testObjectSlot3);
  798. testObjectSlot3.data[1] = 5;
  799. function test(%value)
  800. {
  801. testObjectSlot3.data[1] += 1;
  802. return testObjectSlot3.data[1];
  803. }
  804. return test();
  805. )");
  806. ASSERT_EQ(testSlot3.getInt(), 6);
  807. ConsoleValue testSlot4 = RunScript(R"(
  808. new SimObject(testObjectSlot4);
  809. testObjectSlot4.data[1] = 5;
  810. function test(%value)
  811. {
  812. testObjectSlot4.data[1] -= %value;
  813. return testObjectSlot4.data[1];
  814. }
  815. return test(1);
  816. )");
  817. ASSERT_EQ(testSlot4.getInt(), 4);
  818. ConsoleValue testSlot5 = RunScript(R"(
  819. new SimObject(testObjectSlot5);
  820. testObjectSlot5.data[1] = 8;
  821. function test()
  822. {
  823. testObjectSlot5.data[1] /= 2;
  824. return testObjectSlot5.data[1];
  825. }
  826. return test();
  827. )");
  828. ASSERT_EQ(testSlot5.getInt(), 4);
  829. ConsoleValue testSlot6 = RunScript(R"(
  830. new SimObject(testObjectSlot6);
  831. testObjectSlot6.data[1] = 8;
  832. function test()
  833. {
  834. testObjectSlot6.data[1] *= 2;
  835. return testObjectSlot6.data[1];
  836. }
  837. return test();
  838. )");
  839. ASSERT_EQ(testSlot6.getInt(), 16);
  840. ConsoleValue testSlot7 = RunScript(R"(
  841. new SimObject(testObjectSlot7);
  842. testObjectSlot7.data[1] = 8;
  843. function test()
  844. {
  845. testObjectSlot7.data[1] %= 3;
  846. return testObjectSlot7.data[1];
  847. }
  848. return test();
  849. )");
  850. ASSERT_EQ(testSlot7.getInt(), 2);
  851. }
  852. TEST_F(ScriptTest, MiscTesting)
  853. {
  854. ConsoleValue test1 = RunScript(R"(
  855. function testNotPassedInParameters(%a, %b, %c, %d)
  856. {
  857. if (%d $= "")
  858. return true;
  859. return false;
  860. }
  861. return testNotPassedInParameters(1, 2); // skip passing in %c and %d
  862. )");
  863. ASSERT_EQ(test1.getBool(), true);
  864. ConsoleValue test2 = RunScript(R"(
  865. function SimObject::concatNameTest(%this)
  866. {
  867. return true;
  868. }
  869. new SimObject(WeirdTestObject1);
  870. function testObjectNameConcatination(%i)
  871. {
  872. return (WeirdTestObject @ %i).concatNameTest();
  873. }
  874. return testObjectNameConcatination(1);
  875. )");
  876. ASSERT_EQ(test2.getBool(), true);
  877. }
  878. TEST_F(ScriptTest, RegressionInt)
  879. {
  880. ConsoleValue regression1 = RunScript(R"(
  881. new SimObject(TheRegressionObject);
  882. function doTest()
  883. {
  884. TheRegressionObject.hidden = false;
  885. %previewSize = 100 SPC 100;
  886. %previewScaleSize = 2;
  887. %size = %previewSize.x * %previewScaleSize;
  888. return %size;
  889. }
  890. return doTest();
  891. )");
  892. ASSERT_EQ(regression1.getInt(), 200);
  893. ConsoleValue regression2 = RunScript(R"(
  894. new SimObject(TheRegressionObject2)
  895. {
  896. extent = "100 200";
  897. };
  898. function doTest()
  899. {
  900. %scale = 2;
  901. %position = TheRegressionObject2.extent.x SPC TheRegressionObject2.extent.y * %scale;
  902. return %position.y;
  903. }
  904. return doTest();
  905. )");
  906. ASSERT_EQ(regression2.getInt(), 400);
  907. ConsoleValue regression3 = RunScript(R"(
  908. function noOpInc()
  909. {
  910. %count = 0;
  911. %var[%count++] = 2;
  912. return %var[1];
  913. }
  914. return noOpInc();
  915. )");
  916. ASSERT_EQ(regression3.getInt(), 2);
  917. }
  918. TEST_F(ScriptTest, RegressionFloat)
  919. {
  920. ConsoleValue regression = RunScript(R"(
  921. function doTest()
  922. {
  923. %slider = new GuiSliderCtrl()
  924. {
  925. range = "0 2";
  926. ticks = 5;
  927. active = true;
  928. };
  929. %slider.setValue(0.5);
  930. return %slider.getValue();
  931. }
  932. return doTest();
  933. )");
  934. ASSERT_EQ(regression.getFloat(), 0.5);
  935. }
  936. TEST_F(ScriptTest, RegressionBool)
  937. {
  938. ConsoleValue regression = RunScript(R"(
  939. function SimObject::burnBool(%this, %line)
  940. {
  941. return %line @ "1";
  942. }
  943. function doTest()
  944. {
  945. %obj = new SimObject();
  946. for (%i = 0; %i < 100; %i++)
  947. {
  948. %function = "burnBool";
  949. if (%obj.isMethod(%function))
  950. {
  951. %line = "abcdefg";
  952. %output = %obj.call(%function, %line);
  953. }
  954. }
  955. return true;
  956. }
  957. return doTest();
  958. )");
  959. ASSERT_EQ(regression.getBool(), true);
  960. }
  961. TEST_F(ScriptTest, RegressionString)
  962. {
  963. ConsoleValue regression = RunScript(R"(
  964. function Tween::vectorAdd(%v1, %v2)
  965. {
  966. %temp = "";
  967. for (%i = 0; %i < getWordCount(%v1); %i++) {
  968. %e = getWord(%v1, %i) + getWord(%v2, %i);
  969. %temp = %i == 0 ? %e : %temp SPC %e;
  970. }
  971. return %temp;
  972. }
  973. return Tween::vectorAdd("1 2 3", "4 5 6");
  974. )");
  975. ASSERT_STREQ(regression.getString(), "5 7 9");
  976. ConsoleValue regression2 = RunScript(R"(
  977. function doTest()
  978. {
  979. %button = new GuiIconButtonCtrl()
  980. {
  981. active = true;
  982. };
  983. %button.setExtent(120, 20);
  984. %button.setExtent("120 20");
  985. %button.extent = "120 20";
  986. %button.extent.x = 120;
  987. %button.extent.y = 20;
  988. return %button.extent;
  989. }
  990. return doTest();
  991. )");
  992. ASSERT_STREQ(regression2.getString(), "120 20");
  993. }