Skip to content

Commit

Permalink
Fixed the beEvaluated match verifier to work with va_arg through KWMa…
Browse files Browse the repository at this point in the history
…tchVerifier

KWMessagePatternFunctionalTests -> extended the tests
KWBeEvaluatedMatcherTest -> added additional matcher strings
KWMessagePattern -> fixed style
KWBeEvaluatedMatcher -> fxied the va_arg issue with KWMatchVerifier forwarding
  • Loading branch information
trimmurrti committed May 2, 2016
1 parent c1f73b0 commit 1d448fd
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Classes/Core/KWMessagePattern.m
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ - (NSArray *)argumentFiltersWithFirstArgumentFilter:(id)firstArgumentFilter
NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
[array addObject:(firstArgumentFilter != nil) ? firstArgumentFilter : [KWNull null]];

for (NSUInteger i = 1; i < count; ++i)
{
for (NSUInteger i = 1; i < count; ++i) {
id object = va_arg(argumentList, id);
[array addObject:(object != nil) ? object : [KWNull null]];
}
Expand Down
11 changes: 11 additions & 0 deletions Classes/Matchers/KWBeEvaluatedMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "KWMessageTrackerMatcher.h"
#import "KWMatchVerifier.h"

@interface KWBeEvaluatedMatcher : KWMessageTrackerMatcher

Expand All @@ -21,3 +22,13 @@
- (void)beEvaluatedWithCountAtMost:(NSUInteger)aCount arguments:(id)firstArgument, ...;

@end

@interface KWMatchVerifier (KWBeEvaluatedMatcherAdditions)

// NSInvocation doesn't work with va_arg, so we have to implement it directly in match verifier
- (void)beEvaluatedWithArguments:(id)firstArgument, ...;
- (void)beEvaluatedWithCount:(NSUInteger)aCount arguments:(id)firstArgument, ...;
- (void)beEvaluatedWithCountAtLeast:(NSUInteger)aCount arguments:(id)firstArgument, ...;
- (void)beEvaluatedWithCountAtMost:(NSUInteger)aCount arguments:(id)firstArgument, ...;

@end
62 changes: 57 additions & 5 deletions Classes/Matchers/KWBeEvaluatedMatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ + (NSArray *)matcherStrings {
@"beEvaluatedWithArguments:",
@"beEvaluatedWithCount:arguments:",
@"beEvaluatedWithCountAtLeast:arguments:",
@"beEvaluatedWithCountAtMost:arguments:"];
@"beEvaluatedWithCountAtMost:arguments:",
@"beEvaluatedWithUnspecifiedCountOfMessagePattern:",
@"beEvaluatedWithMessagePattern:countType:count:"];
}

#pragma mark - Configuring Matchers
Expand All @@ -51,7 +53,7 @@ - (void)beEvaluatedWithCountAtMost:(NSUInteger)aCount {
- (void)beEvaluatedWithCountType:(KWCountType)aCountType count:(NSUInteger)aCount {
id pattern = [KWBlockMessagePattern messagePatternWithSignature:[self subjectSignature]];

[self receiveMessagePattern:pattern countType:aCountType count:aCount];
[self beEvaluatedWithMessagePattern:pattern countType:aCountType count:aCount];
}

- (void)beEvaluatedWithUnspecifiedCountOfMessagePattern:(KWBlockMessagePattern *)messagePattern {
Expand All @@ -65,7 +67,8 @@ - (void)beEvaluatedWithUnspecifiedCountOfMessagePattern:(KWBlockMessagePattern *
va_start(listName, firstArgument);

- (void)beEvaluatedWithArguments:(id)firstArgument, ... {
KWStartVAListWithVariableName(argumentList);
va_list argumentList;
va_start(argumentList, firstArgument);

id pattern = [KWBlockMessagePattern messagePatternWithSignature:[self subjectSignature]
firstArgumentFilter:firstArgument
Expand All @@ -80,7 +83,7 @@ - (void)beEvaluatedWithArguments:(id)firstArgument, ... {
id pattern = [KWBlockMessagePattern messagePatternWithSignature:[self subjectSignature] \
firstArgumentFilter:firstArgument \
argumentList:argumentList]; \
[self receiveMessagePattern:pattern countType:aCountType count:aCount]; \
[self beEvaluatedWithMessagePattern:pattern countType:aCountType count:aCount]; \
} while(0)

- (void)beEvaluatedWithCount:(NSUInteger)aCount arguments:(id)firstArgument, ... {
Expand All @@ -100,7 +103,7 @@ - (void)beEvaluatedWithCountAtMost:(NSUInteger)aCount arguments:(id)firstArgumen

#pragma mark - Message Pattern Receiving

- (void)receiveMessagePattern:(KWBlockMessagePattern *)aMessagePattern countType:(KWCountType)aCountType count:(NSUInteger)aCount {
- (void)beEvaluatedWithMessagePattern:(KWBlockMessagePattern *)aMessagePattern countType:(KWCountType)aCountType count:(NSUInteger)aCount {
#if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
@try {
#endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
Expand Down Expand Up @@ -132,3 +135,52 @@ - (NSMethodSignature *)subjectSignature {
}

@end

@implementation KWMatchVerifier (KWBeEvaluatedMatcherAdditions)

#pragma mark - Verifying

#define KWStartVAListWithVariableName(listName) \
va_list listName; \
va_start(listName, firstArgument);

- (void)beEvaluatedWithArguments:(id)firstArgument, ... {
KWStartVAListWithVariableName(argumentList)

id pattern = [KWBlockMessagePattern messagePatternWithSignature:[self beEvaluated_subjectSignature]
firstArgumentFilter:firstArgument
argumentList:argumentList];

[(id)self beEvaluatedWithUnspecifiedCountOfMessagePattern:pattern];
}

#define KWReceiveVAListMessagePatternWithCountType(aCountType) \
do { \
KWStartVAListWithVariableName(argumentList); \
id pattern = [KWBlockMessagePattern messagePatternWithSignature:[self beEvaluated_subjectSignature] \
firstArgumentFilter:firstArgument \
argumentList:argumentList]; \
[(id)self beEvaluatedWithMessagePattern:pattern countType:aCountType count:aCount]; \
} while(0)

- (void)beEvaluatedWithCount:(NSUInteger)aCount arguments:(id)firstArgument, ... {
KWReceiveVAListMessagePatternWithCountType(KWCountTypeExact);
}

- (void)beEvaluatedWithCountAtLeast:(NSUInteger)aCount arguments:(id)firstArgument, ... {
KWReceiveVAListMessagePatternWithCountType(KWCountTypeAtLeast);
}

- (void)beEvaluatedWithCountAtMost:(NSUInteger)aCount arguments:(id)firstArgument, ... {
KWReceiveVAListMessagePatternWithCountType(KWCountTypeAtMost);
}

#undef KWReceiveVAListMessagePatternWithCountType
#undef KWArgumentList

- (NSMethodSignature *)beEvaluated_subjectSignature {
return [self.subject methodSignature];
}

@end

4 changes: 3 additions & 1 deletion Tests/KWBeEvaluatedMatcherTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ - (void)testItShouldHaveTheRightMatcherStrings {
@"beEvaluatedWithArguments:",
@"beEvaluatedWithCount:arguments:",
@"beEvaluatedWithCountAtLeast:arguments:",
@"beEvaluatedWithCountAtMost:arguments:"];
@"beEvaluatedWithCountAtMost:arguments:",
@"beEvaluatedWithUnspecifiedCountOfMessagePattern:",
@"beEvaluatedWithMessagePattern:countType:count:"];

XCTAssertEqualObjects([matcherStrings sortedArrayUsingSelector:@selector(compare:)],
[expectedStrings sortedArrayUsingSelector:@selector(compare:)],
Expand Down
60 changes: 54 additions & 6 deletions Tests/KWMessagePatternFunctionalTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,80 @@
#import "KiwiTestConfiguration.h"
#import "TestClasses.h"

typedef void(^KWTestBlock)(id);
typedef void(^KWVoidTestBlock)();
typedef void(^KWArgumentTestBlock)(id);
typedef void(^KWMultiArgumentTestBlock)(id, id, id);

SPEC_BEGIN(KWMessagePatternFunctionalTests)

describe(@"message patterns", ^{
NSString *description = @"description";
NSString *format = nil;

Cruiser *cruiser = [Cruiser new];

it(@"can match a selector with no arguments", ^{
[[cruiser should] receive:@selector(computeParsecs)];

[cruiser computeParsecs];
});

it(@"can match a selector with a specific single argument", ^{
Cruiser *cruiser = [Cruiser new];
Fighter *fighter = [Fighter mock];

[[cruiser should] receive:@selector(loadFighter:) withArguments:fighter];

[cruiser loadFighter:fighter];
});

it(@"can match a selector with specific multiple arguments", ^{
[[cruiser should] receive:@selector(raiseWithName:description:) withArguments:description, format];

[cruiser raiseWithName:description description:format];
});

it(@"can match a selector with multiple arguments", ^{
[[cruiser should] receive:@selector(raiseWithName:description:)];

[cruiser raiseWithName:description description:format];
});
});

describe(@"block message patterns", ^{
id argument1 = [NSObject new];
id argument2 = [NSObject new];

it(@"can match a call with a call without arguments", ^{
id block = theBlockProxy(^{ });

[[block should] beEvaluated];

((KWVoidTestBlock)block)();
});

it(@"can match a call with a specific single argument", ^{
id argument = [NSObject new];
id block = theBlockProxy(^(id object) { [object description]; });
id block = theBlockProxy(^(id object) { });

[[block should] beEvaluatedWithArguments:argument];
[[block should] beEvaluatedWithArguments:argument1];

((KWTestBlock)block)(argument);
((KWArgumentTestBlock)block)(argument1);
});

it(@"can match a call with specific multiple arguments", ^{
id block = theBlockProxy(^(id object1, id object2, id object3) { });

[[block should] beEvaluatedWithArguments:argument1, nil, argument2];

((KWMultiArgumentTestBlock)block)(argument1, nil, argument2);
});

it(@"can match a call with multiple arguments", ^{
id block = theBlockProxy(^(id object1, id object2, id object3) { });

[[block should] beEvaluated];

((KWMultiArgumentTestBlock)block)(argument1, nil, argument2);
});
});

SPEC_END

0 comments on commit 1d448fd

Please sign in to comment.