main
1//
2// Licensed under the terms in License.txt
3//
4// Copyright 2010 Allen Ding. All rights reserved.
5//
6
7#import "KWContainMatcher.h"
8#import "KWFormatter.h"
9#import "KWGenericMatchingAdditions.h"
10
11@interface KWContainMatcher()
12
13#pragma mark -
14#pragma mark Properties
15
16@property (nonatomic, readwrite, retain) id objects;
17
18@end
19
20@implementation KWContainMatcher
21
22#pragma mark -
23#pragma mark Initializing
24
25- (void)dealloc {
26 [objects release];
27 [super dealloc];
28}
29
30#pragma mark -
31#pragma mark Properties
32
33@synthesize objects;
34
35#pragma mark -
36#pragma mark Getting Matcher Strings
37
38+ (NSArray *)matcherStrings {
39 return @[@"contain:", @"containObjectsInArray:"];
40}
41
42#pragma mark -
43#pragma mark Matching
44
45- (BOOL)evaluate {
46 if (![self.subject respondsToSelector:@selector(containsObjectEqualToOrMatching:)])
47 [NSException raise:@"KWMatcherException" format:@"subject does not respond to -containsObjectEqualToOrMatching:"];
48
49 for (id object in self.objects) {
50 if (![self.subject containsObjectEqualToOrMatching:object])
51 return NO;
52 }
53
54 return YES;
55}
56
57#pragma mark -
58#pragma mark Getting Failure Messages
59
60- (NSString *)objectsPhrase {
61 if ([self.objects count] == 1)
62 return [KWFormatter formatObject:(self.objects)[0]];
63
64 return [NSString stringWithFormat:@"all of %@", [KWFormatter formatObject:self.objects]];
65}
66
67- (NSString *)failureMessageForShould {
68 return [NSString stringWithFormat:@"expected subject to contain %@", [self objectsPhrase]];
69}
70
71- (NSString *)description
72{
73 return [NSString stringWithFormat:@"contain %@", [self objectsPhrase]];
74}
75
76#pragma mark -
77#pragma mark Configuring Matchers
78
79- (void)contain:(id)anObject {
80 self.objects = @[anObject];
81}
82
83- (void)containObjectsInArray:(NSArray *)anArray {
84 self.objects = anArray;
85}
86
87@end
88
89@implementation KWMatchVerifier(KWContainMatcherAdditions)
90
91#pragma mark -
92#pragma mark Verifying
93
94- (void)containObjects:(id)firstObject, ... {
95 NSMutableArray *objects = [NSMutableArray array];
96
97 va_list argumentList;
98 va_start(argumentList, firstObject);
99 id object = firstObject;
100
101 while (object != nil) {
102 [objects addObject:object];
103 object = va_arg(argumentList, id);
104 }
105
106 va_end(argumentList);
107 [(id)self containObjectsInArray:objects];
108}
109
110@end