Skip to content

Commit

Permalink
Merge pull request #455 from Emailrus/feature/matcherEnhancement
Browse files Browse the repository at this point in the history
Added conformsToProtocol feature for typhoon matcher
  • Loading branch information
etolstoy committed Dec 1, 2015
2 parents 61b0015 + acc94aa commit 48c8f9b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
/** If 'option' is member of class 'optionClass' then use 'injection' */
- (void)caseMemberOfClass:(Class)optionClass use:(id)injection;

/** If 'option' conforms to protocol 'optionProtocol' then use 'injection' */
- (void)caseConformsToProtocol:(Protocol *)optionProtocol use:(id)injection;

/** When matcher can't match injection from optionValue, use 'injection' */
- (void)defaultUse:(id)injection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ @interface TyphoonOptionMatch : NSObject
@property (nonatomic, strong) id value;
@property (nonatomic) Class memberClass;
@property (nonatomic) Class kindClass;
@property (nonatomic) Protocol *conformProtocol;
@property (nonatomic, strong) id<TyphoonInjection> injection;

+ (id)matchWithValue:(id)value injection:(id)injection;
+ (id)matchWithKindOfClass:(Class)clazz injection:(id)injection;
+ (id)matchWithMemberOfClass:(Class)clazz injection:(id)injection;
+ (id)matchWithConformsToProtocol:(Protocol *)conformProtocol injection:(id)injection;

@end

Expand Down Expand Up @@ -62,6 +64,14 @@ + (id)matchWithMemberOfClass:(Class)clazz injection:(id)injection
return match;
}

+ (id)matchWithConformsToProtocol:(Protocol *)conformProtocol injection:(id)injection
{
TyphoonOptionMatch *match = [TyphoonOptionMatch new];
match.conformProtocol = conformProtocol;
match.injection = TyphoonMakeInjectionFromObjectIfNeeded(injection);
return match;
}

@end


Expand Down Expand Up @@ -103,6 +113,11 @@ - (void)caseMemberOfClass:(Class)optionClass use:(id)injection
[_matches addObject:[TyphoonOptionMatch matchWithMemberOfClass:optionClass injection:injection]];
}

- (void)caseConformsToProtocol:(Protocol *)optionProtocol use:(id)injection
{
[_matches addObject:[TyphoonOptionMatch matchWithConformsToProtocol:optionProtocol injection:injection]];
}

- (void)useDefinitionWithKeyMatchedOptionValue
{
_useMatchingByName = YES;
Expand Down Expand Up @@ -141,7 +156,8 @@ - (TyphoonOptionMatch *)matchForValue:(id)value
BOOL isEqual = (match.value && [match.value isEqual:value]) || ([match.value isEqual:[TyphoonOptionMatchNilValue class]] && !value);
BOOL isKind = (match.kindClass && [value isKindOfClass:match.kindClass]);
BOOL isMember = (match.memberClass && [value isMemberOfClass:match.memberClass]);
if (isEqual || isKind || isMember) {
BOOL doesConform = (match.conformProtocol && [value conformsToProtocol:match.conformProtocol]);
if (isEqual || isKind || isMember || doesConform) {
return match;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

- (id)definitionMatchedByCustomMatcherFromOption:(NSString *)option withString:(NSString *)string;

- (id)definitionMatchedByCustomMatcherFromProtocolOption:(NSObject *)option;

- (id)definitionMatchedByCustomInjectionsMatcherFromOption:(NSString *)option withString:(NSString *)string;

- (id)definitionWithCircularDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ - (id)definitionMatchedByCustomMatcherFromOption:(NSString *)option withString:(
}];
}

- (id)definitionMatchedByCustomMatcherFromProtocolOption:(NSObject *)option
{
return [TyphoonDefinition withOption:option matcher:^(TyphoonOptionMatcher *matcher) {
[matcher caseConformsToProtocol:@protocol(NSFastEnumeration) use:[self trueString]];
[matcher defaultUse:[self zeroString]];
}];
}

- (id)stringWithText:(NSString *)text
{
return [TyphoonDefinition withClass:[NSString class] configuration:^(TyphoonDefinition *definition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ - (void)test_definition_with_custom_matcher_and_name
XCTAssertEqualObjects([assembly definitionMatchedByCustomMatcherOrNameFromOption:@"nothing"], @"ZERO");
XCTAssertEqualObjects([assembly definitionMatchedByCustomMatcherOrNameFromOption:@"trueString"], @"TRUE");
XCTAssertEqualObjects([assembly definitionMatchedByCustomMatcherOrNameFromOption:@"falseString"], @"FALSE");
XCTAssertEqualObjects([assembly definitionMatchedByCustomMatcherFromProtocolOption:[NSArray new]], @"TRUE");
XCTAssertEqualObjects([assembly definitionMatchedByCustomMatcherFromProtocolOption:[NSString new]], @"ZERO");
}

- (void)test_definition_cant_match_useDefault
Expand Down

0 comments on commit 48c8f9b

Please sign in to comment.