utcfptemplate.pp 7.0 KB

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