ScriptTest.cpp 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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, Basic_SimObject)
  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. }
  516. TEST(Script, Internal_Name)
  517. {
  518. ConsoleValue value = RunScript(R"(
  519. function TheFirstInner::_internalCall(%this)
  520. {
  521. return 5;
  522. }
  523. function a()
  524. {
  525. %grp = new SimGroup();
  526. %obj = new SimObject(TheFirstInner)
  527. {
  528. internalName = "Yay";
  529. };
  530. %grp.add(%obj);
  531. %val = %grp->Yay._internalCall();
  532. %grp.delete();
  533. return %val;
  534. }
  535. return a();
  536. )");
  537. ASSERT_EQ(value.getInt(), 5);
  538. ConsoleValue recursiveValue = RunScript(R"(
  539. function SimGroup::doTheInternalCall(%this)
  540. {
  541. return %this-->Yeah._internalCall2();
  542. }
  543. function TheAnotherObject::_internalCall2(%this)
  544. {
  545. return %this.property;
  546. }
  547. function a()
  548. {
  549. %grp = new SimGroup();
  550. %obj = new SimGroup()
  551. {
  552. internalName = "Yay2";
  553. new SimObject(TheAnotherObject)
  554. {
  555. internalName = "Yeah";
  556. property = 12;
  557. };
  558. };
  559. %grp.add(%obj);
  560. %val = %grp.doTheInternalCall();
  561. %grp.delete();
  562. return %val;
  563. }
  564. return a();
  565. )");
  566. ASSERT_EQ(recursiveValue.getInt(), 12);
  567. }
  568. TEST(Script, Basic_Package)
  569. {
  570. ConsoleValue value = RunScript(R"(
  571. function a() { return 3; }
  572. package overrides
  573. {
  574. function a() { return 5; }
  575. };
  576. return a();
  577. )");
  578. ASSERT_EQ(value.getInt(), 3);
  579. ConsoleValue overrideValue = RunScript(R"(
  580. activatePackage(overrides);
  581. return a();
  582. )");
  583. ASSERT_EQ(overrideValue.getInt(), 5);
  584. ConsoleValue deactivatedValue = RunScript(R"(
  585. deactivatePackage(overrides);
  586. return a();
  587. )");
  588. ASSERT_EQ(deactivatedValue.getInt(), 3);
  589. }
  590. TEST(Script, Sugar_Syntax)
  591. {
  592. ConsoleValue value = RunScript(R"(
  593. function a()
  594. {
  595. %vector = "1 2 3";
  596. return %vector.y;
  597. }
  598. return a();
  599. )");
  600. ASSERT_EQ(value.getInt(), 2);
  601. ConsoleValue setValue = RunScript(R"(
  602. function a()
  603. {
  604. %vector = "1 2 3";
  605. %vector.y = 4;
  606. return %vector.y;
  607. }
  608. return a();
  609. )");
  610. ASSERT_EQ(setValue.getInt(), 4);
  611. ConsoleValue valueArray = RunScript(R"(
  612. function a()
  613. {
  614. %vector[0] = "1 2 3";
  615. return %vector[0].y;
  616. }
  617. return a();
  618. )");
  619. ASSERT_EQ(valueArray.getInt(), 2);
  620. ConsoleValue valueSetArray = RunScript(R"(
  621. function a()
  622. {
  623. %vector[0] = "1 2 3";
  624. %vector[0].z = 5;
  625. return %vector[0].z;
  626. }
  627. return a();
  628. )");
  629. ASSERT_EQ(valueSetArray.getInt(), 5);
  630. ConsoleValue valueStoreCalculated = RunScript(R"(
  631. function a()
  632. {
  633. %extent = 10 SPC 20;
  634. %scaling = 1;
  635. %size = %extent.x * %scaling;
  636. return %size;
  637. }
  638. return a();
  639. )");
  640. ASSERT_EQ(valueStoreCalculated.getInt(), 10);
  641. ConsoleValue globalValueGet = RunScript(R"(
  642. new SimObject(AAAA);
  643. AAAA.doSomething = false;
  644. $vec = "1 2 3";
  645. return $vec.x * 4;
  646. )");
  647. ASSERT_EQ(globalValueGet.getFloat(), 4);
  648. ConsoleValue globalValueSet = RunScript(R"(
  649. new SimObject(AAAAB);
  650. AAAAB.doSomething = false;
  651. $vec2 = "1 2 3";
  652. $vec2.x *= 4;
  653. return $vec2.x;
  654. )");
  655. ASSERT_EQ(globalValueSet.getFloat(), 4);
  656. }
  657. TEST(Script, InnerObjectTests)
  658. {
  659. ConsoleValue theObject = RunScript(R"(
  660. function a()
  661. {
  662. %obj = new SimObject(TheOuterObject)
  663. {
  664. innerObject = new SimObject(TheInnerObject)
  665. {
  666. testField = 123;
  667. position = "1 2 3";
  668. };
  669. };
  670. return %obj;
  671. }
  672. return a();
  673. )");
  674. SimObject* outerObject = Sim::findObject("TheOuterObject");
  675. ASSERT_NE(outerObject, (SimObject*)NULL);
  676. if (outerObject)
  677. {
  678. ASSERT_EQ(theObject.getInt(), Sim::findObject("TheOuterObject")->getId());
  679. }
  680. ASSERT_NE(Sim::findObject("TheInnerObject"), (SimObject*)NULL);
  681. ConsoleValue positionValue = RunScript(R"(
  682. function TheOuterObject::getInnerPosition(%this)
  683. {
  684. return %this.innerObject.position;
  685. }
  686. function a()
  687. {
  688. %position = TheOuterObject.getInnerPosition();
  689. return %position.y;
  690. }
  691. return a();
  692. )");
  693. ASSERT_EQ(positionValue.getInt(), 2);
  694. ConsoleValue nestedFuncCall = RunScript(R"(
  695. function TheInnerObject::test(%this)
  696. {
  697. return %this.testField;
  698. }
  699. return TheOuterObject.innerObject.test();
  700. )");
  701. ASSERT_EQ(nestedFuncCall.getInt(), 123);
  702. }
  703. TEST(Script, MiscTesting)
  704. {
  705. ConsoleValue test1 = RunScript(R"(
  706. function testNotPassedInParameters(%a, %b, %c, %d)
  707. {
  708. if (%d $= "")
  709. return true;
  710. return false;
  711. }
  712. return testNotPassedInParameters(1, 2); // skip passing in %c and %d
  713. )");
  714. ASSERT_EQ(test1.getBool(), true);
  715. ConsoleValue test2 = RunScript(R"(
  716. function SimObject::concatNameTest(%this)
  717. {
  718. return true;
  719. }
  720. new SimObject(WeirdTestObject1);
  721. function testObjectNameConcatination(%i)
  722. {
  723. return (WeirdTestObject @ %i).concatNameTest();
  724. }
  725. return testObjectNameConcatination(1);
  726. )");
  727. ASSERT_EQ(test2.getBool(), true);
  728. }
  729. TEST(Script, MiscRegressions)
  730. {
  731. ConsoleValue regression1 = RunScript(R"(
  732. new SimObject(TheRegressionObject);
  733. function doTest()
  734. {
  735. TheRegressionObject.hidden = false;
  736. %previewSize = 100 SPC 100;
  737. %previewScaleSize = 2;
  738. %size = %previewSize.x * %previewScaleSize;
  739. return %size;
  740. }
  741. return doTest();
  742. )");
  743. ASSERT_EQ(regression1.getInt(), 200);
  744. ConsoleValue regression2 = RunScript(R"(
  745. new SimObject(TheRegressionObject2)
  746. {
  747. extent = "100 200";
  748. };
  749. function doTest()
  750. {
  751. %scale = 2;
  752. %position = TheRegressionObject2.extent.x SPC TheRegressionObject2.extent.y * %scale;
  753. return %position.y;
  754. }
  755. return doTest();
  756. )");
  757. ASSERT_EQ(regression2.getInt(), 400);
  758. ConsoleValue regression3 = RunScript(R"(
  759. function doTest()
  760. {
  761. %button = new GuiIconButtonCtrl()
  762. {
  763. active = true;
  764. };
  765. %button.setExtent(120, 20);
  766. %button.setExtent("120 20");
  767. %button.extent = "120 20";
  768. %button.extent.x = 120;
  769. %button.extent.y = 20;
  770. return %button.extent;
  771. }
  772. return doTest();
  773. )");
  774. ASSERT_STREQ(regression3.getString(), "120 20");
  775. ConsoleValue regression4 = RunScript(R"(
  776. function doTest()
  777. {
  778. %slider = new GuiSliderCtrl()
  779. {
  780. range = "0 2";
  781. ticks = 5;
  782. active = true;
  783. };
  784. %slider.setValue(0.5);
  785. return %slider.getValue();
  786. }
  787. return doTest();
  788. )");
  789. ASSERT_EQ(regression4.getFloat(), 0.5);
  790. ConsoleValue regression5 = RunScript(R"(
  791. function noOpInc()
  792. {
  793. %count = 0;
  794. %var[%count++] = 2;
  795. return %var[1];
  796. }
  797. return noOpInc();
  798. )");
  799. ASSERT_EQ(regression5.getInt(), 2);
  800. ConsoleValue regression6 = RunScript(R"(
  801. function SimObject::crashMe(%this, %line)
  802. {
  803. return %line @ "1";
  804. }
  805. function doTest()
  806. {
  807. %obj = new SimObject();
  808. for (%i = 0; %i < 99999; %i++)
  809. {
  810. %function = "crashMe";
  811. if (%obj.isMethod(%function))
  812. {
  813. %line = "abcdefg";
  814. %output = %obj.call(%function, %line);
  815. }
  816. }
  817. return true;
  818. }
  819. return doTest();
  820. )");
  821. ASSERT_EQ(regression6.getBool(), true);
  822. }
  823. #endif