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