-
Notifications
You must be signed in to change notification settings - Fork 602
/
OCMArg.m
145 lines (123 loc) · 3.22 KB
/
OCMArg.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2009-2021 Erik Doernenburg and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use these files except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#import <objc/runtime.h>
#import "OCMArg.h"
#import "OCMBlockArgCaller.h"
#import "OCMConstraint.h"
#import "OCMPassByRefSetter.h"
@implementation OCMArg
+ (id)any
{
return [OCMAnyConstraint constraint];
}
+ (void *)anyPointer
{
return (void *)0x01234567;
}
+ (id __autoreleasing *)anyObjectRef
{
return (id *)[self anyPointer];
}
+ (SEL)anySelector
{
return NSSelectorFromString(@"aSelectorThatMatchesAnySelector");
}
+ (id)isNil
{
return [OCMIsNilConstraint constraint];
}
+ (id)isNotNil
{
return [OCMIsNotNilConstraint constraint];
}
+ (id)isEqual:(id)value
{
return value;
}
+ (id)isNotEqual:(id)value
{
OCMIsNotEqualConstraint *constraint = [OCMIsNotEqualConstraint constraint];
constraint->testValue = value;
return constraint;
}
+ (id)isKindOfClass:(Class)cls
{
return [[[OCMBlockConstraint alloc] initWithConstraintBlock:^BOOL(id obj) {
return [obj isKindOfClass:cls];
}] autorelease];
}
+ (id)checkWithSelector:(SEL)selector onObject:(id)anObject
{
return [OCMConstraint constraintWithSelector:selector onObject:anObject];
}
+ (id)checkWithBlock:(BOOL (^)(id))block
{
return [[[OCMBlockConstraint alloc] initWithConstraintBlock:block] autorelease];
}
+ (id *)setTo:(id)value
{
return (id *)[[[OCMPassByRefSetter alloc] initWithValue:value] autorelease];
}
+ (void *)setToValue:(NSValue *)value
{
return (id *)[[[OCMPassByRefSetter alloc] initWithValue:value] autorelease];
}
+ (id)invokeBlock
{
return [[[OCMBlockArgCaller alloc] init] autorelease];
}
+ (id)invokeBlockWithArgs:(id)first, ... NS_REQUIRES_NIL_TERMINATION
{
NSMutableArray *params = [NSMutableArray array];
va_list args;
if(first)
{
[params addObject:first];
va_start(args, first);
id obj;
while((obj = va_arg(args, id)))
{
[params addObject:obj];
}
va_end(args);
}
return [[[OCMBlockArgCaller alloc] initWithBlockArguments:params] autorelease];
}
+ (id)defaultValue
{
return [NSNull null];
}
+ (id)resolveSpecialValues:(NSValue *)value
{
const char *type = [value objCType];
if(type[0] == '^')
{
void *pointer = [value pointerValue];
if(pointer == [self anyPointer])
return [OCMArg any];
if((pointer != NULL) && [OCMPassByRefSetter isPassByRefSetterInstance:pointer])
return (id)pointer;
}
else if(type[0] == ':')
{
SEL selector;
[value getValue:&selector];
if(selector == NSSelectorFromString(@"aSelectorThatMatchesAnySelector"))
return [OCMArg any];
}
return value;
}
@end