-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSException_SenTestFailure.m
462 lines (407 loc) · 19.6 KB
/
NSException_SenTestFailure.m
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
/*$Id: NSException_SenTestFailure.m,v 1.20 2005/04/02 03:18:19 phink Exp $*/
// Copyright (c) 1997-2005, Sen:te (Sente SA). All rights reserved.
//
// Use of this source code is governed by the following license:
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// (1) Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// (2) Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL Sente SA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Note: this license is equivalent to the FreeBSD license.
//
// This notice may not be removed from this file.
#import "NSException_SenTestFailure.h"
#import "SenTestCase.h"
#import "SenTestingUtilities.h"
#import <Foundation/Foundation.h>
#include <stdarg.h>
#define AsNotNilObject(object) ((object != nil) ? (id)object : [NSNull null])
@implementation NSException (SenTestFailure)
- (NSString *) filename
/*" The filename containing the code that caused this exception.
"*/
{
return [[self userInfo] objectForKey:SenTestFilenameKey];
}
- (NSSet *) ignoredSubdirectories
{
static NSSet *ignoredSubdirectories = nil;
if (ignoredSubdirectories == nil) {
NSString *path = [[NSBundle bundleForClass:[SenTestCase class]] pathForResource:@"NoSourceDirectoryExtensions" ofType:@"plist"];
ASSIGN (ignoredSubdirectories, [NSSet setWithArray:[[NSString stringWithContentsOfFile:path] propertyList]]);
}
return ignoredSubdirectories;
}
- (NSString *) currentDirectoryPath
{
#ifndef WIN32
return [[NSFileManager defaultManager] currentDirectoryPath];
#else
return [[[[NSProcessInfo processInfo] arguments] lastObject] stringByDeletingLastPathComponent];
#endif
}
- (NSString *) pathForFilename:(NSString *) aFilename
/*" This method returns the path to the file named in aFilename.
If the aFilename is nil or empty then %Unknown.m is returned. If
the file cannot be found in the current directory or any of sub-directories
then the name of aFilename is returned. Note that the current
directory is searched only if it is a project directory. A project directory
is one that contains the file %PB.project or XXX.xcode or XXX.pbproj where
XXX is the name of the current directory.
"*/
{
if ((aFilename == nil) || [aFilename isEqualToString:@""]) {
return @"Unknown.m";
}
else {
BOOL isInProjectDirectory = NO;
NSFileManager *theFileManager = [NSFileManager defaultManager];
NSString *currentDirectoryPath = [self currentDirectoryPath];
NSString *currentDirectory = nil;
NSString *projectPath = nil;
projectPath = [currentDirectoryPath stringByAppendingPathComponent:@"PB.project"];
isInProjectDirectory = [theFileManager fileExistsAtPathOrLink:projectPath];
if ( isInProjectDirectory == NO ) {
NSString *xcodeProjectName = nil;
currentDirectory = [currentDirectoryPath lastPathComponent];
xcodeProjectName = [currentDirectory stringByAppendingPathExtension:@"xcode"];
projectPath = [currentDirectoryPath stringByAppendingPathComponent:xcodeProjectName];
isInProjectDirectory = [theFileManager fileExistsAtPathOrLink:projectPath];
}
if ( isInProjectDirectory == NO ) {
NSString *pbprojProjectName = nil;
currentDirectory = [currentDirectoryPath lastPathComponent];
pbprojProjectName = [currentDirectory stringByAppendingPathExtension:@"pbproj"];
projectPath = [currentDirectoryPath stringByAppendingPathComponent:pbprojProjectName];
isInProjectDirectory = [theFileManager fileExistsAtPathOrLink:projectPath];
}
if ( isInProjectDirectory == YES ) {
NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:currentDirectoryPath];
NSString *each = nil;
while (nil != (each = [directoryEnumerator nextObject])) {
if ([[self ignoredSubdirectories] containsObject:[each pathExtension]]) {
[directoryEnumerator skipDescendents];
}
else if ([[each lastPathComponent] isEqualToString:aFilename]) {
return [currentDirectoryPath stringByAppendingPathComponent:each];
//return each; // for OPENSTEP.
}
}
}
return aFilename;
}
}
- (NSString *) filePathInProject
/*" This method returns the path to the source code file that caused this
exception. If [self filename] is nil or empty then %Unknown.m is returned. If
the file cannot be found in the current directory or any of sub-directories
then the name of aFilename is returned. Note that the current
directory is searched only if it is a project directory. A project directory
is one that contains the file %PB.project or XXX.xcode or XXX.pbproj where
XXX is the name of the current directory.
"*/
{
return [self pathForFilename:[self filename]];
}
- (NSNumber *) lineNumber
/*" The line number of the code that caused the exception.
"*/
{
NSNumber *n = [[self userInfo] objectForKey:SenTestLineNumberKey];
return (n == nil) ? [NSNumber numberWithInt:0] : n;
}
+ (NSException *) failureInFile:(NSString *) filename atLine:(int) lineNumber withDescription:(NSString *)formatString, ...
/*" This method returns a NSException with a name of "SenTestFailureException".
A reason constructed from the formatString and the variable number of
arguments that follow it (just like in the NSString method -stringWithFormat:).
And an user info dictionary that contain the following information for the
given keys:
_{SenFailureTypeKey SenUnconditionalFailure.}
_{SenTestFilenameKey The filename containing the code that caused the exception.}
_{SenTestLineNumberKey The lineNumber of the code that caused the exception.}
_{SenTestDescriptionKey A description constructed from the formatString and
the variable number of arguments that follow it.}
"*/
{
NSString *stkDescription = nil;
NSString *aReason = nil;
NSDictionary *aUserInfo = nil;
if ( formatString != nil ) {
va_list argList;
va_start(argList, formatString);
stkDescription = [[NSString alloc] initWithFormat:formatString
arguments:argList];
[stkDescription autorelease];
va_end(argList);
} else {
stkDescription = @"";
}
aReason = stkDescription;
aUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
SenUnconditionalFailure, SenFailureTypeKey,
filename, SenTestFilenameKey,
[NSNumber numberWithInt:lineNumber], SenTestLineNumberKey,
stkDescription, SenTestDescriptionKey,
nil];
return [self exceptionWithName:SenTestFailureException
reason:aReason
userInfo:aUserInfo];
}
+ (NSException *) failureInCondition:(NSString *) condition isTrue:(BOOL) isTrue inFile:(NSString *) filename atLine:(int) lineNumber withDescription:(NSString *)formatString, ...
/*" This method returns a NSException with a name of "SenTestFailureException".
A reason constructed from the condition, the boolean isTrue and the
formatString and the variable number of arguments that follow it
(just like in the NSString method -stringWithFormat:).
And an user info dictionary that contain the following information for the
given keys:
_{SenFailureTypeKey SenConditionFailure.}
_{SenTestConditionKey The condition (as a string) that caused this failure.}
_{SenTestFilenameKey The filename containing the code that caused the exception.}
_{SenTestLineNumberKey The lineNumber of the code that caused the exception.}
_{SenTestDescriptionKey A description constructed from the formatString and
the variable number of arguments that follow it.}
"*/
{
NSString *stkDescription = nil;
NSString *aReason = nil;
NSDictionary *aUserInfo = nil;
if ( formatString != nil ) {
va_list argList;
va_start(argList, formatString);
stkDescription = [[NSString alloc] initWithFormat:formatString
arguments:argList];
[stkDescription autorelease];
va_end(argList);
} else {
stkDescription = @"";
}
aReason = [NSString stringWithFormat:@"\"%@\" should be %@. %@",
condition, (isTrue ? @"false" : @"true"), stkDescription];
aUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
SenConditionFailure, SenFailureTypeKey,
condition, SenTestConditionKey,
filename, SenTestFilenameKey,
[NSNumber numberWithInt:lineNumber], SenTestLineNumberKey,
stkDescription, SenTestDescriptionKey,
nil];
return [self exceptionWithName:SenTestFailureException
reason:aReason
userInfo:aUserInfo];
}
+ (NSException *) failureInEqualityBetweenObject:(id) left andObject:(id) right inFile:(NSString *) filename atLine:(int) lineNumber withDescription:(NSString *)formatString, ...
/*" This method returns a NSException with a name of "SenTestFailureException".
A reason constructed from the descriptions of the left and right objects and
the formatString and the variable number of arguments that follow it
(just like in the NSString method -stringWithFormat:).
And an user info dictionary that contain the following information for the
given keys:
_{SenFailureTypeKey SenEqualityFailure.}
_{SenTestEqualityLeftKey The left object.}
_{SenTestEqualityRightKey The right object.}
_{SenTestFilenameKey The filename containing the code that caused the exception.}
_{SenTestLineNumberKey The lineNumber of the code that caused the exception.}
_{SenTestDescriptionKey A description constructed from the formatString and
the variable number of arguments that follow it.}
"*/
{
NSString *stkDescription = nil;
NSString *aReason = nil;
NSDictionary *aUserInfo = nil;
if ( formatString != nil ) {
va_list argList;
va_start(argList, formatString);
stkDescription = [[NSString alloc] initWithFormat:formatString
arguments:argList];
[stkDescription autorelease];
va_end(argList);
} else {
stkDescription = @"";
}
aReason = [NSString stringWithFormat:@"'%@' should be equal to '%@' %@",
[left description], [right description], stkDescription];
aUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
SenEqualityFailure, SenFailureTypeKey,
AsNotNilObject (left), SenTestEqualityLeftKey,
AsNotNilObject (right), SenTestEqualityRightKey,
filename, SenTestFilenameKey,
[NSNumber numberWithInt:lineNumber], SenTestLineNumberKey,
stkDescription, SenTestDescriptionKey,
nil];
return [self exceptionWithName:SenTestFailureException
reason:aReason
userInfo:aUserInfo];
}
+ (NSException *) failureInEqualityBetweenValue:(NSValue *) left andValue:(NSValue *) right withAccuracy:(NSValue *) accuracy inFile:(NSString *) filename atLine:(int) lineNumber withDescription:(NSString *)formatString, ...
/*" This method returns a NSException with a name of "SenTestFailureException".
A reason constructed from the descriptions of the left and right objects and
the formatString and the variable number of arguments that follow it
(just like in the NSString method -stringWithFormat:).
And an user info dictionary that contain the following information for the
given keys:
_{SenFailureTypeKey SenEqualityFailure.}
_{SenTestEqualityLeftKey The left object.}
_{SenTestEqualityRightKey The right object.}
_{SenTestEqualityAccuracyKey The difference object.}
_{SenTestFilenameKey The filename containing the code that caused the exception.}
_{SenTestLineNumberKey The lineNumber of the code that caused the exception.}
_{SenTestDescriptionKey A description constructed from the formatString and
the variable number of arguments that follow it.}
"*/
{
NSString *stkDescription = @"";
NSString *aReason = nil;
NSDictionary *aUserInfo = nil;
if (formatString != nil) {
va_list argList;
va_start (argList, formatString);
stkDescription = [[NSString alloc] initWithFormat:formatString arguments:argList];
[stkDescription autorelease];
va_end(argList);
}
if (accuracy != nil) {
aReason = [NSString stringWithFormat:
@"'%@' should be equal to '%@' + or - '%@': %@",
[left contentDescription],
[right contentDescription],
[accuracy contentDescription],
stkDescription];
aUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
SenEqualityFailure, SenFailureTypeKey,
AsNotNilObject (left), SenTestEqualityLeftKey,
AsNotNilObject (right), SenTestEqualityRightKey,
AsNotNilObject (accuracy), SenTestEqualityAccuracyKey,
filename, SenTestFilenameKey,
[NSNumber numberWithInt:lineNumber], SenTestLineNumberKey,
stkDescription, SenTestDescriptionKey,
nil];
}
else {
aReason = [NSString stringWithFormat:@"'%@' should be equal to '%@': %@",
[left contentDescription],
[right contentDescription],
stkDescription];
aUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
SenEqualityFailure, SenFailureTypeKey,
AsNotNilObject (left), SenTestEqualityLeftKey,
AsNotNilObject (right), SenTestEqualityRightKey,
filename, SenTestFilenameKey,
[NSNumber numberWithInt:lineNumber], SenTestLineNumberKey,
stkDescription, SenTestDescriptionKey,
nil];
}
return [self exceptionWithName:SenTestFailureException
reason:aReason
userInfo:aUserInfo];
}
+ (NSException *) failureInRaise:(NSString *) expression exception:(NSException *) exception inFile:(NSString *) filename atLine:(int) lineNumber withDescription:(NSString *)formatString, ...
/*" This method returns a NSException with a name of "SenTestFailureException".
A reason constructed from the expression, the reason for the exception and the
formatString and the variable number of arguments that follow it
(just like in the NSString method -stringWithFormat:).
And an user info dictionary that contain the following information for the
given keys:
_{SenFailureTypeKey SenRaiseFailure.}
_{SenTestConditionKey The expression as a string.}
_{SenTestFilenameKey The filename containing the code that caused the exception.}
_{SenTestLineNumberKey The lineNumber of the code that caused the exception.}
_{SenTestDescriptionKey A description constructed from the formatString and
the variable number of arguments that follow it.}
"*/
{
NSString *stkDescription = nil;
NSString *aReason = nil;
NSDictionary *aUserInfo = nil;
if ( formatString != nil ) {
va_list argList;
va_start(argList, formatString);
stkDescription = [[NSString alloc] initWithFormat:formatString
arguments:argList];
[stkDescription autorelease];
va_end(argList);
} else {
stkDescription = @"";
}
aReason = [NSString stringWithFormat:@"%@ raised %@. %@",
expression, [exception reason], stkDescription];
aUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
SenRaiseFailure, SenFailureTypeKey,
expression, SenTestConditionKey,
filename, SenTestFilenameKey,
[NSNumber numberWithInt:lineNumber], SenTestLineNumberKey,
stkDescription, SenTestDescriptionKey,
nil];
return [self exceptionWithName:SenTestFailureException
reason:aReason
userInfo:aUserInfo];
}
+ (NSException *) failureInRaise:(NSString *) expression inFile:(NSString *) filename atLine:(int) lineNumber withDescription:(NSString *)formatString, ...
/*" This method returns a NSException with a name of "SenTestFailureException".
A reason constructed from the expression and the
formatString and the variable number of arguments that follow it
(just like in the NSString method -stringWithFormat:).
And an user info dictionary that contain the following information for the
given keys:
_{SenFailureTypeKey SenRaiseFailure.}
_{SenTestConditionKey The expression as a string.}
_{SenTestFilenameKey The filename containing the code that caused the exception.}
_{SenTestLineNumberKey The lineNumber of the code that caused the exception.}
_{SenTestDescriptionKey A description constructed from the formatString and
the variable number of arguments that follow it.}
"*/
{
NSString *stkDescription = nil;
NSString *aReason = nil;
NSDictionary *aUserInfo = nil;
if ( formatString != nil ) {
va_list argList;
va_start(argList, formatString);
stkDescription = [[NSString alloc] initWithFormat:formatString
arguments:argList];
[stkDescription autorelease];
va_end(argList);
} else {
stkDescription = @"";
}
aReason = [NSString stringWithFormat:@"%@ should raise. %@",
expression, stkDescription];
aUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
SenRaiseFailure, SenFailureTypeKey,
expression, SenTestConditionKey,
filename, SenTestFilenameKey,
[NSNumber numberWithInt:lineNumber], SenTestLineNumberKey,
stkDescription, SenTestDescriptionKey,
nil];
return [self exceptionWithName:SenTestFailureException
reason:aReason
userInfo:aUserInfo];
}
@end
NSString * const SenTestFailureException = @"SenTestFailureException";
NSString * const SenFailureTypeKey = @"SenFailureTypeKey";
NSString * const SenUnconditionalFailure = @"SenUnconditionalFailure";
NSString * const SenConditionFailure = @"SenConditionFailure";
NSString * const SenEqualityFailure = @"SenEqualityFailure";
NSString * const SenRaiseFailure = @"SenRaiseFailure";
NSString * const SenTestConditionKey = @"SenTestConditionKey";
NSString * const SenTestEqualityLeftKey = @"SenTestEqualityLeftKey";
NSString * const SenTestEqualityRightKey = @"SenTestEqualityRightKey";
NSString * const SenTestEqualityAccuracyKey = @"SenTestEqualityAccuracyKey";
NSString * const SenTestFilenameKey = @"SenTestFilenameKey";
NSString * const SenTestLineNumberKey = @"SenTestLineNumberKey";
NSString * const SenTestDescriptionKey = @"SenTestDescriptionKey";