forked from hypoyao/GYHttpMock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGYMatcher.m
66 lines (59 loc) · 1.56 KB
/
GYMatcher.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
//
// GYMatcher.m
// GYNetwork
//
// Created by hypo on 16/1/13.
// Copyright © 2016年 hypoyao. All rights reserved.
//
#import "GYMatcher.h"
@implementation GYMatcher
+ (instancetype)GYMatcherWithObject:(id)object
{
if ([object isKindOfClass:[NSString class]]) {
return [[GYMatcher alloc] initWithString:object];
} else if ([object isKindOfClass:[NSData class]]) {
return [[GYMatcher alloc] initWithData:object];
} else if ([object isKindOfClass:[NSRegularExpression class]]) {
return [[GYMatcher alloc] initWithRegex:object];
} else {
return nil;
}
}
- (instancetype)initWithString:(NSString *)string
{
if (self = [super init]) {
_string = string;
_matchType = GYMatcherTypeString;
}
return self;
}
- (instancetype)initWithData:(NSData *)data
{
if (self = [super init]) {
_data = data;
_matchType = GYMatcherTypeData;
}
return self;
}
- (instancetype)initWithRegex:(NSRegularExpression *)regex
{
if (self = [super init]) {
_regex = regex;
_matchType = GYMatcherTypeRegex;
}
return self;
}
- (BOOL)match:(GYMatcher *)matcher
{
switch (self.matchType) {
case GYMatcherTypeString:
return [_string isEqualToString:matcher.string];
case GYMatcherTypeData:
return [_data isEqual:matcher.data];
case GYMatcherTypeRegex:
return [_regex numberOfMatchesInString:matcher.string options:0 range:NSMakeRange(0, matcher.string.length)] > 0;
default:
return NO;
}
}
@end