tcwriter.pp 65 KB

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