tcwriter.pp 76 KB

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