ScriptTest.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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, MiscRegressions)
  704. {
  705. ConsoleValue regression1 = RunScript(R"(
  706. new SimObject(TheRegressionObject);
  707. function doTest()
  708. {
  709. TheRegressionObject.hidden = false;
  710. %previewSize = 100 SPC 100;
  711. %previewScaleSize = 2;
  712. %size = %previewSize.x * %previewScaleSize;
  713. return %size;
  714. }
  715. return doTest();
  716. )");
  717. ASSERT_EQ(regression1.getInt(), 200);
  718. }
  719. #endif