-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathUHTMLFilterTests.pas
262 lines (221 loc) · 7.32 KB
/
UHTMLFilterTests.pas
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
unit UHTMLFilterTests;
interface
uses
Classes, SysUtils, dwsXPlatformTests, dwsComp, dwsCompiler, dwsExprs,
dwsHtmlFilter, dwsXPlatform, dwsUtils;
type
THTMLFilterTests = class(TTestCase)
private
FTests: TStringList;
FCompiler: TDelphiWebScript;
FFilter: TdwsHTMLFilter;
FUnit: TdwsHTMLUnit;
public
procedure SetUp; override;
procedure TearDown; override;
procedure DoInclude(const scriptName: String; var scriptSource: String);
published
procedure TestHTMLScript;
procedure TestPatterns;
procedure TestPatternLong;
procedure TestSpecialChars;
procedure TestNotClosed;
procedure TestIncludeFiltered;
procedure TestIncludeFiltered2;
procedure TestEditorMode;
end;
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
implementation
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------
// ------------------ THTMLFilterTests ------------------
// ------------------
// SetUp
//
procedure THTMLFilterTests.SetUp;
begin
FTests := TStringList.Create;
CollectFiles(ExtractFilePath(ParamStr(0)) + 'HTMLFilterScripts' + PathDelim, '*.dws', FTests);
FCompiler := TDelphiWebScript.Create(nil);
FCompiler.OnInclude := DoInclude;
FFilter := TdwsHTMLFilter.Create(nil);
FFilter.PatternOpen := '<?pas';
FFilter.PatternClose := '?>';
FFilter.PatternEval := '=';
FCompiler.Config.Filter := FFilter;
FUnit := TdwsHTMLUnit.Create(nil);
FCompiler.AddUnit(FUnit);
end;
// TearDown
//
procedure THTMLFilterTests.TearDown;
begin
FCompiler.Free;
FFilter.Free;
FUnit.Free;
FTests.Free;
end;
procedure THTMLFilterTests.TestHTMLScript;
var
s: string;
resultFileName : String;
prog: IdwsProgram;
sl : TStringList;
exec : IdwsProgramExecution;
begin
sl:=TStringList.Create;
try
for s in FTests do begin
sl.LoadFromFile(s);
prog := FCompiler.Compile(sl.Text);
CheckEquals('', prog.Msgs.AsInfo, s);
exec:=prog.Execute;
resultFileName:=ChangeFileExt(s, '.txt');
if FileExists(resultFileName) then
sl.LoadFromFile(ChangeFileExt(resultFileName, '.txt'))
else sl.Clear;
CheckEquals(sl.Text, exec.Result.ToString, s);
end;
finally
sl.Free;
end;
end;
// TestPatterns
//
procedure THTMLFilterTests.TestPatterns;
var
locFilter : TdwsHtmlFilter;
begin
locFilter:=TdwsHtmlFilter.Create(nil);
try
locFilter.PatternClose:='';
CheckException(locFilter.CheckPatterns, EHTMLFilterException);
locFilter.PatternClose:='a';
locFilter.PatternOpen:='';
CheckException(locFilter.CheckPatterns, EHTMLFilterException);
locFilter.PatternOpen:='b';
locFilter.PatternEval:='';
CheckException(locFilter.CheckPatterns, EHTMLFilterException);
locFilter.PatternEval:='c';
finally
locFilter.Free;
end;
end;
// TestPatternLong
//
procedure THTMLFilterTests.TestPatternLong;
var
locFilter : TdwsHtmlFilter;
begin
locFilter:=TdwsHtmlFilter.Create(nil);
try
locFilter.PatternEval:='==';
CheckEquals('Print(1);Print(''2'');Print(3);Print(''4'');', locFilter.Process('<?pas==1?>2<?pas==3?>4', nil));
finally
locFilter.Free;
end;
end;
// TestSpecialChars
//
procedure THTMLFilterTests.TestSpecialChars;
var
prog: IdwsProgram;
exec : IdwsProgramExecution;
begin
prog:=FCompiler.Compile('hello'#10'world');
exec:=prog.Execute;
CheckEquals('hello'#10'world', exec.Result.ToString, '#10');
prog:=FCompiler.Compile('hello'#9'world');
exec:=prog.Execute;
CheckEquals('hello'#9'world', exec.Result.ToString, '#9');
prog:=FCompiler.Compile('''');
exec:=prog.Execute;
CheckEquals('''', exec.Result.ToString, 'apos 0');
prog:=FCompiler.Compile('''#13''');
exec:=prog.Execute;
CheckEquals('''#13''', exec.Result.ToString, 'apos 1');
prog:=FCompiler.Compile('''''''');
exec:=prog.Execute;
CheckEquals('''''''', exec.Result.ToString, 'apos 2');
prog:=FCompiler.Compile(''''#13'''');
exec:=prog.Execute;
CheckEquals(''''#13'''', exec.Result.ToString, 'apos 3');
end;
// TestNotClosed
//
procedure THTMLFilterTests.TestNotClosed;
var
prog: IdwsProgram;
exec : IdwsProgramExecution;
begin
prog:=FCompiler.Compile('<?pas Send(''hello'');');
exec:=prog.Execute;
CheckEquals('hello', exec.Result.ToString, 'hello prog');
prog:=FCompiler.Compile('<?pas var hello="world";?><?pas=hello');
exec:=prog.Execute;
CheckEquals('world', exec.Result.ToString, 'hello eval');
end;
// TestIncludeFiltered
//
procedure THTMLFilterTests.TestIncludeFiltered;
var
prog: IdwsProgram;
exec : IdwsProgramExecution;
begin
prog:=FCompiler.Compile('a<?pas'#13#10'{$F "B"}'#13#10'?>c');
exec:=prog.Execute;
CheckEquals('aBc', exec.Result.ToString, 'include filtered 1');
end;
// TestIncludeFiltered2
//
procedure THTMLFilterTests.TestIncludeFiltered2;
var
prog: IdwsProgram;
exec : IdwsProgramExecution;
begin
prog:=FCompiler.Compile('a<?pas'#10'{$F "B"}'#10'{$F "C"}'#10'{$F "D"}'#10'?>f<?pas= "g" ?>');
exec:=prog.Execute;
CheckEquals('aB({C} )();'#9'D'#10'fg', exec.Result.ToString, 'include filtered 1');
end;
// DoInclude
//
procedure THTMLFilterTests.DoInclude(const scriptName: String; var scriptSource: String);
begin
if scriptName='B' then
scriptSource:='B'
else if scriptName='C' then
scriptSource:='({C} )();'
else if scriptName='D' then
scriptSource:=#9'D'#10
else scriptSource := LoadTextFromFile('SimpleScripts\'+scriptName);
end;
// TestEditorMode
//
procedure THTMLFilterTests.TestEditorMode;
begin
FFilter.BeginEditorMode;
try
CheckEquals(' N ', FFilter.Process('a<?pas N ?>b', nil), '1');
CheckEquals(' (N); ', FFilter.Process('a<?pas=N?>b', nil), '2');
CheckEquals(' N'#13#10' '#13#10' M ', FFilter.Process('<?pasN'#13#10'?>'#13#10'<?pasM?>', nil), '3');
CheckEquals(' (1);'+StringOfChar(' ', 1204 + 5)+'(2);',
TrimRight(FFilter.Process('<?pas=1?>'+StringOfChar(' ', 1204)+'<?pas=2?>', nil)), 'many spaces');
CheckEquals(' (1);'+StringOfChar(' ', 1204)+#13#10' (2);',
TrimRight(FFilter.Process('<?pas=1?>'+StringOfChar(' ', 1204)+#13#10'<?pas=2?>', nil)), 'many spaces');
finally
FFilter.EndEditorMode;
end;
end;
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
initialization
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
RegisterTest('HTMLFilterTests', THTMLFilterTests);
end.