forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DUnitX.Assert.pas
1123 lines (984 loc) · 37.1 KB
/
DUnitX.Assert.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) 2015 Vincent Parrett & Contributors }
{ }
{ 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. }
{ }
{***************************************************************************}
unit DUnitX.Assert;
interface
{$I DUnitX.inc}
uses
{$IFDEF USE_NS}
System.Classes,
System.SysUtils;
{$ELSE}
Classes,
SysUtils;
{$ENDIF}
type
TTestLocalMethod = TProc;
TTestMethod = procedure of object;
Assert = class
private
class var fOnAssert: TProc;
class var fTestFailure: ExceptClass;
class var fTestPass: ExceptClass;
class procedure CheckExceptionClass(E: Exception; const exceptionClass: ExceptClass);
class procedure CheckExceptionClassDescendant(E: Exception; const exceptionClass: ExceptClass);
class function AddLineBreak(const msg: string): string;
class procedure DoAssert; inline;
public
class procedure Pass(const message : string = '');
class procedure Fail(const message : string = ''; const errorAddrs : pointer = nil);
class procedure FailFmt(const message : string; const args: array of const; const errorAddrs : pointer = nil);
class procedure NotImplemented;
class procedure AreEqual(const expected : string; const actual : string; const ignoreCase : boolean; const message : string = '');overload;
class procedure AreEqual(const expected : string; const actual : string; const message : string = '');overload;
class procedure AreEqual(const expected, actual : Double; const tolerance : Double; const message : string = '');overload;
class procedure AreEqual(const expected, actual : Double; const message : string = '');overload;
class procedure AreEqual(const expected, actual : Extended; const tolerance : Extended; const message : string = '');overload;
class procedure AreEqual(const expected, actual : Extended; const message : string = '');overload;
class procedure AreEqual(const expected, actual : TClass; const message : string = '');overload;
{$IFNDEF DELPHI_XE_DOWN}
//Delphi 2010 and XE compiler bug breaks this
class procedure AreEqual<T>(const expected, actual : T; const message : string = '');overload;
{$ENDIF}
class procedure AreEqual(const expected, actual : Integer; const message : string = '');overload;
class procedure AreEqual(const expected, actual : cardinal; const message : string = '');overload;
class procedure AreEqual(const expected, actual : boolean; const message : string = '');overload;
class procedure AreEqualMemory(const expected : Pointer; const actual : Pointer; const size : Cardinal; const message : string = '');
class procedure AreNotEqual(const expected : string; const actual : string; const ignoreCase : boolean = true; const message : string = '');overload;
class procedure AreNotEqual(const expected, actual : Extended; const tolerance : Extended; const message : string = '');overload;
class procedure AreNotEqual(const expected, actual : Extended; const message : string = '');overload;
class procedure AreNotEqual(const expected, actual : Double; const tolerance : double; const message : string = '');overload;
class procedure AreNotEqual(const expected, actual : Double; const message : string = '');overload;
class procedure AreNotEqual(const expected, actual : TClass; const message : string = '');overload;
{$IFNDEF DELPHI_XE_DOWN}
//Delphi 2010 and XE compiler bug breaks this
class procedure AreNotEqual<T>(const expected, actual : T; const message : string = '');overload;
{$ENDIF}
class procedure AreNotEqual(const expected, actual : Integer; const message : string = '');overload;
class procedure AreNotEqualMemory(const expected : Pointer; const actual : Pointer; const size : Cardinal; const message : string = '');
class procedure AreSame(const expected, actual : TObject; const message : string = '');overload;
class procedure AreSame(const expected, actual : IInterface; const message : string = '');overload;
class procedure AreNotSame(const expected, actual : TObject; const message : string = '');overload;
class procedure AreNotSame(const expected, actual : 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 Contains<T>(const arr : array of T; const value : T; const message : string = '');overload;
class procedure DoesNotContain<T>(const list : IEnumerable<T>; const value : T; const message : string = '');overload;
class procedure DoesNotContain<T>(const arr : array of T; const value : T; const message : string = '');overload;
{$ENDIF}
class function Implements<T : IInterface>(value : IInterface; const message : string = '' ) : T;
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}
/// <summary>
/// Checks that an exception exactly matching ExceptClass will be raised.
/// </summary>
class procedure WillRaise(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception exactly matching ExceptClass and Message will be raised.
/// </summary>
class procedure WillRaiseWithMessage(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass = nil; const exceptionMsg: string = ''; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception exactly matching ExceptClass will be raised.
/// </summary>
class procedure WillRaise(const AMethod : TTestMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception that descends from ExceptClass will be raised.
/// </summary>
class procedure WillRaiseDescendant(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception that descends from ExceptClass will be raised.
/// </summary>
class procedure WillRaiseDescendant(const AMethod : TTestMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception will be raised.
/// </summary>
class procedure WillRaiseAny(const AMethod : TTestLocalMethod; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception will be raised.
/// </summary>
class procedure WillRaiseAny(const AMethod : TTestMethod; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception exactly matching ExceptClass will not be raised.
/// </summary>
class procedure WillNotRaise(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception exactly matching ExceptClass will not be raised.
/// </summary>
class procedure WillNotRaise(const AMethod : TTestMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception that descends from ExceptClass not will be raised.
/// </summary>
class procedure WillNotRaiseDescendant(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception that descends from ExceptClass not will be raised.
/// </summary>
class procedure WillNotRaiseDescendant(const AMethod : TTestMethod; const exceptionClass : ExceptClass = nil; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception of Any type not will be raised. This method
/// is to complement <see cref="DUnitX.TestFramework|Assert.WillRaiseAny(TTestLocalMethod,string)">
/// WillRaiseAny</see> method, and is not required, as the default behavior of a test
/// is to fail when any exception is raised.
/// </summary>
class procedure WillNotRaiseAny(const AMethod : TTestLocalMethod; const msg : string = ''); overload;
/// <summary>
/// Checks that an exception of Any type not will be raised. This method
/// is to complement <see cref="DUnitX.TestFramework|Assert.WillRaiseAny(TTestMethod,string)">
/// WillRaiseAny</see> method, and is not required, as the default behavior of a test
/// is to fail when any exception is raised.
/// </summary>
class procedure WillNotRaiseAny(const AMethod : TTestMethod; const msg : string = ''); overload;
class procedure Contains(const theString : string; const subString : string; const ignoreCase : boolean = true; const message : string = '');overload;
class procedure DoesNotContain(const theString : string; const subString : string; const ignoreCase : boolean = true; const message : string = '');overload;
class procedure StartsWith(const subString : string; const theString : string;const ignoreCase : boolean = true; const message : string = '');
class procedure EndsWith(const subString : string; const theString : 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;deprecated 'use inheritsfrom - istype is useless';
{$ENDIF}
{$IFDEF SUPPORTS_REGEX}
class procedure IsMatch(const regexPattern : string; const theString : string; const message : string = '');
{$ENDIF}
class property OnAssert: TProc read fOnAssert write fOnAssert;
class property TestFailure: ExceptClass read fTestFailure write fTestFailure;
class property TestPass: ExceptClass read fTestPass write fTestPass;
end;
{$IFDEF DELPHI_XE_DOWN}
function ReturnAddress: Pointer; assembler;
{$ENDIF}
implementation
uses
DUnitX.ResStrs,
Generics.Defaults,
{$IFDEF SUPPORTS_REGEX}
System.RegularExpressions,
{$ENDIF}
{$IFDEF USE_NS}
System.Math,
System.Rtti,
System.StrUtils,
System.TypInfo,
System.Variants;
{$ELSE}
Math,
Rtti,
StrUtils,
TypInfo,
Variants;
{$ENDIF}
{$IFDEF DELPHI_XE_DOWN}
function IsBadPointer(P: Pointer):Boolean;register;
begin
try
Result := (p = nil) or ((Pointer(P^) <> P) and (Pointer(P^) = P));
except
Result := true;
end
end;
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}
{ Assert }
class procedure Assert.AreEqual(const expected, actual, tolerance: Extended; const message: string);
begin
DoAssert;
if not {$IFDEF USE_NS}System.Math.{$ENDIF}SameValue(expected,actual,tolerance) then
FailFmt(SUnexpectedErrorExt ,[expected,actual,message], ReturnAddress);
end;
class procedure Assert.AreEqual(const expected, actual: TClass; const message: string);
var
msg : string;
begin
DoAssert;
if expected <> actual then
begin
msg := ' is not equal to ';
if expected = nil then
msg := 'nil' + msg
else
msg := expected.ClassName + msg;
if actual = nil then
msg := msg + 'nil'
else
msg := msg + actual.ClassName;
if message <> '' then
msg := msg + '. ' + message;
Fail(msg, ReturnAddress);
end;
end;
{$IFNDEF DELPHI_XE_DOWN}
//Delphi 2010 and XE compiler bug breaks this
class procedure Assert.AreEqual<T>(const expected, actual: T; const message: string);
var
comparer : IComparer<T>;
expectedValue, actualValue : TValue;
begin
DoAssert;
comparer := TComparer<T>.Default;
if comparer.Compare(actual,expected) <> 0 then
begin
expectedValue := TValue.From<T>(expected);
actualValue := TValue.From<T>(actual);
FailFmt(SNotEqualErrorStr, [expectedValue.ToString, actualValue.ToString, message], ReturnAddress)
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 expected, actual: Integer; const message: string);
begin
DoAssert;
if expected <> actual then
FailFmt(SUnexpectedErrorInt ,[expected, actual, message], ReturnAddress);
end;
class procedure Assert.AreEqual(const expected, actual: boolean; const message: string);
begin
DoAssert;
if expected <> actual then
FailFmt(SUnexpectedErrorStr ,[BoolToStr(expected, true), BoolToStr(actual, true), message], ReturnAddress);
end;
class procedure Assert.AreEqual(const expected, actual: cardinal; const message: string);
begin
DoAssert;
if expected <> actual then
FailFmt(SUnexpectedErrorInt ,[expected, actual, message], ReturnAddress);
end;
class procedure Assert.AreEqual(const expected, actual, tolerance: Double; const message: string);
begin
DoAssert;
if not {$IFDEF USE_NS}System.Math.{$ENDIF}SameValue(expected,actual,tolerance) then
FailFmt(SUnexpectedErrorDbl ,[expected,actual,message], ReturnAddress);
end;
class procedure Assert.AreEqual(const expected, actual: Double; const message: string);
var
tolerance : Double;
begin
tolerance := 0;
AreEqual(expected, actual, tolerance, message);
end;
class procedure Assert.AreEqual(const expected, actual: Extended; const message: string);
var
tolerance : Extended;
begin
tolerance := 0;
AreEqual(expected, actual, tolerance, message);
end;
class procedure Assert.AreEqualMemory(const expected : Pointer; const actual : Pointer; const size : Cardinal; const message : string);
begin
DoAssert;
if not CompareMem(expected, actual, size) then
Fail(SMemoryValuesNotEqual + message, ReturnAddress);
end;
class procedure Assert.AreNotEqual(const expected, actual, tolerance: Extended; const message: string);
begin
DoAssert;
if {$IFDEF USE_NS}System.Math.{$ENDIF}SameValue(expected, actual, tolerance) then
FailFmt(SEqualsErrorExt ,[expected,actual,message], ReturnAddress);
end;
class procedure Assert.AreNotEqual(const expected, actual: string;const ignoreCase: boolean; const message: string);
function AreNotEqualText(const expected, actual: string; const ignoreCase: boolean): boolean;
begin
if ignoreCase then
Result := SameText(expected, actual)
else
Result := SameStr(expected, actual);
end;
begin
DoAssert;
if AreNotEqualText(expected, actual, ignoreCase) then
FailFmt(SEqualsErrorStr, [expected, actual, message], ReturnAddress);
end;
class procedure Assert.AreNotEqual(const expected, actual: TClass; const message: string);
var
msg : string;
begin
DoAssert;
if expected = actual then
begin
msg := ' is equal to ';
if expected = nil then
msg := 'nil' + msg
else
msg := expected.ClassName + msg;
if actual = nil then
msg := msg + 'nil'
else
msg := msg + actual.ClassName;
if message <> '' then
msg := msg + '. ' + message;
Fail(msg, ReturnAddress);
end;
end;
{$IFNDEF DELPHI_XE_DOWN}
//Delphi 2010 and XE compiler bug breaks this
class procedure Assert.AreNotEqual<T>(const expected, actual: T; const message: string);
var
comparer : IComparer<T>;
expectedValue, actualValue : TValue;
begin
DoAssert;
comparer := TComparer<T>.Default;
if comparer.Compare(actual,expected) = 0 then
begin
expectedValue := TValue.From<T>(expected);
actualValue := TValue.From<T>(actual);
FailFmt(SEqualsErrorStr2,[expectedValue.ToString, actualValue.ToString, message], ReturnAddress);
end;
end;
{$ENDIF}
class procedure Assert.AreNotEqual(const expected, actual: Integer; const message: string);
begin
DoAssert;
if expected = actual then
FailFmt(SEqualsErrorInt ,[expected, actual, message], ReturnAddress);
end;
class procedure Assert.AreNotEqual(const expected, actual: Extended; const message: string);
var
tolerance : Extended;
begin
tolerance := 0;
Assert.AreNotEqual(expected,actual,tolerance,message);
end;
class procedure Assert.AreNotEqual(const expected, actual: Double; const message: string);
var
tolerance : double;
begin
tolerance := 0;
Assert.AreNotEqual(expected,actual,tolerance,message);
end;
class procedure Assert.AreNotEqual(const expected, actual, tolerance: double; const message: string);
begin
DoAssert;
if {$IFDEF USE_NS}System.Math.{$ENDIF}SameValue(expected, actual, tolerance) then
FailFmt(SEqualsErrorDbl ,[expected,actual,message], ReturnAddress);
end;
class procedure Assert.AreNotEqualMemory(const expected, actual: Pointer; const size: Cardinal; const message: string);
begin
DoAssert;
if CompareMem(expected,actual, size) then
Fail(SMemoryValuesEqual + message, ReturnAddress);
end;
class procedure Assert.AreNotSame(const expected, actual: TObject; const message: string);
begin
DoAssert;
if expected.Equals(actual) then
FailFmt(SEqualsErrorObj, [expected.ToString,actual.ToString,message], ReturnAddress);
end;
class procedure Assert.AreNotSame(const expected, actual: IInterface; const message: string);
begin
DoAssert;
if (expected as IInterface) = (actual as IInterface) then
FailFmt(SEqualsErrorIntf,[message], ReturnAddress);
end;
class procedure Assert.AreSame(const expected, actual: IInterface; const message: string);
begin
DoAssert;
if (expected as IInterface) <> (actual as IInterface) then
FailFmt(SNotEqualErrorIntf,[message], ReturnAddress);
end;
class procedure Assert.AreSame(const expected, actual: TObject; const message: string);
begin
DoAssert;
if not expected.Equals(actual) then
FailFmt(SNotEqualErrorObj, [expected.ToString,actual.ToString,message], ReturnAddress);
end;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure Assert.Contains<T>(const list: IEnumerable<T>; const value: T; const message: string);
var
o : T;
comparer : IComparer<T>;
begin
DoAssert;
comparer := TComparer<T>.Default;
for o in list do
begin
if comparer.Compare(o,value) = 0 then
exit;
end;
FailFmt(SValueNotInList,[TValue.From<T>(value).ToString, message], ReturnAddress);
end;
class procedure Assert.Contains<T>(const arr : array of T; const value : T; const message : string = '');
var
o : T;
comparer : IComparer<T>;
begin
DoAssert;
comparer := TComparer<T>.Default;
for o in arr do
begin
if comparer.Compare(o,value) = 0 then
exit;
end;
FailFmt(SValueNotInList,[TValue.From<T>(value).ToString, message], ReturnAddress);
end;
{$ENDIF}
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure Assert.DoesNotContain<T>(const list: IEnumerable<T>; const value: T; const message: string);
var
o : T;
comparer : IComparer<T>;
begin
DoAssert;
comparer := TComparer<T>.Default;
for o in list do
begin
if comparer.Compare(o,value) = 0 then
FailFmt(SValueInList,[TValue.From<T>(value).ToString, message], ReturnAddress);
end;
end;
class procedure Assert.DoesNotContain<T>(const arr : array of T; const value : T; const message : string = '');
var
o : T;
comparer : IComparer<T>;
begin
DoAssert;
comparer := TComparer<T>.Default;
for o in arr do
begin
if comparer.Compare(o,value) = 0 then
FailFmt(SValueInList,[TValue.From<T>(value).ToString, message], ReturnAddress);
end;
end;
{$ENDIF}
class procedure Assert.Fail(const message : string; const errorAddrs : pointer);
begin
//If we have been given a return then use it. (makes the exception appear on level above in the stack)
if errorAddrs <> nil then
raise fTestFailure.Create(message) at errorAddrs
else
//Otherwise use the return address we can currently get to for where to raise the exception
raise fTestFailure.Create(message) at ReturnAddress;
end;
class procedure Assert.FailFmt(const message: string; const args: array of const; const errorAddrs: pointer);
begin
Fail(Format(message, args), errorAddrs);
end;
class procedure Assert.Pass(const message: string);
begin
raise fTestPass.Create(message);
end;
class function Assert.Implements<T>(value: IInterface; const message: string) : T;
begin
DoAssert;
if not Supports(value,GetTypeData(TypeInfo(T)).Guid,result) then
FailFmt(SIntfNotImplemented, [GetTypeName(TypeInfo(T)), message],ReturnAddress);
end;
class procedure Assert.InheritsFrom(const descendant, parent: TClass; const message: string);
var
msg : string;
begin
DoAssert;
if (descendant = nil) or (parent = nil) or (not descendant.InheritsFrom(parent)) then
begin
msg := ' does not inherit from ';
if descendant = nil then
msg := 'nil' + msg
else
msg := descendant.ClassName + msg;
if parent = nil then
msg := msg + 'nil'
else
msg := parent.ClassName + msg;
msg := msg + '.';
if True then
if message <> '' then
msg := msg + ' ' + message;
Fail(msg, ReturnAddress);
end;
end;
class procedure Assert.IsEmpty(const value: IInterfaceList; const message: string);
begin
DoAssert;
if value.Count > 0 then
FailFmt(SListNotEmpty,[message], ReturnAddress);
end;
class procedure Assert.IsEmpty(const value: TList; const message: string);
begin
DoAssert;
if value.Count > 0 then
FailFmt(SListNotEmpty,[message], ReturnAddress);
end;
class procedure Assert.IsEmpty(const value, message: string);
begin
DoAssert;
if Length(value) > 0 then
FailFmt(SStrNotEmpty,[message], ReturnAddress);
end;
class procedure Assert.IsEmpty(const value: Variant; const message: string);
begin
DoAssert;
if VarIsEmpty(value) or VarIsNull(value) then
FailFmt(SVarNotEmpty,[message], ReturnAddress);
end;
class procedure Assert.IsEmpty(const value: TStrings; const message: string);
begin
DoAssert;
if value.Count > 0 then
FailFmt(SListNotEmpty,[message], ReturnAddress);
end;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure Assert.IsEmpty<T>(const value: IEnumerable<T>; const message: string);
var
o : T;
count : integer;
begin
DoAssert;
count := 0;
for o in value do
Inc(count);
if count > 0 then
FailFmt(SListNotEmpty,[message], ReturnAddress);
end;
{$ENDIF}
class procedure Assert.IsFalse(const condition: boolean; const message: string);
begin
DoAssert;
if condition then
FailFmt(SIsFalseError,[message], ReturnAddress);
end;
class procedure Assert.IsNotEmpty(const value: TList; const message: string);
begin
DoAssert;
if value.Count = 0 then
FailFmt(SListEmpty, [message], ReturnAddress);
end;
class procedure Assert.IsNotEmpty(const value: IInterfaceList; const message: string);
begin
DoAssert;
if value.Count = 0 then
FailFmt(SListEmpty, [message], ReturnAddress);
end;
class procedure Assert.IsNotEmpty(const value: TStrings; const message: string);
begin
DoAssert;
if value.Count = 0 then
FailFmt(SListEmpty, [message], ReturnAddress);
end;
class procedure Assert.IsNotEmpty(const value, message: string);
begin
DoAssert;
if value = '' then
FailFmt(SVarEmpty,[message], ReturnAddress);
end;
class procedure Assert.IsNotEmpty(const value: Variant; const message: string);
begin
DoAssert;
if VarIsEmpty(value) then
FailFmt(SVarEmpty,[message], ReturnAddress);
end;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure Assert.IsNotEmpty<T>(const value: IEnumerable<T>; const message: string);
var
x : T;
count : integer;
begin
DoAssert;
count := 0;
for x in value do
Inc(count);
if count = 0 then
FailFmt(SListEmpty,[message], ReturnAddress);
end;
{$ENDIF}
class procedure Assert.IsNotNull(const condition: IInterface; const message: string);
begin
DoAssert;
if condition = nil then
FailFmt(SIntfNil,[message], ReturnAddress);
end;
class procedure Assert.IsNotNull(const condition: Pointer; const message: string);
begin
DoAssert;
if condition = nil then
FailFmt(SPointerNil,[message], ReturnAddress);
end;
class procedure Assert.IsNotNull(const condition: TObject; const message: string);
begin
DoAssert;
if condition = nil then
FailFmt(SObjNil,[message], ReturnAddress);
end;
class procedure Assert.IsNotNull(const condition: Variant; const message: string);
begin
DoAssert;
if VarIsNull(condition) then
FailFmt(SVariantNull,[message], ReturnAddress);
end;
class procedure Assert.IsNull(const condition: Variant; const message: string);
begin
DoAssert;
if not VarIsNull(condition) then
FailFmt(SVariantNotNull,[message], ReturnAddress);
end;
class procedure Assert.IsNull(const condition: IInterface; const message: string);
begin
DoAssert;
if condition <> nil then
FailFmt(SIntfNotNil,[message], ReturnAddress);
end;
class procedure Assert.IsNull(const condition: TObject; const message: string);
begin
DoAssert;
if condition <> nil then
FailFmt(SObjNotNil,[message], ReturnAddress);
end;
class procedure Assert.IsNull(const condition: Pointer; const message: string);
begin
DoAssert;
if condition <> nil then
FailFmt(SPointerNotNil,[message], ReturnAddress);
end;
class procedure Assert.IsTrue(const condition: boolean;const message : string);
begin
DoAssert;
if not condition then
FailFmt(SIsTrueError,[message], ReturnAddress);
end;
class procedure Assert.NotImplemented;
begin
Assert.Fail(SNotImplemented);
end;
{$IFDEF DELPHI_XE_UP}
//Delphi 2010 compiler bug breaks this
class procedure Assert.IsType<T>(const value: T; const message : string);
var
val : TValue;
begin
DoAssert;
val := TValue.From<T>(value);
if not val.IsType<T> then
Fail(STypeError, ReturnAddress);
end;
{$ENDIF}
class procedure Assert.WillNotRaise(const AMethod: TTestMethod; const exceptionClass: ExceptClass; const msg: string);
begin
Assert.WillNotRaise(
procedure
begin
AMethod;
end,
exceptionClass, msg);
end;
class procedure Assert.WillNotRaiseAny(const AMethod: TTestLocalMethod; const msg: string);
begin
DoAssert;
try
AMethod;
except
on e : TObject do // For those who throw exceptions not descending from Exception.
begin
if e is Exception then
begin
FailFmt(SUnexpectedException, [e.ClassName, exception(e).message], ReturnAddress);
end
else
FailFmt(SUnexpectedExceptionAlt, [e.ClassName], ReturnAddress);
end;
end;
end;
class procedure Assert.WillNotRaiseAny(const AMethod: TTestMethod;const msg: string);
begin
Assert.WillNotRaiseAny(
procedure
begin
AMethod;
end,
msg);
end;
class procedure Assert.WillNotRaiseDescendant(const AMethod: TTestLocalMethod; const exceptionClass: ExceptClass; const msg: string);
begin
DoAssert;
try
AMethod;
except
on e : Exception do
begin
if exceptionClass <> nil then
begin
if e is exceptionClass then
Fail(SMethodRaisedException + exceptionClass.ClassName + sLineBreak + e.Message + AddLineBreak(msg), ReturnAddress);
end
else
FailFmt(SMethodRaisedExceptionAlt, [e.ClassName, exceptionClass.ClassName, e.message], ReturnAddress);
end;
end;
end;
class procedure Assert.WillNotRaiseDescendant(const AMethod: TTestMethod; const exceptionClass: ExceptClass; const msg: string);
begin
Assert.WillNotRaise(
procedure
begin
AMethod;
end,
exceptionClass, msg);
end;
class procedure Assert.WillRaise(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass; const msg : string);
begin
DoAssert;
try
AMethod;
except
on E: Exception do
begin
CheckExceptionClass(e, exceptionClass);
Exit;
end;
end;
Fail(SNoException + AddLineBreak(msg), ReturnAddress);
end;
class procedure Assert.WillNotRaise(const AMethod : TTestLocalMethod; const exceptionClass : ExceptClass; const msg : string);
begin
DoAssert;
try
AMethod;
except
on e : Exception do
begin
if exceptionClass <> nil then
begin
if e.ClassType = exceptionClass then
Fail(SMethodRaisedException + exceptionClass.ClassName + sLineBreak + e.Message + AddLineBreak(msg), ReturnAddress);
end
else
FailFmt(SUnexpectedException, [e.ClassName, e.message], ReturnAddress);
end;
end;
end;
class procedure Assert.WillRaise(const AMethod: TTestMethod; const exceptionClass: ExceptClass; const msg: string);
begin
Assert.WillRaise(
procedure
begin
AMethod;
end,
exceptionClass, msg);
end;
class procedure Assert.WillRaiseAny(const AMethod: TTestLocalMethod; const msg: string);
begin
DoAssert;
try
AMethod;
except
on E: Exception do
begin
Exit;
end;
end;
Fail(SNoException + AddLineBreak(msg), ReturnAddress);
end;
class procedure Assert.WillRaiseAny(const AMethod: TTestMethod; const msg: string);
begin
Assert.WillRaiseAny(
procedure
begin
AMethod;
end,
msg);
end;
class procedure Assert.WillRaiseDescendant(const AMethod: TTestLocalMethod; const exceptionClass: ExceptClass; const msg: string);
begin
DoAssert;
try
AMethod;
except
on E: Exception do
begin
CheckExceptionClassDescendant(e, exceptionClass);
Exit;
end;
end;
Fail(SNoException + AddLineBreak(msg), ReturnAddress);
end;
class procedure Assert.WillRaiseDescendant(const AMethod: TTestMethod; const exceptionClass: ExceptClass; const msg: string);
begin
Assert.WillRaiseDescendant(
procedure
begin
AMethod;
end,
exceptionClass, msg);
end;
class procedure Assert.WillRaiseWithMessage(const AMethod: TTestLocalMethod; const exceptionClass: ExceptClass;const exceptionMsg, msg: string);