forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DUnitX.Attributes.pas
370 lines (321 loc) · 11.2 KB
/
DUnitX.Attributes.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
{***************************************************************************}
{ }
{ DUnitX }
{ }
{ Copyright (C) 2015 Vincent Parrett & Contributors }
{ }
{ 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. }
{ }
{***************************************************************************}
unit DUnitX.Attributes;
interface
{$I DUnitX.inc}
uses
{$IFDEF USE_NS}
System.Rtti,
{$ELSE}
Rtti,
{$ENDIF}
DUnitX.Types;
type
/// <summary>
/// A class decorated with this attribute will be tested. The parameters
/// allow you to control which methods are treated as tests. By default
/// only methods decorated with the Test attribute are run as tests.
/// </summary>
TestFixtureAttribute = class(TCustomAttribute)
private
FName : string;
FDescription : string;
public
constructor Create;overload;
constructor Create(const AName : string);overload;
constructor Create(const AName : string; const ADescription : string);overload;
property Name : string read FName;
property Description : string read FDescription;
end;
/// <summary>
/// A TestFixture decorated with this attribute will be tested using it's
/// own thread. This can speed up unit testing when fixtures do not
/// compete for resources and the test machine has enough cores to service
/// the tests.
/// </summary>
/// <remarks>
/// NOTE - CURRENTLY PLANNED BUT NOT IMPLEMENTED!!!
/// </remarks>
TestInOwnThreadAttribute = class(TCustomAttribute)
end;
/// <summary>
/// A method marked with this attribute will run before any tests in. Note
/// that if more than one method is decorated with this attribute the first
/// method found will be executed (not recommended!).
/// </summary>
SetupFixtureAttribute = class(TCustomAttribute)
end;
/// <summary>
/// A method on a test fixture decorated with this attribute will run
/// before each test method is run. Note that if more than one method is
/// decorated with this attribute the first method found will be executed
/// (not recommended!).
/// </summary>
SetupAttribute = class(TCustomAttribute)
end;
/// <summary>
/// A method on a test fixture class decorated with this attribute will be
/// run after each test method is run. Note that if more than one method is
/// decorated with this attribute the first method found will be executed
/// (not recommended!).
/// </summary>
TearDownAttribute = class(TCustomAttribute)
end;
/// <summary>
/// A method marked with this attribute can contain a teardown method that
/// will be run after each all tests in the fixture have executed. Note
/// that if more than one method is decorated with this attribute the first
/// method found will be executed (not recommended!).
/// </summary>
TearDownFixtureAttribute = class(TCustomAttribute)
end;
/// <summary>
/// This attribue is applied to test methods. If a test is successful and
/// produces a memory leak it will be reported. If you do not want the
/// leak reported, then you can add this attribute to the test method.
/// </summary>
IgnoreMemoryLeaks = class(TCustomAttribute)
private
FIgnoreMemoryLeaks : Boolean;
public
constructor Create(const AIgnoreMemoryLeaks : Boolean = True);
property IgnoreLeaks : Boolean read FIgnoreMemoryLeaks;
end;
/// <summary>
/// Marks a test method to fail after the time specified.
/// Currently only support on Win32 & Win64
/// </summary>
/// <remarks>
/// If [MaxTime(1000]] used then the test will fail if the
/// test takes longer than 1000ms
/// </remarks>
MaxTimeAttribute = class(TCustomAttribute)
private
FMaxTime : Cardinal;
public
constructor Create(const AMaxTime : Cardinal);
property MaxTime : Cardinal read FMaxTime;
end;
/// <summary>
/// This attribute marks a method as a test method
/// </summary>
TestAttribute = class(TCustomAttribute)
private
FEnabled : boolean;
public
constructor Create;overload;
constructor Create(const AEnabled : boolean);overload;
property Enabled : boolean read FEnabled;
end;
/// <summary>
/// This attribute allows you to specify a test Category which can be used
/// when filtering the tests to run.
/// </summary>
CategoryAttribute = class(TCustomAttribute)
private
FCategory : string;
public
constructor Create(const ACategory : string);
property Category : string read FCategory;
end;
/// <summary>
/// This attribute will prevent a test from being run. It will still show
/// up in the lists of tests, and reported as an Ignored test
/// </summary>
/// <remarks>
/// This is useful when you need to temporarily stop a test from running.
/// </remarks>
IgnoreAttribute = class(TCustomAttribute)
private
FReason : string;
public
constructor Create(const AReason : string = '');
property Reason : string read FReason;
end;
/// <summary>
/// Marks a test method to be repeated count times.
/// </summary>
/// <remarks>
/// If [RepeatTest(0]] used then the test will be skipped and behaves like
/// IgnoreAttribute
/// </remarks>
RepeatTestAttribute = class(TCustomAttribute)
private
FCount : Cardinal;
public
constructor Create(const ACount : Cardinal);
property Count : Cardinal read FCount;
end;
/// <summary>
/// Internal Structure used for those implementing CustomTestCase or
/// CustomTestCaseSource descendants.
/// </summary>
TestCaseInfo = record
/// <summary>
/// Name of the Test Case
/// </summary>
Name : string;
/// <summary>
/// Values that will be passed to the method being tested.
/// </summary>
Values : TValueArray;
end;
TestCaseInfoArray = TArray<TestCaseInfo>;
/// <summary>
/// Base class for all Test Case Attributes.
/// </summary>
/// <remarks>
/// Class is abstract and should never be, used to annotate a class as a
/// attribute. Instead use a descendant, that implements the GetCaseInfo
/// method.
/// </remarks>
CustomTestCaseAttribute = class abstract(TCustomAttribute)
protected
function GetCaseInfo : TestCaseInfo; virtual; abstract;
public
property CaseInfo : TestCaseInfo read GetCaseInfo;
end;
/// <summary>
/// Base class for all Test Case Source Attributes.
/// </summary>
/// <remarks>
/// <para>
/// Class is abstract and should never be, used to annotate a class as
/// a attribute. Instead use a descendant, that implements the
/// GetCaseInfoArray method.
/// </para>
/// <para>
/// Note: If a method is annotated with a decendant of
/// TestCaseSourceAttribute and returns an empty TestCaseInfoArray,
/// then no test will be shown for the method.
/// </para>
/// </remarks>
CustomTestCaseSourceAttribute = class abstract(TCustomAttribute)
protected
function GetCaseInfoArray : TestCaseInfoArray; virtual; abstract;
public
property CaseInfoArray : TestCaseInfoArray read GetCaseInfoArray;
end;
/// <summary>
/// The TestCaseAttribute allows you to pass values to a test function.
/// Each value is delimited in the string, by default the delimiter is ','
/// </summary>
TestCaseAttribute = class(CustomTestCaseAttribute)
protected
FCaseInfo : TestCaseInfo;
function GetCaseInfo : TestCaseInfo; Override;
function GetName: String;
function GetValues: TValueArray;
public
constructor Create(const ACaseName : string; const AValues : string;const ASeperator : string = ',');overload;
property Name : String read GetName;
property Values : TValueArray read GetValues;
end;
implementation
uses
{$IFDEF USE_NS}
System.Types,
System.StrUtils,
{$ELSE}
Types,
StrUtils,
{$ENDIF}
DUnitX.Utils;
{ TestFixture }
constructor TestFixtureAttribute.Create;
begin
end;
constructor TestFixtureAttribute.Create(const AName: string);
begin
FName := AName;
end;
constructor TestFixtureAttribute.Create(const AName: string; const ADescription : string);
begin
FName := AName;
FDescription := ADescription;
end;
{ IgnoreMemoryLeaks }
constructor IgnoreMemoryLeaks.Create(const AIgnoreMemoryLeaks: Boolean);
begin
inherited Create;
FIgnoreMemoryLeaks := AIgnoreMemoryLeaks;
end;
{ TestAttribute }
constructor TestAttribute.Create;
begin
FEnabled := True;
end;
constructor TestAttribute.Create(const AEnabled: boolean);
begin
FEnabled := AEnabled;
end;
{ CategoryAttribute }
constructor CategoryAttribute.Create(const ACategory: string);
begin
FCategory := ACategory;
end;
{ IgnoreAttribute }
constructor IgnoreAttribute.Create(const AReason: string);
begin
FReason := AReason;
end;
{ RepeatTestAttribute }
constructor RepeatTestAttribute.Create(const ACount: Cardinal);
begin
FCount := ACount;
end;
{ TestCaseAttribute }
constructor TestCaseAttribute.Create(const ACaseName: string; const AValues: string;const ASeperator : string);
var
i: Integer;
l : integer;
lValues : TStringDynArray;
begin
FCaseInfo.Name := ACaseName;
lValues := SplitString(AValues,ASeperator);
l := Length(lValues);
SetLength(FCaseInfo.Values,l);
for i := 0 to l -1 do
FCaseInfo.Values[i] := TValue.From<string>(lValues[i]);
end;
function TestCaseAttribute.GetCaseInfo: TestCaseInfo;
begin
Result := FCaseInfo;
end;
function TestCaseAttribute.GetName: String;
begin
Result := FCaseInfo.Name;
end;
function TestCaseAttribute.GetValues: TValueArray;
begin
Result := FCaseInfo.Values;
end;
{ MaxTimeAttribute }
constructor MaxTimeAttribute.Create(const AMaxTime : Cardinal);
begin
FMaxTime := AMaxTime;
end;
end.