forked from kiwi-bdd/Kiwi
/
KWBeWithinMatcher.m
114 lines (88 loc) · 3.22 KB
/
KWBeWithinMatcher.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// Licensed under the terms in License.txt
//
// Copyright 2010 Allen Ding. All rights reserved.
//
#import "KWBeWithinMatcher.h"
#import "KWFormatter.h"
#import "KWObjCUtilities.h"
#import "KWValue.h"
@interface KWBeWithinMatcher()
#pragma mark -
#pragma mark Properties
@property (nonatomic, readwrite, retain) id distance;
@property (nonatomic, readwrite, retain) id otherValue;
@end
@implementation KWBeWithinMatcher
#pragma mark -
#pragma mark Initializing
- (void)dealloc {
[distance release];
[otherValue release];
[super dealloc];
}
#pragma mark -
#pragma mark Properties
@synthesize distance;
@synthesize otherValue;
#pragma mark -
#pragma mark Getting Matcher Strings
+ (NSArray *)matcherStrings {
return @[@"beWithin:of:", @"equal:withDelta:"];
}
#pragma mark -
#pragma mark Matching
// Evaluation is done by getting the underlying values as the widest data
// types available.
- (BOOL)evaluateForFloatingPoint {
double firstValue = [self.subject doubleValue];
double secondValue = [self.otherValue doubleValue];
double theDistance = [self.distance doubleValue];
double absoluteDifference = firstValue > secondValue ? firstValue - secondValue : secondValue - firstValue;
return absoluteDifference <= theDistance;
}
- (BOOL)evaluateForUnsignedIntegral {
unsigned long long firstValue = [self.subject unsignedLongLongValue];
unsigned long long secondValue = [self.otherValue unsignedLongLongValue];
unsigned long long theDistance = [self.distance unsignedLongLongValue];
unsigned long long absoluteDifference = firstValue > secondValue ? firstValue - secondValue : secondValue - firstValue;
return absoluteDifference <= theDistance;
}
- (BOOL)evaluateForSignedIntegral {
long long firstValue = [self.subject longLongValue];
long long secondValue = [self.otherValue longLongValue];
long long theDistance = [self.distance longLongValue];
long long absoluteDifference = firstValue > secondValue ? firstValue - secondValue : secondValue - firstValue;
return absoluteDifference <= theDistance;
}
- (BOOL)evaluate {
const char *objCType = [self.subject objCType];
if (KWObjCTypeIsFloatingPoint(objCType))
return [self evaluateForFloatingPoint];
else if (KWObjCTypeIsUnsignedIntegral(objCType))
return [self evaluateForUnsignedIntegral];
else
return [self evaluateForSignedIntegral];
}
#pragma mark -
#pragma mark Getting Failure Messages
- (NSString *)failureMessageForShould {
return [NSString stringWithFormat:@"expected subject to be within %@ of %@, got %@",
[KWFormatter formatObject:self.distance],
[KWFormatter formatObject:self.otherValue],
[KWFormatter formatObject:self.subject]];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"be within %@ of %@", self.distance, self.otherValue];
}
#pragma mark -
#pragma mark Configuring Matchers
- (void)beWithin:(id)aDistance of:(id)aValue {
self.distance = aDistance;
self.otherValue = aValue;
}
- (void)equal:(double)aValue withDelta:(double)aDelta {
[self beWithin:[KWValue valueWithDouble:aDelta] of:[KWValue valueWithDouble:aValue]];
}
@end