tcmustache.pas 19 KB

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