Permalink
Browse files

Make it easier to compare Kiwi-boxed values with numbers.

Kiwi-boxed values (using theValue()) can now be compared with natively boxed values
(which use NSNumber) - such boxed values are typically returned by KVC accessors. 

Closes #37.
  • Loading branch information...
1 parent 855f05c commit e443475ead8822788114ccc45ea6f4a602a7605d @lukeredpath lukeredpath committed Jul 13, 2011
Showing with 36 additions and 5 deletions.
  1. +1 −1 Kiwi.xcodeproj/project.pbxproj
  2. +5 −0 Kiwi/KWEqualMatcher.m
  3. +1 −0 Kiwi/KWValue.h
  4. +11 −4 Kiwi/KWValue.m
  5. +18 −0 Tests/KWEqualMatcherTest.m
@@ -357,7 +357,7 @@
F54A4C1411734B82002442C5 /* KWConformToProtocolMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWConformToProtocolMatcher.h; sourceTree = "<group>"; };
F54A4C1511734B82002442C5 /* KWConformToProtocolMatcher.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWConformToProtocolMatcher.m; sourceTree = "<group>"; };
F54A4C46117364AC002442C5 /* KWValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWValue.h; sourceTree = "<group>"; };
- F54A4C47117364AC002442C5 /* KWValue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWValue.m; sourceTree = "<group>"; };
+ F54A4C47117364AC002442C5 /* KWValue.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = KWValue.m; sourceTree = "<group>"; tabWidth = 4; };
F54DB6BC11A9782000E6EDB9 /* KWExpectationType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWExpectationType.h; sourceTree = "<group>"; };
F55252AD116CD4BA0028A401 /* KiwiMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KiwiMacros.h; sourceTree = "<group>"; };
F553B29D11755A00004FCA2E /* KWInequalityMatcherTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWInequalityMatcherTest.m; sourceTree = "<group>"; };
View
@@ -6,6 +6,7 @@
#import "KWEqualMatcher.h"
#import "KWFormatter.h"
+#import "KWValue.h"
@interface KWEqualMatcher()
@@ -42,6 +43,10 @@ + (NSArray *)matcherStrings {
#pragma mark Matching
- (BOOL)evaluate {
+ /** handle this as a special case; KWValue supports NSNumber equality but not vice-versa **/
+ if ([self.subject isKindOfClass:[NSNumber class]] && [self.otherSubject isKindOfClass:[KWValue class]]) {
+ return [self.otherSubject isEqual:self.subject];
+ }
return [self.subject isEqual:self.otherSubject];
}
View
@@ -92,5 +92,6 @@
- (NSComparisonResult)compare:(KWValue *)aValue;
- (BOOL)isEqualToKWValue:(KWValue *)aValue;
+- (BOOL)isEqualToNumber:(NSNumber *)aValue;
@end
View
@@ -326,19 +326,26 @@ - (NSComparisonResult)compare:(KWValue *)aValue {
}
- (BOOL)isEqual:(id)object {
- if (![object isKindOfClass:[KWValue class]])
- return NO;
+ if ([object isKindOfClass:[KWValue class]])
+ return [self isEqualToKWValue:object];
- return [self isEqualToKWValue:object];
+ if ([object isKindOfClass:[NSNumber class]])
+ return [self isEqualToNumber:object];
+
+ return NO;
}
- (BOOL)isEqualToKWValue:(KWValue *)aValue {
if (self.isNumeric && aValue.isNumeric)
- return [[self numberValue] isEqualToNumber:[aValue numberValue]];
+ return [self isEqualToNumber:[aValue numberValue]];
else
return [self.value isEqual:aValue.value];
}
+- (BOOL)isEqualToNumber:(NSNumber *)aValue {
+ return [[self numberValue] isEqualToNumber:aValue];
+}
+
#pragma mark -
#pragma mark Representing Values
View
@@ -40,6 +40,24 @@ - (void)testItShouldNotMatchUnequalObjects {
STAssertFalse([matcher evaluate], @"expected negative match");
}
+- (void)testItShouldMatchKiwiBoxedValuesWithKiwiBoxedValues {
+ id matcher = [KWEqualMatcher matcherWithSubject:theValue(123)];
+ [matcher equal:theValue(123)];
+ STAssertTrue([matcher evaluate], @"expected positive match");
+}
+
+- (void)testItShouldMatchNumberBoxedValuesWithKiwiBoxedValues {
+ id matcher = [KWEqualMatcher matcherWithSubject:[NSNumber numberWithInteger:123]];
+ [matcher equal:theValue(123)];
+ STAssertTrue([matcher evaluate], @"expected positive match");
+}
+
+- (void)testItShouldMatchKiwiBoxedValuesWithNumberBoxedValues {
+ id matcher = [KWEqualMatcher matcherWithSubject:theValue(123)];
+ [matcher equal:[NSNumber numberWithInteger:123]];
+ STAssertTrue([matcher evaluate], @"expected positive match");
+}
+
@end
#endif // #if KW_TESTS_ENABLED

0 comments on commit e443475

Please sign in to comment.