Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
36 Examples/ExampleSpec.m
@@ -8,23 +8,19 @@
8 8 #import "TestClasses.h"
9 9 #import "Kiwi.h"
10 10
  11 +/**
  12 + * Due to the way the compiler works, in order to call dynamically created
  13 + * methods (like our custom mathcers) directly without using performSelector:,
  14 + * the compiler needs to know that the method exists.
  15 + */
  16 +@interface NSObject (UserDefinedMatchers)
  17 +- (void)haveFighters;
  18 +@end
  19 +
11 20 #if KW_TESTS_ENABLED && KW_BLOCKS_ENABLED
12 21
13 22 SPEC_BEGIN(ExampleSpec)
14 23
15   -//[KWMatchers defineMatcher:@"beUppercaseString" as:^(KWMatcherBuilder *builder) {
16   -// [builder match:^(id subject) {
17   -// if ([subject isKindOfClass:[NSString class]]) {
18   -// NSString *string = (NSString *)subject;
19   -// return [[string uppercaseString] isEqualToString:string];
20   -// } else {
21   -// return NO;
22   -// }
23   -// }];
24   -//}];
25   -//
26   -// [[@"SOME_STRING" should] beUppercaseString]
27   -
28 24 describe(@"Cruiser", ^{
29 25 registerMatchers(@"KWT");
30 26
@@ -66,6 +62,20 @@
66 62 [cruiser raiseWithName:@"FooException" description:@"Foo"];
67 63 }) should] raiseWithName:@"FooException"];
68 64 });
  65 +
  66 + defineMatcher(@"haveFighters", ^(KWUserDefinedMatcherBuilder *builder) {
  67 + [builder match:^(id subject) {
  68 + if (![subject isKindOfClass:[Cruiser class]]) {
  69 + return NO;
  70 + }
  71 + Cruiser *cruiser = subject;
  72 + return cruiser.fighters.count > 0;
  73 + }];
  74 + });
  75 +
  76 + it(@"should have fighters (using custom matcher)", ^{
  77 + [[cruiser should] haveFighters];
  78 + });
69 79 });
70 80 });
71 81
2  Kiwi.xcodeproj/project.pbxproj
@@ -417,7 +417,7 @@
417 417 F5BDB86D11C0CAA000DD3474 /* KWAfterAllNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWAfterAllNode.h; sourceTree = "<group>"; };
418 418 F5BDB86E11C0CAA000DD3474 /* KWAfterAllNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWAfterAllNode.m; sourceTree = "<group>"; };
419 419 F5C36E91115C9F0700425FDA /* KWReceiveMatcherTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWReceiveMatcherTest.m; sourceTree = "<group>"; };
420   - F5C4393D11BAB1A200615054 /* ExampleSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleSpec.m; sourceTree = "<group>"; };
  420 + F5C4393D11BAB1A200615054 /* ExampleSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleSpec.m; sourceTree = "<group>"; tabWidth = 4; };
421 421 F5C5CB1911BCC45B0076529B /* KWExampleGroupBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWExampleGroupBuilder.h; sourceTree = "<group>"; };
422 422 F5C5CB1A11BCC45B0076529B /* KWExampleGroupBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWExampleGroupBuilder.m; sourceTree = "<group>"; };
423 423 F5C6FB1C1177CAF00068BBC8 /* KWStubTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWStubTest.m; sourceTree = "<group>"; };
4 Kiwi/KWUserDefinedMatcher.m
@@ -117,7 +117,9 @@ - (void)match:(KWUserDefinedMatcherBlock)block {
117 117 }
118 118
119 119 - (KWUserDefinedMatcher *)buildMatcherWithSubject:(id)subject {
120   - return [KWUserDefinedMatcher matcherWithSubject:subject block:matcherBlock];
  120 + KWUserDefinedMatcher *matcher = [KWUserDefinedMatcher matcherWithSubject:subject block:matcherBlock];
  121 + matcher.selector = selector;
  122 + return matcher;
121 123 }
122 124
123 125 @end
1  Kiwi/Kiwi.h
@@ -49,6 +49,7 @@
49 49 #import "KWItNode.h"
50 50 #import "KWMatchVerifier.h"
51 51 #import "KWMatcher.h"
  52 +#import "KWMatchers.h"
52 53 #import "KWMatcherFactory.h"
53 54 #import "KWMatching.h"
54 55 #import "KWMessagePattern.h"
1  Kiwi/KiwiMacros.h
@@ -46,6 +46,7 @@
46 46 #define it(...) itWithCallSite(KW_THIS_CALLSITE, __VA_ARGS__)
47 47 #define pending(...) pendingWithCallSite(KW_THIS_CALLSITE, __VA_ARGS__)
48 48 #define xit(...) pendingWithCallSite(KW_THIS_CALLSITE, __VA_ARGS__)
  49 + #define defineMatcher(selectorString, block) [KWMatchers defineMatcher:selectorString as:block]
49 50 #endif // #if KW_BLOCKS_ENABLED
50 51
51 52 // If a gcc compatible compiler is available, use the statement and
10 Tests/KWUserDefinedMatcherTest.m
@@ -66,7 +66,7 @@ @interface KWUserDefinedMatcherBuilderTest : SenTestCase
66 66
67 67 @implementation KWUserDefinedMatcherBuilderTest
68 68
69   -- (void)testCanDefineTheBlockToEvaluate
  69 +- (void)testShouldDefineTheBlockToEvaluate
70 70 {
71 71 KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builder];
72 72
@@ -78,6 +78,14 @@ - (void)testCanDefineTheBlockToEvaluate
78 78 STAssertTrue([matcher evaluate], @"expected positive match");
79 79 }
80 80
  81 +- (void)testShouldSetTheSelectorForTheMatcher
  82 +{
  83 + KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builderForSelector:NSSelectorFromString(@"dummySelector")];
  84 + KWUserDefinedMatcher *matcher = [builder buildMatcherWithSubject:@"foo"];
  85 + STAssertEquals(NSSelectorFromString(@"dummySelector"), matcher.selector, @"should set selector");
  86 +
  87 +}
  88 +
81 89 @end
82 90
83 91 #endif

0 comments on commit 924bc5a

Please sign in to comment.
Something went wrong with that request. Please try again.