tests_fptemplate.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. unit tests_fptemplate;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, testregistry;
  6. type
  7. { TTestTemplateParser }
  8. TTestTemplateParser= class(TTestCase)
  9. private
  10. Procedure TestAllowTagParamsBasics_replacetag(Sender : TObject; Const TagString : String; TagParams:TStringList; Out ReplaceText : String);
  11. Procedure TestAllowTagParamsFunctionLike_replacetag(Sender : TObject; Const TagString : String; TagParams:TStringList; Out ReplaceText : String);
  12. Procedure TestAllowTagParamsDelphiStyle_replacetag(Sender : TObject; Const TagString : String; TagParams:TStringList; Out ReplaceText : String);
  13. published
  14. procedure TestBasics;
  15. procedure TestBasicDelimiters;
  16. procedure TestAllowTagParamsBasics;
  17. procedure TestAllowTagParamsFunctionLike;
  18. procedure TestAllowTagParamsDelphiStyle;
  19. end;
  20. implementation
  21. uses
  22. fpTemplate;
  23. procedure TTestTemplateParser.TestBasics;
  24. var
  25. templ: TTemplateParser;
  26. begin
  27. templ := TTemplateParser.Create;
  28. try
  29. templ.Values['dream'] := 'think';
  30. templ.Values['test'] := 'template';
  31. CheckEquals('This is the simplest template I could think of.',
  32. templ.ParseString('This is the simplest {test} I could {dream} of.'));
  33. templ.recursive := true;
  34. templ.Values['val2'] := 'template';
  35. templ.Values['test'] := '{val2} test';
  36. CheckEquals('This is the simplest template test I could think of.',
  37. templ.ParseString('This is the simplest {test} I could {dream} of.'));
  38. finally
  39. templ.free;
  40. end;
  41. end;
  42. procedure TTestTemplateParser.TestBasicDelimiters;
  43. var
  44. templ: TTemplateParser;
  45. begin
  46. templ := TTemplateParser.Create;
  47. try
  48. templ.StartDelimiter:='[-';
  49. templ.EndDelimiter:=')';
  50. templ.Values['dream'] := 'think';
  51. templ.Values['test'] := 'template';
  52. CheckEquals('This is [the] simplest template I could think (of).',
  53. templ.ParseString('This is [the] simplest [-test) I could [-dream) (of).'));
  54. templ.StartDelimiter:='(';
  55. templ.EndDelimiter:='-)';
  56. templ.Values['dream'] := 'think';
  57. templ.Values['test'] := 'template';
  58. CheckEquals('This is [the] simplest template I could think of:-).',
  59. templ.ParseString('This is [the] simplest (test-) I could (dream-) of:-).'));
  60. finally
  61. templ.free;
  62. end;
  63. end;
  64. procedure TTestTemplateParser.TestAllowTagParamsBasics;
  65. var
  66. templ: TTemplateParser;
  67. begin
  68. templ := TTemplateParser.Create;
  69. try
  70. templ.AllowTagParams := true;
  71. templ.OnReplaceTag := @TestAllowTagParamsBasics_replacetag;
  72. CheckEquals('This is the simplest template I could think of.',
  73. templ.ParseString('This is the simplest {test [- param1=test -]} I could {dream} of.'));
  74. CheckEquals('This is the simplest template I could think of.',
  75. templ.ParseString('This is the simplest {test[- param1=test -]} I could {dream} of.'));
  76. templ.ParamValueSeparator:=':';
  77. CheckEquals('This is the simplest template I could think of.',
  78. templ.ParseString('This is the simplest {test [- param1:test -]} I could {dream} of.'));
  79. CheckEquals('This is the simplest template I could think of.',
  80. templ.ParseString('This is the simplest {test [-param1:test -]} I could {dream} of.'));
  81. CheckEquals('This is the simplest template I could think of.',
  82. templ.ParseString('This is the simplest {test [-param1:test -]} I could {dream} of.'));
  83. finally
  84. templ.free;
  85. end;
  86. end;
  87. procedure TTestTemplateParser.TestAllowTagParamsFunctionLike;
  88. var
  89. templ: TTemplateParser;
  90. begin
  91. templ := TTemplateParser.Create;
  92. try
  93. templ.AllowTagParams := true;
  94. templ.ParamStartDelimiter:='(';
  95. templ.ParamEndDelimiter:=')';
  96. templ.OnReplaceTag := @TestAllowTagParamsFunctionLike_replacetag;
  97. CheckEquals('THIS should be uppercased.',
  98. templ.ParseString('{uppercase(This)} should be uppercased.'));
  99. finally
  100. templ.free;
  101. end;
  102. end;
  103. procedure TTestTemplateParser.TestAllowTagParamsDelphiStyle;
  104. var
  105. templ: TTemplateParser;
  106. begin
  107. templ := TTemplateParser.Create;
  108. try
  109. templ.AllowTagParams := true;
  110. templ.StartDelimiter:='<#';
  111. templ.EndDelimiter:='>';
  112. templ.ParamStartDelimiter:=' ';
  113. templ.ParamEndDelimiter:='"';
  114. templ.ParamValueSeparator:='="';
  115. templ.OnReplaceTag := @TestAllowTagParamsDelphiStyle_replacetag;
  116. CheckEquals('Test for a Delphi parameter.',
  117. templ.ParseString('Test for a <#DelphiTag param1="first param" param2="second param">.'));
  118. finally
  119. templ.free;
  120. end;
  121. end;
  122. procedure TTestTemplateParser.TestAllowTagParamsBasics_replacetag(
  123. Sender: TObject; const TagString: String; TagParams: TStringList; out
  124. ReplaceText: String);
  125. begin
  126. if TagString='test' then
  127. begin
  128. CheckEquals(1,TagParams.Count);
  129. CheckEquals('param1',TagParams.Names[0]);
  130. CheckEquals('test ',TagParams.ValueFromIndex[0]);
  131. ReplaceText := 'template'
  132. end
  133. else if TagString='dream' then ReplaceText := 'think';
  134. end;
  135. procedure TTestTemplateParser.TestAllowTagParamsFunctionLike_replacetag(
  136. Sender: TObject; const TagString: String; TagParams: TStringList; out
  137. ReplaceText: String);
  138. begin
  139. if TagString='uppercase' then
  140. begin
  141. CheckEquals(1,TagParams.Count);
  142. ReplaceText:=UpperCase(TagParams[0]);
  143. end;
  144. end;
  145. procedure TTestTemplateParser.TestAllowTagParamsDelphiStyle_replacetag(
  146. Sender: TObject; const TagString: String; TagParams: TStringList; out
  147. ReplaceText: String);
  148. begin
  149. CheckEquals(2,TagParams.Count);
  150. CheckEquals('param1',TagParams.Names[0]);
  151. CheckEquals('first param',TagParams.ValueFromIndex[0]);
  152. CheckEquals('param2',TagParams.Names[1]);
  153. CheckEquals('second param',TagParams.ValueFromIndex[1]);
  154. ReplaceText := 'Delphi parameter'
  155. end;
  156. initialization
  157. RegisterTest(TTestTemplateParser);
  158. end.