ScriptTest.cpp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. ConsoleValue forEach5 = RunScript(R"(
  317. function SimObject::ret1(%this)
  318. {
  319. return 1;
  320. }
  321. function SimSet::doForeach5(%this)
  322. {
  323. %count = 0;
  324. foreach (%obj in %this)
  325. {
  326. %count += %obj.ret1();
  327. }
  328. return %count;
  329. }
  330. function a()
  331. {
  332. %set = new SimSet();
  333. %set.add(new SimObject());
  334. %set.add(new SimObject());
  335. %set.add(new SimObject());
  336. return %set.doForeach5();
  337. }
  338. return a();
  339. )");
  340. ASSERT_EQ(forEach5.getInt(), 3);
  341. ConsoleValue forEachContinue = RunScript(R"(
  342. function SimSet::foreach6(%this)
  343. {
  344. %count = 0;
  345. foreach (%obj in %this)
  346. {
  347. if (%obj.getName() $= "A")
  348. continue;
  349. %count++;
  350. }
  351. return %count;
  352. }
  353. function a()
  354. {
  355. %set = new SimSet();
  356. %set.add(new SimObject(A));
  357. %set.add(new SimObject());
  358. %set.add(new SimObject());
  359. return %set.foreach6();
  360. }
  361. return a();
  362. )");
  363. ASSERT_EQ(forEachContinue.getInt(), 2);
  364. ConsoleValue forEachReturn = RunScript(R"(
  365. function SimSet::findA(%this)
  366. {
  367. foreach (%obj in %this)
  368. {
  369. if (%obj.getName() $= "A")
  370. return 76;
  371. }
  372. return 0;
  373. }
  374. function a()
  375. {
  376. %set = new SimSet();
  377. %set.add(new SimObject(A));
  378. %set.add(new SimObject());
  379. %set.add(new SimObject());
  380. return %set.findA();
  381. }
  382. return a();
  383. )");
  384. ASSERT_EQ(forEachReturn.getInt(), 76);
  385. ConsoleValue forEachNestedReturn = RunScript(R"(
  386. function SimSet::findA(%this)
  387. {
  388. foreach (%obj in %this)
  389. {
  390. foreach (%innerObj in %this)
  391. {
  392. if (%innerObj.getName() $= "A")
  393. return 42;
  394. }
  395. }
  396. return 0;
  397. }
  398. function a()
  399. {
  400. %set = new SimSet();
  401. %set.add(new SimObject(A));
  402. %set.add(new SimObject());
  403. %set.add(new SimObject());
  404. %group = new SimGroup();
  405. %group.add(%set);
  406. return %set.findA();
  407. }
  408. return a();
  409. )");
  410. ASSERT_EQ(forEachNestedReturn.getInt(), 42);
  411. }
  412. TEST(Script, TorqueScript_Array_Testing)
  413. {
  414. ConsoleValue value = RunScript(R"(
  415. function t(%idx) { %a[%idx] = 2; return %a[%idx]; }
  416. return t(5);
  417. )");
  418. ASSERT_EQ(value.getInt(), 2);
  419. ConsoleValue value2 = RunScript(R"(
  420. function t(%idx) { %a[%idx, 0] = 2; return %a[%idx, 0]; }
  421. return t(5);
  422. )");
  423. ASSERT_EQ(value2.getInt(), 2);
  424. }
  425. TEST(Script, SimObject_Tests)
  426. {
  427. ConsoleValue object = RunScript(R"(
  428. return new SimObject(FudgeCollector)
  429. {
  430. fudge = "Chocolate";
  431. };
  432. )");
  433. ASSERT_NE(Sim::findObject(object), (SimObject*)NULL);
  434. ConsoleValue propertyValue = RunScript(R"(
  435. return FudgeCollector.fudge;
  436. )");
  437. ASSERT_STREQ(propertyValue.getString(), "Chocolate");
  438. ConsoleValue funcReturn = RunScript(R"(
  439. function SimObject::fooFunc(%this)
  440. {
  441. return "Bar";
  442. }
  443. return FudgeCollector.fooFunc();
  444. )");
  445. ASSERT_STREQ(funcReturn.getString(), "Bar");
  446. ConsoleValue parentFn = RunScript(R"(
  447. new SimObject(Hello);
  448. function SimObject::fooFunc2(%this)
  449. {
  450. return "Bar";
  451. }
  452. function Hello::fooFunc2(%this)
  453. {
  454. %bar = Parent::fooFunc2(%this);
  455. return "Foo" @ %bar;
  456. }
  457. return Hello.fooFunc2();
  458. )");
  459. ASSERT_STREQ(parentFn.getString(), "FooBar");
  460. ConsoleValue simpleFieldTest = RunScript(R"(
  461. function a()
  462. {
  463. FudgeCollector.field = "A";
  464. return FudgeCollector.field;
  465. }
  466. return a();
  467. )");
  468. ASSERT_STREQ(simpleFieldTest.getString(), "A");
  469. ConsoleValue grp = RunScript(R"(
  470. new SimGroup(FudgeCollectorGroup)
  471. {
  472. theName = "fudge";
  473. new SimObject(ChocolateFudge)
  474. {
  475. type = "Chocolate";
  476. };
  477. new SimObject(PeanutButterFudge)
  478. {
  479. type = "Peanut Butter";
  480. field["a"] = "Yes";
  481. };
  482. };
  483. return FudgeCollectorGroup.getId();
  484. )");
  485. SimGroup* simGroup = dynamic_cast<SimGroup*>(Sim::findObject(grp));
  486. ASSERT_NE(simGroup, (SimGroup*)NULL);
  487. ASSERT_EQ(simGroup->size(), 2);
  488. simGroup->deleteObject();
  489. ConsoleValue fieldTest = RunScript(R"(
  490. function a()
  491. {
  492. %obj = new SimObject();
  493. %obj.field = "A";
  494. %obj.val[%obj.field] = "B";
  495. %value = %obj.val["A"];
  496. %obj.delete();
  497. return %value;
  498. }
  499. return a();
  500. )");
  501. ASSERT_STREQ(fieldTest.getString(), "B");
  502. ConsoleValue fieldOpTest = RunScript(R"(
  503. function a()
  504. {
  505. %obj = new SimObject();
  506. %obj.field = 1;
  507. %obj.field += 2;
  508. %value = %obj.field;
  509. %obj.delete();
  510. return %value;
  511. }
  512. return a();
  513. )");
  514. ASSERT_EQ(fieldOpTest.getInt(), 3);
  515. ConsoleValue inheritedObjectTest = RunScript(R"(
  516. function SimObject::testClass(%this)
  517. {
  518. return 4;
  519. }
  520. function SuperFooBar::doSuperTest(%this)
  521. {
  522. return 5;
  523. }
  524. function FooBar::test(%this)
  525. {
  526. return 2;
  527. }
  528. new SimObject(GrandFooBar)
  529. {
  530. superClass = "SuperFooBar";
  531. };
  532. new SimObject(Foo : GrandFooBar)
  533. {
  534. class = "FooBar";
  535. };
  536. new SimObject(Bar : Foo);
  537. function Bar::doTheAddition(%this)
  538. {
  539. return %this.testClass() + %this.test() + %this.doSuperTest();
  540. }
  541. return Bar.doTheAddition();
  542. )");
  543. ASSERT_EQ(inheritedObjectTest.getInt(), 11);
  544. }
  545. TEST(Script, Internal_Name)
  546. {
  547. ConsoleValue value = RunScript(R"(
  548. function TheFirstInner::_internalCall(%this)
  549. {
  550. return 5;
  551. }
  552. function a()
  553. {
  554. %grp = new SimGroup();
  555. %obj = new SimObject(TheFirstInner)
  556. {
  557. internalName = "Yay";
  558. };
  559. %grp.add(%obj);
  560. %val = %grp->Yay._internalCall();
  561. %grp.delete();
  562. return %val;
  563. }
  564. return a();
  565. )");
  566. ASSERT_EQ(value.getInt(), 5);
  567. ConsoleValue recursiveValue = RunScript(R"(
  568. function SimGroup::doTheInternalCall(%this)
  569. {
  570. return %this-->Yeah._internalCall2();
  571. }
  572. function TheAnotherObject::_internalCall2(%this)
  573. {
  574. return %this.property;
  575. }
  576. function a()
  577. {
  578. %grp = new SimGroup();
  579. %obj = new SimGroup()
  580. {
  581. internalName = "Yay2";
  582. new SimObject(TheAnotherObject)
  583. {
  584. internalName = "Yeah";
  585. property = 12;
  586. };
  587. };
  588. %grp.add(%obj);
  589. %val = %grp.doTheInternalCall();
  590. %grp.delete();
  591. return %val;
  592. }
  593. return a();
  594. )");
  595. ASSERT_EQ(recursiveValue.getInt(), 12);
  596. }
  597. TEST(Script, Basic_Package)
  598. {
  599. ConsoleValue value = RunScript(R"(
  600. function a() { return 3; }
  601. package overrides
  602. {
  603. function a() { return 5; }
  604. };
  605. return a();
  606. )");
  607. ASSERT_EQ(value.getInt(), 3);
  608. ConsoleValue overrideValue = RunScript(R"(
  609. activatePackage(overrides);
  610. return a();
  611. )");
  612. ASSERT_EQ(overrideValue.getInt(), 5);
  613. ConsoleValue deactivatedValue = RunScript(R"(
  614. deactivatePackage(overrides);
  615. return a();
  616. )");
  617. ASSERT_EQ(deactivatedValue.getInt(), 3);
  618. }
  619. TEST(Script, Sugar_Syntax)
  620. {
  621. ConsoleValue value = RunScript(R"(
  622. function a()
  623. {
  624. %vector = "1 2 3";
  625. return %vector.y;
  626. }
  627. return a();
  628. )");
  629. ASSERT_EQ(value.getInt(), 2);
  630. ConsoleValue setValue = RunScript(R"(
  631. function a()
  632. {
  633. %vector = "1 2 3";
  634. %vector.y = 4;
  635. return %vector.y;
  636. }
  637. return a();
  638. )");
  639. ASSERT_EQ(setValue.getInt(), 4);
  640. ConsoleValue valueArray = RunScript(R"(
  641. function a()
  642. {
  643. %vector[0] = "1 2 3";
  644. return %vector[0].y;
  645. }
  646. return a();
  647. )");
  648. ASSERT_EQ(valueArray.getInt(), 2);
  649. ConsoleValue valueSetArray = RunScript(R"(
  650. function a()
  651. {
  652. %vector[0] = "1 2 3";
  653. %vector[0].z = 5;
  654. return %vector[0].z;
  655. }
  656. return a();
  657. )");
  658. ASSERT_EQ(valueSetArray.getInt(), 5);
  659. ConsoleValue valueStoreCalculated = RunScript(R"(
  660. function a()
  661. {
  662. %extent = 10 SPC 20;
  663. %scaling = 1;
  664. %size = %extent.x * %scaling;
  665. return %size;
  666. }
  667. return a();
  668. )");
  669. ASSERT_EQ(valueStoreCalculated.getInt(), 10);
  670. ConsoleValue globalValueGet = RunScript(R"(
  671. new SimObject(AAAA);
  672. AAAA.doSomething = false;
  673. $vec = "1 2 3";
  674. return $vec.x * 4;
  675. )");
  676. ASSERT_EQ(globalValueGet.getFloat(), 4);
  677. ConsoleValue globalValueSet = RunScript(R"(
  678. new SimObject(AAAAB);
  679. AAAAB.doSomething = false;
  680. $vec2 = "1 2 3";
  681. $vec2.x *= 4;
  682. return $vec2.x;
  683. )");
  684. ASSERT_EQ(globalValueSet.getFloat(), 4);
  685. }
  686. TEST(Script, InnerObjectTests)
  687. {
  688. ConsoleValue theObject = RunScript(R"(
  689. function a()
  690. {
  691. %obj = new SimObject(TheOuterObject)
  692. {
  693. innerObject = new SimObject(TheInnerObject)
  694. {
  695. testField = 123;
  696. position = "1 2 3";
  697. };
  698. };
  699. return %obj;
  700. }
  701. return a();
  702. )");
  703. SimObject* outerObject = Sim::findObject("TheOuterObject");
  704. ASSERT_NE(outerObject, (SimObject*)NULL);
  705. if (outerObject)
  706. {
  707. ASSERT_EQ(theObject.getInt(), Sim::findObject("TheOuterObject")->getId());
  708. }
  709. ASSERT_NE(Sim::findObject("TheInnerObject"), (SimObject*)NULL);
  710. ConsoleValue positionValue = RunScript(R"(
  711. function TheOuterObject::getInnerPosition(%this)
  712. {
  713. return %this.innerObject.position;
  714. }
  715. function a()
  716. {
  717. %position = TheOuterObject.getInnerPosition();
  718. return %position.y;
  719. }
  720. return a();
  721. )");
  722. ASSERT_EQ(positionValue.getInt(), 2);
  723. ConsoleValue nestedFuncCall = RunScript(R"(
  724. function TheInnerObject::test(%this)
  725. {
  726. return %this.testField;
  727. }
  728. return TheOuterObject.innerObject.test();
  729. )");
  730. ASSERT_EQ(nestedFuncCall.getInt(), 123);
  731. }
  732. TEST(Script, MiscTesting)
  733. {
  734. ConsoleValue test1 = RunScript(R"(
  735. function testNotPassedInParameters(%a, %b, %c, %d)
  736. {
  737. if (%d $= "")
  738. return true;
  739. return false;
  740. }
  741. return testNotPassedInParameters(1, 2); // skip passing in %c and %d
  742. )");
  743. ASSERT_EQ(test1.getBool(), true);
  744. ConsoleValue test2 = RunScript(R"(
  745. function SimObject::concatNameTest(%this)
  746. {
  747. return true;
  748. }
  749. new SimObject(WeirdTestObject1);
  750. function testObjectNameConcatination(%i)
  751. {
  752. return (WeirdTestObject @ %i).concatNameTest();
  753. }
  754. return testObjectNameConcatination(1);
  755. )");
  756. ASSERT_EQ(test2.getBool(), true);
  757. }
  758. TEST(Script, MiscRegressions)
  759. {
  760. ConsoleValue regression1 = RunScript(R"(
  761. new SimObject(TheRegressionObject);
  762. function doTest()
  763. {
  764. TheRegressionObject.hidden = false;
  765. %previewSize = 100 SPC 100;
  766. %previewScaleSize = 2;
  767. %size = %previewSize.x * %previewScaleSize;
  768. return %size;
  769. }
  770. return doTest();
  771. )");
  772. ASSERT_EQ(regression1.getInt(), 200);
  773. ConsoleValue regression2 = RunScript(R"(
  774. new SimObject(TheRegressionObject2)
  775. {
  776. extent = "100 200";
  777. };
  778. function doTest()
  779. {
  780. %scale = 2;
  781. %position = TheRegressionObject2.extent.x SPC TheRegressionObject2.extent.y * %scale;
  782. return %position.y;
  783. }
  784. return doTest();
  785. )");
  786. ASSERT_EQ(regression2.getInt(), 400);
  787. ConsoleValue regression3 = RunScript(R"(
  788. function doTest()
  789. {
  790. %button = new GuiIconButtonCtrl()
  791. {
  792. active = true;
  793. };
  794. %button.setExtent(120, 20);
  795. %button.setExtent("120 20");
  796. %button.extent = "120 20";
  797. %button.extent.x = 120;
  798. %button.extent.y = 20;
  799. return %button.extent;
  800. }
  801. return doTest();
  802. )");
  803. ASSERT_STREQ(regression3.getString(), "120 20");
  804. ConsoleValue regression4 = RunScript(R"(
  805. function doTest()
  806. {
  807. %slider = new GuiSliderCtrl()
  808. {
  809. range = "0 2";
  810. ticks = 5;
  811. active = true;
  812. };
  813. %slider.setValue(0.5);
  814. return %slider.getValue();
  815. }
  816. return doTest();
  817. )");
  818. ASSERT_EQ(regression4.getFloat(), 0.5);
  819. ConsoleValue regression5 = RunScript(R"(
  820. function noOpInc()
  821. {
  822. %count = 0;
  823. %var[%count++] = 2;
  824. return %var[1];
  825. }
  826. return noOpInc();
  827. )");
  828. ASSERT_EQ(regression5.getInt(), 2);
  829. ConsoleValue regression6 = RunScript(R"(
  830. function SimObject::crashMe(%this, %line)
  831. {
  832. return %line @ "1";
  833. }
  834. function doTest()
  835. {
  836. %obj = new SimObject();
  837. for (%i = 0; %i < 99999; %i++)
  838. {
  839. %function = "crashMe";
  840. if (%obj.isMethod(%function))
  841. {
  842. %line = "abcdefg";
  843. %output = %obj.call(%function, %line);
  844. }
  845. }
  846. return true;
  847. }
  848. return doTest();
  849. )");
  850. ASSERT_EQ(regression6.getBool(), true);
  851. ConsoleValue regression7 = RunScript(R"(
  852. function Tween::vectorAdd(%v1, %v2)
  853. {
  854. %temp = "";
  855. for (%i = 0; %i < getWordCount(%v1); %i++) {
  856. %e = getWord(%v1, %i) + getWord(%v2, %i);
  857. %temp = %i == 0 ? %e : %temp SPC %e;
  858. }
  859. return %temp;
  860. }
  861. return Tween::vectorAdd("1 2 3", "4 5 6");
  862. )");
  863. ASSERT_STREQ(regression7.getString(), "5 7 9");
  864. }
  865. #endif