Permalink
Browse files

Improving conformTo matcher

  • Loading branch information...
1 parent c8b5d9a commit 39af76d1dcd5ad890084437fcc3e7b23a577adc2 @robb robb committed Oct 11, 2013
@@ -25,8 +25,8 @@
4913B4D61411E45800040ECB /* EXPMatchers+beGreaterThanOrEqualTo.h in Headers */ = {isa = PBXBuildFile; fileRef = 4913B4D31411E45800040ECB /* EXPMatchers+beGreaterThanOrEqualTo.h */; settings = {ATTRIBUTES = (Public, ); }; };
4913B4D71411E45800040ECB /* EXPMatchers+beGreaterThanOrEqualTo.m in Sources */ = {isa = PBXBuildFile; fileRef = 4913B4D41411E45800040ECB /* EXPMatchers+beGreaterThanOrEqualTo.m */; };
4913B4D81411E45800040ECB /* EXPMatchers+beGreaterThanOrEqualTo.m in Sources */ = {isa = PBXBuildFile; fileRef = 4913B4D41411E45800040ECB /* EXPMatchers+beGreaterThanOrEqualTo.m */; };
- 541B02AC1805DFD9000DA87C /* EXPMatchers+conformTo.h in Headers */ = {isa = PBXBuildFile; fileRef = 541B02AA1805DFD9000DA87C /* EXPMatchers+conformTo.h */; };
- 541B02AD1805DFD9000DA87C /* EXPMatchers+conformTo.h in Headers */ = {isa = PBXBuildFile; fileRef = 541B02AA1805DFD9000DA87C /* EXPMatchers+conformTo.h */; };
+ 541B02AC1805DFD9000DA87C /* EXPMatchers+conformTo.h in Headers */ = {isa = PBXBuildFile; fileRef = 541B02AA1805DFD9000DA87C /* EXPMatchers+conformTo.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 541B02AD1805DFD9000DA87C /* EXPMatchers+conformTo.h in Headers */ = {isa = PBXBuildFile; fileRef = 541B02AA1805DFD9000DA87C /* EXPMatchers+conformTo.h */; settings = {ATTRIBUTES = (Public, ); }; };
541B02AE1805DFD9000DA87C /* EXPMatchers+conformTo.m in Sources */ = {isa = PBXBuildFile; fileRef = 541B02AB1805DFD9000DA87C /* EXPMatchers+conformTo.m */; };
541B02AF1805DFD9000DA87C /* EXPMatchers+conformTo.m in Sources */ = {isa = PBXBuildFile; fileRef = 541B02AB1805DFD9000DA87C /* EXPMatchers+conformTo.m */; };
541B02B11805E24C000DA87C /* EXPMatchers+conformToTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 541B02B01805E24C000DA87C /* EXPMatchers+conformToTest.m */; };
@@ -3,17 +3,30 @@
#import <objc/runtime.h>
EXPMatcherImplementationBegin(conformTo, (Protocol *expected)) {
+ BOOL actualIsNil = (actual == nil);
+ BOOL expectedIsNil = (expected == nil);
+
+ prerequisite(^BOOL{
+ return !(actualIsNil || expectedIsNil);
+ });
+
match(^BOOL{
return [actual conformsToProtocol:expected];
});
- NSString *name = [NSString stringWithUTF8String:protocol_getName(expected)];
-
failureMessageForTo(^NSString *{
+ if(actualIsNil) return @"the object is nil/null";
+ if(expectedIsNil) return @"the protocol is nil/null";
+
+ NSString *name = NSStringFromProtocol(expected);
return [NSString stringWithFormat:@"expected: %@ to conform to %@", actual, name];
});
failureMessageForNotTo(^NSString *{
+ if(actualIsNil) return @"the object is nil/null";
+ if(expectedIsNil) return @"the protocol is nil/null";
+
+ NSString *name = NSStringFromProtocol(expected);
return [NSString stringWithFormat:@"expected: %@ not to conform to %@", actual, name];
});
}
@@ -19,6 +19,9 @@ - (void)test_conformTo {
assertFail(test_expect([Foo class]).conformTo(@protocol(Protocol)), @"expected: Foo to conform to Protocol");
assertFail(test_expect(foo).conformTo(@protocol(Protocol)), ([NSString stringWithFormat:@"expected: <Foo: %p> to conform to Protocol", foo]));
+
+ assertFail(test_expect(nil).conformTo(@protocol(Protocol)), @"the object is nil/null");
+ assertFail(test_expect([Foo class]).conformTo(nil), @"the protocol is nil/null");
}
- (void)test_toNot_conformTo {
@@ -27,6 +30,9 @@ - (void)test_toNot_conformTo {
assertFail(test_expect([Baz class]).notTo.conformTo(@protocol(Protocol)), @"expected: Baz not to conform to Protocol");
assertFail(test_expect(baz).notTo.conformTo(@protocol(Protocol)), ([NSString stringWithFormat:@"expected: <Baz: %p> not to conform to Protocol", baz]));
+
+ assertFail(test_expect(nil).notTo.conformTo(@protocol(Protocol)), @"the object is nil/null");
+ assertFail(test_expect([Foo class]).notTo.conformTo(nil), @"the protocol is nil/null");
}
@end

0 comments on commit 39af76d

Please sign in to comment.