tcwriter.pp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  1. unit tcwriter;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, testregistry, jsbase, jstree, jswriter;
  6. type
  7. { TTestJSWriter }
  8. TTestJSWriter = class(TTestCase)
  9. private
  10. FElement: TJSElement;
  11. FTextWriter: TBufferWriter;
  12. FWriter: TJSWriter;
  13. protected
  14. procedure SetUp; override;
  15. procedure TearDown; override;
  16. Procedure WriteElement(JS : TJSElement); // Set element in Element, write. Freed on teardown
  17. Procedure AssertResult(Const Msg, Result : String); // Compare result;
  18. Procedure AssertResult(Const Msg : string; Result : UnicodeString); // Compare result;
  19. Procedure AssertWrite(Const Msg, Result : String; AElement : TJSElement); // Call writelement, compare result;
  20. Procedure AssertWrite(Const Msg : string; Result : UnicodeString; AElement : TJSElement); // Call writelement, compare result;
  21. Function CreateIdent(Const AName : String) : TJSPrimaryExpressionIdent;
  22. Function CreateLiteral(Const AValue : TJSString) : TJSLiteral;
  23. Function CreateLiteral(Const AValue : Integer) : TJSLiteral;
  24. Function CreateLiteral(Const AValue : Boolean) : TJSLiteral;
  25. Property TextWriter : TBufferWriter Read FTextWriter;
  26. Property Writer : TJSWriter Read FWriter;
  27. Property Element : TJSElement read FElement;
  28. end;
  29. TTestTestJSWriter = Class(TTestJSWriter)
  30. published
  31. procedure TestEmpty;
  32. end;
  33. { TTestLiteralWriter }
  34. TTestLiteralWriter= class(TTestJSWriter)
  35. published
  36. Procedure TestInteger;
  37. Procedure TestBooleanTrue;
  38. Procedure TestBooleanFalse;
  39. Procedure TestUndefined;
  40. Procedure TestNull;
  41. Procedure TestString;
  42. Procedure TestStringQuote;
  43. Procedure TestStringBackslash;
  44. Procedure TestStringslash;
  45. Procedure TestStringsBack;
  46. Procedure TestStringsTab;
  47. Procedure TestStringsLineFeed;
  48. Procedure TestStringsFormFeed;
  49. Procedure TestStringsCarriageReturn;
  50. Procedure TestArrayEmpty;
  51. Procedure TestArrayEmptyCompact;
  52. Procedure TestArrayOneElement;
  53. Procedure TestArrayOneElementCompact;
  54. Procedure TestArrayOneElementIndent;
  55. Procedure TestArrayTwoElements;
  56. Procedure TestArrayTwoElementsCompact;
  57. Procedure TestArrayTwoElementsCompact2;
  58. Procedure TestArrayThreeElementsCompact;
  59. Procedure TestObjectEmpty;
  60. Procedure TestObjectEmptyCompact;
  61. Procedure TestObjectOneElement;
  62. Procedure TestObjectOneElementCompact;
  63. Procedure TestObjectOneElementIndent;
  64. Procedure TestObjectOneElementCompactQuoted;
  65. Procedure TestObjectTwoElements;
  66. Procedure TestObjectTwoElementCompact;
  67. Procedure TestObjectTwoElementCompact2;
  68. Procedure TestObjectTwoElementCompactQuoted;
  69. Procedure TestObjectThreeElementsCompact;
  70. end;
  71. { TTestStatementWriter }
  72. TTestStatementWriter = class(TTestJSWriter)
  73. Public
  74. Procedure TestAssignment(Const Msg : String; AClass : TJSAssignStatementClass; Result : String;ACompact : Boolean);
  75. Function CreateAssignment(AClass : TJSAssignStatementClass) : TJSAssignStatement;
  76. published
  77. Procedure TestEmptyStatement;
  78. Procedure TestEmptyStatementComment;
  79. Procedure TestEmptyStatementBlock;
  80. Procedure TestEmptyStatementBlockIndent;
  81. Procedure TestEmptyStatementBlockCompact;
  82. Procedure TestVarDeclaration;
  83. Procedure TestVarDeclarationInit;
  84. Procedure TestVarListDeclaration;
  85. Procedure TestVarListDeclarationInit;
  86. Procedure TestVarDeclarationStatement;
  87. Procedure TestVarListDeclarationStatement;
  88. Procedure TestVarListDeclarationStatement2Vars;
  89. Procedure TestReturnStatement;
  90. Procedure TestLabeledStatement;
  91. Procedure TestLabeledStatementCompact;
  92. Procedure TestContinueStatement;
  93. Procedure TestContinueTargetStatement;
  94. Procedure TestBreakStatement;
  95. Procedure TestBreakTargetStatement;
  96. Procedure TestAssignmentStatementSimple;
  97. Procedure TestAssignmentStatementSimpleCompact;
  98. Procedure TestAssignmentStatementAdd;
  99. Procedure TestAssignmentStatementAddCompact;
  100. Procedure TestAssignmentStatementSubtract;
  101. Procedure TestAssignmentStatementSubtractCompact;
  102. Procedure TestAssignmentStatementMultiply;
  103. Procedure TestAssignmentStatementMultiplyCompact;
  104. Procedure TestAssignmentStatementDivide;
  105. Procedure TestAssignmentStatementDivideCompact;
  106. Procedure TestAssignmentStatementShift;
  107. Procedure TestAssignmentStatementShiftCompact;
  108. Procedure TestAssignmentStatementRShift;
  109. Procedure TestAssignmentStatementRShiftCompact;
  110. Procedure TestAssignmentStatementURShift;
  111. Procedure TestAssignmentStatementURShiftCompact;
  112. Procedure TestAssignmentStatementMod;
  113. Procedure TestAssignmentStatementModCompact;
  114. Procedure TestAssignmentStatementBinaryOr;
  115. Procedure TestAssignmentStatementBinaryOrCompact;
  116. Procedure TestAssignmentStatementBinaryXOr;
  117. Procedure TestAssignmentStatementBinaryXOrCompact;
  118. Procedure TestAssignmentStatementBinaryAnd;
  119. Procedure TestAssignmentStatementBinaryAndCompact;
  120. Procedure TestForStatementEmpty;
  121. Procedure TestForStatementFull;
  122. Procedure TestForStatementCompact;
  123. Procedure TestForInStatement;
  124. Procedure TestWhileStatement;
  125. Procedure TestDoWhileStatement;
  126. Procedure TestSwitchStatementEmpty;
  127. Procedure TestSwitchStatementEmptyCompact;
  128. Procedure TestSwitchStatementOneElement;
  129. Procedure TestSwitchStatementOneElementCompact;
  130. Procedure TestSwitchStatementTwoElements;
  131. Procedure TestSwitchStatementTwoElementsCompact;
  132. Procedure TestSwitchStatementTwoElementsDefault;
  133. Procedure TestSwitchStatementTwoElementsDefaultCompact;
  134. Procedure TestSwitchStatementTwoElementsOneEmpty;
  135. Procedure TestSwitchStatementTwoElementsOneEmptyCompact;
  136. Procedure TestIfThen;
  137. Procedure TestIfThenElse;
  138. Procedure TestStatementListEmpty;
  139. Procedure TestStatementListEmptyCompact;
  140. Procedure TestStatementListOneStatement;
  141. Procedure TestStatementListOneStatementCompact;
  142. Procedure TestStatementListTwoStatements;
  143. Procedure TestStatementListTwoStatementsCompact;
  144. Procedure TestEmptyFunctionDef;
  145. Procedure TestEmptyFunctionDefCompact;
  146. Procedure TestFunctionDefParams;
  147. Procedure TestFunctionDefParamsCompact;
  148. Procedure TestFunctionDefBody1;
  149. Procedure TestFunctionDefBody1Compact;
  150. Procedure TestFunctionDefBody2;
  151. Procedure TestFunctionDefBody2Compact;
  152. Procedure TestTryCatch;
  153. Procedure TestTryCatchCompact;
  154. Procedure TestTryFinally;
  155. Procedure TestTryFinallyCompact;
  156. Procedure TestTryCatchFinally;
  157. Procedure TestTryCatchFinallyCompact;
  158. Procedure TestWith;
  159. Procedure TestWithCompact;
  160. Procedure TestSourceElements;
  161. Procedure TestSourceElementsCompact;
  162. end;
  163. { TTestExpressionWriter }
  164. TTestExpressionWriter= class(TTestJSWriter)
  165. Protected
  166. Procedure TestUnary(Const Msg : String; AClass : TJSUnaryClass; Result : String);
  167. Procedure TestBinary(Const Msg : String; AClass : TJSBinaryClass; Result : String;ACompact : Boolean);
  168. Published
  169. Procedure TestIdent;
  170. Procedure TestThis;
  171. Procedure TestThrowStatement;
  172. Procedure TestUnaryDelete;
  173. Procedure TestUnaryVoid;
  174. Procedure TestUnaryTypeOf;
  175. Procedure TestPrefixPlusPLus;
  176. Procedure TestPrefixMinusMinus;
  177. Procedure TestUnaryMinus;
  178. Procedure TestUnaryPlus;
  179. Procedure TestUnaryInv;
  180. Procedure TestUnaryNot;
  181. Procedure TestPostPlusPLus;
  182. Procedure TestPostMinusMinus;
  183. Procedure TestBinaryLogicalOr;
  184. Procedure TestBinaryLogicalOrCompact;
  185. Procedure TestBinaryLogicalAnd;
  186. Procedure TestBinaryLogicalAndCompact;
  187. Procedure TestBinaryBitwiseOr;
  188. Procedure TestBinaryBitwiseOrCompact;
  189. Procedure TestBinaryBitwiseAnd;
  190. Procedure TestBinaryBitwiseAndCompact;
  191. Procedure TestBinaryBitwiseXOr;
  192. Procedure TestBinaryBitwiseXOrCompact;
  193. Procedure TestBinaryEQ;
  194. Procedure TestBinaryEQCompact;
  195. Procedure TestBinaryNE;
  196. Procedure TestBinaryNECompact;
  197. Procedure TestBinarySEQ;
  198. Procedure TestBinarySEQCompact;
  199. Procedure TestBinarySNE;
  200. Procedure TestBinarySNECompact;
  201. Procedure TestBinaryLT;
  202. Procedure TestBinaryLTCompact;
  203. Procedure TestBinaryGT;
  204. Procedure TestBinaryGTCompact;
  205. Procedure TestBinaryLE;
  206. Procedure TestBinaryLECompact;
  207. Procedure TestBinaryGE;
  208. Procedure TestBinaryGECompact;
  209. Procedure TestBinaryIN;
  210. Procedure TestBinaryINCompact;
  211. Procedure TestBinaryInstanceOf;
  212. Procedure TestBinaryInstanceOfCompact;
  213. Procedure TestBinaryLShift;
  214. Procedure TestBinaryLShiftOfCompact;
  215. Procedure TestBinaryRShift;
  216. Procedure TestBinaryRShiftOfCompact;
  217. Procedure TestBinaryURShift;
  218. Procedure TestBinaryURShiftOfCompact;
  219. Procedure TestBinaryPlus;
  220. Procedure TestBinaryPlusCompact;
  221. Procedure TestBinaryMinus;
  222. Procedure TestBinaryMinusCompact;
  223. Procedure TestBinaryMultiply;
  224. Procedure TestBinaryMultiplyCompact;
  225. Procedure TestBinaryDivide;
  226. Procedure TestBinaryDivideCompact;
  227. Procedure TestBinaryMod;
  228. Procedure TestBinaryModCompact;
  229. Procedure TestBinaryComma;
  230. Procedure TestBinaryCommaCompact;
  231. Procedure TestDotMember;
  232. Procedure TestArgMember;
  233. Procedure TestNewMember;
  234. Procedure TestNewMemberCompact;
  235. Procedure TestNewMemberNoArgs;
  236. Procedure TestCall;
  237. Procedure TestCallCompact;
  238. Procedure TestCallCompact2;
  239. Procedure TestCallNoArgs;
  240. Procedure TestConditional;
  241. Procedure TestRegularExpressionLiteral;
  242. Procedure TestRegularExpressionLiteralFlags;
  243. end;
  244. implementation
  245. { TTestExpressionWriter }
  246. Procedure TTestExpressionWriter.TestUnary(Const Msg: String;
  247. AClass: TJSUnaryClass; Result: String);
  248. Var
  249. U : TJSUnary;
  250. begin
  251. U:=AClass.Create(0,0);
  252. U.A:=CreateIdent('a');
  253. AssertWrite(Msg,Result,U);
  254. end;
  255. Procedure TTestExpressionWriter.TestBinary(Const Msg: String;
  256. AClass: TJSBinaryClass; Result: String; ACompact: Boolean);
  257. Var
  258. U : TJSBinary;
  259. begin
  260. if ACompact then
  261. Writer.Options:=Writer.Options+[woCompact];
  262. U:=AClass.Create(0,0);
  263. U.A:=CreateIdent('a');
  264. U.B:=CreateIdent('b');
  265. AssertWrite(Msg,Result,U);
  266. end;
  267. Procedure TTestExpressionWriter.TestIdent;
  268. begin
  269. AssertWrite('ABC','ABC',CreateIdent('ABC'));
  270. end;
  271. Procedure TTestExpressionWriter.TestThis;
  272. begin
  273. AssertWrite('this','this',TJSPrimaryExpressionThis.Create(0,0));
  274. end;
  275. Procedure TTestExpressionWriter.TestThrowStatement;
  276. begin
  277. TestUnary('Throw expresssion',TJSThrowStatement,'throw a');
  278. end;
  279. Procedure TTestExpressionWriter.TestUnaryDelete;
  280. begin
  281. TestUnary('Delete expresssion',TJSUnaryDeleteExpression,'delete a');
  282. end;
  283. Procedure TTestExpressionWriter.TestUnaryVoid;
  284. begin
  285. TestUnary('Void expresssion',TJSUnaryVoidExpression,'void a');
  286. end;
  287. Procedure TTestExpressionWriter.TestUnaryTypeOf;
  288. begin
  289. TestUnary('typeof expresssion',TJSUnaryTypeOfExpression,'typeof a');
  290. end;
  291. Procedure TTestExpressionWriter.TestPrefixPlusPLus;
  292. begin
  293. TestUnary('prefix ++ expresssion',TJSUnaryPrePlusPlusExpression,'++a');
  294. end;
  295. Procedure TTestExpressionWriter.TestPrefixMinusMinus;
  296. begin
  297. TestUnary('prefix -- expresssion',TJSUnaryPreMinusMinusExpression,'--a');
  298. end;
  299. Procedure TTestExpressionWriter.TestUnaryMinus;
  300. begin
  301. TestUnary('unary - expresssion',TJSUnaryMinusExpression,'-a');
  302. end;
  303. Procedure TTestExpressionWriter.TestUnaryPlus;
  304. begin
  305. TestUnary('unary + expresssion',TJSUnaryPlusExpression,'+a');
  306. end;
  307. Procedure TTestExpressionWriter.TestUnaryInv;
  308. begin
  309. TestUnary('unary invert expresssion',TJSUnaryInvExpression,'~a');
  310. end;
  311. Procedure TTestExpressionWriter.TestUnaryNot;
  312. begin
  313. TestUnary('unary not expresssion',TJSUnaryNotExpression,'!a');
  314. end;
  315. Procedure TTestExpressionWriter.TestPostPlusPLus;
  316. begin
  317. TestUnary('postfix ++ expresssion',TJSUnaryPostPlusPlusExpression,'a++');
  318. end;
  319. Procedure TTestExpressionWriter.TestPostMinusMinus;
  320. begin
  321. TestUnary('postfix -- expresssion',TJSUnaryPostMinusMinusExpression,'a--');
  322. end;
  323. Procedure TTestExpressionWriter.TestBinaryLogicalOr;
  324. begin
  325. TestBinary('logical or',TJSLogicalOrExpression,'(a || b)',False);
  326. end;
  327. Procedure TTestExpressionWriter.TestBinaryLogicalOrCompact;
  328. begin
  329. TestBinary('logical or',TJSLogicalOrExpression,'(a||b)',True);
  330. end;
  331. Procedure TTestExpressionWriter.TestBinaryLogicalAnd;
  332. begin
  333. TestBinary('logical or',TJSLogicalAndExpression,'(a && b)',False);
  334. end;
  335. Procedure TTestExpressionWriter.TestBinaryLogicalAndCompact;
  336. begin
  337. TestBinary('logical or',TJSLogicalAndExpression,'(a&&b)',True);
  338. end;
  339. Procedure TTestExpressionWriter.TestBinaryBitwiseOr;
  340. begin
  341. TestBinary('Bitwise or',TJSBitwiseOrExpression,'(a | b)',False);
  342. end;
  343. Procedure TTestExpressionWriter.TestBinaryBitwiseOrCompact;
  344. begin
  345. TestBinary('Bitwise or',TJSBitwiseOrExpression,'(a|b)',True);
  346. end;
  347. Procedure TTestExpressionWriter.TestBinaryBitwiseAnd;
  348. begin
  349. TestBinary('Bitwise and',TJSBitwiseAndExpression,'(a & b)',False);
  350. end;
  351. Procedure TTestExpressionWriter.TestBinaryBitwiseAndCompact;
  352. begin
  353. TestBinary('Bitwise and',TJSBitwiseAndExpression,'(a&b)',True);
  354. end;
  355. Procedure TTestExpressionWriter.TestBinaryBitwiseXOr;
  356. begin
  357. TestBinary('Bitwise xor',TJSBitwiseXOrExpression,'(a ^ b)',False);
  358. end;
  359. Procedure TTestExpressionWriter.TestBinaryBitwiseXOrCompact;
  360. begin
  361. TestBinary('Bitwise xor',TJSBitwiseXOrExpression,'(a^b)',True);
  362. end;
  363. Procedure TTestExpressionWriter.TestBinaryEQ;
  364. begin
  365. TestBinary('Equal',TJSEqualityExpressionEQ,'(a == b)',False);
  366. end;
  367. Procedure TTestExpressionWriter.TestBinaryEQCompact;
  368. begin
  369. TestBinary('Equal',TJSEqualityExpressionEQ,'(a==b)',True);
  370. end;
  371. Procedure TTestExpressionWriter.TestBinaryNE;
  372. begin
  373. TestBinary('Not Equal',TJSEqualityExpressionNE,'(a != b)',False);
  374. end;
  375. Procedure TTestExpressionWriter.TestBinaryNECompact;
  376. begin
  377. TestBinary('Not Equal',TJSEqualityExpressionNE,'(a!=b)',True);
  378. end;
  379. Procedure TTestExpressionWriter.TestBinarySEQ;
  380. begin
  381. TestBinary('Strictly Equal',TJSEqualityExpressionSEQ,'(a === b)',False);
  382. end;
  383. Procedure TTestExpressionWriter.TestBinarySEQCompact;
  384. begin
  385. TestBinary('Strictly Equal',TJSEqualityExpressionSEQ,'(a===b)',True);
  386. end;
  387. Procedure TTestExpressionWriter.TestBinarySNE;
  388. begin
  389. TestBinary('Strictly Equal',TJSEqualityExpressionSNE,'(a !== b)',False);
  390. end;
  391. Procedure TTestExpressionWriter.TestBinarySNECompact;
  392. begin
  393. TestBinary('Strictly Equal',TJSEqualityExpressionSNE,'(a!==b)',True);
  394. end;
  395. Procedure TTestExpressionWriter.TestBinaryLT;
  396. begin
  397. TestBinary('Less than',TJSRelationalExpressionLT,'(a < b)',False);
  398. end;
  399. Procedure TTestExpressionWriter.TestBinaryLTCompact;
  400. begin
  401. TestBinary('Less than',TJSRelationalExpressionLT,'(a<b)',True);
  402. end;
  403. Procedure TTestExpressionWriter.TestBinaryGT;
  404. begin
  405. TestBinary('Greater than',TJSRelationalExpressionGT,'(a > b)',False);
  406. end;
  407. Procedure TTestExpressionWriter.TestBinaryGTCompact;
  408. begin
  409. TestBinary('Greater than',TJSRelationalExpressionGT,'(a>b)',True);
  410. end;
  411. Procedure TTestExpressionWriter.TestBinaryLE;
  412. begin
  413. TestBinary('Less than or equal',TJSRelationalExpressionLE,'(a <= b)',False);
  414. end;
  415. Procedure TTestExpressionWriter.TestBinaryLECompact;
  416. begin
  417. TestBinary('Less than or equal',TJSRelationalExpressionLE,'(a<=b)',True);
  418. end;
  419. Procedure TTestExpressionWriter.TestBinaryGE;
  420. begin
  421. TestBinary('Greater than or equal',TJSRelationalExpressionGE,'(a >= b)',False);
  422. end;
  423. Procedure TTestExpressionWriter.TestBinaryGECompact;
  424. begin
  425. TestBinary('Greater than or equal',TJSRelationalExpressionGE,'(a>=b)',True);
  426. end;
  427. Procedure TTestExpressionWriter.TestBinaryIN;
  428. begin
  429. TestBinary('Prop in Object',TJSRelationalExpressionIN,'(a in b)',False);
  430. end;
  431. Procedure TTestExpressionWriter.TestBinaryINCompact;
  432. begin
  433. TestBinary('Prop in Object',TJSRelationalExpressionIN,'(a in b)',True);
  434. end;
  435. Procedure TTestExpressionWriter.TestBinaryInstanceOf;
  436. begin
  437. TestBinary('A instanceof Object',TJSRelationalExpressionInStanceOf,'(a instanceof b)',False);
  438. end;
  439. Procedure TTestExpressionWriter.TestBinaryInstanceOfCompact;
  440. begin
  441. TestBinary('A instanceof Object',TJSRelationalExpressionInStanceOf,'(a instanceof b)',true);
  442. end;
  443. Procedure TTestExpressionWriter.TestBinaryLShift;
  444. begin
  445. TestBinary('A lshift B',TJSLShiftExpression,'(a << b)',False);
  446. end;
  447. Procedure TTestExpressionWriter.TestBinaryLShiftOfCompact;
  448. begin
  449. TestBinary('A lshift B',TJSLShiftExpression,'(a<<b)',True);
  450. end;
  451. Procedure TTestExpressionWriter.TestBinaryRShift;
  452. begin
  453. TestBinary('A rshift B',TJSRShiftExpression,'(a >> b)',False);
  454. end;
  455. Procedure TTestExpressionWriter.TestBinaryRShiftOfCompact;
  456. begin
  457. TestBinary('A rshift B',TJSRShiftExpression,'(a>>b)',True);
  458. end;
  459. Procedure TTestExpressionWriter.TestBinaryURShift;
  460. begin
  461. TestBinary('A urshift B',TJSURShiftExpression,'(a >>> b)',False);
  462. end;
  463. Procedure TTestExpressionWriter.TestBinaryURShiftOfCompact;
  464. begin
  465. TestBinary('A urshift B',TJSURShiftExpression,'(a>>>b)',True);
  466. end;
  467. Procedure TTestExpressionWriter.TestBinaryPlus;
  468. begin
  469. TestBinary('A plus B',TJSAdditiveExpressionPlus,'(a + b)',False);
  470. end;
  471. Procedure TTestExpressionWriter.TestBinaryPlusCompact;
  472. begin
  473. TestBinary('A plus B',TJSAdditiveExpressionPlus,'(a+b)',True);
  474. end;
  475. Procedure TTestExpressionWriter.TestBinaryMinus;
  476. begin
  477. TestBinary('A minus B',TJSAdditiveExpressionMinus,'(a - b)',False);
  478. end;
  479. Procedure TTestExpressionWriter.TestBinaryMinusCompact;
  480. begin
  481. TestBinary('A minus B',TJSAdditiveExpressionMinus,'(a-b)',True);
  482. end;
  483. Procedure TTestExpressionWriter.TestBinaryMultiply;
  484. begin
  485. TestBinary('A multiply B',TJSMultiplicativeExpressionMul,'(a * b)',False);
  486. end;
  487. Procedure TTestExpressionWriter.TestBinaryMultiplyCompact;
  488. begin
  489. TestBinary('A multiply B',TJSMultiplicativeExpressionMul,'(a*b)',True);
  490. end;
  491. Procedure TTestExpressionWriter.TestBinaryDivide;
  492. begin
  493. TestBinary('A divide B',TJSMultiplicativeExpressionDiv,'(a / b)',False);
  494. end;
  495. Procedure TTestExpressionWriter.TestBinaryDivideCompact;
  496. begin
  497. TestBinary('A divide B',TJSMultiplicativeExpressionDiv,'(a/b)',True);
  498. end;
  499. Procedure TTestExpressionWriter.TestBinaryMod;
  500. begin
  501. TestBinary('A mod B',TJSMultiplicativeExpressionMod,'(a % b)',False);
  502. end;
  503. Procedure TTestExpressionWriter.TestBinaryModCompact;
  504. begin
  505. TestBinary('A mod B',TJSMultiplicativeExpressionMod,'(a%b)',True);
  506. end;
  507. Procedure TTestExpressionWriter.TestBinaryComma;
  508. begin
  509. TestBinary('A comma B',TJSCommaExpression,'(a , b)',False);
  510. end;
  511. Procedure TTestExpressionWriter.TestBinaryCommaCompact;
  512. begin
  513. TestBinary('A comma B',TJSCommaExpression,'(a,b)',True);
  514. end;
  515. Procedure TTestExpressionWriter.TestDotMember;
  516. Var
  517. U : TJSDotMemberExpression;
  518. begin
  519. U:=TJSDotMemberExpression.Create(0,0);
  520. U.Mexpr:=CreateIdent('a');
  521. U.Name:='b';
  522. AssertWrite('member b of object a (a.b)','a.b',U);
  523. end;
  524. Procedure TTestExpressionWriter.TestArgMember;
  525. Var
  526. U : TJSBracketMemberExpression;
  527. begin
  528. U:=TJSBracketMemberExpression.Create(0,0);
  529. U.Mexpr:=CreateIdent('a');
  530. U.Name:=CreateIdent('b');
  531. AssertWrite('member b of object a (a[b])','a[b]',U);
  532. end;
  533. Procedure TTestExpressionWriter.TestNewMember;
  534. Var
  535. U : TJSNewMemberExpression;
  536. begin
  537. U:=TJSNewMemberExpression.Create(0,0);
  538. U.Mexpr:=CreateIdent('a');;
  539. U.Args:=TJSArguments.Create(0,0);
  540. U.Args.Elements.AddElement;
  541. U.Args.Elements[0].Expr:=CreateLiteral(123);
  542. AssertWrite('member b of object a (a[b])','new a('+slinebreak+'123'+sLineBreak+')',U);
  543. end;
  544. Procedure TTestExpressionWriter.TestNewMemberCompact;
  545. Var
  546. U : TJSNewMemberExpression;
  547. begin
  548. Writer.Options:=Writer.Options+[woCompact];
  549. U:=TJSNewMemberExpression.Create(0,0);
  550. U.Mexpr:=CreateIdent('a');
  551. U.Args:=TJSArguments.Create(0,0);
  552. U.Args.Elements.AddElement;
  553. U.Args.Elements[0].Expr:=CreateLiteral(123);
  554. AssertWrite('new a(123)','new a(123)',U);
  555. end;
  556. Procedure TTestExpressionWriter.TestNewMemberNoArgs;
  557. Var
  558. U : TJSNewMemberExpression;
  559. begin
  560. U:=TJSNewMemberExpression.Create(0,0);
  561. U.Mexpr:=CreateIdent('a');
  562. AssertWrite('new a()','new a()',U);
  563. end;
  564. Procedure TTestExpressionWriter.TestCall;
  565. Var
  566. U : TJSCallExpression;
  567. begin
  568. U:=TJSCallExpression.Create(0,0);
  569. U.Expr:=CreateIdent('a');
  570. U.Args:=TJSArguments.Create(0,0);
  571. U.Args.Elements.AddElement;
  572. U.Args.Elements[0].Expr:=CreateLiteral(123);
  573. AssertWrite('call a(123)','a('+slinebreak+'123'+sLineBreak+')',U);
  574. end;
  575. Procedure TTestExpressionWriter.TestCallCompact;
  576. Var
  577. U : TJSCallExpression;
  578. begin
  579. Writer.Options:=Writer.Options+[woCompact];
  580. U:=TJSCallExpression.Create(0,0);
  581. U.Expr:=CreateIdent('a');
  582. U.Args:=TJSArguments.Create(0,0);
  583. U.Args.Elements.AddElement;
  584. U.Args.Elements[0].Expr:=CreateLiteral(123);
  585. AssertWrite('call a(123)','a(123)',U);
  586. end;
  587. Procedure TTestExpressionWriter.TestCallCompact2;
  588. Var
  589. U : TJSCallExpression;
  590. begin
  591. Writer.Options:=Writer.Options+[woCompactArguments];
  592. U:=TJSCallExpression.Create(0,0);
  593. U.Expr:=CreateIdent('a');
  594. U.Args:=TJSArguments.Create(0,0);
  595. U.Args.Elements.AddElement;
  596. U.Args.Elements[0].Expr:=CreateLiteral(123);
  597. U.Args.Elements.AddElement;
  598. U.Args.Elements[1].Expr:=CreateLiteral(456);
  599. AssertWrite('call a(123,456)','a(123, 456)',U);
  600. end;
  601. Procedure TTestExpressionWriter.TestCallNoArgs;
  602. Var
  603. U : TJSCallExpression;
  604. begin
  605. U:=TJSCallExpression.Create(0,0);
  606. U.Expr:=CreateIdent('a');
  607. AssertWrite('call a()','a()',U);
  608. end;
  609. Procedure TTestExpressionWriter.TestConditional;
  610. Var
  611. U : TJSConditionalExpression;
  612. begin
  613. U:=TJSConditionalExpression.Create(0,0);
  614. U.A:=CreateIdent('a');
  615. U.B:=CreateIdent('b');
  616. U.C:=CreateIdent('c');
  617. AssertWrite('a ? b : c','(a ? b : c)',U);
  618. end;
  619. Procedure TTestExpressionWriter.TestRegularExpressionLiteral;
  620. Var
  621. S : TJSRegularExpressionLiteral;
  622. begin
  623. S:=TJSRegularExpressionLiteral.Create(0,0);
  624. S.Pattern.AsString:='a';
  625. AssertWrite('/a/','/a/',S);
  626. end;
  627. Procedure TTestExpressionWriter.TestRegularExpressionLiteralFlags;
  628. Var
  629. S : TJSRegularExpressionLiteral;
  630. begin
  631. S:=TJSRegularExpressionLiteral.Create(0,0);
  632. S.Pattern.AsString:='a';
  633. S.PatternFlags.AsString:='g';
  634. AssertWrite('/a/g','/a/g',S);
  635. end;
  636. { ---------------------------------------------------------------------
  637. TTestStatementWriter
  638. ---------------------------------------------------------------------}
  639. Procedure TTestStatementWriter.TestAssignment(Const Msg: String;
  640. AClass: TJSAssignStatementClass; Result: String; ACompact: Boolean);
  641. Var
  642. U : TJSAssignStatement;
  643. begin
  644. if ACompact then
  645. Writer.Options:=Writer.Options+[woCompact];
  646. U:=CreateAssignment(AClass);
  647. AssertWrite(Msg,Result,U);
  648. end;
  649. Function TTestStatementWriter.CreateAssignment(AClass: TJSAssignStatementClass
  650. ): TJSAssignStatement;
  651. begin
  652. if AClass=Nil then
  653. AClass := TJSSimpleAssignStatement;
  654. Result:=AClass.Create(0,0);
  655. Result.LHS:=CreateIdent('a');
  656. Result.Expr:=CreateIdent('b');
  657. end;
  658. Procedure TTestStatementWriter.TestEmptyStatement;
  659. begin
  660. AssertWrite('Empty statement','',TJSEmptyStatement.Create(0,0));
  661. end;
  662. Procedure TTestStatementWriter.TestEmptyStatementComment;
  663. begin
  664. Writer.Options:=[woEmptyStatementAsComment,woUseUTF8];
  665. AssertWrite('Empty statement as comment','/* Empty statement */',TJSEmptyStatement.Create(0,0));
  666. end;
  667. Procedure TTestStatementWriter.TestEmptyStatementBlock;
  668. begin
  669. AssertWrite('Empty statement block','{'+sLineBreak+'}',TJSEmptyBlockStatement.Create(0,0));
  670. end;
  671. Procedure TTestStatementWriter.TestEmptyStatementBlockIndent;
  672. begin
  673. Writer.IndentSize:=2;
  674. Writer.Indent;
  675. AssertWrite('Empty statement block',' {'+sLineBreak+' }',TJSEmptyBlockStatement.Create(0,0));
  676. end;
  677. Procedure TTestStatementWriter.TestEmptyStatementBlockCompact;
  678. begin
  679. Writer.Options:=[woCompact,woUseUTF8];
  680. AssertWrite('Empty statement block','{}',TJSEmptyBlockStatement.Create(0,0));
  681. end;
  682. Procedure TTestStatementWriter.TestVarDeclaration;
  683. Var
  684. V : TJSVarDeclaration;
  685. L : TJSPrimaryExpressionIdent;
  686. begin
  687. V:=TJSVarDeclaration.Create(0,0);
  688. V.Name:='a';
  689. AssertWrite('simple var','a',V);
  690. end;
  691. Procedure TTestStatementWriter.TestVarDeclarationInit;
  692. Var
  693. V : TJSVarDeclaration;
  694. L : TJSLiteral;
  695. begin
  696. V:=TJSVarDeclaration.Create(0,0);
  697. V.Name:='a';
  698. V.Init:=CreateLiteral(1);
  699. AssertWrite('simple var, init ','a = 1',V);
  700. end;
  701. Procedure TTestStatementWriter.TestVarListDeclaration;
  702. Var
  703. B,L : TJSVariableDeclarationList;
  704. V : TJSVarDeclaration;
  705. begin
  706. L:=TJSVariableDeclarationList.Create(0,0);
  707. V:=TJSVarDeclaration.Create(0,0);
  708. V.Name:='a';
  709. L.A:=V;
  710. B:=TJSVariableDeclarationList.Create(0,0);
  711. V:=TJSVarDeclaration.Create(0,0);
  712. V.Name:='b';
  713. B.A:=V;
  714. V.Init:=CreateLiteral(1);
  715. L.B:=B;
  716. AssertWrite('simple var list ','a, b = 1',L);
  717. end;
  718. Procedure TTestStatementWriter.TestVarListDeclarationInit;
  719. Var
  720. B,L : TJSVariableDeclarationList;
  721. V : TJSVarDeclaration;
  722. begin
  723. L:=TJSVariableDeclarationList.Create(0,0);
  724. V:=TJSVarDeclaration.Create(0,0);;
  725. V.Name:='a';
  726. L.A:=V;
  727. B:=TJSVariableDeclarationList.Create(0,0);
  728. V:=TJSVarDeclaration.Create(0,0);;
  729. V.Name:='b';
  730. B.A:=V;
  731. L.B:=B;
  732. AssertWrite('simple var list ','a, b',L);
  733. end;
  734. Procedure TTestStatementWriter.TestVarDeclarationStatement;
  735. Var
  736. S : TJSVariableStatement;
  737. V : TJSVarDeclaration;
  738. L : TJSPrimaryExpressionIdent;
  739. begin
  740. S:=TJSVariableStatement.Create(0,0);
  741. V:=TJSVarDeclaration.Create(0,0);
  742. S.A:=V;
  743. V.Name:='a';
  744. AssertWrite('simple var','var a',S);
  745. end;
  746. Procedure TTestStatementWriter.TestVarListDeclarationStatement;
  747. Var
  748. S : TJSVariableStatement;
  749. V : TJSVarDeclaration;
  750. L : TJSVariableDeclarationList;
  751. begin
  752. S:=TJSVariableStatement.Create(0,0);
  753. L:=TJSVariableDeclarationList.Create(0,0);
  754. V:=TJSVarDeclaration.Create(0,0);
  755. L.A:=V;
  756. S.A:=L;
  757. V.Name:='a';
  758. AssertWrite('simple var','var a',S);
  759. end;
  760. Procedure TTestStatementWriter.TestVarListDeclarationStatement2Vars;
  761. Var
  762. S : TJSVariableStatement;
  763. V : TJSVarDeclaration;
  764. L : TJSVariableDeclarationList;
  765. begin
  766. S:=TJSVariableStatement.Create(0,0);
  767. L:=TJSVariableDeclarationList.Create(0,0);
  768. S.A:=L;
  769. V:=TJSVarDeclaration.Create(0,0);
  770. L.A:=V;
  771. V.Name:='a';
  772. L.B:=TJSVariableDeclarationList.Create(0,0);
  773. L:=TJSVariableDeclarationList(L.B);
  774. V:=TJSVarDeclaration.Create(0,0);
  775. L.A:=V;
  776. V.Name:='b';
  777. AssertWrite('simple 2 vars','var a, b',S);
  778. end;
  779. Procedure TTestStatementWriter.TestReturnStatement;
  780. Var
  781. S : TJSReturnStatement;
  782. begin
  783. S:=TJSReturnStatement.Create(0,0);
  784. S.Expr:=CreateIdent('a');
  785. AssertWrite('simple return','return a',S);
  786. end;
  787. Procedure TTestStatementWriter.TestLabeledStatement;
  788. Var
  789. LS : TJSLabeledStatement;
  790. S : TJSReturnStatement;
  791. begin
  792. LS:=TJSLabeledStatement.Create(0,0);
  793. LS.TheLabel:=TJSLabel.Create;
  794. LS.TheLabel.Name:='loc';
  795. S:=TJSReturnStatement.Create(0,0);
  796. S.Expr:=CreateIDent('a');
  797. LS.A:=S;
  798. AssertWrite('simple return','loc:'+sLineBreak+'return a',LS);
  799. end;
  800. Procedure TTestStatementWriter.TestLabeledStatementCompact;
  801. Var
  802. LS : TJSLabeledStatement;
  803. S : TJSReturnStatement;
  804. begin
  805. Writer.Options:=[woCompact,woUseUTF8];
  806. LS:=TJSLabeledStatement.Create(0,0);
  807. LS.TheLabel:=TJSLabel.Create;
  808. LS.TheLabel.Name:='loc';
  809. S:=TJSReturnStatement.Create(0,0);
  810. S.Expr:=CreateIdent('a');
  811. LS.A:=S;
  812. AssertWrite('simple return','loc: return a',LS);
  813. end;
  814. Procedure TTestStatementWriter.TestContinueStatement;
  815. Var
  816. S : TJSContinueStatement;
  817. begin
  818. S:=TJSContinueStatement.Create(0,0);
  819. AssertWrite('simple continue','continue',S);
  820. end;
  821. Procedure TTestStatementWriter.TestContinueTargetStatement;
  822. Var
  823. S : TJSContinueStatement;
  824. begin
  825. S:=TJSContinueStatement.Create(0,0);
  826. S.TargetName:='a';
  827. AssertWrite('continue a','continue a',S);
  828. end;
  829. Procedure TTestStatementWriter.TestBreakStatement;
  830. Var
  831. S : TJSBreakStatement;
  832. begin
  833. S:=TJSBreakStatement.Create(0,0);
  834. AssertWrite('simple break','break',S);
  835. end;
  836. Procedure TTestStatementWriter.TestBreakTargetStatement;
  837. Var
  838. S : TJSBreakStatement;
  839. begin
  840. S:=TJSBreakStatement.Create(0,0);
  841. S.TargetName:='a';
  842. AssertWrite('simple break a','break a',S);
  843. end;
  844. Procedure TTestStatementWriter.TestAssignmentStatementSimple;
  845. begin
  846. TestAssignment('Simple assignment',TJSSimpleAssignStatement,'a = b',False);
  847. end;
  848. Procedure TTestStatementWriter.TestAssignmentStatementSimpleCompact;
  849. begin
  850. TestAssignment('Simple assignment',TJSSimpleAssignStatement,'a=b',True);
  851. end;
  852. Procedure TTestStatementWriter.TestAssignmentStatementAdd;
  853. begin
  854. TestAssignment('Add assignment',TJSAddEqAssignStatement,'a += b',False);
  855. end;
  856. Procedure TTestStatementWriter.TestAssignmentStatementAddCompact;
  857. begin
  858. TestAssignment('Add assignment',TJSAddEqAssignStatement,'a+=b',True);
  859. end;
  860. Procedure TTestStatementWriter.TestAssignmentStatementSubtract;
  861. begin
  862. TestAssignment('Subtract assignment',TJSSubEqAssignStatement,'a -= b',False);
  863. end;
  864. Procedure TTestStatementWriter.TestAssignmentStatementSubtractCompact;
  865. begin
  866. TestAssignment('Subtract assignment',TJSSubEqAssignStatement,'a-=b',True);
  867. end;
  868. Procedure TTestStatementWriter.TestAssignmentStatementMultiply;
  869. begin
  870. TestAssignment('Multiply assignment',TJSMulEqAssignStatement,'a *= b',False);
  871. end;
  872. Procedure TTestStatementWriter.TestAssignmentStatementMultiplyCompact;
  873. begin
  874. TestAssignment('Multiply assignment',TJSMulEqAssignStatement,'a*=b',True);
  875. end;
  876. Procedure TTestStatementWriter.TestAssignmentStatementDivide;
  877. begin
  878. TestAssignment('Divide assignment',TJSDivEqAssignStatement,'a /= b',False);
  879. end;
  880. Procedure TTestStatementWriter.TestAssignmentStatementDivideCompact;
  881. begin
  882. TestAssignment('Divide assignment',TJSDivEqAssignStatement,'a/=b',True);
  883. end;
  884. Procedure TTestStatementWriter.TestAssignmentStatementShift;
  885. begin
  886. TestAssignment('Shift assignment',TJSLShiftEqAssignStatement,'a <<= b',False);
  887. end;
  888. Procedure TTestStatementWriter.TestAssignmentStatementShiftCompact;
  889. begin
  890. TestAssignment('Shift assignment',TJSLShiftEqAssignStatement,'a<<=b',True);
  891. end;
  892. Procedure TTestStatementWriter.TestAssignmentStatementRShift;
  893. begin
  894. TestAssignment('RShift assignment',TJSRShiftEqAssignStatement,'a >>= b',False);
  895. end;
  896. Procedure TTestStatementWriter.TestAssignmentStatementRShiftCompact;
  897. begin
  898. TestAssignment('RShift assignment',TJSRShiftEqAssignStatement,'a>>=b',True);
  899. end;
  900. Procedure TTestStatementWriter.TestAssignmentStatementURShift;
  901. begin
  902. TestAssignment('URShift assignment',TJSURShiftEqAssignStatement,'a >>>= b',False);
  903. end;
  904. Procedure TTestStatementWriter.TestAssignmentStatementURShiftCompact;
  905. begin
  906. TestAssignment('URShift assignment',TJSURShiftEqAssignStatement,'a>>>=b',True);
  907. end;
  908. Procedure TTestStatementWriter.TestAssignmentStatementMod;
  909. begin
  910. TestAssignment('Mod assignment',TJSModEqAssignStatement,'a %= b',False);
  911. end;
  912. Procedure TTestStatementWriter.TestAssignmentStatementModCompact;
  913. begin
  914. TestAssignment('Mod assignment',TJSModEqAssignStatement,'a%=b',True);
  915. end;
  916. Procedure TTestStatementWriter.TestAssignmentStatementBinaryOr;
  917. begin
  918. TestAssignment('Binary or assignment',TJSOrEqAssignStatement,'a |= b',False);
  919. end;
  920. Procedure TTestStatementWriter.TestAssignmentStatementBinaryOrCompact;
  921. begin
  922. TestAssignment('Binary or assignment',TJSOrEqAssignStatement,'a |= b',False);
  923. end;
  924. Procedure TTestStatementWriter.TestAssignmentStatementBinaryXOr;
  925. begin
  926. TestAssignment('Binary xor assignment',TJSXOrEqAssignStatement,'a ^= b',False);
  927. end;
  928. Procedure TTestStatementWriter.TestAssignmentStatementBinaryXOrCompact;
  929. begin
  930. TestAssignment('Binary xor assignment',TJSXOrEqAssignStatement,'a^=b',True);
  931. end;
  932. Procedure TTestStatementWriter.TestAssignmentStatementBinaryAnd;
  933. begin
  934. TestAssignment('Binary and assignment',TJSAndEqAssignStatement,'a &= b',False);
  935. end;
  936. Procedure TTestStatementWriter.TestAssignmentStatementBinaryAndCompact;
  937. begin
  938. TestAssignment('Binary and assignment',TJSAndEqAssignStatement,'a&=b',True);
  939. end;
  940. Procedure TTestStatementWriter.TestForStatementEmpty;
  941. Var
  942. S : TJSForStatement;
  943. begin
  944. S:=TJSForStatement.Create(0,0);
  945. S.Body:=TJSEmptyBlockStatement.Create(0,0);
  946. AssertWrite('neverending for','for (; ; ) {'+sLineBreak+'}',S);
  947. end;
  948. Procedure TTestStatementWriter.TestForStatementFull;
  949. Var
  950. S : TJSForStatement;
  951. UPP : TJSUnaryPostPlusPlusExpression;
  952. CL : TJSRelationalExpressionLT;
  953. L : TJSLiteral;
  954. sa : TJSSimpleAssignStatement;
  955. begin
  956. SA:=TJSSimpleAssignStatement.Create(0,0);
  957. SA.LHS:=CreateIdent('i');
  958. SA.Expr:=CreateLiteral(0);
  959. UPP:=TJSUnaryPostPlusPlusExpression.Create(0,0);
  960. UPP.A:=CreateIdent('i');
  961. CL:=TJSRelationalExpressionLT.Create(0,0);
  962. CL.A:=CreateIdent('i');
  963. CL.B:=CreateLiteral(10);
  964. S:=TJSForStatement.Create(0,0);
  965. S.Init:=SA;
  966. S.Incr:=UPP;
  967. S.Cond:=CL;
  968. S.Body:=TJSEmptyBlockStatement.Create(0,0);
  969. AssertWrite('for i:=0 to 9','for (i = 0; (i < 10); i++) {'+sLineBreak+'}',S);
  970. end;
  971. Procedure TTestStatementWriter.TestForStatementCompact;
  972. Var
  973. S : TJSForStatement;
  974. UPP : TJSUnaryPostPlusPlusExpression;
  975. CL : TJSRelationalExpressionLT;
  976. L : TJSLiteral;
  977. sa : TJSSimpleAssignStatement;
  978. begin
  979. SA:=TJSSimpleAssignStatement.Create(0,0);
  980. SA.LHS:=CreateIdent('i');
  981. SA.Expr:=CreateLiteral(0);
  982. UPP:=TJSUnaryPostPlusPlusExpression.Create(0,0);
  983. UPP.A:=CreateIdent('i');
  984. CL:=TJSRelationalExpressionLT.Create(0,0);
  985. CL.A:=CreateIdent('i');
  986. CL.B:=CreateLiteral(10);
  987. S:=TJSForStatement.Create(0,0);
  988. S.Init:=SA;
  989. S.Incr:=UPP;
  990. S.Cond:=CL;
  991. S.Body:=TJSEmptyBlockStatement.Create(0,0);
  992. Writer.Options:=[woCompact,woUseUTF8];
  993. AssertWrite('for i:=0 to 9','for (i=0; (i<10); i++) {}',S);
  994. end;
  995. Procedure TTestStatementWriter.TestForInStatement;
  996. Var
  997. S : TJSForInStatement;
  998. begin
  999. S:=TJSForInStatement.Create(0,0);
  1000. S.LHS:=CreateIdent('a');
  1001. S.List:=CreateIdent('b');
  1002. S.Body:=TJSEmptyBlockStatement.Create(0,0);
  1003. AssertWrite('for a in b','for (a in b) {'+sLineBreak+'}',S);
  1004. end;
  1005. Procedure TTestStatementWriter.TestWhileStatement;
  1006. Var
  1007. S : TJSWhileStatement;
  1008. begin
  1009. S:=TJSWhileStatement.Create(0,0);
  1010. S.Cond:=CreateIdent('a');
  1011. S.Body:=TJSEmptyBlockStatement.Create(0,0);
  1012. AssertWrite('while a ','while (a) {'+sLineBreak+'}',S);
  1013. end;
  1014. Procedure TTestStatementWriter.TestDoWhileStatement;
  1015. Var
  1016. S : TJSDoWhileStatement;
  1017. begin
  1018. S:=TJSDoWhileStatement.Create(0,0);
  1019. S.Cond:=CreateIdent('a');
  1020. S.Body:=TJSEmptyBlockStatement.Create(0,0);
  1021. AssertWrite('do while a ','do {'+sLineBreak+'} while (a)',S);
  1022. end;
  1023. Procedure TTestStatementWriter.TestSwitchStatementEmpty;
  1024. Var
  1025. S : TJSSwitchStatement;
  1026. begin
  1027. S:=TJSSwitchStatement.Create(0,0);
  1028. S.Cond:=CreateIdent('a');
  1029. AssertWrite('switch ','switch (a) {'+sLineBreak+'}',S);
  1030. end;
  1031. Procedure TTestStatementWriter.TestSwitchStatementEmptyCompact;
  1032. Var
  1033. S : TJSSwitchStatement;
  1034. begin
  1035. Writer.Options:=[woCompact,woUseUTF8];
  1036. S:=TJSSwitchStatement.Create(0,0);
  1037. S.Cond:=CreateIdent('a');
  1038. AssertWrite('switch ','switch (a) {}',S);
  1039. end;
  1040. Procedure TTestStatementWriter.TestSwitchStatementOneElement;
  1041. Var
  1042. S : TJSSwitchStatement;
  1043. C : TJSCaseElement;
  1044. begin
  1045. // Writer.Options:=[woCompact,woUseUTF8];
  1046. S:=TJSSwitchStatement.Create(0,0);
  1047. S.Cond:=CreateIdent('a');
  1048. C:=S.Cases.AddCase;
  1049. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1050. C.Expr:=CreateIdent('c');
  1051. AssertWrite('switch ','switch (a) {'+sLineBreak+'case c:'+sLineBreak+'{'+sLineBreak+'}'+sLineBreak+'}',S);
  1052. end;
  1053. Procedure TTestStatementWriter.TestSwitchStatementOneElementCompact;
  1054. Var
  1055. S : TJSSwitchStatement;
  1056. C : TJSCaseElement;
  1057. begin
  1058. Writer.Options:=[woCompact,woUseUTF8];
  1059. S:=TJSSwitchStatement.Create(0,0);
  1060. S.Cond:=CreateIdent('a');
  1061. C:=S.Cases.AddCase;
  1062. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1063. C.Expr:=CreateIdent('c');
  1064. AssertWrite('switch ','switch (a) {case c: {}}',S);
  1065. end;
  1066. Procedure TTestStatementWriter.TestSwitchStatementTwoElements;
  1067. Var
  1068. S : TJSSwitchStatement;
  1069. C : TJSCaseElement;
  1070. begin
  1071. // Writer.Options:=[woCompact,woUseUTF8];
  1072. S:=TJSSwitchStatement.Create(0,0);
  1073. S.Cond:=CreateIdent('a');
  1074. C:=S.Cases.AddCase;
  1075. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1076. C.Expr:=CreateIdent('c');
  1077. C:=S.Cases.AddCase;
  1078. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1079. C.Expr:=CreateIdent('d');
  1080. AssertWrite('switch ','switch (a) {'+sLineBreak+'case c:'+sLineBreak+'{'+sLineBreak+'}'+sLineBreak+'case d:'+sLineBreak+'{'+sLineBreak+'}'+sLineBreak+'}',S);
  1081. end;
  1082. Procedure TTestStatementWriter.TestSwitchStatementTwoElementsCompact;
  1083. Var
  1084. S : TJSSwitchStatement;
  1085. C : TJSCaseElement;
  1086. begin
  1087. Writer.Options:=[woCompact,woUseUTF8];
  1088. S:=TJSSwitchStatement.Create(0,0);
  1089. S.Cond:=CreateIdent('a');
  1090. C:=S.Cases.AddCase;
  1091. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1092. C.Expr:=CreateIdent('c');
  1093. C:=S.Cases.AddCase;
  1094. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1095. C.Expr:=CreateIdent('d');
  1096. AssertWrite('switch ','switch (a) {case c: {}case d: {}}',S);
  1097. end;
  1098. Procedure TTestStatementWriter.TestSwitchStatementTwoElementsDefault;
  1099. Var
  1100. S : TJSSwitchStatement;
  1101. C : TJSCaseElement;
  1102. begin
  1103. S:=TJSSwitchStatement.Create(0,0);
  1104. S.Cond:=CreateIdent('a');
  1105. C:=S.Cases.AddCase;
  1106. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1107. C.Expr:=CreateIdent('c');
  1108. C:=S.Cases.AddCase;
  1109. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1110. C.Expr:=CreateIdent('d');
  1111. C:=S.Cases.AddCase;
  1112. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1113. S.TheDefault:=C;
  1114. AssertWrite('switch ','switch (a) {'+sLineBreak+'case c:'+sLineBreak+'{'+sLineBreak+'}'+sLineBreak+'case d:'+sLineBreak+'{'+sLineBreak+'}'+sLineBreak+'default:'+sLineBreak+'{'+sLineBreak+'}'+sLineBreak+'}',S);
  1115. end;
  1116. Procedure TTestStatementWriter.TestSwitchStatementTwoElementsDefaultCompact;
  1117. Var
  1118. S : TJSSwitchStatement;
  1119. C : TJSCaseElement;
  1120. begin
  1121. Writer.Options:=[woCompact,woUseUTF8];
  1122. S:=TJSSwitchStatement.Create(0,0);
  1123. S.Cond:=CreateIdent('a');
  1124. C:=S.Cases.AddCase;
  1125. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1126. C.Expr:=CreateIdent('c');
  1127. C:=S.Cases.AddCase;
  1128. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1129. C.Expr:=CreateIdent('d');
  1130. C:=S.Cases.AddCase;
  1131. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1132. S.TheDefault:=C;
  1133. AssertWrite('switch ','switch (a) {case c: {}case d: {}default: {}}',S);
  1134. end;
  1135. Procedure TTestStatementWriter.TestSwitchStatementTwoElementsOneEmpty;
  1136. Var
  1137. S : TJSSwitchStatement;
  1138. C : TJSCaseElement;
  1139. begin
  1140. S:=TJSSwitchStatement.Create(0,0);
  1141. S.Cond:=CreateIdent('a');
  1142. C:=S.Cases.AddCase;
  1143. C.Expr:=CreateIdent('c');
  1144. C:=S.Cases.AddCase;
  1145. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1146. C.Expr:=CreateIdent('d');
  1147. C:=S.Cases.AddCase;
  1148. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1149. S.TheDefault:=C;
  1150. AssertWrite('switch ','switch (a) {'+sLineBreak+'case c:'+sLineBreak+'case d:'+sLineBreak+'{'+sLineBreak+'}'+sLineBreak+'default:'+sLineBreak+'{'+sLineBreak+'}'+sLineBreak+'}',S);
  1151. end;
  1152. Procedure TTestStatementWriter.TestSwitchStatementTwoElementsOneEmptyCompact;
  1153. Var
  1154. S : TJSSwitchStatement;
  1155. C : TJSCaseElement;
  1156. begin
  1157. Writer.Options:=[woCompact,woUseUTF8];
  1158. S:=TJSSwitchStatement.Create(0,0);
  1159. S.Cond:=CreateIdent('a');
  1160. C:=S.Cases.AddCase;
  1161. C.Expr:=CreateIdent('c');
  1162. C:=S.Cases.AddCase;
  1163. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1164. C.Expr:=CreateIdent('d');
  1165. C:=S.Cases.AddCase;
  1166. C.Body:=TJSEmptyBlockStatement.Create(0,0);;
  1167. S.TheDefault:=C;
  1168. AssertWrite('switch ','switch (a) {case c: case d: {}default: {}}',S);
  1169. end;
  1170. Procedure TTestStatementWriter.TestIfThen;
  1171. Var
  1172. S : TJSIfStatement;
  1173. begin
  1174. // Writer.Options:=[woCompact,woUseUTF8];
  1175. S:=TJSIfStatement.Create(0,0);
  1176. S.Cond:=CreateIdent('a');
  1177. S.btrue:=TJSEmptyBlockStatement.Create(0,0);
  1178. AssertWrite('if then','if (a) {'+sLineBreak+'}',S);
  1179. end;
  1180. Procedure TTestStatementWriter.TestIfThenElse;
  1181. Var
  1182. S : TJSIfStatement;
  1183. begin
  1184. // Writer.Options:=[woCompact,woUseUTF8];
  1185. S:=TJSIfStatement.Create(0,0);
  1186. S.Cond:=CreateIdent('a');
  1187. S.btrue:=TJSEmptyBlockStatement.Create(0,0);
  1188. S.bfalse:=TJSEmptyBlockStatement.Create(0,0);
  1189. AssertWrite('if then','if (a) {'+sLineBreak+'} else {'+sLineBreak+'}',S);
  1190. end;
  1191. Procedure TTestStatementWriter.TestStatementListEmpty;
  1192. Var
  1193. S : TJSStatementList;
  1194. begin
  1195. // Writer.Options:=[woCompact,woUseUTF8];
  1196. S:=TJSStatementList.Create(0,0);
  1197. AssertWrite('Statement list','{'+sLineBreak+'}'+sLineBreak,S);
  1198. end;
  1199. Procedure TTestStatementWriter.TestStatementListEmptyCompact;
  1200. Var
  1201. S : TJSStatementList;
  1202. begin
  1203. Writer.Options:=[woCompact,woUseUTF8];
  1204. S:=TJSStatementList.Create(0,0);
  1205. AssertWrite('Statement list','{}',S);
  1206. end;
  1207. Procedure TTestStatementWriter.TestStatementListOneStatement;
  1208. Var
  1209. S : TJSStatementList;
  1210. begin
  1211. // Writer.Options:=[woCompact,woUseUTF8];
  1212. S:=TJSStatementList.Create(0,0);
  1213. S.A:=CreateAssignment(nil);
  1214. AssertWrite('Statement list','{'+sLineBreak+'a = b;'+sLineBreak+'}'+sLineBreak,S);
  1215. end;
  1216. Procedure TTestStatementWriter.TestStatementListOneStatementCompact;
  1217. Var
  1218. S : TJSStatementList;
  1219. begin
  1220. Writer.Options:=[woCompact,woUseUTF8];
  1221. S:=TJSStatementList.Create(0,0);
  1222. S.A:=CreateAssignment(nil);
  1223. AssertWrite('Statement list','{a=b}',S);
  1224. end;
  1225. Procedure TTestStatementWriter.TestStatementListTwoStatements;
  1226. Var
  1227. S : TJSStatementList;
  1228. begin
  1229. // Writer.Options:=[woCompact,woUseUTF8];
  1230. S:=TJSStatementList.Create(0,0);
  1231. S.A:=CreateAssignment(nil);
  1232. S.B:=CreateAssignment(nil);
  1233. AssertWrite('Statement list','{'+sLineBreak+'a = b;'+sLineBreak+'a = b;'+sLineBreak+'}'+sLineBreak,S);
  1234. end;
  1235. Procedure TTestStatementWriter.TestStatementListTwoStatementsCompact;
  1236. Var
  1237. S : TJSStatementList;
  1238. begin
  1239. Writer.Options:=[woCompact,woUseUTF8];
  1240. S:=TJSStatementList.Create(0,0);
  1241. S.A:=CreateAssignment(nil);
  1242. S.B:=CreateAssignment(nil);
  1243. AssertWrite('Statement list','{a=b; a=b}',S);
  1244. end;
  1245. Procedure TTestStatementWriter.TestEmptyFunctionDef;
  1246. Var
  1247. FD : TJSFunctionDeclarationStatement;
  1248. begin
  1249. FD:=TJSFunctionDeclarationStatement.Create(0,0);
  1250. FD.AFunction:=TJSFuncDef.Create;
  1251. FD.AFunction.Name:='a';
  1252. AssertWrite('Empty function','function a() {'+sLineBreak+'}'+sLineBreak,FD);
  1253. end;
  1254. Procedure TTestStatementWriter.TestEmptyFunctionDefCompact;
  1255. Var
  1256. FD : TJSFunctionDeclarationStatement;
  1257. begin
  1258. Writer.Options:=[woCompact,woUseUTF8];
  1259. FD:=TJSFunctionDeclarationStatement.Create(0,0);
  1260. FD.AFunction:=TJSFuncDef.Create;
  1261. FD.AFunction.Name:='a';
  1262. AssertWrite('Empty function, compact','function a() {}',FD);
  1263. end;
  1264. Procedure TTestStatementWriter.TestFunctionDefParams;
  1265. Var
  1266. FD : TJSFunctionDeclarationStatement;
  1267. begin
  1268. // Writer.Options:=[woCompact,woUseUTF8];
  1269. FD:=TJSFunctionDeclarationStatement.Create(0,0);
  1270. FD.AFunction:=TJSFuncDef.Create;
  1271. FD.AFunction.Name:='a';
  1272. FD.AFunction.Params.Add('b');
  1273. FD.AFunction.Params.Add('c');
  1274. FD.AFunction.Params.Add('d');
  1275. AssertWrite('Empty function, 3 params','function a(b, c, d) {'+sLineBreak+'}'+sLineBreak,FD);
  1276. end;
  1277. Procedure TTestStatementWriter.TestFunctionDefParamsCompact;
  1278. Var
  1279. FD : TJSFunctionDeclarationStatement;
  1280. begin
  1281. Writer.Options:=[woCompact,woUseUTF8];
  1282. FD:=TJSFunctionDeclarationStatement.Create(0,0);
  1283. FD.AFunction:=TJSFuncDef.Create;
  1284. FD.AFunction.Name:='a';
  1285. FD.AFunction.Params.Add('b');
  1286. FD.AFunction.Params.Add('c');
  1287. FD.AFunction.Params.Add('d');
  1288. AssertWrite('Empty function, 3 params, compact','function a(b,c,d) {}',FD);
  1289. end;
  1290. Procedure TTestStatementWriter.TestFunctionDefBody1;
  1291. Var
  1292. FD : TJSFunctionDeclarationStatement;
  1293. R : TJSReturnStatement;
  1294. begin
  1295. Writer.IndentSize:=2;
  1296. // Writer.Options:=[woCompact,woUseUTF8];
  1297. FD:=TJSFunctionDeclarationStatement.Create(0,0);
  1298. FD.AFunction:=TJSFuncDef.Create;
  1299. FD.AFunction.Name:='a';
  1300. FD.AFunction.Body:=TJSFunctionBody.Create(0,0);
  1301. R:=TJSReturnStatement.Create(0,0);
  1302. R.Expr:=CreateLiteral(0);
  1303. FD.AFunction.Body.A:=R;
  1304. AssertWrite('1 statement, ','function a() {'+sLineBreak+' return 0;'+sLineBreak+'}'+sLineBreak,FD);
  1305. end;
  1306. Procedure TTestStatementWriter.TestFunctionDefBody1Compact;
  1307. Var
  1308. FD : TJSFunctionDeclarationStatement;
  1309. R : TJSReturnStatement;
  1310. begin
  1311. Writer.IndentSize:=2;
  1312. Writer.Options:=[woCompact,woUseUTF8];
  1313. FD:=TJSFunctionDeclarationStatement.Create(0,0);
  1314. FD.AFunction:=TJSFuncDef.Create;
  1315. FD.AFunction.Name:='a';
  1316. FD.AFunction.Body:=TJSFunctionBody.Create(0,0);
  1317. R:=TJSReturnStatement.Create(0,0);
  1318. R.Expr:=CreateLiteral(0);
  1319. FD.AFunction.Body.A:=R;
  1320. AssertWrite('1 statement, compact','function a() {return 0; }',FD);
  1321. end;
  1322. Procedure TTestStatementWriter.TestFunctionDefBody2;
  1323. Var
  1324. FD : TJSFunctionDeclarationStatement;
  1325. R : TJSReturnStatement;
  1326. L : TJSStatementList;
  1327. A : TJSAssignStatement;
  1328. M : TJSMultiplicativeExpressionMul;
  1329. begin
  1330. Writer.IndentSize:=2;
  1331. // Writer.Options:=[woCompact,woUseUTF8];
  1332. FD:=TJSFunctionDeclarationStatement.Create(0,0);
  1333. FD.AFunction:=TJSFuncDef.Create;
  1334. FD.AFunction.Name:='a';
  1335. FD.AFunction.Body:=TJSFunctionBody.Create(0,0);
  1336. FD.AFunction.Params.Add('b');
  1337. R:=TJSReturnStatement.Create(0,0);
  1338. R.Expr:=CreateIdent('b');
  1339. L:=TJSStatementList.Create(0,0);
  1340. A:=TJSSimpleAssignStatement.Create(0,0);
  1341. A.LHS:=CreateIdent('b');
  1342. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1343. M.A:=CreateIdent('b');
  1344. M.B:=CreateLiteral(10);
  1345. A.Expr:=M;
  1346. L.A:=A;
  1347. L.B:=R;
  1348. FD.AFunction.Body.A:=L;
  1349. AssertWrite('Function, 2 statements','function a(b) {'+sLineBreak+' b = (b * 10);'+sLineBreak+' return b;'+sLineBreak+'}'+sLineBreak,FD);
  1350. end;
  1351. Procedure TTestStatementWriter.TestFunctionDefBody2Compact;
  1352. Var
  1353. FD : TJSFunctionDeclarationStatement;
  1354. R : TJSReturnStatement;
  1355. L : TJSStatementList;
  1356. A : TJSAssignStatement;
  1357. M : TJSMultiplicativeExpressionMul;
  1358. begin
  1359. Writer.IndentSize:=2;
  1360. Writer.Options:=[woCompact,woUseUTF8];
  1361. FD:=TJSFunctionDeclarationStatement.Create(0,0);
  1362. FD.AFunction:=TJSFuncDef.Create;
  1363. FD.AFunction.Name:='a';
  1364. FD.AFunction.Body:=TJSFunctionBody.Create(0,0);
  1365. FD.AFunction.Params.Add('b');
  1366. R:=TJSReturnStatement.Create(0,0);
  1367. R.Expr:=CreateIdent('b');
  1368. L:=TJSStatementList.Create(0,0);
  1369. A:=TJSSimpleAssignStatement.Create(0,0);
  1370. A.LHS:=CreateIdent('b');
  1371. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1372. M.A:=CreateIdent('b');
  1373. M.B:=CreateLiteral(10);
  1374. A.Expr:=M;
  1375. L.A:=A;
  1376. L.B:=R;
  1377. FD.AFunction.Body.A:=L;
  1378. AssertWrite('Function, 2 statements, compact','function a(b) {b=(b*10); return b}',FD);
  1379. end;
  1380. Procedure TTestStatementWriter.TestTryCatch;
  1381. Var
  1382. T : TJSTryCatchStatement;
  1383. A : TJSAssignStatement;
  1384. M : TJSMultiplicativeExpressionMul;
  1385. begin
  1386. Writer.IndentSize:=2;
  1387. T:=TJSTryCatchStatement.Create(0,0);
  1388. T.Ident:='e';
  1389. A:=TJSSimpleAssignStatement.Create(0,0);
  1390. A.LHS:=CreateIdent('b');
  1391. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1392. M.A:=CreateIdent('b');
  1393. M.B:=CreateLiteral(10);
  1394. A.Expr:=M;
  1395. T.Block:=A;
  1396. A:=TJSSimpleAssignStatement.Create(0,0);
  1397. A.LHS:=CreateIdent('b');
  1398. A.Expr:=CreateLiteral(1);
  1399. T.BCatch:=A;
  1400. AssertWrite('Try catch','try {'+sLineBreak+' b = (b * 10)'+sLineBreak+'}'+sLineBreak+'catch (e) {'+sLineBreak+' b = 1'+sLineBreak+'}'+sLineBreak,T);
  1401. end;
  1402. Procedure TTestStatementWriter.TestTryCatchCompact;
  1403. Var
  1404. T : TJSTryCatchStatement;
  1405. A : TJSAssignStatement;
  1406. M : TJSMultiplicativeExpressionMul;
  1407. begin
  1408. Writer.IndentSize:=2;
  1409. Writer.Options:=[woCompact,woUseUTF8];
  1410. T:=TJSTryCatchStatement.Create(0,0);
  1411. T.Ident:='e';
  1412. A:=TJSSimpleAssignStatement.Create(0,0);
  1413. A.LHS:=CreateIdent('b');
  1414. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1415. M.A:=CreateIdent('b');
  1416. M.B:=CreateLiteral(10);
  1417. A.Expr:=M;
  1418. T.Block:=A;
  1419. A:=TJSSimpleAssignStatement.Create(0,0);
  1420. A.LHS:=CreateIdent('b');
  1421. A.Expr:=CreateLiteral(1);
  1422. T.BCatch:=A;
  1423. AssertWrite('Try catch compact','try {b=(b*10)} catch (e) {b=1}',T);
  1424. end;
  1425. Procedure TTestStatementWriter.TestTryFinally;
  1426. Var
  1427. T : TJSTryFinallyStatement;
  1428. A : TJSAssignStatement;
  1429. M : TJSMultiplicativeExpressionMul;
  1430. begin
  1431. Writer.IndentSize:=2;
  1432. T:=TJSTryFinallyStatement.Create(0,0);
  1433. T.Ident:='e';
  1434. A:=TJSSimpleAssignStatement.Create(0,0);
  1435. A.LHS:=CreateIdent('b');
  1436. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1437. M.A:=CreateIdent('b');
  1438. M.B:=CreateLiteral(10);
  1439. A.Expr:=M;
  1440. T.Block:=A;
  1441. A:=TJSSimpleAssignStatement.Create(0,0);
  1442. A.LHS:=CreateIdent('b');
  1443. A.Expr:=CreateLiteral(1);
  1444. T.BFinally:=A;
  1445. AssertWrite('Try finally ','try {'+sLineBreak+' b = (b * 10)'+sLineBreak+'}'+sLineBreak+'finally {'+sLineBreak+' b = 1'+sLineBreak+'}'+sLineBreak,T);
  1446. end;
  1447. Procedure TTestStatementWriter.TestTryFinallyCompact;
  1448. Var
  1449. T : TJSTryFinallyStatement;
  1450. A : TJSAssignStatement;
  1451. M : TJSMultiplicativeExpressionMul;
  1452. begin
  1453. Writer.IndentSize:=2;
  1454. Writer.Options:=[woCompact,woUseUTF8];
  1455. T:=TJSTryFinallyStatement.Create(0,0);
  1456. T.Ident:='e';
  1457. A:=TJSSimpleAssignStatement.Create(0,0);
  1458. A.LHS:=CreateIdent('b');
  1459. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1460. M.A:=CreateIdent('b');
  1461. M.B:=CreateLiteral(10);
  1462. A.Expr:=M;
  1463. T.Block:=A;
  1464. A:=TJSSimpleAssignStatement.Create(0,0);
  1465. A.LHS:=CreateIdent('b');
  1466. A.Expr:=CreateLiteral(1);
  1467. T.BFinally:=A;
  1468. AssertWrite('Try finally compact','try {b=(b*10)} finally {b=1}',T);
  1469. end;
  1470. Procedure TTestStatementWriter.TestTryCatchFinally;
  1471. Var
  1472. T : TJSTryCatchFinallyStatement;
  1473. A : TJSAssignStatement;
  1474. M : TJSMultiplicativeExpressionMul;
  1475. begin
  1476. Writer.IndentSize:=2;
  1477. T:=TJSTryCatchFinallyStatement.Create(0,0);
  1478. T.Ident:='e';
  1479. A:=TJSSimpleAssignStatement.Create(0,0);
  1480. A.LHS:=CreateIdent('b');
  1481. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1482. M.A:=CreateIdent('b');
  1483. M.B:=CreateLiteral(10);
  1484. A.Expr:=M;
  1485. T.Block:=A;
  1486. A:=TJSSimpleAssignStatement.Create(0,0);
  1487. A.LHS:=CreateIdent('b');
  1488. A.Expr:=CreateLiteral(10);
  1489. T.BCatch:=A;
  1490. A:=TJSSimpleAssignStatement.Create(0,0);
  1491. A.LHS:=CreateIdent('b');
  1492. A.Expr:=CreateLiteral(1);
  1493. T.BFinally:=A;
  1494. AssertWrite('Try finally ','try {'+sLineBreak+' b = (b * 10)'+sLineBreak+'}'+sLineBreak+'catch (e) {'+sLineBreak+' b = 10'+sLineBreak+'}'+sLineBreak+'finally {'+sLineBreak+' b = 1'+sLineBreak+'}'+sLineBreak,T);
  1495. end;
  1496. Procedure TTestStatementWriter.TestTryCatchFinallyCompact;
  1497. Var
  1498. T : TJSTryCatchFinallyStatement;
  1499. A : TJSAssignStatement;
  1500. M : TJSMultiplicativeExpressionMul;
  1501. begin
  1502. Writer.IndentSize:=2;
  1503. Writer.Options:=[woCompact,woUseUTF8];
  1504. T:=TJSTryCatchFinallyStatement.Create(0,0);
  1505. T.Ident:='e';
  1506. A:=TJSSimpleAssignStatement.Create(0,0);
  1507. A.LHS:=CreateIdent('b');
  1508. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1509. M.A:=CreateIdent('b');
  1510. M.B:=CreateLiteral(10);
  1511. A.Expr:=M;
  1512. T.Block:=A;
  1513. A:=TJSSimpleAssignStatement.Create(0,0);
  1514. A.LHS:=CreateIdent('b');
  1515. A.Expr:=CreateLiteral(10);
  1516. T.BCatch:=A;
  1517. A:=TJSSimpleAssignStatement.Create(0,0);
  1518. A.LHS:=CreateIdent('b');
  1519. A.Expr:=CreateLiteral(1);
  1520. T.BFinally:=A;
  1521. AssertWrite('Try finally ','try {b=(b*10)} catch (e) {b=10} finally {b=1}',T);
  1522. end;
  1523. Procedure TTestStatementWriter.TestWith;
  1524. Var
  1525. T : TJSWithStatement;
  1526. A : TJSAssignStatement;
  1527. M : TJSMultiplicativeExpressionMul;
  1528. begin
  1529. Writer.IndentSize:=2;
  1530. // Writer.Options:=[woCompact,woUseUTF8];
  1531. T:=TJSWithStatement.Create(0,0);
  1532. T.A:=CreateIdent('e');
  1533. A:=TJSSimpleAssignStatement.Create(0,0);
  1534. A.LHS:=CreateIdent('b');
  1535. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1536. M.A:=CreateIdent('b');
  1537. M.B:=CreateLiteral(10);
  1538. A.Expr:=M;
  1539. T.B:=A;
  1540. AssertWrite('With statement ','with (e)'+slineBreak+' b = (b * 10)',T);
  1541. end;
  1542. Procedure TTestStatementWriter.TestWithCompact;
  1543. Var
  1544. T : TJSWithStatement;
  1545. A : TJSAssignStatement;
  1546. M : TJSMultiplicativeExpressionMul;
  1547. begin
  1548. Writer.IndentSize:=2;
  1549. Writer.Options:=[woCompact,woUseUTF8];
  1550. T:=TJSWithStatement.Create(0,0);
  1551. T.A:=CreateIdent('e');
  1552. A:=TJSSimpleAssignStatement.Create(0,0);
  1553. A.LHS:=CreateIdent('b');
  1554. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1555. M.A:=CreateIdent('b');
  1556. M.B:=CreateLiteral(10);
  1557. A.Expr:=M;
  1558. T.B:=A;
  1559. AssertWrite('With statement ','with (e) b=(b*10)',T);
  1560. end;
  1561. Procedure TTestStatementWriter.TestSourceElements;
  1562. Var
  1563. T : TJSSourceElements;
  1564. A : TJSAssignStatement;
  1565. M : TJSMultiplicativeExpressionMul;
  1566. begin
  1567. Writer.IndentSize:=2;
  1568. // Writer.Options:=[woCompact,woUseUTF8];
  1569. T:=TJSSourceElements.Create(0,0);
  1570. A:=TJSSimpleAssignStatement.Create(0,0);
  1571. A.LHS:=CreateIdent('b');
  1572. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1573. M.A:=CreateIdent('b');
  1574. M.B:=CreateLiteral(10);
  1575. A.Expr:=M;
  1576. T.Statements.AddNode.Node:=A;
  1577. A:=TJSSimpleAssignStatement.Create(0,0);
  1578. A.LHS:=CreateIdent('c');
  1579. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1580. M.A:=CreateIdent('c');
  1581. M.B:=CreateLiteral(2);
  1582. A.Expr:=M;
  1583. T.Statements.AddNode.Node:=A;
  1584. AssertWrite('Statement lists ','b = (b * 10);'+sLineBreak+'c = (c * 2);'+sLineBreak,T);
  1585. end;
  1586. Procedure TTestStatementWriter.TestSourceElementsCompact;
  1587. Var
  1588. T : TJSSourceElements;
  1589. A : TJSAssignStatement;
  1590. M : TJSMultiplicativeExpressionMul;
  1591. begin
  1592. Writer.IndentSize:=2;
  1593. Writer.Options:=[woCompact,woUseUTF8];
  1594. T:=TJSSourceElements.Create(0,0);
  1595. A:=TJSSimpleAssignStatement.Create(0,0);
  1596. A.LHS:=CreateIdent('b');
  1597. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1598. M.A:=CreateIdent('b');
  1599. M.B:=CreateLiteral(10);
  1600. A.Expr:=M;
  1601. T.Statements.AddNode.Node:=A;
  1602. A:=TJSSimpleAssignStatement.Create(0,0);
  1603. A.LHS:=CreateIdent('c');
  1604. M:=TJSMultiplicativeExpressionMul.Create(0,0);
  1605. M.A:=CreateIdent('c');
  1606. M.B:=CreateLiteral(2);
  1607. A.Expr:=M;
  1608. T.Statements.AddNode.Node:=A;
  1609. AssertWrite('Statement lists compact','b=(b*10); c=(c*2);',T);
  1610. end;
  1611. { ---------------------------------------------------------------------
  1612. TTestLiteralWriter
  1613. ---------------------------------------------------------------------}
  1614. Procedure TTestLiteralWriter.TestInteger;
  1615. begin
  1616. AssertWrite('1','1',CreateLiteral(1));
  1617. end;
  1618. Procedure TTestLiteralWriter.TestBooleanTrue;
  1619. Var
  1620. L : TJSLiteral;
  1621. begin
  1622. L:=TJSLiteral.Create(0,0,'');
  1623. L.Value.Asboolean:=True;
  1624. AssertWrite('true','true',L);
  1625. end;
  1626. Procedure TTestLiteralWriter.TestBooleanFalse;
  1627. Var
  1628. L : TJSLiteral;
  1629. begin
  1630. L:=TJSLiteral.Create(0,0,'');
  1631. L.Value.Asboolean:=False;
  1632. AssertWrite('false','false',L);
  1633. end;
  1634. Procedure TTestLiteralWriter.TestUndefined;
  1635. Var
  1636. L : TJSLiteral;
  1637. begin
  1638. L:=TJSLiteral.Create(0,0,'');
  1639. AssertWrite('undefined','undefined',L);
  1640. end;
  1641. Procedure TTestLiteralWriter.TestNull;
  1642. Var
  1643. L : TJSLiteral;
  1644. begin
  1645. L:=TJSLiteral.Create(0,0,'');
  1646. L.Value.IsNull:=True;
  1647. AssertWrite('null','null',L);
  1648. end;
  1649. Procedure TTestLiteralWriter.TestString;
  1650. Var
  1651. L : TJSLiteral;
  1652. begin
  1653. L:=TJSLiteral.Create(0,0,'');
  1654. L.Value.AsString:='abcd';
  1655. AssertWrite('abcd','"abcd"',L);
  1656. end;
  1657. Procedure TTestLiteralWriter.TestStringQuote;
  1658. Var
  1659. L : TJSLiteral;
  1660. begin
  1661. L:=TJSLiteral.Create(0,0,'');
  1662. L.Value.AsString:='ab"cd';
  1663. AssertWrite('ab"cd','"ab\"cd"',L);
  1664. end;
  1665. Procedure TTestLiteralWriter.TestStringBackslash;
  1666. Var
  1667. L : TJSLiteral;
  1668. begin
  1669. L:=TJSLiteral.Create(0,0,'');
  1670. L.Value.AsString:='ab\cd';
  1671. AssertWrite('ab\cd','"ab\\cd"',L);
  1672. end;
  1673. Procedure TTestLiteralWriter.TestStringslash;
  1674. Var
  1675. L : TJSLiteral;
  1676. begin
  1677. L:=TJSLiteral.Create(0,0,'');
  1678. L.Value.AsString:='ab/cd';
  1679. AssertWrite('ab/cd','"ab\/cd"',L);
  1680. end;
  1681. Procedure TTestLiteralWriter.TestStringsBack;
  1682. Var
  1683. L : TJSLiteral;
  1684. begin
  1685. L:=TJSLiteral.Create(0,0,'');
  1686. L.Value.AsString:='ab'#8'cd';
  1687. AssertWrite('ab'#8'cd','"ab\bcd"',L);
  1688. end;
  1689. Procedure TTestLiteralWriter.TestStringsTab;
  1690. Var
  1691. L : TJSLiteral;
  1692. begin
  1693. L:=TJSLiteral.Create(0,0,'');
  1694. L.Value.AsString:='ab'#9'cd';
  1695. AssertWrite('ab'#9'cd','"ab\tcd"',L);
  1696. end;
  1697. Procedure TTestLiteralWriter.TestStringsLineFeed;
  1698. Var
  1699. L : TJSLiteral;
  1700. begin
  1701. L:=TJSLiteral.Create(0,0,'');
  1702. L.Value.AsString:='ab'#10'cd';
  1703. AssertWrite('ab'#10'cd','"ab\ncd"',L);
  1704. end;
  1705. Procedure TTestLiteralWriter.TestStringsFormFeed;
  1706. Var
  1707. L : TJSLiteral;
  1708. begin
  1709. L:=TJSLiteral.Create(0,0,'');
  1710. L.Value.AsString:='ab'#12'cd';
  1711. AssertWrite('ab'#12'cd','"ab\fcd"',L);
  1712. end;
  1713. Procedure TTestLiteralWriter.TestStringsCarriageReturn;
  1714. Var
  1715. L : TJSLiteral;
  1716. begin
  1717. L:=TJSLiteral.Create(0,0,'');
  1718. L.Value.AsString:='ab'#13'cd';
  1719. AssertWrite('ab'#13'cd','"ab\rcd"',L);
  1720. end;
  1721. Procedure TTestLiteralWriter.TestArrayEmpty;
  1722. Var
  1723. L : TJSArrayLiteral;
  1724. begin
  1725. L:=TJSArrayLiteral.Create(0,0);
  1726. AssertWrite('Empty array ','[]',L); // Always
  1727. end;
  1728. Procedure TTestLiteralWriter.TestArrayEmptyCompact;
  1729. Var
  1730. L : TJSArrayLiteral;
  1731. begin
  1732. Writer.Options:=[woCompact,woUseUTF8];
  1733. L:=TJSArrayLiteral.Create(0,0);
  1734. AssertWrite('Empty array ','[]',L);
  1735. end;
  1736. Procedure TTestLiteralWriter.TestArrayOneElement;
  1737. Var
  1738. L : TJSArrayLiteral;
  1739. I : TJSLiteral;
  1740. begin
  1741. L:=TJSArrayLiteral.Create(0,0);
  1742. I:=TJSLiteral.Create(0,0);
  1743. I.Value.AsNumber:=1;
  1744. L.Elements.AddElement.Expr:=I;
  1745. AssertWrite('Empty array ','['+sLineBreak+'1'+sLineBreak+']',L);
  1746. end;
  1747. Procedure TTestLiteralWriter.TestArrayOneElementCompact;
  1748. Var
  1749. L : TJSArrayLiteral;
  1750. I : TJSLiteral;
  1751. begin
  1752. Writer.Options:=[woCompact,woUseUTF8];
  1753. L:=TJSArrayLiteral.Create(0,0);
  1754. I:=TJSLiteral.Create(0,0);
  1755. I.Value.AsNumber:=1;
  1756. L.Elements.AddElement.Expr:=I;
  1757. AssertWrite('Empty array ','[1]',L);
  1758. end;
  1759. Procedure TTestLiteralWriter.TestArrayOneElementIndent;
  1760. Var
  1761. L : TJSArrayLiteral;
  1762. I : TJSLiteral;
  1763. begin
  1764. Writer.IndentSize:=2;
  1765. L:=TJSArrayLiteral.Create(0,0);
  1766. I:=TJSLiteral.Create(0,0);
  1767. I.Value.AsNumber:=1;
  1768. L.Elements.AddElement.Expr:=I;
  1769. AssertWrite('Empty array ','['+sLineBreak+' 1'+sLineBreak+']',L);
  1770. end;
  1771. Procedure TTestLiteralWriter.TestArrayTwoElements;
  1772. Var
  1773. L : TJSArrayLiteral;
  1774. I : TJSLiteral;
  1775. begin
  1776. L:=TJSArrayLiteral.Create(0,0);
  1777. I:=TJSLiteral.Create(0,0);
  1778. I.Value.AsNumber:=1;
  1779. L.Elements.AddElement.Expr:=I;
  1780. I:=TJSLiteral.Create(0,0);
  1781. I.Value.AsNumber:=2;
  1782. L.Elements.AddElement.Expr:=I;
  1783. AssertWrite('Empty array ','['+sLineBreak+'1,'+sLineBreak+'2'+sLineBreak+']',L);
  1784. end;
  1785. Procedure TTestLiteralWriter.TestArrayTwoElementsCompact;
  1786. Var
  1787. L : TJSArrayLiteral;
  1788. I : TJSLiteral;
  1789. begin
  1790. Writer.Options:=[woCompact,woUseUTF8];
  1791. L:=TJSArrayLiteral.Create(0,0);
  1792. I:=TJSLiteral.Create(0,0);
  1793. I.Value.AsNumber:=1;
  1794. L.Elements.AddElement.Expr:=I;
  1795. I:=TJSLiteral.Create(0,0);
  1796. I.Value.AsNumber:=2;
  1797. L.Elements.AddElement.Expr:=I;
  1798. AssertWrite('Empty array ','[1, 2]',L);
  1799. end;
  1800. Procedure TTestLiteralWriter.TestArrayTwoElementsCompact2;
  1801. Var
  1802. L : TJSArrayLiteral;
  1803. I : TJSLiteral;
  1804. begin
  1805. Writer.Options:=[woCompactArrayLiterals,woUseUTF8];
  1806. L:=TJSArrayLiteral.Create(0,0);
  1807. I:=TJSLiteral.Create(0,0);
  1808. I.Value.AsNumber:=1;
  1809. L.Elements.AddElement.Expr:=I;
  1810. I:=TJSLiteral.Create(0,0);
  1811. I.Value.AsNumber:=2;
  1812. L.Elements.AddElement.Expr:=I;
  1813. AssertWrite('Empty array ','[1, 2]',L);
  1814. end;
  1815. Procedure TTestLiteralWriter.TestArrayThreeElementsCompact;
  1816. Var
  1817. L : TJSArrayLiteral;
  1818. I : TJSLiteral;
  1819. begin
  1820. Writer.Options:=[woCompact,woUseUTF8];
  1821. L:=TJSArrayLiteral.Create(0,0);
  1822. I:=TJSLiteral.Create(0,0);
  1823. I.Value.AsNumber:=1;
  1824. L.Elements.AddElement.Expr:=I;
  1825. I:=TJSLiteral.Create(0,0);
  1826. I.Value.AsNumber:=2;
  1827. L.Elements.AddElement.Expr:=I;
  1828. I:=TJSLiteral.Create(0,0);
  1829. I.Value.AsNumber:=3;
  1830. L.Elements.AddElement.Expr:=I;
  1831. AssertWrite('Empty array ','[1, 2, 3]',L);
  1832. end;
  1833. Procedure TTestLiteralWriter.TestObjectEmpty;
  1834. Var
  1835. L : TJSObjectLiteral;
  1836. begin
  1837. L:=TJSObjectLiteral.Create(0,0);
  1838. AssertWrite('Empty object ','{}',L); // Always
  1839. end;
  1840. Procedure TTestLiteralWriter.TestObjectEmptyCompact;
  1841. Var
  1842. L : TJSObjectLiteral;
  1843. begin
  1844. Writer.Options:=[woCompact,woUseUTF8];
  1845. L:=TJSObjectLiteral.Create(0,0);
  1846. AssertWrite('Empty object ','{}',L); // Always
  1847. end;
  1848. Procedure TTestLiteralWriter.TestObjectOneElement;
  1849. Var
  1850. L : TJSObjectLiteral;
  1851. E : TJSObjectLiteralElement;
  1852. I : TJSLiteral;
  1853. begin
  1854. L:=TJSObjectLiteral.Create(0,0);
  1855. E:=L.Elements.AddElement;
  1856. I:=TJSLiteral.Create(0,0);
  1857. I.Value.AsNumber:=1;
  1858. E.Expr:=I;
  1859. E.Name:='abc';
  1860. AssertWrite('Empty object ','{'+slineBreak+'abc: 1'+sLineBreak+'}',L);
  1861. end;
  1862. Procedure TTestLiteralWriter.TestObjectOneElementCompact;
  1863. Var
  1864. L : TJSObjectLiteral;
  1865. E : TJSObjectLiteralElement;
  1866. I : TJSLiteral;
  1867. begin
  1868. L:=TJSObjectLiteral.Create(0,0);
  1869. E:=L.Elements.AddElement;
  1870. I:=TJSLiteral.Create(0,0);
  1871. I.Value.AsNumber:=1;
  1872. E.Expr:=I;
  1873. E.Name:='abc';
  1874. Writer.Options:=[woCompact,woUseUTF8];
  1875. AssertWrite('Empty object ','{abc: 1}',L);
  1876. end;
  1877. Procedure TTestLiteralWriter.TestObjectOneElementIndent;
  1878. Var
  1879. L : TJSObjectLiteral;
  1880. E : TJSObjectLiteralElement;
  1881. I : TJSLiteral;
  1882. begin
  1883. L:=TJSObjectLiteral.Create(0,0);
  1884. E:=L.Elements.AddElement;
  1885. I:=TJSLiteral.Create(0,0);
  1886. I.Value.AsNumber:=1;
  1887. E.Expr:=I;
  1888. E.Name:='abc';
  1889. Writer.IndentSize:=2;
  1890. AssertWrite('Empty object ','{'+slineBreak+' abc: 1'+sLineBreak+'}',L);
  1891. end;
  1892. Procedure TTestLiteralWriter.TestObjectOneElementCompactQuoted;
  1893. Var
  1894. L : TJSObjectLiteral;
  1895. E : TJSObjectLiteralElement;
  1896. I : TJSLiteral;
  1897. begin
  1898. L:=TJSObjectLiteral.Create(0,0);
  1899. E:=L.Elements.AddElement;
  1900. I:=TJSLiteral.Create(0,0);
  1901. I.Value.AsNumber:=1;
  1902. E.Expr:=I;
  1903. E.Name:='abc';
  1904. Writer.Options:=[woCompact,woUseUTF8,woQuoteElementNames];
  1905. AssertWrite('Empty object ','{"abc": 1}',L);
  1906. end;
  1907. Procedure TTestLiteralWriter.TestObjectTwoElements;
  1908. Var
  1909. L : TJSObjectLiteral;
  1910. E : TJSObjectLiteralElement;
  1911. I : TJSLiteral;
  1912. begin
  1913. L:=TJSObjectLiteral.Create(0,0);
  1914. E:=L.Elements.AddElement;
  1915. I:=TJSLiteral.Create(0,0);
  1916. I.Value.AsNumber:=1;
  1917. E.Expr:=I;
  1918. E.Name:='abc';
  1919. E:=L.Elements.AddElement;
  1920. I:=TJSLiteral.Create(0,0);
  1921. I.Value.AsNumber:=2;
  1922. E.Expr:=I;
  1923. E.Name:='efg';
  1924. AssertWrite('Empty object ','{'+slineBreak+'abc: 1,'+sLineBreak+'efg: 2'+slineBreak+'}',L);
  1925. end;
  1926. Procedure TTestLiteralWriter.TestObjectTwoElementCompact;
  1927. Var
  1928. L : TJSObjectLiteral;
  1929. E : TJSObjectLiteralElement;
  1930. I : TJSLiteral;
  1931. begin
  1932. L:=TJSObjectLiteral.Create(0,0);
  1933. E:=L.Elements.AddElement;
  1934. I:=TJSLiteral.Create(0,0);
  1935. I.Value.AsNumber:=1;
  1936. E.Expr:=I;
  1937. E.Name:='abc';
  1938. E:=L.Elements.AddElement;
  1939. I:=TJSLiteral.Create(0,0);
  1940. I.Value.AsNumber:=2;
  1941. E.Expr:=I;
  1942. E.Name:='efg';
  1943. Writer.Options:=[woCompact,woUseUTF8];
  1944. AssertWrite('Empty object ','{abc: 1, efg: 2}',L);
  1945. end;
  1946. Procedure TTestLiteralWriter.TestObjectTwoElementCompact2;
  1947. Var
  1948. L : TJSObjectLiteral;
  1949. E : TJSObjectLiteralElement;
  1950. I : TJSLiteral;
  1951. begin
  1952. L:=TJSObjectLiteral.Create(0,0);
  1953. E:=L.Elements.AddElement;
  1954. I:=TJSLiteral.Create(0,0);
  1955. I.Value.AsNumber:=1;
  1956. E.Expr:=I;
  1957. E.Name:='abc';
  1958. E:=L.Elements.AddElement;
  1959. I:=TJSLiteral.Create(0,0);
  1960. I.Value.AsNumber:=2;
  1961. E.Expr:=I;
  1962. E.Name:='efg';
  1963. Writer.Options:=[woCompactObjectLiterals,woUseUTF8];
  1964. AssertWrite('Empty object ','{abc: 1, efg: 2}',L);
  1965. end;
  1966. Procedure TTestLiteralWriter.TestObjectTwoElementCompactQuoted;
  1967. Var
  1968. L : TJSObjectLiteral;
  1969. E : TJSObjectLiteralElement;
  1970. I : TJSLiteral;
  1971. begin
  1972. L:=TJSObjectLiteral.Create(0,0);
  1973. E:=L.Elements.AddElement;
  1974. I:=TJSLiteral.Create(0,0);
  1975. I.Value.AsNumber:=1;
  1976. E.Expr:=I;
  1977. E.Name:='abc';
  1978. E:=L.Elements.AddElement;
  1979. I:=TJSLiteral.Create(0,0);
  1980. I.Value.AsNumber:=2;
  1981. E.Expr:=I;
  1982. E.Name:='efg';
  1983. Writer.Options:=[woCompact,woUseUTF8,woQuoteElementNames];
  1984. AssertWrite('Empty object ','{"abc": 1, "efg": 2}',L);
  1985. end;
  1986. Procedure TTestLiteralWriter.TestObjectThreeElementsCompact;
  1987. Var
  1988. L : TJSObjectLiteral;
  1989. E : TJSObjectLiteralElement;
  1990. I : TJSLiteral;
  1991. begin
  1992. L:=TJSObjectLiteral.Create(0,0);
  1993. E:=L.Elements.AddElement;
  1994. I:=TJSLiteral.Create(0,0);
  1995. I.Value.AsNumber:=1;
  1996. E.Expr:=I;
  1997. E.Name:='abc';
  1998. E:=L.Elements.AddElement;
  1999. I:=TJSLiteral.Create(0,0);
  2000. I.Value.AsNumber:=2;
  2001. E.Expr:=I;
  2002. E.Name:='efg';
  2003. E:=L.Elements.AddElement;
  2004. I:=TJSLiteral.Create(0,0);
  2005. I.Value.AsNumber:=3;
  2006. E.Expr:=I;
  2007. E.Name:='hij';
  2008. Writer.Options:=[woCompact,woUseUTF8];
  2009. AssertWrite('Empty object ','{abc: 1, efg: 2, hij: 3}',L);
  2010. end;
  2011. { ---------------------------------------------------------------------
  2012. TTestJSWriter
  2013. ---------------------------------------------------------------------}
  2014. procedure TTestJSWriter.SetUp;
  2015. begin
  2016. FTextWriter:=TBufferWriter.Create(120);
  2017. FWriter:=TJSWriter.Create(FTextWriter);
  2018. end;
  2019. procedure TTestJSWriter.TearDown;
  2020. begin
  2021. FreeAndNil(FWriter);
  2022. FreeAndNil(FTextWriter);
  2023. FreeAndNil(FElement);
  2024. end;
  2025. Procedure TTestJSWriter.WriteElement(JS: TJSElement);
  2026. begin
  2027. FElement:=JS;
  2028. FWriter.WriteJS(JS);
  2029. end;
  2030. Procedure TTestJSWriter.AssertResult(Const Msg, Result: String);
  2031. Var
  2032. S : AnsiString;
  2033. begin
  2034. S:=FTextWriter.AsAnsistring;
  2035. AssertEquals(Msg,Result,S);
  2036. end;
  2037. Procedure TTestJSWriter.AssertResult(Const Msg: string; Result: UnicodeString);
  2038. Var
  2039. S : UnicodeString;
  2040. begin
  2041. S:=FTextWriter.AsUnicodeString;
  2042. AssertEquals(Msg,Result,S);
  2043. end;
  2044. Procedure TTestJSWriter.AssertWrite(Const Msg, Result: String;
  2045. AElement: TJSElement);
  2046. begin
  2047. WriteElement(AElement);
  2048. AssertResult(Msg,Result);
  2049. end;
  2050. Procedure TTestJSWriter.AssertWrite(Const Msg: string; Result: UnicodeString;
  2051. AElement: TJSElement);
  2052. begin
  2053. WriteElement(AElement);
  2054. AssertResult(Msg,Result);
  2055. end;
  2056. Function TTestJSWriter.CreateIdent(Const AName: String): TJSPrimaryExpressionIdent;
  2057. begin
  2058. Result:=TJSPrimaryExpressionIdent.Create(0,0);
  2059. Result.Name:=AName;
  2060. end;
  2061. Function TTestJSWriter.CreateLiteral(Const AValue: TJSString): TJSLiteral;
  2062. begin
  2063. Result:=TJSLiteral.Create(0,0);
  2064. Result.Value.AsString:=Avalue;
  2065. end;
  2066. Function TTestJSWriter.CreateLiteral(Const AValue: Integer): TJSLiteral;
  2067. begin
  2068. Result:=TJSLiteral.Create(0,0);
  2069. Result.Value.AsNumber:=Avalue;
  2070. end;
  2071. Function TTestJSWriter.CreateLiteral(Const AValue: Boolean): TJSLiteral;
  2072. begin
  2073. Result:=TJSLiteral.Create(0,0);
  2074. Result.Value.AsBoolean:=Avalue;
  2075. end;
  2076. { ---------------------------------------------------------------------
  2077. TTestTestJSWriter
  2078. ---------------------------------------------------------------------}
  2079. procedure TTestTestJSWriter.TestEmpty;
  2080. begin
  2081. AssertNotNull('Have text writer',TextWriter);
  2082. AssertNotNull('Have JS writer',Writer);
  2083. AssertNull('Have no element',Element);
  2084. AssertSame('Correct text writer for js writer',TextWriter,Writer.Writer);
  2085. AssertEquals('No indent',0,Writer.IndentSize);
  2086. if not (Writer.Options=[woUseUTF8]) then
  2087. Fail('Options are not using UTF8');
  2088. end;
  2089. Initialization
  2090. RegisterTests([TTestTestJSWriter,TTestLiteralWriter,TTestExpressionWriter,TTestStatementWriter]);
  2091. end.