main
1//
2// Licensed under the terms in License.txt
3//
4// Copyright 2010 Allen Ding. All rights reserved.
5//
6
7#import "KWBlockRaiseMatcher.h"
8#import "KWBlock.h"
9
10@interface KWBlockRaiseMatcher()
11
12#pragma mark -
13#pragma mark Properties
14
15@property (nonatomic, readwrite, retain) NSException *exception;
16@property (nonatomic, readwrite, retain) NSException *actualException;
17
18@end
19
20@implementation KWBlockRaiseMatcher
21
22#pragma mark -
23#pragma mark Initializing
24
25- (void)dealloc {
26 [exception release];
27 [actualException release];
28 [super dealloc];
29}
30
31#pragma mark -
32#pragma mark Properties
33
34@synthesize exception;
35@synthesize actualException;
36
37#pragma mark -
38#pragma mark Getting Matcher Strings
39
40+ (NSArray *)matcherStrings {
41 return @[@"raise",
42 @"raiseWithName:",
43 @"raiseWithReason:",
44 @"raiseWithName:reason:"];
45}
46
47#pragma mark -
48#pragma mark Matching
49
50- (BOOL)evaluate {
51 if (![self.subject isKindOfClass:[KWBlock class]])
52 [NSException raise:@"KWMatcherException" format:@"subject must be a KWBlock"];
53
54 @try {
55 [self.subject call];
56 } @catch (NSException *anException) {
57 self.actualException = anException;
58
59 if ([self.exception name] != nil && ![[self.exception name] isEqualToString:[anException name]])
60 return NO;
61
62 if ([self.exception reason] != nil && ![[self.exception reason] isEqualToString:[anException reason]])
63 return NO;
64
65 return YES;
66 }
67
68 return NO;
69}
70
71#pragma mark -
72#pragma mark Getting Failure Messages
73
74+ (NSString *)exceptionPhraseWithException:(NSException *)anException {
75 if (anException == nil)
76 return @"nothing";
77
78 NSString *namePhrase = nil;
79
80 if ([anException name] == nil)
81 namePhrase = @"exception";
82 else
83 namePhrase = [anException name];
84
85 if ([anException reason] == nil)
86 return namePhrase;
87
88 return [NSString stringWithFormat:@"%@ \"%@\"", namePhrase, [anException reason]];
89}
90
91- (NSString *)failureMessageForShould {
92 return [NSString stringWithFormat:@"expected %@, but %@ raised",
93 [[self class] exceptionPhraseWithException:self.exception],
94 [[self class] exceptionPhraseWithException:self.actualException]];
95}
96
97- (NSString *)failureMessageForShouldNot {
98 return [NSString stringWithFormat:@"expected %@ not to be raised",
99 [[self class] exceptionPhraseWithException:self.actualException]];
100}
101
102#pragma mark -
103#pragma mark Configuring Matchers
104
105- (void)raise {
106 [self raiseWithName:nil reason:nil];
107}
108
109- (void)raiseWithName:(NSString *)aName {
110 [self raiseWithName:aName reason:nil];
111}
112
113- (void)raiseWithReason:(NSString *)aReason {
114 [self raiseWithName:nil reason:aReason];
115}
116
117- (void)raiseWithName:(NSString *)aName reason:(NSString *)aReason {
118 self.exception = [NSException exceptionWithName:aName reason:aReason userInfo:nil];
119}
120
121- (NSString *)description
122{
123 return [NSString stringWithFormat:@"raise %@", [[self class] exceptionPhraseWithException:self.exception]];
124}
125
126@end