forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDUnitX.Tests.Assert.pas
408 lines (361 loc) · 10.8 KB
/
DUnitX.Tests.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
{***************************************************************************}
{ }
{ DUnitX }
{ }
{ Copyright (C) 2012 Vincent Parrett }
{ }
{ 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.Tests.Assert;
interface
uses
DUnitX.TestFramework;
type
{+M}
[TestFixture]
TTestsAssert = class
published
[Test]
procedure Pass_Throws_ETestPass_Exception;
[Test]
procedure Pass_Throws_ETestPass_Exception_With_Message;
[Test]
procedure Fail_Throws_ETestFailure_Exception;
[Test]
procedure Fail_Throws_ETestFailure_Exception_With_Message;
[Test]
procedure Fail_Throws_ETestFailure_Exception_With_Return_Address_Reference;
[Test]
procedure Fail_Throws_ETestFailure_Exception_With_Caller_Address_Reference;
[Test]
procedure AreEqual_String_Throws_No_Exception_When_Values_Are_Equal;
[Test]
procedure AreEqual_String_Throws_ETestFailure_When_Values_Are_NotEqual;
[Test]
procedure AreEqual_Extended_Throws_No_Exception_When_Values_Are_Equal;
[Test]
procedure AreEqual_Extended_Throws_ETestFailure_When_Values_Are_NotEqual;
[Test]
procedure AreEqual_TClass_Throws_No_Exception_When_Classes_Are_Equal;
[Test]
procedure AreEqual_TClass_Throws_ETestFailure_When_Classes_Are_NotEqual;
[Test]
procedure AreEqual_T_Throws_No_Exception_When_Interfaces_Are_Equal;
[Test]
procedure AreEqual_T_Throws_ETestFailure_When_Interfaces_Are_NotEqual;
[Test]
procedure AreEqual_T_Throws_ETestFailure_When_Interfaces_Are_Nil;
[Test]
procedure AreEqual_T_Throws_No_Exception_When_Objects_Are_Equal;
[Test]
procedure AreEqual_T_Throws_ETestFailure_When_Objects_Are_NotEqual;
[Test]
procedure AreEqual_T_Throws_ETestFailure_When_Objects_Are_Nil;
[Test]
procedure AreEqualMemory_Throws_No_Exception_When_Pointers_Are_Equal;
[Test]
procedure AreEqualMemory_Throws_ETestFailure_When_Pointers_Are_NotEqual;
[Test]
procedure Warn_Throws_ETestWarning_Exception;
[Test]
procedure AreEqual_Throws_No_Exception_When_Values_Are_Exactly_Equal;
end;
implementation
uses
Delphi.Mocks,
SysUtils;
type
{$M+}
IMockInterface = interface
['{8DB1A216-0E95-4241-9522-C50DF99AFB71}']
procedure Stub;
end;
{$M-}
TMockClassOne = class(TObject)
end;
TMockClassTwo = class(TObject)
end;
{ TTestAssert }
procedure TTestsAssert.Fail_Throws_ETestFailure_Exception;
begin
Assert.WillRaise(
procedure
begin
Assert.Fail
end, ETestFailure);
end;
procedure TTestsAssert.Fail_Throws_ETestFailure_Exception_With_Caller_Address_Reference;
var
MyProc : TTestLocalMethod;
begin
MyProc := procedure
begin
Assert.WillRaise(
procedure
begin
Assert.Fail;
//We will raise at this point, when we return.
end, ETestFailure);
end;
Assert.WillNotRaise(MyProc, ETestFailure);
end;
procedure TTestsAssert.Fail_Throws_ETestFailure_Exception_With_Message;
const
EXPECTED_EXCEPTION_MSG = 'Failed Message';
begin
Assert.WillRaise(
procedure
begin
Assert.Fail(EXPECTED_EXCEPTION_MSG);
end, ETestFailure, EXPECTED_EXCEPTION_MSG);
end;
procedure TTestsAssert.Fail_Throws_ETestFailure_Exception_With_Return_Address_Reference;
var
MyProc : TTestLocalMethod;
begin
MyProc := procedure
begin
Assert.WillNotRaise(
procedure
begin
Assert.Fail('', @MyProc);
//We will not raise internally, as we are passing the address of our caller
end, ETestFailure);
//We will raise it here, when MyProc returns.
end;
Assert.WillRaise(MyProc, ETestFailure);
end;
procedure TTestsAssert.Pass_Throws_ETestPass_Exception;
begin
Assert.WillRaise(
procedure
begin
Assert.Pass;
end, ETestPass);
end;
procedure TTestsAssert.Pass_Throws_ETestPass_Exception_With_Message;
const
EXPECTED_EXCEPTION_MSG = 'Passing Message';
begin
Assert.WillRaise(
procedure
begin
Assert.Pass(EXPECTED_EXCEPTION_MSG);
end, ETestPass, EXPECTED_EXCEPTION_MSG);
end;
procedure TTestsAssert.Warn_Throws_ETestWarning_Exception;
begin
Assert.WillRaise(
procedure
begin
Assert.Warn;
end, ETestWarning);
end;
procedure TTestsAssert.AreEqualMemory_Throws_ETestFailure_When_Pointers_Are_NotEqual;
begin
Assert.WillNotRaise(
procedure
begin
Assert.AreEqualMemory(, @mock, SizeOf(mock));
end, ETestFailure);
end;
procedure TTestsAssert.AreEqualMemory_Throws_No_Exception_When_Pointers_Are_Equal;
var
mock : IInterface;
begin
mock := TInterfacedObject.Create();
Assert.WillNotRaise(
procedure
begin
Assert.AreEqualMemory(@mock, @mock, SizeOf(mock));
end, ETestFailure);
end;
procedure TTestsAssert.AreEqual_Extended_Throws_ETestFailure_When_Values_Are_NotEqual;
const
ACTUAL_EXTENDED = 1.19E20;
EXPECTED_EXTENDED = 1.18E20;
TOLERANCE_EXTENDED = 0.001E20;
begin
Assert.WillRaise(
procedure
begin
Assert.AreEqual(ACTUAL_EXTENDED, EXPECTED_EXTENDED, TOLERANCE_EXTENDED);
end, ETestFailure, Format('[%e] with in [%e] from [%e]', [ACTUAL_EXTENDED, TOLERANCE_EXTENDED, EXPECTED_EXTENDED]));
end;
procedure TTestsAssert.AreEqual_Extended_Throws_No_Exception_When_Values_Are_Equal;
const
ACTUAL_EXTENDED = 1.19E20;
EXPECTED_EXTENDED = 1.18E20;
TOLERANCE_EXTENDED = 0.011E20;
begin
Assert.WillNotRaise(
procedure
begin
Assert.AreEqual(ACTUAL_EXTENDED, EXPECTED_EXTENDED, TOLERANCE_EXTENDED);
end, Exception);
end;
procedure TTestsAssert.AreEqual_String_Throws_ETestFailure_When_Values_Are_NotEqual;
const
ACTUAL_STRING = 'the brown dog jumped something';
EXPECTED_STRING = 'SOMETHING JUMPED THE BROWN DOG';
begin
Assert.WillRaise(
procedure
begin
Assert.AreEqual(ACTUAL_STRING, EXPECTED_STRING);
end, ETestFailure, Format('[%s] is Not Equal to [%s] %s', [ACTUAL_STRING, EXPECTED_STRING, '']));
end;
procedure TTestsAssert.AreEqual_String_Throws_No_Exception_When_Values_Are_Equal;
const
ACTUAL_AND_EXPECTED_STRING = 'the brown dog jumped something';
begin
Assert.WillNotRaise(
procedure
begin
Assert.AreEqual(ACTUAL_AND_EXPECTED_STRING, ACTUAL_AND_EXPECTED_STRING);
end, Exception);
end;
procedure TTestsAssert.AreEqual_TClass_Throws_ETestFailure_When_Classes_Are_NotEqual;
begin
Assert.WillRaise(
procedure
begin
Assert.AreEqual(TMockClassOne, TMockClassTwo);
end, ETestFailure);
end;
procedure TTestsAssert.AreEqual_TClass_Throws_No_Exception_When_Classes_Are_Equal;
begin
Assert.WillNotRaise(
procedure
begin
Assert.AreEqual(TMockClassOne, TMockClassOne);
end, ETestFailure);
end;
procedure TTestsAssert.AreEqual_Throws_No_Exception_When_Values_Are_Exactly_Equal;
var
actualAndExpected, tolerance : Extended;
begin
//Ensure they are the same value.
actualAndExpected := 1.1;
tolerance := 0;
Assert.WillNotRaise(
procedure
begin
Assert.AreEqual(actualAndExpected, actualAndExpected, tolerance);
end, Exception);
end;
procedure TTestsAssert.AreEqual_T_Throws_ETestFailure_When_Interfaces_Are_NotEqual;
var
mock : IInterface;
mock2 : IInterface;
begin
mock := TInterfacedObject.Create();
mock2 := TInterfacedObject.Create();
Assert.WillRaise(
procedure
begin
Assert.AreEqual<IInterface>(mock, mock2);
end, ETestFailure);
end;
procedure TTestsAssert.AreEqual_T_Throws_ETestFailure_When_Objects_Are_Nil;
var
mock : TObject;
nilObject : TObject;
begin
mock := TObject.Create();
try
nilObject := nil;
Assert.WillRaise(
procedure
begin
Assert.AreEqual<TObject>(mock, nilObject);
end, ETestFailure);
Assert.WillRaise(
procedure
begin
Assert.AreEqual<TObject>(nilObject, mock);
end, ETestFailure);
finally
FreeAndNil(mock);
end;
end;
procedure TTestsAssert.AreEqual_T_Throws_ETestFailure_When_Objects_Are_NotEqual;
var
mock : TObject;
mock2 : TObject;
begin
mock := TObject.Create;
mock2 := TObject.Create;
try
Assert.WillRaise(
procedure
begin
Assert.AreEqual<TObject>(mock, mock2);
end, ETestFailure);
finally
FreeAndNil(mock);
FreeAndNil(mock2);
end;
end;
procedure TTestsAssert.AreEqual_T_Throws_ETestFailure_When_Interfaces_Are_Nil;
var
mock : IInterface;
begin
mock := TInterfacedObject.Create();
Assert.WillRaise(
procedure
begin
Assert.AreEqual<IInterface>(mock, nil);
end, ETestFailure);
Assert.WillRaise(
procedure
begin
Assert.AreEqual<IInterface>(nil, mock);
end, ETestFailure);
end;
procedure TTestsAssert.AreEqual_T_Throws_No_Exception_When_Interfaces_Are_Equal;
var
mock : IInterface;
begin
mock := TInterfacedObject.Create();
Assert.WillNotRaise(
procedure
begin
Assert.AreEqual<IInterface>(mock, mock);
end, ETestFailure);
end;
procedure TTestsAssert.AreEqual_T_Throws_No_Exception_When_Objects_Are_Equal;
var
mock : TObject;
begin
mock := TObject.Create;
try
Assert.WillNotRaise(
procedure
begin
Assert.AreEqual<TObject>(mock, mock);
end, ETestFailure);
finally
FreeAndNil(mock);
end;
end;
initialization
TDUnitX.RegisterTestFixture(TTestsAssert);
end.