main
 1//
 2// Licensed under the terms in License.txt
 3//
 4// Copyright 2010 Allen Ding. All rights reserved.
 5//
 6
 7#import "KWBeBetweenMatcher.h"
 8#import "KWFormatter.h"
 9
10@interface KWBeBetweenMatcher()
11
12#pragma mark -
13#pragma mark Properties
14
15@property (nonatomic, readwrite, retain) id lowerEndpoint;
16@property (nonatomic, readwrite, retain) id upperEndpoint;
17
18@end
19
20@implementation KWBeBetweenMatcher
21
22#pragma mark -
23#pragma mark Initializing
24
25- (void)dealloc {
26    [lowerEndpoint release];
27    [upperEndpoint release];
28    [super dealloc];
29}
30
31#pragma mark -
32#pragma mark Properties
33
34@synthesize lowerEndpoint;
35@synthesize upperEndpoint;
36
37#pragma mark -
38#pragma mark Getting Matcher Strings
39
40+ (NSArray *)matcherStrings {
41    return @[@"beBetween:and:", @"beInTheIntervalFrom:to:"];
42}
43
44#pragma mark -
45#pragma mark Matching
46
47- (BOOL)evaluate {
48    if (![self.subject respondsToSelector:@selector(compare:)])
49        [NSException raise:@"KWMatcherException" format:@"subject does not respond to -compare:"];
50
51    NSComparisonResult lowerResult = [self.subject compare:self.lowerEndpoint];
52    NSComparisonResult upperResult = [self.subject compare:self.upperEndpoint];
53    return (lowerResult == NSOrderedDescending || lowerResult == NSOrderedSame) &&
54           (upperResult == NSOrderedAscending || upperResult == NSOrderedSame);
55}
56
57#pragma mark -
58#pragma mark Getting Failure Messages
59
60- (NSString *)failureMessageForShould {
61    return [NSString stringWithFormat:@"expected subject to be in the interval [%@, %@], got %@",
62                                      [KWFormatter formatObject:self.lowerEndpoint],
63                                      [KWFormatter formatObject:self.upperEndpoint],
64                                      [KWFormatter formatObject:self.subject]];
65}
66
67- (NSString *)description
68{
69  return [NSString stringWithFormat:@"be between %@ and %@", self.lowerEndpoint, self.upperEndpoint];
70}
71
72#pragma mark -
73#pragma mark Configuring Matchers
74
75- (void)beBetween:(id)aLowerEndpoint and:(id)anUpperEndpoint {
76    [self beInTheIntervalFrom:aLowerEndpoint to:anUpperEndpoint];
77}
78
79- (void)beInTheIntervalFrom:(id)aLowerEndpoint to:(id)anUpperEndpoint {
80    self.lowerEndpoint = aLowerEndpoint;
81    self.upperEndpoint = anUpperEndpoint;
82}
83
84@end