Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Wire up the user defined matcher example with a bit of syntatic sugar.

Unfortunately, in order to call the matcher directly, it does need to be declared
in the interface to satisfy the compiler.
  • Loading branch information...
commit 924bc5a0648eca193be8bcdda9702219ff28b384 1 parent 5605114
Luke Redpath authored
View
36 Examples/ExampleSpec.m
@@ -8,23 +8,19 @@
#import "TestClasses.h"
#import "Kiwi.h"
+/**
+ * Due to the way the compiler works, in order to call dynamically created
+ * methods (like our custom mathcers) directly without using performSelector:,
+ * the compiler needs to know that the method exists.
+ */
+@interface NSObject (UserDefinedMatchers)
+- (void)haveFighters;
+@end
+
#if KW_TESTS_ENABLED && KW_BLOCKS_ENABLED
SPEC_BEGIN(ExampleSpec)
-//[KWMatchers defineMatcher:@"beUppercaseString" as:^(KWMatcherBuilder *builder) {
-// [builder match:^(id subject) {
-// if ([subject isKindOfClass:[NSString class]]) {
-// NSString *string = (NSString *)subject;
-// return [[string uppercaseString] isEqualToString:string];
-// } else {
-// return NO;
-// }
-// }];
-//}];
-//
-// [[@"SOME_STRING" should] beUppercaseString]
-
describe(@"Cruiser", ^{
registerMatchers(@"KWT");
@@ -66,6 +62,20 @@
[cruiser raiseWithName:@"FooException" description:@"Foo"];
}) should] raiseWithName:@"FooException"];
});
+
+ defineMatcher(@"haveFighters", ^(KWUserDefinedMatcherBuilder *builder) {
+ [builder match:^(id subject) {
+ if (![subject isKindOfClass:[Cruiser class]]) {
+ return NO;
+ }
+ Cruiser *cruiser = subject;
+ return cruiser.fighters.count > 0;
+ }];
+ });
+
+ it(@"should have fighters (using custom matcher)", ^{
+ [[cruiser should] haveFighters];
+ });
});
});
View
2  Kiwi.xcodeproj/project.pbxproj
@@ -417,7 +417,7 @@
F5BDB86D11C0CAA000DD3474 /* KWAfterAllNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWAfterAllNode.h; sourceTree = "<group>"; };
F5BDB86E11C0CAA000DD3474 /* KWAfterAllNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWAfterAllNode.m; sourceTree = "<group>"; };
F5C36E91115C9F0700425FDA /* KWReceiveMatcherTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWReceiveMatcherTest.m; sourceTree = "<group>"; };
- F5C4393D11BAB1A200615054 /* ExampleSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleSpec.m; sourceTree = "<group>"; };
+ F5C4393D11BAB1A200615054 /* ExampleSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleSpec.m; sourceTree = "<group>"; tabWidth = 4; };
F5C5CB1911BCC45B0076529B /* KWExampleGroupBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWExampleGroupBuilder.h; sourceTree = "<group>"; };
F5C5CB1A11BCC45B0076529B /* KWExampleGroupBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWExampleGroupBuilder.m; sourceTree = "<group>"; };
F5C6FB1C1177CAF00068BBC8 /* KWStubTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWStubTest.m; sourceTree = "<group>"; };
View
4 Kiwi/KWUserDefinedMatcher.m
@@ -117,7 +117,9 @@ - (void)match:(KWUserDefinedMatcherBlock)block {
}
- (KWUserDefinedMatcher *)buildMatcherWithSubject:(id)subject {
- return [KWUserDefinedMatcher matcherWithSubject:subject block:matcherBlock];
+ KWUserDefinedMatcher *matcher = [KWUserDefinedMatcher matcherWithSubject:subject block:matcherBlock];
+ matcher.selector = selector;
+ return matcher;
}
@end
View
1  Kiwi/Kiwi.h
@@ -49,6 +49,7 @@
#import "KWItNode.h"
#import "KWMatchVerifier.h"
#import "KWMatcher.h"
+#import "KWMatchers.h"
#import "KWMatcherFactory.h"
#import "KWMatching.h"
#import "KWMessagePattern.h"
View
1  Kiwi/KiwiMacros.h
@@ -46,6 +46,7 @@
#define it(...) itWithCallSite(KW_THIS_CALLSITE, __VA_ARGS__)
#define pending(...) pendingWithCallSite(KW_THIS_CALLSITE, __VA_ARGS__)
#define xit(...) pendingWithCallSite(KW_THIS_CALLSITE, __VA_ARGS__)
+ #define defineMatcher(selectorString, block) [KWMatchers defineMatcher:selectorString as:block]
#endif // #if KW_BLOCKS_ENABLED
// If a gcc compatible compiler is available, use the statement and
View
10 Tests/KWUserDefinedMatcherTest.m
@@ -66,7 +66,7 @@ @interface KWUserDefinedMatcherBuilderTest : SenTestCase
@implementation KWUserDefinedMatcherBuilderTest
-- (void)testCanDefineTheBlockToEvaluate
+- (void)testShouldDefineTheBlockToEvaluate
{
KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builder];
@@ -78,6 +78,14 @@ - (void)testCanDefineTheBlockToEvaluate
STAssertTrue([matcher evaluate], @"expected positive match");
}
+- (void)testShouldSetTheSelectorForTheMatcher
+{
+ KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builderForSelector:NSSelectorFromString(@"dummySelector")];
+ KWUserDefinedMatcher *matcher = [builder buildMatcherWithSubject:@"foo"];
+ STAssertEquals(NSSelectorFromString(@"dummySelector"), matcher.selector, @"should set selector");
+
+}
+
@end
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.