tcwriter.pp 71 KB

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