forked from kiwi-bdd/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKWContainStringMatcher.m
89 lines (66 loc) · 2.08 KB
/
KWContainStringMatcher.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// KWContainStringMatcher.m
// Kiwi
//
// Created by Kristopher Johnson on 4/28/13.
// Copyright (c) 2013 Allen Ding. All rights reserved.
//
#import "KWContainStringMatcher.h"
#import "KWFormatter.h"
@interface KWContainStringMatcher ()
@property (nonatomic, copy) NSString *substring;
@property (nonatomic) NSStringCompareOptions options;
@end
@implementation KWContainStringMatcher
#pragma mark -
#pragma mark Getting Matcher Strings
+ (NSArray *)matcherStrings {
return @[@"containString:",
@"containString:options:",
@"startWithString:",
@"endWithString:"];
}
#pragma mark -
#pragma mark Matching
- (BOOL)evaluate {
NSString *subjectString = (NSString *)self.subject;
if (![subjectString isKindOfClass:[NSString class]]) {
[NSException raise:@"KWMatcherException" format:@"subject is not a string"];
return NO;
}
NSRange range = [subjectString rangeOfString:self.substring options:self.options];
return (range.location != NSNotFound);
}
#pragma mark -
#pragma mark Getting Failure Messages
- (NSString *)failureMessageForShould {
return [NSString stringWithFormat:@"%@ did not contain string \"%@\"",
[KWFormatter formatObject:self.subject],
self.substring];
}
- (NSString *)failureMessageForShouldNot {
return [NSString stringWithFormat:@"expected subject not to contain string \"%@\"",
self.substring];
}
- (NSString *)description {
return [NSString stringWithFormat:@"contain substring \"%@\"", self.substring];
}
#pragma mark -
#pragma mark Configuring matchers
- (void)containString:(NSString *)substring {
self.substring = substring;
self.options = 0;
}
- (void)containString:(NSString *)substring options:(NSStringCompareOptions)options {
self.substring = substring;
self.options = options;
}
- (void)startWithString:(NSString *)prefix {
self.substring = prefix;
self.options = NSAnchoredSearch;
}
- (void)endWithString:(NSString *)suffix {
self.substring = suffix;
self.options = NSAnchoredSearch | NSBackwardsSearch;
}
@end