main
1//
2// Licensed under the terms in License.txt
3//
4// Copyright 2010 Allen Ding. All rights reserved.
5//
6
7#import "KWFailure.h"
8#import <SenTestingKit/SenTestingKit.h>
9#import "KWCallSite.h"
10
11@implementation KWFailure
12
13#pragma mark -
14#pragma mark Initializing
15
16- (id)initWithCallSite:(KWCallSite *)aCallSite message:(NSString *)aMessage {
17 if ((self = [super init])) {
18 callSite = [aCallSite retain];
19 message = [aMessage copy];
20 }
21
22 return self;
23}
24
25- (id)initWithCallSite:(KWCallSite *)aCallSite format:(NSString *)format, ... {
26 va_list argumentList;
27 va_start(argumentList, format);
28 NSString *aMessage = [[[NSString alloc] initWithFormat:format arguments:argumentList] autorelease];
29 return [self initWithCallSite:aCallSite message:aMessage];
30}
31
32+ (id)failureWithCallSite:(KWCallSite *)aCallSite message:(NSString *)aMessage {
33 return [[[self alloc] initWithCallSite:aCallSite message:aMessage] autorelease];
34}
35
36+ (id)failureWithCallSite:(KWCallSite *)aCallSite format:(NSString *)format, ... {
37 va_list argumentList;
38 va_start(argumentList, format);
39 NSString *message = [[[NSString alloc] initWithFormat:format arguments:argumentList] autorelease];
40 return [self failureWithCallSite:aCallSite message:message];
41}
42
43- (void)dealloc {
44 [callSite release];
45 [message release];
46 [super dealloc];
47}
48
49#pragma mark -
50#pragma mark Properties
51
52@synthesize message;
53@synthesize callSite;
54
55#pragma mark -
56#pragma mark Getting Exception Representations
57
58- (NSException *)exceptionValue {
59 NSDictionary *userInfo = nil;
60 if (self.callSite) {
61 NSNumber *lineNumber = @(self.callSite.lineNumber);
62 userInfo = @{SenTestFilenameKey: self.callSite.filename,
63 SenTestLineNumberKey: lineNumber};
64 }
65 return [NSException exceptionWithName:@"KWFailureException" reason:message userInfo:userInfo];
66}
67
68@end