tcwriter.pp 73 KB

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