2
0

ScriptTest.cpp 26 KB

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