123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- unit utcFPTemplate;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, punit, fpTemplate;
- procedure RegisterTests;
- implementation
- type
- TTestCallbacks = class(TObject)
- public
- procedure TestAllowTagParamsBasics_replacetag(Sender : TObject; Const TagString : String; TagParams:TStringList; Out ReplaceText : String);
- procedure TestAllowTagParamsFunctionLike_replacetag(Sender : TObject; Const TagString : String; TagParams:TStringList; Out ReplaceText : String);
- procedure TestAllowTagParamsDelphiStyle_replacetag(Sender : TObject; Const TagString : String; TagParams:TStringList; Out ReplaceText : String);
- end;
- var
- Callbacks: TTestCallbacks;
- function SuiteSetup: TTestString;
- begin
- Result := '';
- Callbacks := TTestCallbacks.Create;
- end;
- function SuiteTearDown: TTestString;
- begin
- Result := '';
- Callbacks.Free;
- end;
- procedure TTestCallbacks.TestAllowTagParamsBasics_replacetag(
- Sender: TObject; const TagString: String; TagParams: TStringList; out
- ReplaceText: String);
- begin
- if TagString='test' then
- begin
- AssertEquals('Callback basics: Param count', 1, TagParams.Count);
- AssertEquals('Callback basics: Param name', 'param1', TagParams.Names[0]);
- AssertEquals('Callback basics: Param value', 'test ', TagParams.ValueFromIndex[0]);
- ReplaceText := 'template'
- end
- else if TagString='dream' then ReplaceText := 'think';
- end;
- procedure TTestCallbacks.TestAllowTagParamsFunctionLike_replacetag(
- Sender: TObject; const TagString: String; TagParams: TStringList; out
- ReplaceText: String);
- begin
- if TagString='uppercase' then
- begin
- AssertEquals('Callback function-like: Param count', 1, TagParams.Count);
- ReplaceText:=UpperCase(TagParams[0]);
- end;
- end;
- procedure TTestCallbacks.TestAllowTagParamsDelphiStyle_replacetag(
- Sender: TObject; const TagString: String; TagParams: TStringList; out
- ReplaceText: String);
- begin
- AssertEquals('Callback delphi-style: Param count', 2, TagParams.Count);
- AssertEquals('Callback delphi-style: Param 1 name', 'param1', TagParams.Names[0]);
- AssertEquals('Callback delphi-style: Param 1 value', 'first param', TagParams.ValueFromIndex[0]);
- AssertEquals('Callback delphi-style: Param 2 name', 'param2', TagParams.Names[1]);
- AssertEquals('Callback delphi-style: Param 2 value', 'second param', TagParams.ValueFromIndex[1]);
- ReplaceText := 'Delphi parameter'
- end;
- Function TFPtemplate_TestBasics : TTestString;
- var
- templ: TTemplateParser;
- begin
- Result:='';
- templ := TTemplateParser.Create;
- try
- templ.Values['dream'] := 'think';
- templ.Values['test'] := 'template';
- AssertEquals('TestBasics simple replace', 'This is the simplest template I could think of.',
- templ.ParseString('This is the simplest {test} I could {dream} of.'));
- templ.recursive := true;
- templ.Values['val2'] := 'template';
- templ.Values['test'] := '{val2} test';
- AssertEquals('TestBasics recursive replace', 'This is the simplest template test I could think of.',
- templ.ParseString('This is the simplest {test} I could {dream} of.'));
- finally
- templ.free;
- end;
- end;
- Function TFPtemplate_TestBasicDelimiters : TTestString;
- var
- templ: TTemplateParser;
- begin
- Result:='';
- templ := TTemplateParser.Create;
- try
- templ.StartDelimiter:='[-';
- templ.EndDelimiter:=')';
- templ.Values['dream'] := 'think';
- templ.Values['test'] := 'template';
- AssertEquals('TestBasicDelimiters custom 1', 'This is [the] simplest template I could think (of).',
- templ.ParseString('This is [the] simplest [-test) I could [-dream) (of).'));
- templ.StartDelimiter:='(';
- templ.EndDelimiter:='-)';
- templ.Values['dream'] := 'think';
- templ.Values['test'] := 'template';
- AssertEquals('TestBasicDelimiters custom 2', 'This is [the] simplest template I could think of:-).',
- templ.ParseString('This is [the] simplest (test-) I could (dream-) of:-).'));
- finally
- templ.free;
- end;
- end;
- Function TFPtemplate_TestAllowTagParamsBasics : TTestString;
- var
- templ: TTemplateParser;
- begin
- Result:='';
- templ := TTemplateParser.Create;
- try
- templ.AllowTagParams := true;
- templ.OnReplaceTag := @Callbacks.TestAllowTagParamsBasics_replacetag;
- AssertEquals('TestAllowTagParamsBasics 1', 'This is the simplest template I could think of.',
- templ.ParseString('This is the simplest {test [- param1=test -]} I could {dream} of.'));
- AssertEquals('TestAllowTagParamsBasics 2', 'This is the simplest template I could think of.',
- templ.ParseString('This is the simplest {test[- param1=test -]} I could {dream} of.'));
- templ.ParamValueSeparator:=':';
- AssertEquals('TestAllowTagParamsBasics 3', 'This is the simplest template I could think of.',
- templ.ParseString('This is the simplest {test [- param1:test -]} I could {dream} of.'));
- AssertEquals('TestAllowTagParamsBasics 4', 'This is the simplest template I could think of.',
- templ.ParseString('This is the simplest {test [-param1:test -]} I could {dream} of.'));
- AssertEquals('TestAllowTagParamsBasics 5', 'This is the simplest template I could think of.',
- templ.ParseString('This is the simplest {test [-param1:test -]} I could {dream} of.'));
- finally
- templ.free;
- end;
- end;
- Function TFPtemplate_TestAllowTagParamsFunctionLike : TTestString;
- var
- templ: TTemplateParser;
- begin
- Result:='';
- templ := TTemplateParser.Create;
- try
- templ.AllowTagParams := true;
- templ.ParamStartDelimiter:='(';
- templ.ParamEndDelimiter:=')';
- templ.OnReplaceTag := @Callbacks.TestAllowTagParamsFunctionLike_replacetag;
- AssertEquals('TestAllowTagParamsFunctionLike', 'THIS should be uppercased.',
- templ.ParseString('{uppercase(This)} should be uppercased.'));
- finally
- templ.free;
- end;
- end;
- Function TFPtemplate_TestAllowTagParamsDelphiStyle : TTestString;
- var
- templ: TTemplateParser;
- begin
- Result:='';
- templ := TTemplateParser.Create;
- try
- templ.AllowTagParams := true;
- templ.StartDelimiter:='<#';
- templ.EndDelimiter:='>';
- templ.ParamStartDelimiter:=' ';
- templ.ParamEndDelimiter:='"';
- templ.ParamValueSeparator:='="';
- templ.OnReplaceTag := @Callbacks.TestAllowTagParamsDelphiStyle_replacetag;
- AssertEquals('TestAllowTagParamsDelphiStyle', 'Test for a Delphi parameter.',
- templ.ParseString('Test for a <#DelphiTag param1="first param" param2="second param">.'));
- finally
- templ.free;
- end;
- end;
- procedure RegisterTests;
- begin
- AddSuite('TFPtemplateTests', @SuiteSetup, @SuiteTearDown);
- AddTest('TestBasics', @TFPtemplate_TestBasics, 'TFPtemplateTests');
- AddTest('TestBasicDelimiters', @TFPtemplate_TestBasicDelimiters, 'TFPtemplateTests');
- AddTest('TestAllowTagParamsBasics', @TFPtemplate_TestAllowTagParamsBasics, 'TFPtemplateTests');
- AddTest('TestAllowTagParamsFunctionLike', @TFPtemplate_TestAllowTagParamsFunctionLike, 'TFPtemplateTests');
- AddTest('TestAllowTagParamsDelphiStyle', @TFPtemplate_TestAllowTagParamsDelphiStyle, 'TFPtemplateTests');
- end;
- end.
|