main
1//
2// Licensed under the terms in License.txt
3//
4// Copyright 2010 Allen Ding. All rights reserved.
5//
6
7#import "KWMatcher.h"
8#import "KWFormatter.h"
9#import "KWFutureObject.h"
10
11@implementation KWMatcher
12
13#pragma mark -
14#pragma mark Initializing
15
16- (id)initWithSubject:(id)anObject {
17 if ((self = [super init])) {
18 subject = [anObject retain];
19 }
20
21 return self;
22}
23
24+ (id)matcherWithSubject:(id)anObject {
25 return [[[self alloc] initWithSubject:anObject] autorelease];
26}
27
28- (void)dealloc {
29 [subject release];
30 [super dealloc];
31}
32
33#pragma mark -
34#pragma mark Properties
35
36@synthesize subject;
37
38- (id)subject
39{
40 if ([subject isKindOfClass:[KWFutureObject class]]) {
41 return [(KWFutureObject *)subject object];
42 }
43 return subject;
44}
45
46#pragma mark -
47#pragma mark Getting Matcher Strings
48
49+ (NSArray *)matcherStrings {
50 return nil;
51}
52
53#pragma mark -
54#pragma mark Getting Matcher Compatability
55
56+ (BOOL)canMatchSubject:(id)anObject {
57 return YES;
58}
59
60#pragma mark -
61#pragma mark Matching
62
63- (BOOL)evaluate {
64 [NSException raise:NSInternalInconsistencyException format:@"%@ must override -evaluate",
65 [KWFormatter formatObject:[self class]]];
66 return NO;
67}
68
69#pragma mark -
70#pragma mark Getting Failure Messages
71
72- (NSString *)failureMessageForShould {
73 return @"subject did not meet expectation";
74}
75
76- (NSString *)failureMessageForShouldNot {
77 NSString *failureMessageForShould = [self failureMessageForShould];
78 NSRange markerRange = [failureMessageForShould rangeOfString:@" to "];
79
80 if (markerRange.location == NSNotFound)
81 return @"subject did not meet expectation";
82
83 NSRange replacementRange = NSMakeRange(0, markerRange.location + markerRange.length);
84 NSString *message = [failureMessageForShould stringByReplacingOccurrencesOfString:@" to "
85 withString:@" not to "
86 options:0
87 range:replacementRange];
88 return message;
89}
90
91@end