forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DUnitX.CategoryExpression.pas
262 lines (228 loc) · 6.85 KB
/
DUnitX.CategoryExpression.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
{***************************************************************************}
{ }
{ DUnitX }
{ }
{ Copyright (C) 2013 Vincent Parrett }
{ }
{ vincent@finalbuilder.com }
{ http://www.finalbuilder.com }
{ }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{ This unit is largely a port of the NUnit CategoryExpression class }
{ Copyright © 2012-2014 Charlie Poole }
{ License : http://nunit.org/index.php?p=vsTestAdapterLicense&r=2.6.3 }
{ }
{***************************************************************************}
unit DUnitX.CategoryExpression;
interface
uses
DUnitX.Extensibility,
DUnitX.Filters;
{$I DUnitX.inc}
type
/// <summary>
/// CategoryExpression parses strings representing boolean
/// combinations of categories according to the following
/// grammar:
/// CategoryName ::= string not containing any of ',', '&', '+', '-'
/// CategoryFilter ::= CategoryName | CategoryFilter ',' CategoryName
/// CategoryPrimitive ::= CategoryFilter | '-' CategoryPrimitive
/// CategoryTerm ::= CategoryPrimitive | CategoryTerm '&' CategoryPrimitive
/// </summary>
TCategoryExpression = class
private
FText : string;
FNext : integer;
FToken : string;
FFilter : ITestFilter;
protected
function GetTerm: ITestFilter;
function GetExpression: ITestFilter;
function GetCategoryFilter: ICategoryFilter;
function GetPrimitive: ITestFilter;
function GetToken: string;
function NextIsOperator: boolean;
procedure SkipWhiteSpace;
function EndOfText : boolean;
function GetFilter: ITestFilter;
constructor Create(const text : string);
public
class function CreateFilter(const text : string) : ITestFilter;
end;
implementation
uses
Character,
SysUtils,
StrUtils;
const
Operators : array[1..7] of Char = (',', ';', '-', '|', '+', '(', ')') ;
{ TCategoryExpression }
constructor TCategoryExpression.Create(const text: string);
begin
FText := text;
if FText <> ''then
FNext := 1
else
FNext := MaxInt; //force end of text
end;
class function TCategoryExpression.CreateFilter(const text : string): ITestFilter;
var
expr : TCategoryExpression;
begin
expr := TCategoryExpression.Create(text);
try
result := expr.GetFilter;
finally
expr.Free;
end;
end;
function IndexOfAny(const value : string; const AnyOf: array of Char; StartIndex : Integer): Integer;
var
i, j : Integer;
c: Char;
Max: Integer;
begin
Max := Length(value);
i := StartIndex;
while i < Max do
begin
for j := 0 to Length(AnyOf) -1 do
begin
c := AnyOf[j];
if value[i] = c then
Exit(i);
end;
Inc(i);
end;
Result := -1;
end;
function TCategoryExpression.GetExpression : ITestFilter;
var
orFilter : IOrFilter;
begin
result := GetTerm;
if FToken <> '|' then
exit;
orFilter := TOrFilter.Create(result);
while FToken = '|' do
begin
GetToken();
orFilter.Add(GetTerm());
end;
result := orFilter;
end;
function TCategoryExpression.GetTerm : ITestFilter;
var
filter : IAndFilter;
prim : ITestFilter;
tok : string;
begin
prim := GetPrimitive;
if ( FToken <> '+') and (FToken <> '-' ) then
exit(prim);
filter := TAndFilter.Create(prim);
while ( FToken = '+') or (FToken = '-' ) do
begin
tok := FToken;
GetToken();
prim := GetPrimitive();
if tok = '-' then
filter.Add(TNotFilter.Create(prim) as ITestFilter)
else
filter.Add(prim);
end;
result := filter;
end;
function TCategoryExpression.GetPrimitive : ITestFilter;
begin
if FToken = '-' then
begin
GetToken;
result := TNotFilter.Create(GetPrimitive);
exit;
end
else if FToken = '(' then
begin
GetToken; // Skip (
result := GetExpression;
GetToken(); // Skip ')'
exit;
end;
result := GetCategoryFilter;
end;
function TCategoryExpression.GetCategoryFilter : ICategoryFilter;
begin
result := TCategoryFilter.Create(FToken);
while( (GetToken = ',') or (FToken = ';' )) do
result.Add(GetToken);
end;
function TCategoryExpression.GetToken : string;
var
idx : integer;
begin
SkipWhiteSpace;
if EndOfText then
FToken := ''
else if NextIsOperator then
begin
FToken := Copy(FText, FNext, 1);
Inc(FNext);
end
else
begin
idx := IndexOfAny(FText, Operators, FNext);
if idx < 0 then
idx := Length(FText) + 1;
FToken := TrimRight(Copy(FText,FNext, idx - FNext));
FNext := idx;
end;
result := FToken;
end;
function TCategoryExpression.EndOfText : boolean;
begin
result := FNext > Length(FText);
end;
procedure TCategoryExpression.SkipWhiteSpace;
begin
while( (FNext < Length(Ftext)) and {$IFDEF DELPHI_XE4_UP}FText[FNext].IsWhiteSpace{$ELSE}TCharacter.IsWhiteSpace(FText[FNext]){$ENDIF} ) do
Inc(FNext);
end;
function TCategoryExpression.NextIsOperator : boolean;
var
op : Char;
begin
result := false;
if not EndOfText then
begin
for op in Operators do
begin
if FText[FNext] = op then
Exit(true);
end;
end;
end;
function TCategoryExpression.GetFilter: ITestFilter;
begin
if FFilter <> nil then
exit(FFilter);
if GetToken = '' then
FFilter := TEmptyFilter.Create
else
FFilter := GetExpression;
Result := FFilter;
end;
end.