forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DUnitX.TestFixture.pas
592 lines (502 loc) · 18.5 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
{***************************************************************************}
{ }
{ 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.TestFixture;
interface
{$I DUnitX.inc}
uses
{$IFDEF USE_NS}
System.Generics.Collections,
System.Rtti,
System.SysUtils,
{$ELSE}
Generics.Collections,
Rtti,
SysUtils,
{$ENDIF}
DUnitX.Types,
DUnitX.Attributes,
DUnitX.TestFramework,
DUnitX.Extensibility,
DUnitX.InternalInterfaces,
DUnitX.WeakReference,
DUnitX.Generics;
type
TDUnitXTestFixture = class(TWeakReferencedObject, ITestFixture,ITestFixtureInfo)
private class var
FRttiContext : TRttiContext;
private
FFixtureType : TRttiType;
FTestClass : TClass;
FUnitName : string;
FName : string;
FNameSpace : string;
FDescription : string;
FCategories : TList<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(const ACaseInfo : TestCaseInfo; const ACategory : string; const AMethod : TRttiMethod; const ATestEnabled : Boolean) : ITest;
//used by GenerateFixtureFromClass to be tests from a method with out test cases
function CreateTestFromMethod(const AMethod : TRttiMethod; const ACategory : string; const ATestEnabled : Boolean;const AIgnored : Boolean;const 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 GetUnitName : string;
function GetDescription : string;
function GetCategories : TList<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;
function GetHasChildTests: Boolean;
function IsNameSpaceOnly : boolean;
procedure OnMethodExecuted(const AMethod : TTestMethod);
procedure ExecuteFixtureTearDown;
procedure InitFixtureInstance;
procedure InternalInitFixtureInstance(const isConstructing : boolean);
function AddTest(const AMethodName : string; const AMethod : TTestMethod; const AName : string; const ACategory : string; const AEnabled : boolean = true;const AIgnored : boolean = false; const AIgnoreReason : string = ''; const AMaxTime :cardinal = 0; AExpectedException: ExceptClass = nil; const AExceptionInheritance: TExceptionInheritance = exExact) : ITest;
function AddTestCase(const AMethodName : string; const ACaseName : string; const AName : string; const ACategory : string; const AMethod : TRttiMethod; const AEnabled : boolean; const AArgs : TValueArray) : ITest;
function AddChildFixture(const ATestClass : TClass; const AName : string; const ACategory : string) : ITestFixture;overload;
function AddChildFixture(const AInstance : TObject; const AName : string; const ACategory : 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);
class constructor Create;
class destructor Destroy;
public
constructor Create(const AName : string; const ACategory : string; const AInstance : TObject; const AUnitName : string);overload;
constructor Create(const AName : string; const ACategory : string; const AClass : TClass; const AUnitName : string);overload;
destructor Destroy;override;
end;
implementation
uses
{$IFDEF USE_NS}
System.TypInfo,
System.Generics.Defaults,
{$ELSE}
TypInfo,
Generics.Defaults,
{$ENDIF}
DUnitX.Test,
DUnitX.Utils;
{ TDUnitXTestFixture }
constructor TDUnitXTestFixture.Create(const AName : string; const ACategory : string; const AClass : TClass; const AUnitName : string);
var
fixtureAttrib : TestFixtureAttribute;
IgnoreMemoryLeakAttrib: IgnoreMemoryLeaks;
i : integer;
categories : TArray<string>;
cat : string;
begin
inherited Create;
FTestClass := AClass;
FUnitName := AUnitName;
FTests := TTestList.Create;
FCategories := TList<string>.Create(TComparer<string>.Construct(
function(const Left, Right : string) : integer
begin
result := AnsiCompareText(Left,Right);
end));
if ACategory <> '' then
begin
categories := TStrUtils.SplitString(ACategory,',');
for cat in categories do
begin
FCategories.Add(Trim(cat));
end;
end;
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;
FFixtureType := FRttiContext.GetType(FTestClass);
if FFixtureType.TryGetAttributeOfType<IgnoreMemoryLeaks>(IgnoreMemoryLeakAttrib) then
FIgnoreMemoryLeaks := IgnoreMemoryLeakAttrib.IgnoreLeaks;
fixtureAttrib := nil;
if FFixtureType.TryGetAttributeOfType<TestFixtureAttribute>(fixtureAttrib) then
FDescription := fixtureAttrib.Description;
InternalInitFixtureInstance(true);
end;
destructor TDUnitXTestFixture.Destroy;
begin
if FFixtureInstance <> nil then
FFixtureInstance.Free;
if FChildren <> nil then
FChildren.Clear;
FChildren := nil;
FCategories.Free;
FTests.Clear;
FTests := nil;
inherited;
end;
procedure TDUnitXTestFixture.ExecuteFixtureTearDown;
begin
if Assigned(FTearDownFixtureMethod) then
begin
if FTearDownFixtureIsDestructor then
begin
FFixtureInstance.Free;
FFixtureInstance := nil;
end
else
FTearDownFixtureMethod();
end;
end;
function TDUnitXTestFixture.GetActiveTestCount: cardinal;
begin
//TODO: Return the active count, currently fudged to be the count.
Result := GetTestCount;
end;
function TDUnitXTestFixture.GetCategories : TList<string>;
begin
result := FCategories;
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.GetHasChildTests: Boolean;
var
fixture: ITestFixture;
begin
result := GetHasChildren;
if result then
begin
result := false;
for fixture in FChildren do
begin
result := fixture.HasTests;
if result then
exit;
result := fixture.HasChildTests;
if result then
exit;
end;
end;
end;
function TDUnitXTestFixture.GetHasTests: boolean;
var
test : ITest;
begin
result := false;
if FTests <> nil then
for test in FTests do
begin
if test.Enabled then
exit(true);
end;
end;
function TDUnitXTestFixture.GetIgnoreMemoryLeaksForMethod(
AMethod: TRttiMethod): Boolean;
var
IgnoreMemoryLeak: IgnoreMemoryLeaks;
begin
if AMethod.TryGetAttributeOfType<IgnoreMemoryLeaks>(IgnoreMemoryLeak) then
result := IgnoreMemoryLeak.IgnoreLeaks
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.GetUnitName: string;
begin
result := FUnitName;
end;
procedure TDUnitXTestFixture.InitFixtureInstance;
begin
InternalInitFixtureInstance(false);
end;
procedure TDUnitXTestFixture.InternalInitFixtureInstance(const isConstructing: boolean);
var
test : ITest;
{$IFDEF DELPHI_XE_UP}
method : TRttiMethod;
{$ENDIF}
begin
if FFixtureInstance = nil then
begin
//it's a dummy namespace fixture, don't bother with the rest.
if FFixtureType.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 FFixtureType.TryGetConstructor(method) then
begin
FIgnoreFixtureSetup := true;
FFixtureInstance := method.Invoke(TRttiInstanceType(FFixtureType).MetaclassType, []).AsObject;
end
else
{$ENDIF}
FFixtureInstance := FTestClass.Create;
//Don't do this if we are called from the constructor as it's not needed.
if not isConstructing then
begin
//The fixture instance has changed, need to update the tests to run on the new instance.
for test in FTests do
(test as ITestExecute).UpdateInstance(FFixtureInstance);
end;
end;
end;
function TDUnitXTestFixture.IsNameSpaceOnly: boolean;
begin
result := FTestClass = TObject;
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 and (TMethod(AMethod).Code = TMethod(FTearDownFixtureMethod).Code) then
FFixtureInstance := nil;
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; const ACategory : string): ITestFixture;
begin
result := TDUnitXTestFixture.Create(AName,ACategory, ATestClass,ATestClass.UnitName);
if FChildren = nil then
FChildren := TTestFixtureList.Create;
FChildren.Add(result);
end;
function TDUnitXTestFixture.AddChildFixture(const AInstance: TObject; const AName: string; const ACategory : string): ITestFixture;
begin
result := TDUnitXTestFixture.Create(AName,ACategory, AInstance,AInstance.ClassType.UnitName);
if FChildren = nil then
FChildren := TTestFixtureList.Create;
FChildren.Add(result);
end;
function TDUnitXTestFixture.AddTest(const AMethodName : string; const AMethod : TTestMethod; const AName : string; const ACategory : string; const AEnabled : boolean;const AIgnored : boolean; const AIgnoreReason : string; const AMaxTime :cardinal; AExpectedException: ExceptClass; const AExceptionInheritance: TExceptionInheritance): ITest;
begin
if AExpectedException = nil then
result := TDUnitXTest.Create(Self, AMethodName, AName, ACategory, AMethod, AEnabled, AIgnored, AIgnoreReason, AMaxTime)
else
result := TDUnitXExceptionTest.Create(Self, AMethodName, AName, ACategory, AMethod, AEnabled, AIgnored, AIgnoreReason, AMaxTime, AExpectedException, AExceptionInheritance);
FTests.Add(Result);
end;
function TDUnitXTestFixture.AddTestCase(const AMethodName : string; const ACaseName, AName: string; const ACategory : string;const AMethod: TRttiMethod; const AEnabled: boolean; const AArgs: TValueArray): ITest;
begin
result := TDUnitXTestCase.Create(FFixtureInstance, Self, AMethodName, ACaseName, AName, ACategory, AMethod, AEnabled, AArgs);
FTests.Add(result);
end;
class constructor TDUnitXTestFixture.Create;
begin
FRttiContext := TRttiContext.Create;
end;
class destructor TDUnitXTestFixture.Destroy;
begin
FRttiContext.Free;
end;
constructor TDUnitXTestFixture.Create(const AName: string; const ACategory : string; const AInstance: TObject; const AUnitName : string);
begin
FFixtureInstance := AInstance;
Create(AName, ACategory, AInstance.ClassType,AUnitName);
end;
function TDUnitXTestFixture.CreateTestFromMethod(const AMethod: TRttiMethod; const ACategory : string; const ATestEnabled, AIgnored: Boolean; const AIgnoredReason: String): ITest;
var
Meth : TMethod;
begin
meth.Code := AMethod.CodeAddress;
meth.Data := FFixtureInstance;
result := TDUnitXTest.Create(Self, AMethod.Name, AMethod.Name, ACategory, TTestMethod(meth),ATestEnabled,AIgnored,AIgnoredReason);
result.IgnoreMemoryLeaks := GetIgnoreMemoryLeaksForMethod(AMethod);
end;
function TDUnitXTestFixture.CreateTestFromTestCase(const ACaseInfo : TestCaseInfo; const ACategory : string; const AMethod : TRttiMethod; const ATestEnabled : Boolean) : ITest;
begin
result := TDUnitXTestCase.Create(FFixtureInstance, Self, AMethod.Name, ACaseInfo.Name, AMethod.Name, ACategory, AMethod, ATestEnabled, ACaseInfo.Values);
result.IgnoreMemoryLeaks := getIgnoreMemoryLeaksForMethod(AMethod);
end;
end.