utest.markdown.fpdocrender.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2025 by Michael Van Canneyt
  4. Markdown FPDoc renderer tests
  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 UTest.Markdown.FPDocRender;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, fpcunit, testregistry,
  16. MarkDown.Elements,
  17. MarkDown.FPDocRender;
  18. type
  19. { TTestFPDocRender }
  20. TTestFPDocRender = Class(TTestCase)
  21. private
  22. FFPDocRenderer : TMarkDownFPDocRenderer;
  23. FDocument: TMarkDownDocument;
  24. FParent : TMarkDownBlock;
  25. Public
  26. const
  27. cIndent = ' ';
  28. cNLIndent = sLineBreak+cIndent;
  29. cNLIndent2 = sLineBreak+cIndent+' ';
  30. cNLIndent4 = sLineBreak+cIndent+' ';
  31. Procedure SetUp; override;
  32. Procedure TearDown; override;
  33. Procedure StartDoc;
  34. function CreateTextBlock(aParent: TMarkdownBlock; const aText,aTextNode: string; aNodeStyle : TNodeStyles=[]): TMarkDownTextBlock;
  35. function CreateParagraphBlock(const aTextNode: string): TMarkdownBlock;
  36. function CreateQuotedBlock(const aTextNode: string): TMarkdownBlock;
  37. function CreateHeadingBlock(const aTextNode: string; aLevel : integer): TMarkdownBlock;
  38. function CreateListBlock(aOrdered : boolean; const aListItemText : string): TMarkDownListBlock;
  39. function CreateListItemBlock(aParent: TMarkDownContainerBlock; const aText: string): TMarkDownListItemBlock;
  40. function AppendTextNode(aBlock: TMarkDownTextBlock; const aText: string; aNodeStyle : TNodeStyles) : TMarkDownTextNode;
  41. procedure TestRender(const aFPDoc : string; aEnvelope : boolean = False);
  42. Property Renderer : TMarkDownFPDocRenderer Read FFPDocRenderer;
  43. Property Document : TMarkDownDocument Read FDocument;
  44. Published
  45. procedure TestHookup;
  46. procedure TestEmpty;
  47. procedure TestEmptyPackageName;
  48. procedure TestTextBlockEmpty;
  49. procedure TestTextBlockText;
  50. procedure TestTextBlockTextStrong;
  51. procedure TestTextBlockTextEmph;
  52. procedure TestTextBlockTextDelete;
  53. procedure TestTextBlockTextStrongEmph;
  54. procedure TestTextBlockTextStrongEmphSplit1;
  55. procedure TestTextBlockTextStrongEmphSplit2;
  56. procedure TestPragraphBlockEmpty;
  57. procedure TestPragraphBlockText;
  58. procedure TestQuotedBlockEmpty;
  59. procedure TestQuotedBlockText;
  60. procedure TestUnorderedListEmpty;
  61. procedure TestUnorderedListOneItem;
  62. end;
  63. implementation
  64. { TTestFPDocRender }
  65. procedure TTestFPDocRender.SetUp;
  66. begin
  67. FFPDocRenderer:=TMarkDownFPDocRenderer.Create(Nil);
  68. FDocument:=TMarkDownDocument.Create(Nil,1);
  69. FParent:=FDocument;
  70. end;
  71. procedure TTestFPDocRender.TearDown;
  72. begin
  73. FParent:=nil;
  74. FreeAndNil(FDocument);
  75. FreeAndNil(FFPDocRenderer);
  76. end;
  77. procedure TTestFPDocRender.StartDoc;
  78. var
  79. l : TMarkDownBlock;
  80. begin
  81. CreateHeadingBlock('unit1',1);
  82. CreateHeadingBlock('a',2);
  83. CreateHeadingBlock('descr',3);
  84. end;
  85. function TTestFPDocRender.CreateTextBlock(aParent: TMarkdownBlock; const aText, aTextNode: string; aNodeStyle: TNodeStyles): TMarkDownTextBlock;
  86. begin
  87. Result:=TMarkDownTextBlock.Create(aParent,1,aText);
  88. if aTextNode<>'' then
  89. AppendTextNode(Result,aTextNode,aNodeStyle);
  90. end;
  91. function TTestFPDocRender.CreateParagraphBlock(const aTextNode: string): TMarkdownBlock;
  92. begin
  93. Result:=TMarkDownParagraphBlock.Create(FParent,1);
  94. if aTextNode<>'' then
  95. CreateTextBlock(Result,aTextNode,aTextNode);
  96. end;
  97. function TTestFPDocRender.CreateQuotedBlock(const aTextNode: string): TMarkdownBlock;
  98. begin
  99. Result:=TMarkDownQuoteBlock.Create(FParent,1);
  100. if aTextNode<>'' then
  101. CreateTextBlock(Result,aTextNode,aTextNode);
  102. end;
  103. function TTestFPDocRender.CreateHeadingBlock(const aTextNode: string; aLevel: integer): TMarkdownBlock;
  104. begin
  105. Result:=TMarkDownHeadingBlock.Create(FParent,1,aLevel);
  106. if aTextNode<>'' then
  107. CreateTextBlock(Result,aTextNode,aTextNode);
  108. end;
  109. function TTestFPDocRender.CreateListItemBlock(aParent: TMarkDownContainerBlock; const aText: string): TMarkDownListItemBlock;
  110. var
  111. lPar : TMarkDownParagraphBlock;
  112. begin
  113. Result:=TMarkDownListItemBlock.Create(aParent,1);
  114. lPar:=TMarkDownParagraphBlock.Create(Result,1);
  115. CreateTextBlock(lPar,'',aText);
  116. end;
  117. function TTestFPDocRender.CreateListBlock(aOrdered: boolean; const aListItemText: string): TMarkDownListBlock;
  118. begin
  119. Result:=TMarkDownListBlock.Create(FParent,1);
  120. Result.ordered:=aOrdered;
  121. if aListItemText<>'' then
  122. CreateListItemBlock(Result,aListItemText);
  123. end;
  124. function TTestFPDocRender.AppendTextNode(aBlock: TMarkDownTextBlock; const aText: string; aNodeStyle: TNodeStyles): TMarkDownTextNode;
  125. var
  126. p : TPosition;
  127. t : TMarkdownTextNode;
  128. begin
  129. if aBlock.Nodes=Nil then
  130. aBlock.Nodes:=TMarkDownTextNodeList.Create(True);
  131. p.col:=Length(aBlock.Text);
  132. p.line:=1;
  133. t:=TMarkDownTextNode.Create(p,nkText);
  134. t.addText(aText);
  135. t.active:=False;
  136. T.Styles:=aNodeStyle;
  137. aBlock.Nodes.Add(t);
  138. Result:=T;
  139. end;
  140. procedure TTestFPDocRender.TestRender(const aFPDoc: string; aEnvelope: boolean);
  141. const
  142. prefix = '<?xml version="1.0" encoding="utf-8"?>'+sLineBreak
  143. +'<fpdoc-descriptions>'+sLineBreak
  144. +' <package>'+sLineBreak
  145. +' <module name="unit1">'+sLineBreak
  146. +' <element name="a">'+sLineBreak;
  147. prefixEmpty = prefix
  148. + ' <descr/>'+sLineBreak;
  149. prefixContent = prefix
  150. + ' <descr>';
  151. Suffix = ' </element>'+sLineBreak
  152. + ' </module>'+sLineBreak
  153. + ' </package>'+sLineBreak
  154. + '</fpdoc-descriptions>';
  155. SuffixContent = '</descr>'+sLineBreak
  156. + Suffix;
  157. SuffixEmpty = Suffix;
  158. var
  159. L : TStrings;
  160. lFPDoc: string;
  161. begin
  162. L:=TstringList.Create;
  163. try
  164. L.SkipLastLineBreak:=True;
  165. Renderer.RenderDocument(FDocument,L);
  166. if not aEnvelope then
  167. lFPDoc:=aFPDoc
  168. else
  169. begin
  170. if aFPDoc='' then
  171. lFPDoc:=PrefixEmpty+SuffixEmpty
  172. else
  173. lFPDoc:=PrefixContent+aFpdoc+SuffixContent
  174. end;
  175. assertEquals('Correct FPDoc: ',lFPDoc,L.Text);
  176. finally
  177. L.Free;
  178. end;
  179. end;
  180. procedure TTestFPDocRender.TestHookup;
  181. begin
  182. AssertNotNull('Have renderer',FFPDocRenderer);
  183. AssertNotNull('Have document',FDocument);
  184. AssertEquals('Have empty document',0,FDocument.blocks.Count);
  185. end;
  186. procedure TTestFPDocRender.TestEmpty;
  187. begin
  188. TestRender('<?xml version="1.0" encoding="utf-8"?>'+sLineBreak
  189. +'<fpdoc-descriptions>'+sLineBreak
  190. +' <package/>'+sLineBreak
  191. +'</fpdoc-descriptions>');
  192. end;
  193. procedure TTestFPDocRender.TestEmptyPackageName;
  194. begin
  195. Renderer.PackageName:='a';
  196. TestRender('<?xml version="1.0" encoding="utf-8"?>'+sLineBreak
  197. +'<fpdoc-descriptions>'+sLineBreak
  198. +' <package name="a"/>'+sLineBreak
  199. +'</fpdoc-descriptions>');
  200. end;
  201. procedure TTestFPDocRender.TestTextBlockEmpty;
  202. begin
  203. StartDoc;
  204. CreateTextBlock(Document,'a','');
  205. TestRender('',True);
  206. end;
  207. procedure TTestFPDocRender.TestTextBlockText;
  208. begin
  209. StartDoc;
  210. CreateTextBlock(Document,'a','a');
  211. TestRender('a',True);
  212. end;
  213. procedure TTestFPDocRender.TestTextBlockTextStrong;
  214. begin
  215. StartDoc;
  216. CreateTextBlock(Document,'a','a',[nsStrong]);
  217. TestRender(cNlIndent2+'<b>a</b>'+CnlIndent,True);
  218. end;
  219. procedure TTestFPDocRender.TestTextBlockTextEmph;
  220. begin
  221. StartDoc;
  222. CreateTextBlock(Document,'a','a',[nsEmph]);
  223. TestRender(cNlIndent2+'<i>a</i>'+cNlIndent,True);
  224. end;
  225. procedure TTestFPDocRender.TestTextBlockTextDelete;
  226. begin
  227. StartDoc;
  228. CreateTextBlock(Document,'a','a',[nsDelete]);
  229. TestRender(cNlIndent2+'<u>a</u>'+cNlindent,True);
  230. end;
  231. procedure TTestFPDocRender.TestTextBlockTextStrongEmph;
  232. begin
  233. StartDoc;
  234. CreateTextBlock(Document,'a','a',[nsStrong,nsEmph]);
  235. TestRender(cNlIndent2+'<b>'+cNLIndent4+'<i>a</i>'+cNlIndent2+'</b>'+cNlIndent,True);
  236. end;
  237. procedure TTestFPDocRender.TestTextBlockTextStrongEmphSplit1;
  238. var
  239. lBlock : TMarkDownTextBlock;
  240. begin
  241. StartDoc;
  242. lBlock:=CreateTextBlock(Document,'a','a ',[nsStrong]);
  243. AppendTextNode(lBlock,'b',[nsStrong,nsemph]);
  244. TestRender(cNlIndent2+'<b>a <i>b</i>'+cNlIndent2+'</b>'+cNlIndent,True);
  245. end;
  246. procedure TTestFPDocRender.TestTextBlockTextStrongEmphSplit2;
  247. var
  248. lBlock : TMarkDownTextBlock;
  249. begin
  250. StartDoc;
  251. lBlock:=CreateTextBlock(Document,'a','a',[nsEmph,nsStrong]);
  252. AppendTextNode(lBlock,' b',[nsStrong]);
  253. TestRender(cNlIndent2+'<b>'+cNlIndent4+'<i>a</i> b</b>'+cNlIndent,True);
  254. end;
  255. procedure TTestFPDocRender.TestPragraphBlockEmpty;
  256. begin
  257. StartDoc;
  258. CreateParagraphBlock('');
  259. TestRender(cNlIndent2+'<p/>'+cNlIndent,true);
  260. end;
  261. procedure TTestFPDocRender.TestPragraphBlockText;
  262. begin
  263. StartDoc;
  264. CreateParagraphBlock('a');
  265. TestRender(cNlIndent2+'<p>a</p>'+cNlIndent,true);
  266. end;
  267. procedure TTestFPDocRender.TestQuotedBlockEmpty;
  268. begin
  269. StartDoc;
  270. CreateQuotedBlock('');
  271. TestRender(cNlIndent2+'<remark/>'+cNlIndent,true);
  272. end;
  273. procedure TTestFPDocRender.TestQuotedBlockText;
  274. begin
  275. StartDoc;
  276. CreateQuotedBlock('a');
  277. TestRender(cNlIndent2+'<remark>a</remark>'+cNlIndent,true);
  278. end;
  279. procedure TTestFPDocRender.TestUnorderedListEmpty;
  280. begin
  281. StartDoc;
  282. CreateListBlock(false,'');
  283. TestRender(cNlIndent2+'<ul/>'+cNlIndent,True);
  284. end;
  285. procedure TTestFPDocRender.TestUnorderedListOneItem;
  286. begin
  287. StartDoc;
  288. CreateListBlock(false,'a');
  289. TestRender(cNlIndent2+'<ul>'+cNlIndent4+'<li>a</li>'+cNlIndent2+'</ul>'+cNlIndent,True);
  290. end;
  291. initialization
  292. Registertest(TTestFPDocRender);
  293. end.