tcmustache.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 2021 by Michael Van Canneyt ([email protected])
  4. Test cases for basic mustache parser support
  5. See the File COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit tcmustache;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, fpcunit, testregistry, fpmustache, tcbasemustache;
  16. type
  17. { TTestMustacheParser }
  18. TTestMustacheParser= class(TBaseMustacheTest)
  19. private
  20. protected
  21. Function CreateParser : TMustacheParser; override;
  22. Public
  23. procedure SetUp; override;
  24. procedure TearDown; override;
  25. published
  26. procedure TestEmpty;
  27. procedure TestText;
  28. procedure TestVariable;
  29. procedure TestVariableErrNonClosed;
  30. procedure TestVariableAlternateStartStop;
  31. procedure TestDottedVariable;
  32. procedure TestVariableNoUnescape;
  33. procedure TestVariableNoUnescapeErrNonClosed;
  34. procedure TestVariableNoUnescapeAlternateStartStop;
  35. procedure TestComment;
  36. procedure TestCommentSurround;
  37. procedure TestCommentStandalone;
  38. procedure TestCommentStandaloneSpaced;
  39. procedure TestSetDelimiter;
  40. procedure TestSetDelimiterErrInvalid;
  41. procedure TestSection;
  42. procedure TestSectionNested;
  43. procedure TestSectionErrNotClosed;
  44. procedure TestSectionErrWrongClosed;
  45. procedure TestSectionErrNotStarted;
  46. procedure TestTextSection;
  47. procedure TestPartial;
  48. procedure TestParametricPartial;
  49. procedure TestParametricPartialWithBlock;
  50. procedure TestBlock;
  51. end;
  52. { TTestMustacheOutput }
  53. TTestMustacheOutput = class(TTestCase)
  54. Published
  55. Procedure TestStringOutput;
  56. end;
  57. { TTestMustacheElement }
  58. TTestMustacheElement = class(TTestCase)
  59. private
  60. FContext: TTestContext;
  61. FEl: TMustacheElement;
  62. Foutput: TMustacheStringOutput;
  63. procedure DoCallBack(const aName: TMustacheString; var aHandled: Boolean;
  64. var aValue: TMustacheString);
  65. Public
  66. Procedure SetUp; override;
  67. Procedure TearDown; override;
  68. Property Context : TTestContext Read FContext;
  69. Property Output : TMustacheStringOutput Read Foutput;
  70. Property El : TMustacheElement Read FEl;
  71. Published
  72. Procedure TestEmpty;
  73. Procedure TestTextElement;
  74. Procedure TestTextElementNoEscape;
  75. Procedure TestTextElementComment;
  76. Procedure TestTextElementPrefix;
  77. procedure TestTextElementPrefixNotLast;
  78. procedure TestTextElementPrefixLast;
  79. Procedure TestVariableElement;
  80. Procedure TestVariableElementNoEscape;
  81. Procedure TestVariableElementEscape;
  82. Procedure TestSectionEmpty;
  83. Procedure TestSectionValue;
  84. Procedure TestSectionValueFalse;
  85. Procedure TestSectionValueNull;
  86. Procedure TestSectionValueEmptyArray;
  87. Procedure TestSectionValueArray1El;
  88. Procedure TestSectionValueArray2El;
  89. Procedure TestSectionValueArray2ElValue;
  90. Procedure TestSectionValueArray1ElValueSuper;
  91. Procedure TestSectionValueArray2ElValueSuper;
  92. Procedure TestParentElement;
  93. Procedure TestParentElementRender;
  94. Procedure TestParentElementRenderPrefix;
  95. end;
  96. implementation
  97. uses Typinfo;
  98. Const
  99. SNeedsQuoting = '< > & "';
  100. SQuotedResult = '&lt; &gt; &amp; &quot;';
  101. { TTestMustacheElement }
  102. procedure TTestMustacheElement.DoCallBack(const aName: TMustacheString;
  103. var aHandled: Boolean; var aValue: TMustacheString);
  104. begin
  105. aValue:='';
  106. end;
  107. procedure TTestMustacheElement.SetUp;
  108. begin
  109. inherited SetUp;
  110. FOutput:=TMustacheStringOutput.Create;
  111. FContext:=TTestContext.Create(@DoCallBack);
  112. end;
  113. procedure TTestMustacheElement.TearDown;
  114. begin
  115. FreeAndNil(FContext);
  116. FreeAndNil(FOutput);
  117. FreeAndNil(FEl);
  118. inherited TearDown;
  119. end;
  120. procedure TTestMustacheElement.TestEmpty;
  121. begin
  122. AssertNotNull('Have output',Output);
  123. end;
  124. procedure TTestMustacheElement.TestTextElement;
  125. begin
  126. Fel:=TMustacheTextElement.Create(metText,Nil,0);
  127. El.Render(Nil,Output);
  128. AssertEquals('No output','',Output.Data);
  129. El.Data:='me';
  130. El.Render(Nil,Output);
  131. AssertEquals('Correct output','me',Output.Data);
  132. end;
  133. procedure TTestMustacheElement.TestTextElementNoEscape;
  134. begin
  135. Fel:=TMustacheTextElement.Create(metText,Nil,0);
  136. El.Data:=SNeedsQuoting;
  137. El.Render(Nil,Output);
  138. AssertEquals('Correct output',SNeedsQuoting,Output.Data);
  139. end;
  140. procedure TTestMustacheElement.TestTextElementComment;
  141. begin
  142. Fel:=TMustacheTextElement.Create(metComment,Nil,0);
  143. El.Data:='Something';
  144. El.Render(Nil,Output);
  145. AssertEquals('Correct output','',Output.Data);
  146. end;
  147. procedure TTestMustacheElement.TestTextElementPrefix;
  148. begin
  149. Fel:=TMustacheTextElement.Create(metText,Nil,0);
  150. El.Data:='me'#10'you';
  151. El.Render(Nil,Output,Nil,' ');
  152. AssertEquals('Correct output 1','me'#10' you',Output.Data);
  153. end;
  154. procedure TTestMustacheElement.TestTextElementPrefixNotLast;
  155. begin
  156. Fel:=TMustacheTextElement.Create(metText,Nil,0);
  157. El.Data:='me'#10'you'#10;
  158. El.Render(Nil,Output,Nil,' ');
  159. AssertEquals('Correct output 2','me'#10' you'#10' ',Output.Data);
  160. end;
  161. procedure TTestMustacheElement.TestTextElementPrefixLast;
  162. begin
  163. Fel:=TMustacheTextElement.Create(metText,Nil,0);
  164. El.Data:='me'#10'you'#10;
  165. El.Render(Nil,Output,Nil,' ',True);
  166. AssertEquals('Correct output 2','me'#10' you'#10,Output.Data);
  167. end;
  168. procedure TTestMustacheElement.TestVariableElement;
  169. begin
  170. Fel:=TMustacheVariableElement.Create(metText,Nil,0);
  171. Context.Values.Values['name']:='abc';
  172. El.Data:='name';
  173. El.Render(Context,Output);
  174. AssertEquals('Correct output','abc',Output.Data);
  175. end;
  176. procedure TTestMustacheElement.TestVariableElementNoEscape;
  177. begin
  178. Fel:=TMustacheVariableElement.Create(metText,Nil,0);
  179. Context.Values.Values['name']:=SNeedsQuoting;
  180. El.Data:='{name}';
  181. El.Render(Context,Output);
  182. AssertEquals('Correct output',SNeedsQuoting,Output.Data);
  183. end;
  184. procedure TTestMustacheElement.TestVariableElementEscape;
  185. begin
  186. Fel:=TMustacheVariableElement.Create(metText,Nil,0);
  187. Context.Values.Values['name']:=SNeedsQuoting;
  188. El.Data:='name';
  189. El.Render(Context,Output);
  190. AssertEquals('Correct output',SQuotedResult,Output.Data);
  191. end;
  192. procedure TTestMustacheElement.TestSectionEmpty;
  193. Var
  194. T : TMustacheTextElement;
  195. begin
  196. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  197. Fel.Data:='s';
  198. T:=TMustacheTextElement.Create(metText,Nil,0);
  199. Fel.AddChild(T);
  200. T.Data:='a';
  201. Fel.Render(Context,Output);
  202. AssertEquals('No output','',Output.Data);
  203. end;
  204. procedure TTestMustacheElement.TestSectionValue;
  205. Var
  206. T : TMustacheTextElement;
  207. begin
  208. Context.SetValue('s','b');
  209. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  210. Fel.Data:='s';
  211. T:=TMustacheTextElement.Create(metText,Nil,0);
  212. Fel.AddChild(T);
  213. T.Data:='a';
  214. Fel.Render(Context,Output);
  215. AssertEquals('Single pass','a',Output.Data);
  216. end;
  217. procedure TTestMustacheElement.TestSectionValueFalse;
  218. Var
  219. T : TMustacheTextElement;
  220. begin
  221. Context.SetValue('s','<false>');
  222. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  223. Fel.Data:='s';
  224. T:=TMustacheTextElement.Create(metText,Nil,0);
  225. Fel.AddChild(T);
  226. T.Data:='a';
  227. Fel.Render(Context,Output);
  228. AssertEquals('no pass','',Output.Data);
  229. end;
  230. procedure TTestMustacheElement.TestSectionValueNull;
  231. Var
  232. T : TMustacheTextElement;
  233. begin
  234. Context.SetValue('s','<null>');
  235. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  236. Fel.Data:='s';
  237. T:=TMustacheTextElement.Create(metText,Nil,0);
  238. Fel.AddChild(T);
  239. T.Data:='a';
  240. Fel.Render(Context,Output);
  241. AssertEquals('no pass','',Output.Data);
  242. end;
  243. procedure TTestMustacheElement.TestSectionValueEmptyArray;
  244. Var
  245. T : TMustacheTextElement;
  246. begin
  247. Context.SetValue('s','[]');
  248. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  249. Fel.Data:='s';
  250. T:=TMustacheTextElement.Create(metText,Nil,0);
  251. Fel.AddChild(T);
  252. T.Data:='a';
  253. Fel.Render(Context,Output);
  254. AssertEquals('no pass','',Output.Data);
  255. end;
  256. procedure TTestMustacheElement.TestSectionValueArray1El;
  257. Var
  258. T : TMustacheTextElement;
  259. begin
  260. Context.SetValue('s','[]');
  261. Context.SetValue('s[0]','toto');
  262. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  263. Fel.Data:='s';
  264. T:=TMustacheTextElement.Create(metText,Nil,0);
  265. Fel.AddChild(T);
  266. T.Data:='a';
  267. Fel.Render(Context,Output);
  268. AssertEquals('Single pass','a',Output.Data);
  269. end;
  270. procedure TTestMustacheElement.TestSectionValueArray2El;
  271. Var
  272. T : TMustacheTextElement;
  273. begin
  274. Context.SetValue('s','[]');
  275. Context.SetValue('s[0]','toto');
  276. Context.SetValue('s[1]','tata');
  277. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  278. Fel.Data:='s';
  279. T:=TMustacheTextElement.Create(metText,Nil,0);
  280. Fel.AddChild(T);
  281. T.Data:='a';
  282. Fel.Render(Context,Output);
  283. AssertEquals('Double pass','aa',Output.Data);
  284. end;
  285. procedure TTestMustacheElement.TestSectionValueArray2ElValue;
  286. Var
  287. T : TMustacheElement;
  288. begin
  289. Context.SetValue('s','[]');
  290. Context.SetValue('s[0]','{}');
  291. Context.SetValue('s[0].a','1');
  292. Context.SetValue('s[1]','{}');
  293. Context.SetValue('s[1].a','2');
  294. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  295. Fel.Data:='s';
  296. T:=TMustacheVariableElement.Create(metVariable,Nil,0);
  297. Fel.AddChild(T);
  298. T.Data:='a';
  299. Fel.Render(Context,Output);
  300. AssertEquals('Double pass','12',Output.Data);
  301. end;
  302. procedure TTestMustacheElement.TestSectionValueArray1ElValueSuper;
  303. Var
  304. T : TMustacheElement;
  305. begin
  306. Context.SetValue('s','[]');
  307. Context.SetValue('s[0]','{}');
  308. Context.SetValue('s[0].b','1');
  309. Context.SetValue('a','2');
  310. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  311. Fel.Data:='s';
  312. T:=TMustacheVariableElement.Create(metVariable,Nil,0);
  313. Fel.AddChild(T);
  314. T.Data:='a';
  315. Fel.Render(Context,Output);
  316. AssertEquals('Single pass','2',Output.Data);
  317. end;
  318. procedure TTestMustacheElement.TestSectionValueArray2ElValueSuper;
  319. Var
  320. T : TMustacheElement;
  321. begin
  322. Context.SetValue('s','[]');
  323. Context.SetValue('s[0]','{}');
  324. Context.SetValue('s[0].b','1');
  325. Context.SetValue('s[1]','{}');
  326. Context.SetValue('s[1].b','2');
  327. Context.SetValue('a','.a.');
  328. Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
  329. Fel.Data:='s';
  330. T:=TMustacheVariableElement.Create(metVariable,Nil,0);
  331. Fel.AddChild(T);
  332. T.Data:='a';
  333. T:=TMustacheVariableElement.Create(metVariable,Nil,0);
  334. Fel.AddChild(T);
  335. T.Data:='b';
  336. Fel.Render(Context,Output);
  337. AssertEquals('Single pass','.a.1.a.2',Output.Data);
  338. end;
  339. procedure TTestMustacheElement.TestParentElement;
  340. Var
  341. SEl : TMustacheElement;
  342. begin
  343. Fel:=TMustacheParentElement.Create(metSection,Nil,0);
  344. Sel:=TMustacheTextElement.Create(metText,Fel,0);
  345. AssertSame('Parent stored',Fel,Sel.Parent);
  346. AssertEquals('Not added to parent',0,FEl.ChildCount);
  347. Fel.AddChild(Sel);
  348. AssertEquals('added to parent - count',1,FEl.ChildCount);
  349. AssertSame('added to parent - stored',Sel,FEl.Children[0]);
  350. end;
  351. procedure TTestMustacheElement.TestParentElementRender;
  352. Var
  353. SEl : TMustacheElement;
  354. begin
  355. Fel:=TMustacheParentElement.Create(metSection,Nil,0);
  356. Sel:=TMustacheTextElement.Create(metText,Fel,0);
  357. Sel.Data:='a';
  358. Fel.AddChild(Sel);
  359. Sel:=TMustacheTextElement.Create(metText,Fel,0);
  360. Sel.Data:='b';
  361. Fel.AddChild(Sel);
  362. Sel:=TMustacheTextElement.Create(metText,Fel,0);
  363. Sel.Data:='c';
  364. Fel.AddChild(Sel);
  365. Fel.Render(Context,Output);
  366. AssertEquals('Correct output','abc',Output.Data);
  367. end;
  368. procedure TTestMustacheElement.TestParentElementRenderPrefix;
  369. Var
  370. SEl : TMustacheElement;
  371. begin
  372. Fel:=TMustacheParentElement.Create(metSection,Nil,0);
  373. Sel:=TMustacheTextElement.Create(metText,Fel,0);
  374. Sel.Data:='a'#10'b';
  375. Fel.AddChild(Sel);
  376. Sel:=TMustacheTextElement.Create(metText,Fel,0);
  377. Sel.Data:='d'#10'e';
  378. Fel.AddChild(Sel);
  379. Sel:=TMustacheTextElement.Create(metText,Fel,0);
  380. Sel.Data:='f'#10;
  381. Fel.AddChild(Sel);
  382. Fel.Render(Context,Output,Nil,' ');
  383. AssertEquals('Correct output','a'#10' bd'#10' ef'#10,Output.Data);
  384. end;
  385. { TTestMustacheOutput }
  386. procedure TTestMustacheOutput.TestStringOutput;
  387. Var
  388. SO : TMustacheStringOutput;
  389. begin
  390. SO:=TMustacheStringOutput.Create;
  391. try
  392. AssertEquals('Empty start','',SO.Data);
  393. SO.Output('abc');
  394. AssertEquals('Output 1','abc',SO.Data);
  395. SO.Output('def');
  396. AssertEquals('Output 2','abcdef',SO.Data);
  397. finally
  398. SO.Free;
  399. end;
  400. end;
  401. function TTestMustacheParser.CreateParser: TMustacheParser;
  402. begin
  403. Result:=TMustacheParser.Create;
  404. end;
  405. procedure TTestMustacheParser.SetUp;
  406. begin
  407. inherited SetUp;
  408. end;
  409. procedure TTestMustacheParser.TearDown;
  410. begin
  411. inherited TearDown;
  412. end;
  413. procedure TTestMustacheParser.TestEmpty;
  414. begin
  415. AssertNotNull('Have parser',Parser);
  416. AssertNull('Have no result',ParseResult);
  417. AssertEquals('Have no template','',Template);
  418. end;
  419. procedure TTestMustacheParser.TestText;
  420. begin
  421. Template:='a simple text';
  422. CallParser;
  423. AssertResultCount(1);
  424. AssertElement(0,metText,'a simple text');
  425. end;
  426. procedure TTestMustacheParser.TestVariable;
  427. Var
  428. el : TMustacheVariableElement;
  429. begin
  430. Template:='{{a}}';
  431. CallParser;
  432. AssertResultCount(1);
  433. el:=AssertElement(0,metVariable,'a',TMustacheVariableElement) as TMustacheVariableElement;
  434. AssertFalse('unescape',El.NoUnescape);
  435. end;
  436. procedure TTestMustacheParser.TestVariableErrNonClosed;
  437. begin
  438. Template:='{{a';
  439. AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
  440. Template:='{{a}';
  441. AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
  442. end;
  443. procedure TTestMustacheParser.TestVariableAlternateStartStop;
  444. Var
  445. el : TMustacheVariableElement;
  446. begin
  447. Parser.StartTag:='<<';
  448. Parser.StopTag:='>>';
  449. Template:='<<a>>';
  450. CallParser;
  451. AssertResultCount(1);
  452. el:=AssertElement(0,metVariable,'a',TMustacheVariableElement) as TMustacheVariableElement;
  453. AssertFalse('unescape',El.NoUnescape);
  454. end;
  455. procedure TTestMustacheParser.TestDottedVariable;
  456. begin
  457. Template:='{{a.b}}';
  458. CallParser;
  459. AssertResultCount(1);
  460. AssertElement(0,metVariable,'a.b');
  461. end;
  462. procedure TTestMustacheParser.TestVariableNoUnescape;
  463. Var
  464. el : TMustacheVariableElement;
  465. begin
  466. Template:='{{{a}}}';
  467. CallParser;
  468. AssertResultCount(1);
  469. el:=AssertElement(0,metVariable,'a',TMustacheVariableElement) as TMustacheVariableElement;
  470. AssertTrue('unescape',El.NoUnescape);
  471. end;
  472. procedure TTestMustacheParser.TestVariableNoUnescapeErrNonClosed;
  473. begin
  474. Template:='{{{a';
  475. AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
  476. Template:='{{{a}';
  477. AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
  478. Template:='{{{a}}';
  479. AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
  480. end;
  481. procedure TTestMustacheParser.TestVariableNoUnescapeAlternateStartStop;
  482. Var
  483. el : TMustacheVariableElement;
  484. begin
  485. Parser.StartTag:='<<';
  486. Parser.StopTag:='>>';
  487. Template:='<<{a}>>';
  488. CallParser;
  489. AssertResultCount(1);
  490. el:=AssertElement(0,metVariable,'a',TMustacheVariableElement) as TMustacheVariableElement;
  491. AssertTrue('unescape',El.NoUnescape);
  492. end;
  493. procedure TTestMustacheParser.TestComment;
  494. begin
  495. Parser.StartTag:='<<';
  496. Parser.StopTag:='>>';
  497. Template:='<<! a comment>>';
  498. CallParser;
  499. AssertResultCount(1);
  500. AssertElement(0,metComment,' a comment',TMustacheTextElement);
  501. end;
  502. procedure TTestMustacheParser.TestCommentSurround;
  503. begin
  504. Template:='ab{{! a comment}}cd';
  505. CallParser;
  506. AssertResultCount(3);
  507. AssertElement(0,metText,'ab',TMustacheTextElement);
  508. AssertElement(1,metComment,' a comment',TMustacheTextElement);
  509. AssertElement(2,metText,'cd',TMustacheTextElement);
  510. end;
  511. procedure TTestMustacheParser.TestCommentStandalone;
  512. begin
  513. Template:='a'+sLineBreak+'{{! a comment}}'+sLineBreak+'b';
  514. CallParser;
  515. AssertResultCount(3);
  516. AssertElement(0,metText,'a'+sLineBreak,TMustacheTextElement);
  517. AssertElement(1,metComment,' a comment',TMustacheTextElement);
  518. AssertElement(2,metText,'b',TMustacheTextElement);
  519. end;
  520. procedure TTestMustacheParser.TestCommentStandaloneSpaced;
  521. begin
  522. Template:='a'+sLineBreak+' {{! a comment}} '+sLineBreak+'b';
  523. CallParser;
  524. AssertResultCount(3);
  525. AssertElement(0,metText,'a'+sLineBreak,TMustacheTextElement);
  526. AssertElement(1,metComment,' a comment',TMustacheTextElement);
  527. AssertElement(2,metText,'b',TMustacheTextElement);
  528. end;
  529. procedure TTestMustacheParser.TestSetDelimiter;
  530. begin
  531. Template:='{{=<< >>=}}<<! a comment>>';
  532. CallParser;
  533. AssertResultCount(1);
  534. AssertElement(0,metComment,' a comment',TMustacheTextElement);
  535. end;
  536. procedure TTestMustacheParser.TestSetDelimiterErrInvalid;
  537. begin
  538. Template:='{{=== ===}}';
  539. AssertException('Have error',EMustache,@CallParser,'Invalid set delimiter Stop value: == in "== =="');
  540. end;
  541. procedure TTestMustacheParser.TestSection;
  542. Var
  543. el : TMustacheSectionElement;
  544. begin
  545. Template:='{{#a}}{{/a}}';
  546. CallParser;
  547. AssertResultCount(1);
  548. el:=AssertElement(0,metSection,'a',TMustacheSectionElement) as TMustacheSectionElement;
  549. AssertEquals('No elements in section',0,el.ChildCount);
  550. end;
  551. procedure TTestMustacheParser.TestSectionNested;
  552. Var
  553. el : TMustacheSectionElement;
  554. begin
  555. Template:='{{#a}}{{#b}}{{/b}}{{/a}}';
  556. CallParser;
  557. AssertResultCount(1);
  558. el:=AssertElement(0,metSection,'a',TMustacheSectionElement) as TMustacheSectionElement;
  559. AssertEquals('elements in section',1,el.ChildCount);
  560. el:=AssertElement(el,0,metSection,'b',TMustacheSectionElement) as TMustacheSectionElement;
  561. AssertEquals('elements in section sub',0,el.ChildCount);
  562. end;
  563. procedure TTestMustacheParser.TestSectionErrNotClosed;
  564. begin
  565. Template:='{{#a}}';
  566. AssertException('Have error',EMustache,@CallParser,'Structural error: Section "a" on position 1 is not closed.');
  567. end;
  568. procedure TTestMustacheParser.TestSectionErrWrongClosed;
  569. begin
  570. Template:='{{#a}}{{#b}}{{/a}}{{/b}}';
  571. AssertException('Have error',EMustache,@CallParser,'Structural error: Section "b" on position 7 is closed by tag "a" on position 13.');
  572. end;
  573. procedure TTestMustacheParser.TestSectionErrNotStarted;
  574. begin
  575. Template:='{{/a}}';
  576. AssertException('Have error',EMustache,@CallParser,'Structural error: Section "a" on position 1 was never opened.');
  577. end;
  578. procedure TTestMustacheParser.TestTextSection;
  579. Var
  580. el : TMustacheSectionElement;
  581. begin
  582. Template:='{{#a}}bbb{{/a}}';
  583. CallParser;
  584. AssertResultCount(1);
  585. el:=AssertElement(0,metSection,'a',TMustacheSectionElement) as TMustacheSectionElement;
  586. AssertEquals('No elements in section',1,el.ChildCount);
  587. AssertElement(el,0,metText,'bbb');
  588. end;
  589. procedure TTestMustacheParser.TestPartial;
  590. Var
  591. el : TMustachePartialElement;
  592. begin
  593. AddPartial('part','bcd');
  594. Template:='a{{>part}}e';
  595. CallParser;
  596. AssertResultCount(3);
  597. AssertElement(0,metText,'a',TMustacheTextElement);
  598. el:=AssertElement(1,metPartial,'part',TMustachePartialElement) as TMustachePartialElement;
  599. AssertElement(2,metText,'e',TMustacheTextElement);
  600. AssertEquals('Correct partial','part',El.Partial.Data);
  601. AssertEquals('Correct partial',1,El.Partial.ChildCount);
  602. AssertElement(el.Partial,0,metText,'bcd',TMustacheTextElement);
  603. end;
  604. procedure TTestMustacheParser.TestParametricPartial;
  605. var
  606. El: TMustachePartialElement;
  607. begin
  608. AddPartial('part','bcd');
  609. Template:='a{{<part}}{{/part}}e';
  610. CallParser;
  611. AssertResultCount(3);
  612. AssertElement(0,metText,'a',TMustacheTextElement);
  613. El:=AssertElement(1,metParametricPartial,'part',TMustachePartialElement) as TMustachePartialElement;
  614. AssertElement(2,metText,'e',TMustacheTextElement);
  615. AssertEquals('Correct parametric partial','part',El.Partial.Data);
  616. AssertEquals('Correct parametric partial',1,El.Partial.ChildCount);
  617. AssertElement(El.Partial,0,metText,'bcd',TMustacheTextElement);
  618. end;
  619. procedure TTestMustacheParser.TestParametricPartialWithBlock;
  620. var
  621. El: TMustachePartialElement;
  622. ElBlock: TMustacheBlockElement;
  623. begin
  624. AddPartial('part','bcd');
  625. Template:='a{{<part}}{{$x}}y{{/x}}{{/part}}e';
  626. CallParser;
  627. AssertResultCount(3);
  628. AssertElement(0,metText,'a',TMustacheTextElement);
  629. El:=AssertElement(1,metParametricPartial,'part',TMustachePartialElement) as TMustachePartialElement;
  630. AssertElement(2,metText,'e',TMustacheTextElement);
  631. AssertEquals('Correct parametric partial','part',El.Partial.Data);
  632. AssertEquals('Correct parametric partial',1,El.Partial.ChildCount);
  633. AssertElement(El.Partial,0,metText,'bcd',TMustacheTextElement);
  634. AssertEquals('elements in parametric partial',1,El.ChildCount);
  635. ElBlock:=AssertElement(El,0,metBlock,'x',TMustacheBlockElement) as TMustacheBlockElement;
  636. AssertEquals('elements in block',1,ElBlock.ChildCount);
  637. AssertElement(ElBlock,0,metText,'y',TMustacheTextElement);
  638. end;
  639. procedure TTestMustacheParser.TestBlock;
  640. var
  641. El: TMustacheBlockElement;
  642. begin
  643. Template:='{{$a}}bbb{{/a}}';
  644. CallParser;
  645. AssertResultCount(1);
  646. El:=AssertElement(0,metBlock,'a',TMustacheBlockElement) as TMustacheBlockElement;
  647. AssertEquals('No elements in block',1,El.ChildCount);
  648. AssertElement(El,0,metText,'bbb');
  649. end;
  650. initialization
  651. RegisterTests([TTestMustacheParser,TTestMustacheOutput,TTestMustacheElement]);
  652. end.