forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDUnitX.TestFramework.pas
1623 lines (1358 loc) · 53 KB
/
DUnitX.TestFramework.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{***************************************************************************}
{ }
{ 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.TestFramework;
interface
uses
classes,
SysUtils,
TypInfo,
Rtti,
TimeSpan,
DUnitX.Generics,
DUnitX.Extensibility,
Generics.Collections;
//TODO: Automatic support for https://code.google.com/p/delphi-code-coverage/ would be cool
{$I DUnitX.inc}
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);
/// <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 IgnoreMemoryLeaks : Boolean read FIgnoreMemoryLeaks;
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 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;
TValueArray = DUnitX.Extensibility.TValueArray;
/// <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 = array of 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;
TTestMethod = DUnitX.Extensibility.TTestMethod;
TTestLocalMethod = reference to procedure;
TLogLevel = (ltInformation, ltWarning, ltError);
const
TLogLevelDesc : array[TLogLevel] of string = ('Info', 'Warn', 'Err');
type
{$IFDEF DELPHI_XE2_UP}
/// <summary>
/// This helper class is intended to redirect the writeln method to a test
/// runner so that it is logged correctly.
/// </summary>
TTestFixtureHelper = class helper for TObject
public
procedure Log(const logType : TLogLevel; const msg : string);overload;
procedure Log(const msg : string);overload;
//for backwards compatibilty with DUnit tests.
procedure Status(const msg : string);
//redirects WriteLn to our loggers.
procedure WriteLn(const msg : string);overload;
procedure WriteLn;overload;
end;
{$ENDIF}
Assert = class
private
class procedure CheckExceptionClass(E: Exception; const exceptionClass: ExceptClass);
class function AddLineBreak(const msg: string): string;
public
class procedure Pass(const message : string = '');
class procedure Fail(const message : string = ''; const errorAddrs : pointer = nil);
class procedure AreEqual(const left : string; const right : string; const ignoreCase : boolean; const message : string);overload;
class procedure AreEqual(const left : string; const right : string; const message : string = '');overload;
class procedure AreEqual(const left, right : Extended; const tolerance : Extended; const message : string = '');overload;
class procedure AreEqual(const left, right : Extended; const message : string = '');overload;
class procedure AreEqual(const left, right : TClass; const message : string = '');overload;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure AreEqual<T>(const left, right : T; const message : string = '');overload;
{$ENDIF}
class procedure AreEqual(const left, right : Integer; const message : string = '');overload;
class procedure AreEqualMemory(const left : Pointer; const right : Pointer; const size : Cardinal; message : string = '');
class procedure AreNotEqual(const left : string; const right : string; const ignoreCase : boolean = true; const message : string = '');overload;
class procedure AreNotEqual(const left, right : Extended; const tolerance : Extended; const message : string = '');overload;
class procedure AreNotEqual(const left, right : TClass; const message : string = '');overload;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure AreNotEqual<T>(const left, right : T; const message : string = '');overload;
{$ELSE}
class procedure AreNotEqual(const left, right : Integer; const message : string = '');overload;
{$ENDIF}
class procedure AreNotEqualMemory(const left : Pointer; const right : Pointer; const size : Cardinal; message : string = '');
class procedure AreSame(const left, right : TObject; const message : string = '');overload;
class procedure AreSame(const left, right : IInterface; const message : string = '');overload;
class procedure AreNotSame(const left, right : TObject; const message : string = '');overload;
class procedure AreNotSame(const left, right : IInterface; const message : string = '');overload;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure Contains<T>(const list : IEnumerable<T>; const value : T; const message : string = '');overload;
class procedure DoesNotContain<T>(const list : IEnumerable<T>; const value : T; const message : string = '');overload;
{$ENDIF}
class procedure IsTrue(const condition : boolean; const message : string = '');
class procedure IsFalse(const condition : boolean; const message : string = '');
class procedure IsNull(const condition : TObject; const message : string = '');overload;
class procedure IsNull(const condition : Pointer; const message : string = '');overload;
class procedure IsNull(const condition : IInterface; const message : string = '');overload;
class procedure IsNull(const condition : Variant; const message : string = '');overload;
class procedure IsNotNull(const condition : TObject; const message : string = '');overload;
class procedure IsNotNull(const condition : Pointer; const message : string = '');overload;
class procedure IsNotNull(const condition : IInterface; const message : string = '');overload;
class procedure IsNotNull(const condition : Variant; const message : string = '');overload;
class procedure IsEmpty(const value : string; const message : string = '');overload;
class procedure IsEmpty(const value : Variant; const message : string = '');overload;
class procedure IsEmpty(const value : TStrings; const message : string = '');overload;
class procedure IsEmpty(const value : TList; const message : string = '');overload;
class procedure IsEmpty(const value : IInterfaceList; const message : string = '');overload;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure IsEmpty<T>(const value : IEnumerable<T>; const message : string = '');overload;
{$ENDIF}
class procedure IsNotEmpty(const value : string; const message : string = '');overload;
class procedure IsNotEmpty(const value : Variant; const message : string = '');overload;
class procedure IsNotEmpty(const value : TStrings; const message : string = '');overload;
class procedure IsNotEmpty(const value : TList; const message : string = '');overload;
class procedure IsNotEmpty(const value : IInterfaceList; const message : string = '');overload;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure IsNotEmpty<T>(const value : IEnumerable<T>; const message : string = '');overload;
{$ENDIF}
class procedure WillRaise(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
class procedure WillRaiseWithMessage(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass = nil; const exceptionMsg: string = ''; const msg : string = ''); overload;
class procedure WillRaise(const AMethod : TTestMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
class procedure WillNotRaise(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
class procedure WillNotRaise(const AMethod : TTestMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
class procedure Contains(const theString : string; const subString : string; const ignoreCase : boolean = true; const message : string = '');overload;
class procedure StartsWith(const theString : string; const subString : string;const ignoreCase : boolean = true; const message : string = '');
class procedure EndsWith(const theString : string; const subString : string;const ignoreCase : boolean = true; const message : string = '');
class procedure InheritsFrom(const descendant : TClass; const parent : TClass; const message : string = '');
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure IsType<T>(const value : T; const message : string = '');overload;
{$ENDIF}
{$IFDEF SUPPORTS_REGEX}
class procedure IsMatch(const regexPattern : string; const theString : string; const message : string = '');
{$ENDIF}
end;
{$M+}
ITestFixtureInfo = interface;
{$M-}
{$M+}
ITestInfo = interface
['{FF61A6EB-A76B-4BE7-887A-598EBBAE5611}']
function GetName : string;
function GetFullName : string;
function GetActive : boolean;
function GetTestFixture : ITestFixtureInfo;
function GetTestStartTime : TDateTime;
function GetTestEndTime : TDateTime;
function GetTestDuration : TTimeSpan;
function GetEnabled : boolean;
procedure SetEnabled(const value : boolean);
property Name : string read GetName;
property FullName : string read GetFullName;
property Enabled : boolean read GetEnabled write SetEnabled;
property Active : boolean read GetActive;
property Fixture : ITestFixtureInfo read GetTestFixture;
end;
ITestInfoList = interface(IList<ITestInfo>)
['{1C614DC2-537F-4229-863D-669E4211074E}']
end;
TTestInfoList = class(TDUnitXList<ITestInfo>, ITestInfoList);
{$M-}
{$M+}
ITestFixtureInfo = interface
['{9E98B1E8-583A-49FC-B409-9B6937E22E81}']
function GetName : string;
function GetNameSpace : string;
function GetFullName : string;
function GetDescription : string;
function GetTests : IList<ITestInfo>;
function GetTestClass : TClass;
function GetSetupMethodName : string;
function GetSetupFixtureMethodName : string;
function GetTearDownMethodName : string;
function GetTearDownFixtureMethodName : string;
function GetTestInOwnThread : boolean;
function GetHasChildren : boolean;
function GetTestCount : cardinal;
function GetActiveTestCount : cardinal;
property Name : string read GetName;
property NameSpace : string read GetNameSpace;
property FullName : string read GetFullName;
property Description : string read GetDescription;
property HasChildFixtures : boolean read GetHasChildren;
property TestClass : TClass read GetTestClass;
property Tests : IList<ITestInfo> read GetTests;
property SetupMethodName : string read GetSetupMethodName;
property SetupFixtureMethodName : string read GetSetupFixtureMethodName;
property TearDownMethodName : string read GetTearDownMethodName;
property TearDownFixtureMethodName : string read GetTearDownFixtureMethodName;
property TestInOwnThread : boolean read GetTestInOwnThread;
property TestCount : cardinal read GetTestCount;
property ActiveTestCount : cardinal read GetActiveTestCount;
end;
ITestFixtureInfoList = interface(IList<ITestFixtureInfo>)
['{DEE229E7-1450-4DC1-BEEA-562461439084}']
end;
{$M-}
TTestFixtureInfoList = class(TDUnitXList<ITestFixtureInfo>, ITestFixtureInfoList);
{$M+}
IResult = interface
['{AEA1E458-157B-4B3A-9474-44EDFB3EE7A1}']
function GetStartTime : TDateTime;
function GetFinishTime : TDateTime;
function GetDuration : TTimeSpan;
//Timing
property StartTime : TDateTime read GetStartTime;
property FinishTime : TDateTime read GetFinishTime;
property Duration : TTimeSpan read GetDuration;
end;
{$M-}
{$SCOPEDENUMS ON}
TTestResultType = (Pass,Failure,Error,Ignored,MemoryLeak);
{$M+}
ITestResult = interface(IResult)
['{EFD44ABA-4F3E-435C-B8FC-1F8EB4B35A3B}']
function GetTest : ITestInfo;
function GetResult : boolean;
function GetResultType : TTestResultType;
function GetMessage : string;
function GetStackTrace : string;
//Test
property Test : ITestInfo read GetTest;
//Results
property Result : boolean read GetResult;
property ResultType : TTestResultType read GetResultType;
property Message : string read GetMessage;
property StackTrace : string read GetStackTrace;
end;
{$M-}
ITestError = interface(ITestResult)
['{375941C6-CEFD-44E5-9646-30D7915B8A71}']
function GetExceptionClass : ExceptClass;
function GetExceptionMessage : string;
function GetExceptionLocationInfo : string;
function GetExceptionAddressInfo : string;
property ExceptionClass : ExceptClass read GetExceptionClass;
property ExceptionMessage : string read GetExceptionMessage;
property ExceptionLocationInfo : string read GetExceptionLocationInfo;
property ExceptionAddressInfo : string read GetExceptionAddressInfo;
end;
IFixtureResult = interface(IResult)
['{7264579D-495E-4E00-A15D-751E6A65BEF6}']
function GetErrorCount : integer;
function GetFailureCount : integer;
function GetIgnoredCount : integer;
function GetPassCount : integer;
function GetHasFailures : boolean;
function GetTestResultCount : integer;
function GetChildCount : integer;
function GetFixture : ITestFixtureInfo;
function GetTestResults : IList<ITestResult>;
function GetChildren : IList<IFixtureResult>;
function GetFailures : IList<ITestResult>;
function GetErrors : IList<ITestError>;
function GetPasses : IList<ITestResult>;
function GetName : string;
function GetNamespace : string;
procedure Reduce;
property HasFailures : Boolean read GetHasFailures;
property FailureCount : integer read GetFailureCount;
property ErrorCount : integer read GetErrorCount;
property IgnoredCount : integer read GetIgnoredCount;
property PassCount : integer read GetPassCount;
property ResultCount : integer read GetTestResultCount;
property ChildCount : integer read GetChildCount;
property Name : string read GetName;
property Namespace : string read GetNamespace;
property Fixture : ITestFixtureInfo read GetFixture;
property Children : IList<IFixtureResult> read GetChildren;
property TestResults : IList<ITestResult> read GetTestResults;
property Failures : IList<ITestResult> read GetFailures;
property Errors : IList<ITestError> read GetErrors;
property Pesses : IList<ITestResult> read GetPasses;
end;
{$M+}
IRunResults = interface(IResult)
['{4A335B76-33E3-48FD-87DF-9462428C60DA}']
function GetFixtureCount : integer;
function GetTestCount : integer;
function GetAllPassed : boolean;
function GetFailureCount : integer;
function GetErrorCount : integer;
function GetMemoryLeakCount : Integer;
function GetPassCount : integer;
function GetIgnoredCount : integer;
function GetSuccessRate : integer;
function GetFixtureResults : IEnumerable<IFixtureResult>;
function GetAllTestResults : IEnumerable<ITestResult>;
function ToString : string;
property TestCount : integer read GetTestCount;
property FixtureCount : integer read GetFixtureCount;
property FailureCount : integer read GetFailureCount;
property ErrorCount : integer read GetErrorCount;
property IgnoredCount : integer read GetIgnoredCount;
property PassCount : integer read GetPassCount;
property MemoryLeakCount : integer read GetMemoryLeakCount;
property SuccessRate : integer read GetSuccessRate;
//means all enabled/not ingored tests passed.
property AllPassed : boolean read GetAllPassed;
property FixtureResults : IEnumerable<IFixtureResult> read GetFixtureResults;
end;
{$M-}
ITestLogger = interface
['{AADCA392-421C-4060-8D47-79D7CAAB0EEF}']
/// <summary>
/// Called at the start of testing. The default console logger prints the
/// DUnitX banner.
/// </summary>
procedure OnTestingStarts(const threadId, testCount, testActiveCount : Cardinal);
/// <summary>
/// //Called before a Fixture is run.
/// </summary>
procedure OnStartTestFixture(const threadId : Cardinal; const fixture : ITestFixtureInfo);
/// <summary>
/// //Called before a fixture Setup method is run
/// </summary>
procedure OnSetupFixture(const threadId : Cardinal; const fixture : ITestFixtureInfo);
/// <summary>
/// Called after a fixture setup method is run.
/// </summary>
procedure OnEndSetupFixture(const threadId : Cardinal; const fixture : ITestFixtureInfo);
/// <summary>
/// Called before a Test method is run.
/// </summary>
procedure OnBeginTest(const threadId : Cardinal;const Test: ITestInfo);
/// <summary>
/// Called before a test setup method is run.
/// </summary>
procedure OnSetupTest(const threadId : Cardinal;const Test: ITestInfo);
/// <summary>
/// Called after a test setup method is run.
/// </summary>
procedure OnEndSetupTest(const threadId : Cardinal;const Test: ITestInfo);
/// <summary>
/// Called before a Test method is run.
/// </summary>
procedure OnExecuteTest(const threadId : Cardinal;const Test: ITestInfo);
/// <summary>
/// Called when a test succeeds
/// </summary>
procedure OnTestSuccess(const threadId : Cardinal;const Test: ITestResult);
/// <summary>
/// Called when a test errors.
/// </summary>
procedure OnTestError(const threadId : Cardinal;const Error: ITestError);
/// <summary>
/// Called when a test fails.
/// </summary>
procedure OnTestFailure(const threadId : Cardinal;const Failure: ITestError);
/// <summary>
/// //called when a test is ignored.
/// </summary>
procedure OnTestIgnored(const threadId : Cardinal; const AIgnored: ITestResult);
/// <summary>
/// //called when a test memory leaks.
/// </summary>
procedure OnTestMemoryLeak(const threadId : Cardinal; const Test: ITestResult);
/// <summary>
/// //allows tests to write to the log.
/// </summary>
procedure OnLog(const logType : TLogLevel; const msg : string);
/// <summary>
/// //called before a Test Teardown method is run.
/// </summary>
procedure OnTeardownTest(const threadId : Cardinal;const Test: ITestInfo);
/// <summary>
/// //called after a test teardown method is run.
/// </summary>
procedure OnEndTeardownTest(const threadId : Cardinal; const Test: ITestInfo);
/// <summary>
/// //called after a test method and teardown is run.
/// </summary>
procedure OnEndTest(const threadId : Cardinal;const Test: ITestResult);
/// <summary>
/// //called before a Fixture Teardown method is called.
/// </summary>
procedure OnTearDownFixture(const threadId : Cardinal; const fixture : ITestFixtureInfo);
/// <summary>
/// //called after a Fixture Teardown method is called.
/// </summary>
procedure OnEndTearDownFixture(const threadId : Cardinal; const fixture : ITestFixtureInfo);
/// <summary>
/// //called after a Fixture has run.
/// </summary>
procedure OnEndTestFixture(const threadId : Cardinal; const results : IFixtureResult);
/// <summary>
/// //called after all fixtures have run.
/// </summary>
procedure OnTestingEnds(const RunResults: IRunResults);
end;
TRunnerExitBehavior = (Continue, //The runner will exit normally
Pause, //The runner will pause after displaying it's results
HaltOnFailures //??
);
ITestRunner = interface
['{06C0D8D2-B2D7-42F9-8D23-8F2D8A75263F}']
procedure AddLogger(const value : ITestLogger);
function GetUseCommandLineOptions : boolean;
procedure SetUseCommandLineOptions(const value : boolean);
function GetExitBehavior : TRunnerExitBehavior;
procedure SetExitBehavior(const value : TRunnerExitBehavior);
function GetUseRTTI : boolean;
procedure SetUseRTTI(const value : boolean);
//This is exposed for the GUI Runner cast as ITestFixtureList.
function BuildFixtures : IInterface;
function Execute : IRunResults;
property ExitBehavior : TRunnerExitBehavior read GetExitBehavior write SetExitBehavior;
property UseCommandLineOptions : boolean read GetUseCommandLineOptions write SetUseCommandLineOptions;
procedure Log(const logType : TLogLevel; const msg : string);overload;
procedure Log(const msg : string);overload;
//for backwards compatibilty with DUnit tests.
procedure Status(const msg : string);
//redirects WriteLn to our loggers.
procedure WriteLn(const msg : string);overload;
procedure WriteLn;overload;
/// <summary>
/// When true, test fixtures will be found by using RTTI to search for
/// classes decorated as TestFixtures. Note for this to work you may
/// need to use {$STRONGLINKTYPES ON} otherwise the classes may not get
/// linked as they are not referenced. When False you will need to
/// register the fixtures using TDUnitX.RegisterTestFixture
/// </summary>
property UseRTTI : boolean read GetUseRTTI write SetUseRTTI;
end;
ICommandLine = interface
['{A86D66B3-2CD3-4E11-B0A8-9602EB5790CB}']
function GetHideBanner : boolean;
procedure SetHideBanner(const value : boolean);
function GetLogLevel : TLogLevel;
function HasOption(const optionName : string) : boolean;
function GetOptionValue(const optionName : string) : string;
//standard options
property HideBanner : boolean read GetHideBanner write SetHideBanner; // -b
property LogLevel : TLogLevel read GetLogLevel; // -l:e -l:w -l:i
end;
TDUnitX = class
public class var
RegisteredFixtures : TDictionary<TClass,string>;
RegisteredPlugins : TList<IPlugin>;
public
class constructor Create;
class destructor Destroy;
class function CreateRunner : ITestRunner;overload;
class function CreateRunner(const useCommandLineOptions : boolean) : ITestRunner;overload;
class function CreateRunner(const ALogger : ITestLogger) : ITestRunner;overload;
class function CreateRunner(const useCommandLineOptions : boolean; const ALogger : ITestLogger) : ITestRunner;overload;
class procedure RegisterTestFixture(const AClass : TClass; const AName : string = '' );
class procedure RegisterPlugin(const plugin : IPlugin);
class function CommandLine : ICommandLine;
class function CurrentRunner : ITestRunner;
end;
// Register an implementation via TDUnitXIoC.DefaultContainer
IStacktraceProvider = interface
['{382288B7-932C-4B6E-8417-660FFCA849EB}']
function GetStackTrace(const ex: Exception; const exAddressAddress: Pointer) : string;
function PointerToLocationInfo(const Addrs: Pointer): string;
function PointerToAddressInfo(Addrs: Pointer): string;
end;
IMemoryLeakMonitor = interface
['{A374A4D0-9BF6-4E01-8A29-647F92CBF41C}']
procedure PreSetup;
procedure PostSetUp;
procedure PreTest;
procedure PostTest;
procedure PreTearDown;
procedure PostTearDown;
function SetUpMemoryAllocated: Int64;
function TearDownMemoryAllocated: Int64;
function TestMemoryAllocated: Int64;
end;
ETestFrameworkException = class(Exception);
ENotImplemented = class(ETestFrameworkException);
//base exception for any internal exceptions which cause the test to stop
EAbort = class(ETestFrameworkException);
ETestFailure = class(EAbort);
ETestPass = class(EAbort);
ENoTestsRegistered = class(ETestFrameworkException);
{$IFDEF DELPHI_XE_DOWN}
function ReturnAddress: Pointer; assembler;
{$ENDIF}
implementation
uses
DUnitX.TestRunner,
DUnitX.Utils,
DUnitX.Commandline,
DUnitX.IoC,
DUnitX.MemoryLeakMonitor.Default,
DUnitX.FixtureProviderPlugin,
Variants,
Math,
StrUtils,
Types,
{$IFDEF SUPPORTS_REGEX}
RegularExpressions,
{$ENDIF}
Generics.Defaults;
function IsBadPointer(P: Pointer):Boolean;register;
begin
try
Result := (p = nil) or ((Pointer(P^) <> P) and (Pointer(P^) = P));
except
Result := true;
end
end;
{$IFDEF DELPHI_XE_DOWN}
function ReturnAddress: Pointer; assembler;
const
CallerIP = $4;
asm
mov eax, ebp
call IsBadPointer
test eax,eax
jne @@Error
mov eax, [ebp].CallerIP
sub eax, 5 // 5 bytes for call
push eax
call IsBadPointer
test eax,eax
pop eax
je @@Finish
@@Error:
xor eax, eax
@@Finish:
end;
{$ENDIF}
{ TestFixture }
constructor TestFixtureAttribute.Create(const AName: string);
begin
FName := AName;
end;
constructor TestFixtureAttribute.Create;
begin
end;
constructor TestFixtureAttribute.Create(const AName: string; const ADescription : string);
begin
FName := AName;
FDescription := ADescription;
end;
{ Assert }
class procedure Assert.AreEqual(const left, right, tolerance: Extended; const message: string);
begin
if not Math.SameValue(left,right,tolerance) then
Fail(Format('left %g but got %g - %s' ,[left,right,message]), ReturnAddress);
end;
class procedure Assert.AreEqual(const left, right: TClass; const message: string);
var
msg : string;
begin
if left <> right then
begin
msg := ' is not equal to ';
if left = nil then
msg := 'nil' + msg
else
msg := left.ClassName + msg;
if right = nil then
msg := msg + 'nil'
else
msg := msg + right.ClassName;
if message <> '' then
msg := msg + '. ' + message;
Fail(msg, ReturnAddress);
end;
end;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure Assert.AreEqual<T>(const left, right: T; const message: string);
var
comparer : IComparer<T>;
leftvalue, rightvalue : TValue;
pInfo : PTypeInfo;
tInfo : TValue;
begin
comparer := TComparer<T>.Default;
if comparer.Compare(right,left) <> 0 then
begin
leftValue := TValue.From<T>(left);
rightValue := TValue.From<T>(right);
pInfo := TypeInfo(string);
if leftValue.IsEmpty or rightvalue.IsEmpty then
Fail(Format('left is not equal to right - %s', [message]), ReturnAddress)
else
begin
if leftValue.TryCast(pInfo,tInfo) then
Fail(Format('left %s but got %s - %s', [leftValue.AsString, rightValue.AsString, message]), ReturnAddress)
else
Fail(Format('left is not equal to right - %s', [message]), ReturnAddress)
end;
end;
end;
{$ENDIF}
class function Assert.AddLineBreak(const msg: string): string;
begin
if msg <> '' then
Result := sLineBreak + msg
else
Result := '';
end;
class procedure Assert.AreEqual(const left, right: Integer; const message: string);
begin
if left <> right then
Fail(Format('left %d but got %d - %s' ,[left, right, message]), ReturnAddress);
end;
class procedure Assert.AreEqual(const left, right: Extended; const message: string);
begin
AreEqual(left, right, 0, message);
end;
class procedure Assert.AreEqualMemory(const left : Pointer; const right : Pointer; const size : Cardinal; message : string);
begin
if not CompareMem(left, right, size) then
Fail('Memory values are not equal. ' + message, ReturnAddress);
end;
class procedure Assert.AreNotEqual(const left, right, tolerance: Extended; const message: string);
begin
if Math.SameValue(left, right, tolerance) then
Fail(Format('%g equals right %g %s' ,[left,right,message]), ReturnAddress);
end;
class procedure Assert.AreNotEqual(const left, right: string;const ignoreCase: boolean; const message: string);
function AreNotEqualText(const left, right: string; const ignoreCase: boolean): boolean;
begin
if ignoreCase then
Result := SameText(left, right)
else
Result := SameStr(left, right);
end;
begin
if AreNotEqualText(left, right, ignoreCase) then
Fail(Format('[%s] is Equal to [%s] %s', [left, right, message]), ReturnAddress);
end;
class procedure Assert.AreNotEqual(const left, right: TClass; const message: string);
var
msg : string;
begin
if left = right then
begin
msg := ' is equal to ';
if left = nil then
msg := 'nil' + msg
else
msg := left.ClassName + msg;
if right = nil then
msg := msg + 'nil'
else
msg := msg + right.ClassName;
if message <> '' then
msg := msg + '. ' + message;
Fail(msg, ReturnAddress);
end;
end;
{$IFDEF DELPHI_XE_UP}