forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDUnitX.TestFixture.pas
459 lines (382 loc) · 14.2 KB
/
DUnitX.TestFixture.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
{***************************************************************************}
{ }
{ DUnitX }
{ }
{ Copyright (C) 2013 Vincent Parrett }
{ }
{ 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.TestFixture;
interface
uses
DUnitX.TestFramework,
DUnitX.Extensibility,
DUnitX.InternalInterfaces,
DUnitX.WeakReference,
DUnitX.Generics,
Rtti;
{$I DUnitX.inc}
type
TDUnitXTestFixture = class(TWeakReferencedObject, ITestFixture, ITestFixtureInfo)
class var
FRttiContext : TRttiContext;
private
FTestClass : TClass;
FName : string;
FNameSpace : string;
FDescription : string;
FEnabled : boolean;
FTests : ITestList;
FTestInfos : IList<ITestInfo>;
FSetupMethod : TTestMethod;
FSetupFixtureMethod : TTestMethod;
FTearDownMethod : TTestMethod;
FTearDownFixtureMethod : TTestMethod;
FFixtureInstance : TObject;
FTestInOwnThread : boolean;
FSetupFixtureMethodName : string;
FSetupMethodName : string;
FTearDownMethodName : string;
FTearDownFixtureMethodName : string;
FChildren : ITestFixtureList;
FTearDownFixtureIsDestructor : boolean;
FIgnoreMemoryLeaks : Boolean;
FIgnoreFixtureSetup : boolean;
protected
//used by GenerateFixtureFromClass to be tests from TestCaseInfo
function CreateTestFromTestCase(ACaseInfo : TestCaseInfo; AMethod : TRttiMethod; ATestEnabled : Boolean) : ITest;
//used by GenerateFixtureFromClass to be tests from a method with out test cases
function CreateTestFromMethod(AMethod : TRttiMethod; ATestEnabled : Boolean;AIgnored : Boolean;AIgnoredReason: String) : ITest;
// Check overriding attribute, other wise uses returns fixture value
function GetIgnoreMemoryLeaksForMethod(AMethod : TRttiMethod) : Boolean;
function GetEnabled: Boolean;
procedure SetEnabled(const value: Boolean);
function GetName: string;
function GetNameSpace : string;
function GetFullName : string;
function GetDescription : string;
function GetTests: ITestList;
function ITestFixtureInfo.GetTests = ITestFixtureInfo_GetTests;
function ITestFixtureInfo_GetTests : IList<ITestInfo>;
function GetTestClass : TClass;
function GetSetupMethod : TTestMethod;
function GetSetupFixtureMethod : TTestMethod;
function GetTearDownMethod : TTestMethod;
function GetTearDownFixtureMethod : TTestMethod;
function GetTestInOwnThread : boolean;
function GetSetupFixtureMethodName: string;
function GetSetupMethodName: string;
function GetTearDownMethodName: string;
function GetTearDownFixtureMethodName: string;
function GetFixtureInstance : TObject;
function GetTestCount : cardinal;
function GetActiveTestCount : cardinal;
function GetChildren: ITestFixtureList;
function GetHasChildren : boolean;
function GetHasTests : boolean;
procedure OnMethodExecuted(const AMethod : TTestMethod);
function AddTest(const AMethod : TTestMethod; const AName : string; const AEnabled : boolean = true;const AIgnored : boolean = false; const AIgnoreReason : string = '') : ITest;
function AddTestCase(const ACaseName : string; const AName : string; const AMethod : TRttiMethod; const AEnabled : boolean; const AArgs : TValueArray) : ITest;
function AddChildFixture(const ATestClass : TClass; const AName : string) : ITestFixture;overload;
function AddChildFixture(const AInstance : TObject; const AName : string) : ITestFixture;overload;
procedure SetSetupTestMethod(const AMethodName : string; const AMethod : TTestMethod);
procedure SetSetupFixtureMethod(const AMethodName : string; const AMethod : TTestMethod);
procedure SetTearDownTestMethod(const AMethodName : string; const AMethod : TTestMethod);
procedure SetTearDownFixtureMethod(const AMethodName : string; const AMethod : TTestMethod; const AIsDestructor : boolean);
procedure SetTestInOwnThread(const value: Boolean);
public
constructor Create(const AName : string; const AInstance : TObject);overload;
constructor Create(const AName : string; const AClass : TClass);overload;
destructor Destroy;override;
class constructor Create;
end;
implementation
uses
TypInfo,
SysUtils,
DUnitX.Test,
DUnitX.Utils,
Generics.Collections;
{ TDUnitXTestFixture }
constructor TDUnitXTestFixture.Create(const AName : string; const AClass : TClass);
var
fixtureAttrib : TestFixtureAttribute;
IgnoreMemoryLeak: IgnoreMemoryLeaks;
method : TRttiMethod;
rType : TRttiType;
i : integer;
begin
FTestClass := AClass;
FTests := TTestList.Create;
i := LastDelimiter('.',AName);
if i <> 0 then
begin
FNameSpace := Copy(AName,1,i -1);
FName := Copy(AName,i+1,Length(AName));
end
else
begin
FName := AName;
FNameSpace := AName;
end;
FEnabled := True;
FIgnoreMemoryLeaks := False;
rType := FRttiContext.GetType(FTestClass);
if rType.TryGetAttributeOfType<IgnoreMemoryLeaks>(IgnoreMemoryLeak) then
FIgnoreMemoryLeaks := IgnoreMemoryLeak.IgnoreMemoryLeaks;
fixtureAttrib := nil;
if rType.TryGetAttributeOfType<TestFixtureAttribute>(fixtureAttrib) then
FDescription := fixtureAttrib.Description;
if FFixtureInstance = nil then
begin
//it's a dummy namespace fixture, don't bother with the rest.
if rType.Handle = TypeInfo(TObject) then
begin
FFixtureInstance := FTestClass.Create;
exit;
end;
FIgnoreFixtureSetup := false;
{$IFDEF DELPHI_XE_UP}
//NOTE: Causes Delphi 2010 to be inconsistent with produced exe. Will sometimes crash with AV when generating fixtures.
//If there is a parameterless constructor declared then we will use that as the
//fixture Setup method.
if rType.TryGetConstructor(method) then
begin
FIgnoreFixtureSetup := true;
FFixtureInstance := method.Invoke(TRttiInstanceType(rtype).MetaclassType, []).AsObject;
end
else
{$ENDIF}
FFixtureInstance := FTestClass.Create;
end;
end;
destructor TDUnitXTestFixture.Destroy;
begin
if FFixtureInstance <> nil then
FFixtureInstance.Free;
FTests := nil;
inherited;
end;
function TDUnitXTestFixture.GetActiveTestCount: cardinal;
begin
//TODO: Return the active count, currently fudged to be the count.
Result := GetTestCount;
end;
function TDUnitXTestFixture.GetChildren: ITestFixtureList;
begin
if FChildren = nil then
FChildren := TTestFixtureList.Create;
result := FChildren;
end;
function TDUnitXTestFixture.GetDescription: string;
begin
result := FDescription;
end;
function TDUnitXTestFixture.GetEnabled: Boolean;
begin
result := FEnabled;
end;
function TDUnitXTestFixture.GetFixtureInstance: TObject;
begin
result := FFixtureInstance;
end;
function TDUnitXTestFixture.GetFullName: string;
begin
if FName <> FNameSpace then
result := FNameSpace + '.' + FName
else
Result := FName;
end;
function TDUnitXTestFixture.GetHasChildren: boolean;
begin
result := (FChildren <> nil) and (FChildren.Count > 0);
end;
function TDUnitXTestFixture.GetHasTests: boolean;
begin
result := (FTests <> nil) and (FTests.Count > 0);
end;
function TDUnitXTestFixture.GetIgnoreMemoryLeaksForMethod(
AMethod: TRttiMethod): Boolean;
var
IgnoreMemoryLeak: IgnoreMemoryLeaks;
begin
if AMethod.TryGetAttributeOfType<IgnoreMemoryLeaks>(IgnoreMemoryLeak) then
result := IgnoreMemoryLeak.IgnoreMemoryLeaks
else
result := FIgnoreMemoryLeaks;
end;
function TDUnitXTestFixture.GetName: string;
begin
result := FName;
end;
function TDUnitXTestFixture.GetNameSpace: string;
begin
result := FNameSpace;
end;
function TDUnitXTestFixture.GetSetupFixtureMethod: TTestMethod;
begin
result := FSetupFixtureMethod;
end;
function TDUnitXTestFixture.GetSetupFixtureMethodName: string;
begin
result := FSetupFixtureMethodName;
end;
function TDUnitXTestFixture.GetSetupMethod: TTestMethod;
begin
result := FSetupMethod;
end;
function TDUnitXTestFixture.GetSetupMethodName: string;
begin
result := FSetupMethodName;
end;
function TDUnitXTestFixture.GetTearDownFixtureMethod: TTestMethod;
begin
result := FTearDownFixtureMethod;
end;
function TDUnitXTestFixture.GetTearDownFixtureMethodName: string;
begin
result := FTearDownFixtureMethodName;
end;
function TDUnitXTestFixture.GetTearDownMethod: TTestMethod;
begin
result := FTearDownMethod;
end;
function TDUnitXTestFixture.GetTearDownMethodName: string;
begin
result := FTearDownMethodName;
end;
function TDUnitXTestFixture.GetTestClass: TClass;
begin
result := FTestClass;
end;
function TDUnitXTestFixture.GetTestCount: cardinal;
begin
Result := FTests.Count;
end;
function TDUnitXTestFixture.GetTestInOwnThread: boolean;
begin
result := FTestInOwnThread;
end;
function TDUnitXTestFixture.GetTests: ITestList;
begin
result := FTests;
end;
function TDUnitXTestFixture.ITestFixtureInfo_GetTests: IList<ITestInfo>;
var
test : ITest;
begin
//TODO: Need to test that this isn't accessed between updates to FTests.
if FTestInfos = nil then
begin
FTestInfos := TDUnitXList<ITestInfo>.Create;
for test in FTests do
FTestInfos.Add(test as ITestInfo);
end;
result := FTestInfos;
end;
procedure TDUnitXTestFixture.OnMethodExecuted(const AMethod: TTestMethod);
begin
if FTearDownFixtureIsDestructor then
begin
if TMethod(AMethod).Code = TMethod(FTearDownFixtureMethod).Code then
FFixtureInstance := nil;
end;
end;
procedure TDUnitXTestFixture.SetEnabled(const value: Boolean);
begin
FEnabled := value;
end;
procedure TDUnitXTestFixture.SetSetupFixtureMethod(const AMethodName: string; const AMethod: TTestMethod);
begin
if not FIgnoreFixtureSetup then
begin
FSetupFixtureMethodName := AMethodName;
FSetupFixtureMethod := AMethod;
end;
end;
procedure TDUnitXTestFixture.SetSetupTestMethod(const AMethodName: string; const AMethod: TTestMethod);
begin
FSetupMethodName := AMethodName;
FSetupMethod := AMethod;
end;
procedure TDUnitXTestFixture.SetTearDownFixtureMethod(const AMethodName: string; const AMethod: TTestMethod; const AIsDestructor : boolean);
begin
FTearDownFixtureMethodName := AMethodName;
FTearDownFixtureMethod := AMethod;
FTearDownFixtureIsDestructor := AIsDestructor;
end;
procedure TDUnitXTestFixture.SetTearDownTestMethod(const AMethodName: string; const AMethod: TTestMethod);
begin
FTearDownMethodName := AMethodName;
FTearDownMethod := AMethod;
end;
procedure TDUnitXTestFixture.SetTestInOwnThread(const value: Boolean);
begin
FTestInOwnThread := value;
end;
function TDUnitXTestFixture.AddChildFixture(const ATestClass: TClass; const AName: string): ITestFixture;
begin
result := TDUnitXTestFixture.Create(AName,ATestClass);
if FChildren = nil then
FChildren := TTestFixtureList.Create;
FChildren.Add(result);
end;
function TDUnitXTestFixture.AddChildFixture(const AInstance: TObject; const AName: string): ITestFixture;
begin
result := TDUnitXTestFixture.Create(AName,AInstance);
if FChildren = nil then
FChildren := TTestFixtureList.Create;
FChildren.Add(result);
end;
function TDUnitXTestFixture.AddTest(const AMethod : TTestMethod; const AName : string; const AEnabled : boolean;const AIgnored : boolean; const AIgnoreReason : string): ITest;
begin
result := TDUnitXTest.Create(Self, AName, AMethod,AEnabled,AIgnored,AIgnoreReason);
FTests.Add(Result);
end;
function TDUnitXTestFixture.AddTestCase(const ACaseName, AName: string; const AMethod: TRttiMethod; const AEnabled: boolean; const AArgs: TValueArray): ITest;
begin
result := TDUnitXTestCase.Create(FFixtureInstance, Self, AName, AMethod.Name, AMethod, AEnabled, AArgs);
FTests.Add(result);
end;
class constructor TDUnitXTestFixture.Create;
begin
FRttiContext := TRttiContext.Create;
end;
constructor TDUnitXTestFixture.Create(const AName: string; const AInstance: TObject);
begin
FFixtureInstance := AInstance;
Create(AName,AInstance.ClassType);
end;
function TDUnitXTestFixture.CreateTestFromMethod(AMethod: TRttiMethod;
ATestEnabled, AIgnored: Boolean; AIgnoredReason: String): ITest;
var
Meth : TMethod;
begin
meth.Code := AMethod.CodeAddress;
meth.Data := FFixtureInstance;
result := TDUnitXTest.Create(Self, AMethod.Name, TTestMethod(meth),ATestEnabled,AIgnored,AIgnoredReason);
result.IgnoreMemoryLeaks := GetIgnoreMemoryLeaksForMethod(AMethod);
end;
function TDUnitXTestFixture.CreateTestFromTestCase(ACaseInfo : TestCaseInfo; AMethod : TRttiMethod; ATestEnabled : Boolean) : ITest;
begin
result := TDUnitXTestCase.Create(FFixtureInstance, Self, ACaseInfo.Name, AMethod.Name, AMethod, ATestEnabled, ACaseInfo.Values);
result.IgnoreMemoryLeaks := getIgnoreMemoryLeaksForMethod(AMethod);
end;
end.