Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Allow failure messages to be customized when creating dynamic matchers.

  • Loading branch information...
commit 8737c71a0804a4ea00f1f5bc8a0a5c837e1b7213 1 parent 924bc5a
Luke Redpath authored
2  Kiwi.xcodeproj/project.pbxproj
@@ -261,7 +261,7 @@
261 261 A352EA1812EDC8160049C691 /* KWHamcrestMatcher.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWHamcrestMatcher.m; sourceTree = "<group>"; };
262 262 A352EA1A12EDC8380049C691 /* KWHamcrestMatcherTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWHamcrestMatcherTest.m; sourceTree = "<group>"; };
263 263 A385CAE713AA7EA200DCA951 /* KWUserDefinedMatcherTest.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = KWUserDefinedMatcherTest.m; sourceTree = "<group>"; tabWidth = 4; };
264   - A385CAE913AA7ED800DCA951 /* KWUserDefinedMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWUserDefinedMatcher.h; sourceTree = "<group>"; };
  264 + A385CAE913AA7ED800DCA951 /* KWUserDefinedMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.h; path = KWUserDefinedMatcher.h; sourceTree = "<group>"; tabWidth = 4; };
265 265 A385CAEA13AA7ED800DCA951 /* KWUserDefinedMatcher.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = KWUserDefinedMatcher.m; sourceTree = "<group>"; tabWidth = 4; };
266 266 A385CAED13AAC9B600DCA951 /* KWMatchers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWMatchers.h; sourceTree = "<group>"; };
267 267 A385CAEE13AAC9B700DCA951 /* KWMatchers.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = KWMatchers.m; sourceTree = "<group>"; tabWidth = 4; };
2  Kiwi/KWMatcher.h
@@ -8,7 +8,7 @@
8 8 #import "KWMatching.h"
9 9
10 10 @interface KWMatcher : NSObject<KWMatching> {
11   -@private
  11 +@protected
12 12 id subject;
13 13 }
14 14
27 Kiwi/KWUserDefinedMatcher.h
@@ -16,25 +16,44 @@ typedef BOOL (^KWUserDefinedMatcherBlock)();
16 16 KWUserDefinedMatcherBlock matcherBlock;
17 17 SEL selector;
18 18 NSInvocation *invocation;
  19 + NSString *failureMessageForShould;
  20 + NSString *failureMessageForShouldNot;
19 21 }
20 22 @property (nonatomic, assign) SEL selector;
  23 +@property (nonatomic, copy) NSString *failureMessageForShould;
  24 +@property (nonatomic, copy) NSString *failureMessageForShouldNot;
  25 +@property (nonatomic, assign) KWUserDefinedMatcherBlock matcherBlock;
21 26
22   -+ (id)matcherWithSubject:(id)subject block:(KWUserDefinedMatcherBlock)aBlock;
23   -- (id)initWithSubject:(id)subject block:(KWUserDefinedMatcherBlock)aBlock;
  27 ++ (id)matcherWithSubject:(id)aSubject block:(KWUserDefinedMatcherBlock)aBlock;
  28 +- (id)initWithSubject:(id)aSubject block:(KWUserDefinedMatcherBlock)aBlock;
  29 +- (void)setSubject:(id)aSubject;
24 30 @end
25 31
26 32 #pragma mark -
27 33
  34 +typedef NSString * (^KWUserDefinedMatcherMessageBlock)(id);
  35 +
28 36 @interface KWUserDefinedMatcherBuilder : NSObject
29 37 {
30   - KWUserDefinedMatcherBlock matcherBlock;
31   - SEL selector;
  38 + KWUserDefinedMatcher *matcher;
  39 + KWUserDefinedMatcherMessageBlock failureMessageForShouldBlock;
  40 + KWUserDefinedMatcherMessageBlock failureMessageForShouldNotBlock;
32 41 }
33 42 @property (nonatomic, readonly) NSString *key;
34 43
35 44 + (id)builder;
36 45 + (id)builderForSelector:(SEL)aSelector;
37 46 - (id)initWithSelector:(SEL)aSelector;
  47 +
  48 +#pragma mark -
  49 +#pragma mark Configuring The Matcher
  50 +
38 51 - (void)match:(KWUserDefinedMatcherBlock)block;
  52 +- (void)failureMessageForShould:(KWUserDefinedMatcherMessageBlock)block;
  53 +- (void)failureMessageForShouldNot:(KWUserDefinedMatcherMessageBlock)block;
  54 +
  55 +#pragma mark -
  56 +#pragma mark Buiding The Matcher
  57 +
39 58 - (KWUserDefinedMatcher *)buildMatcherWithSubject:(id)subject;
40 59 @end
61 Kiwi/KWUserDefinedMatcher.m
@@ -11,15 +11,18 @@
11 11 @implementation KWUserDefinedMatcher
12 12
13 13 @synthesize selector;
  14 +@synthesize failureMessageForShould;
  15 +@synthesize failureMessageForShouldNot;
  16 +@synthesize matcherBlock;
14 17
15   -+ (id)matcherWithSubject:(id)subject block:(KWUserDefinedMatcherBlock)aBlock
  18 ++ (id)matcherWithSubject:(id)aSubject block:(KWUserDefinedMatcherBlock)aBlock
16 19 {
17   - return [[[self alloc] initWithSubject:subject block:aBlock] autorelease];
  20 + return [[[self alloc] initWithSubject:aSubject block:aBlock] autorelease];
18 21 }
19 22
20   -- (id)initWithSubject:(id)subject block:(KWUserDefinedMatcherBlock)aBlock
  23 +- (id)initWithSubject:(id)aSubject block:(KWUserDefinedMatcherBlock)aBlock
21 24 {
22   - if ((self = [super initWithSubject:subject])) {
  25 + if ((self = [super initWithSubject:aSubject])) {
23 26 matcherBlock = [aBlock copy];
24 27 }
25 28 return self;
@@ -46,6 +49,13 @@ - (BOOL)evaluate
46 49 return result;
47 50 }
48 51
  52 +- (void)setSubject:(id)aSubject {
  53 + if (aSubject != subject) {
  54 + [subject release];
  55 + subject = [aSubject retain];
  56 + }
  57 +}
  58 +
49 59 #pragma mark -
50 60 #pragma mark Message forwarding
51 61
@@ -102,23 +112,54 @@ + (id)builderForSelector:(SEL)aSelector {
102 112
103 113 - (id)initWithSelector:(SEL)aSelector {
104 114 if ((self = [super init])) {
105   - selector = aSelector;
  115 + matcher = [[KWUserDefinedMatcher alloc] init];
  116 + matcher.selector = aSelector;
106 117 }
107 118 return self;
108 119 }
109 120
  121 +- (void)dealloc
  122 +{
  123 + [matcher release];
  124 + [failureMessageForShouldBlock release];
  125 + [super dealloc];
  126 +}
  127 +
110 128 - (NSString *)key {
111   - return NSStringFromSelector(selector);
  129 + return NSStringFromSelector(matcher.selector);
112 130 }
113 131
  132 +#pragma mark -
  133 +#pragma mark Configuring The Matcher
  134 +
114 135 - (void)match:(KWUserDefinedMatcherBlock)block {
115   - Block_release(matcherBlock);
116   - matcherBlock = Block_copy(block);
  136 + matcher.matcherBlock = block;
117 137 }
118 138
  139 +- (void)failureMessageForShould:(KWUserDefinedMatcherMessageBlock)block {
  140 + [failureMessageForShouldBlock release];
  141 + failureMessageForShouldBlock = [block copy];
  142 +}
  143 +
  144 +- (void)failureMessageForShouldNot:(KWUserDefinedMatcherMessageBlock)block {
  145 + [failureMessageForShouldNotBlock release];
  146 + failureMessageForShouldNotBlock = [block copy];
  147 +}
  148 +
  149 +#pragma mark -
  150 +#pragma mark Buiding The Matcher
  151 +
119 152 - (KWUserDefinedMatcher *)buildMatcherWithSubject:(id)subject {
120   - KWUserDefinedMatcher *matcher = [KWUserDefinedMatcher matcherWithSubject:subject block:matcherBlock];
121   - matcher.selector = selector;
  153 + [matcher setSubject:subject];
  154 +
  155 + if (failureMessageForShouldBlock) {
  156 + [matcher setFailureMessageForShould:failureMessageForShouldBlock(subject)];
  157 + }
  158 +
  159 + if (failureMessageForShouldNotBlock) {
  160 + [matcher setFailureMessageForShouldNot:failureMessageForShouldNotBlock(subject)];
  161 + }
  162 +
122 163 return matcher;
123 164 }
124 165
24 Tests/KWUserDefinedMatcherTest.m
@@ -86,6 +86,30 @@ - (void)testShouldSetTheSelectorForTheMatcher
86 86
87 87 }
88 88
  89 +- (void)testCanSetTheFailureMessageForShould
  90 +{
  91 + KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builder];
  92 +
  93 + [builder failureMessageForShould:^(id subject) {
  94 + return [NSString stringWithFormat:@"failure message containing subject %@", subject];
  95 + }];
  96 +
  97 + KWUserDefinedMatcher *matcher = [builder buildMatcherWithSubject:@"foo"];
  98 + STAssertEquals(@"failure message containing subject foo", [matcher failureMessageForShould], @"should set failure message for should");
  99 +}
  100 +
  101 +- (void)testCanSetTheFailureMessageForShouldNot
  102 +{
  103 + KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builder];
  104 +
  105 + [builder failureMessageForShouldNot:^(id subject) {
  106 + return [NSString stringWithFormat:@"failure message containing subject %@", subject];
  107 + }];
  108 +
  109 + KWUserDefinedMatcher *matcher = [builder buildMatcherWithSubject:@"foo"];
  110 + STAssertEquals(@"failure message containing subject foo", [matcher failureMessageForShouldNot], @"should set failure message for should");
  111 +}
  112 +
89 113 @end
90 114
91 115 #endif

0 comments on commit 8737c71

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