1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
{
This file is part of the Free Pascal Run time library.
Copyright (c) 2021 by Michael Van Canneyt (michael@freepascal.org)
Helper classes for Mustache test cases
See the File COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit tcbasemustache;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, fpcunit, fpmustache;
type
{ TTestContext }
(* StringList with following encoding
// Null value
aName=<null>
// false value
aName=<null>
// plain value
aName=AValue
// Object value & member. Object value must be present
SubObj={}
SubObj.aName=aValue
// Array and members. Array value must be present
SubObj.SubArr=[]
SubObj.SubArr[0]={}
SubObj.SubArr[0].aName=aValue
SubObj.SubArr[1]={}
Subobj.SubArr[1].aName=aValue
*)
TTestContext = class (TMustacheContext)
Private
FValues : TStringList;
FPath : String;
public
Constructor Create(aCallback: TGetTextValueEvent); override;
Destructor destroy; override;
Function GetTextValue(Const aName : TMustacheString) : TMustacheString; override;
Function MoveNextSectionItem(Const aName : TMustacheString) : Boolean; override;
Function PushSection(Const aName : TMustacheString) : TMustacheSectionType; override;
Procedure PopSection(Const aName : TMustacheString); override;
Procedure SetValue(const aPath,aValue : string);
Property Values : TStringList read FValues;
end;
TBaseMustacheTest = class(TTestCase)
Private
FPartials: TStrings;
FTemplate: String;
FResult: TMustacheElement;
FParser: TMustacheParser;
Protected
Function CreateParser : TMustacheParser; virtual; abstract;
Procedure DoGetPartial(const aName: TMustacheString; var aHandled: Boolean; var aValue: TMustacheString);
Public
Class Procedure AssertEquals(Msg : String; aExpected,aActual : TMustacheElementType); overload;
Class Function AssertElement(aParent : TMustacheElement; aIndex: Integer; aType: TMustacheElementType; aData: String; aClass : TMustacheElementClass = Nil) : TMustacheElement; overload;
Function AssertElement(aIndex: Integer; aType: TMustacheElementType; aData: String; aClass : TMustacheElementClass = Nil) : TMustacheElement; overload;
Procedure AssertResultCount(aCount : Integer);
procedure SetUp; override;
procedure TearDown; override;
Procedure CallParser;
Procedure AddPartial(Const aName,aText: TMustacheString);
Property Partials : TStrings Read FPartials;
Property Template : String Read FTemplate Write FTemplate;
property ParseResult : TMustacheElement Read FResult;
property Parser : TMustacheParser Read FParser;
end;
implementation
uses strutils, typinfo;
{ TTestContext }
constructor TTestContext.Create(aCallback: TGetTextValueEvent);
begin
inherited Create(aCallback);
FValues:=TStringList.Create;
FValues.OwnsObjects:=True;
end;
destructor TTestContext.destroy;
begin
FreeAndNil(FValues);
inherited destroy;
end;
function TTestContext.GetTextValue(const aName: TMustacheString
): TMustacheString;
Var
aPath,N : String;
Done : Boolean;
begin
Result:='';
aPath:=FPath;
Done:=False;
Repeat
if aPath<>'' then
N:=aPath+'.'+aName
else
begin
N:=aName;
Done:=True;
end;
Result:=FValues.Values[N];
if not Done then
aPath:=Copy(aPath,1,RPos('.',aPath)-1);
until (Result<>'') or Done;
end;
function TTestContext.MoveNextSectionItem(const aName: TMustacheString
): Boolean;
Var
L,P,Idx : Integer;
N : String;
begin
L:=Length(FPath);
if (L>0) and (FPath[L]=']') then
begin
P:=RPos('[',FPath)+1;
Idx:=StrToIntDef(Copy(FPath,P,L-P),-1);
N:=Copy(FPath,1,P-1)+IntToStr(Idx+1)+']';
Result:=FValues.Values[N]<>''; // We could check for {}
if Result then
FPath:=N;
end;
end;
function TTestContext.PushSection(const aName: TMustacheString): TMustacheSectionType;
Var
aPath,S : String;
begin
if FPath<>'' then
FPath:=FPath+'.';
aPath:=FPath+aName;
S:=Values.Values[aPath];
if S='{}' then
begin
FPath:=aPath;
result:=mstSingle;
end;
if S='[]' then
begin
if Values.Values[aPath+'[0]']='' then
Result:=mstNone
else
begin
FPath:=aPath+'[-1]';
result:=mstList;
end;
end
else if (s='<null>') or (s='<false>') or (s='') then
begin
Result:=mstNone;
end
else
begin
FPath:=aPath;
result:=mstSingle;
end;
end;
procedure TTestContext.PopSection(const aName: TMustacheString);
begin
FPath:=Copy(FPath,1,RPos('.',FPath)-1);
end;
procedure TTestContext.SetValue(const aPath, aValue: string);
begin
Values.Values[aPath]:=aValue;
end;
{ TBaseMustacheTest }
procedure TBaseMustacheTest.SetUp;
begin
Inherited;
FParser:=CreateParser;
FParser.Partials:=TMustachePartialList.Create(metRoot,Nil,0);
FParser.OnGetPartial:=@DoGetPartial;
FPartials:=TStringList.Create;
TStringList(FPartials).OwnsObjects:=True;
end;
procedure TBaseMustacheTest.TearDown;
begin
FreeAndNil(FPartials);
FreeAndNil(FResult);
FParser.Partials.Free;
FreeAndNil(FParser);
Inherited;
end;
procedure TBaseMustacheTest.DoGetPartial(const aName: TMustacheString;
var aHandled: Boolean; var aValue: TMustacheString);
begin
aValue:=FPartials.Values[aName];
aHandled:=FPartials.IndexOfName(aName)<>-1;
end;
class function TBaseMustacheTest.AssertElement(aParent: TMustacheElement;
aIndex: Integer; aType: TMustacheElementType; aData: String;
aClass: TMustacheElementClass): TMustacheElement;
Var
El : TMustacheElement;
aChild : String;
begin
AssertNotNull('Have parent',aParent);
AssertTrue(Format('Index %d in range 0..%d',[aIndex,aParent.ChildCount-1]),(aIndex>=0) and (aIndex<aParent.ChildCount));
EL:=aParent.Children[aIndex];
aChild:=Format('Child %d',[aIndex]);
AssertNotNull('Have result '+aChild,El);
AssertEquals(aChild+' has correct type',aType,El.ElementType);
AssertEquals(aChild+' has correct data',aData,El.Data);
if (aClass<>Nil) then
AssertEquals(aChild+' has correct class',aClass,el.Classtype);
Result:=El;
end;
function TBaseMustacheTest.AssertElement(aIndex: Integer;
aType: TMustacheElementType; aData: String; aClass : TMustacheElementClass = Nil): TMustacheElement;
begin
AssertNotNull('Have result',FResult);
Result:=AssertElement(FResult,aIndex,aType,aData,aClass);
end;
procedure TBaseMustacheTest.AssertResultCount(aCount: Integer);
begin
AssertNotNull('Have result',FResult);
AssertEquals('Result count',aCount,FResult.ChildCount);
end;
procedure TBaseMustacheTest.CallParser;
begin
Parser.Template:=Template;
FResult:=Parser.Parse;
end;
procedure TBaseMustacheTest.AddPartial(const aName, aText: TMustacheString);
//Var
// T : TMustacheTextElement;
begin
// T:=TMustacheTextElement.Create(metText,Nil,0);
// T.Data:=aText;
FPartials.Add(aName+'='+atext);
end;
class procedure TBaseMustacheTest.AssertEquals(Msg: String; aExpected,
aActual: TMustacheElementType);
begin
AssertEquals(Msg,GetEnumName(typeInfo(TMustacheElementType),Ord(aExpected)),
GetEnumName(typeInfo(TMustacheElementType),Ord(aActual)));
end;
end.
|