OLD | NEW |
| (Empty) |
1 //------------------------------------------------------------------------------
--------- | |
2 // $Id: NSInvocationOCMAdditionsTests.m 55 2009-10-16 06:42:18Z erik $ | |
3 // Copyright (c) 2006-2008 by Mulle Kybernetik. See License file for details. | |
4 //------------------------------------------------------------------------------
--------- | |
5 | |
6 #import "NSInvocationOCMAdditionsTests.h" | |
7 #import "NSInvocation+OCMAdditions.h" | |
8 | |
9 #define TestString @"foo" | |
10 #define TestInt 1 | |
11 | |
12 @implementation NSInvocationOCMAdditionsTests | |
13 | |
14 - (void)testInvocationDescriptionWithNoArguments | |
15 { | |
16 SEL selector = @selector(lowercaseString); | |
17 NSMethodSignature *signature = [[NSString string] methodSignatureForSele
ctor:selector]; | |
18 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
19 [invocation setSelector:selector]; | |
20 | |
21 STAssertEqualObjects(@"lowercaseString", [invocation invocationDescripti
on], @""); | |
22 } | |
23 | |
24 - (void)testInvocationDescriptionWithObjectArgument | |
25 { | |
26 SEL selector = @selector(isEqualToNumber:); | |
27 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
28 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
29 [invocation setSelector:selector]; | |
30 // Give it one argument (starts at index 2) | |
31 NSNumber *argument = [NSNumber numberWithInt:TestInt]; | |
32 [invocation setArgument:&argument atIndex:2]; | |
33 | |
34 NSString *expected = [NSString stringWithFormat:@"isEqualToNumber:%d", T
estInt]; | |
35 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
36 } | |
37 | |
38 - (void)testInvocationDescriptionWithNSStringArgument | |
39 { | |
40 SEL selector = @selector(isEqualToString:); | |
41 NSMethodSignature *signature = [[NSString string] methodSignatureForSele
ctor:selector]; | |
42 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
43 [invocation setSelector:selector]; | |
44 // Give it one argument (starts at index 2) | |
45 NSString *argument = [NSString stringWithString:TestString]; | |
46 [invocation setArgument:&argument atIndex:2]; | |
47 | |
48 NSString *expected = [NSString stringWithFormat:@"isEqualToString:@\"%@\
"", TestString]; | |
49 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
50 } | |
51 | |
52 - (void)testInvocationDescriptionWithObjectArguments | |
53 { | |
54 SEL selector = @selector(setValue:forKey:); | |
55 NSMethodSignature *signature = [[NSArray array] methodSignatureForSelect
or:selector]; | |
56 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
57 [invocation setSelector:selector]; | |
58 // Give it two arguments | |
59 NSNumber *argumentOne = [NSNumber numberWithInt:TestInt]; | |
60 NSString *argumentTwo = [NSString stringWithString:TestString]; | |
61 [invocation setArgument:&argumentOne atIndex:2]; | |
62 [invocation setArgument:&argumentTwo atIndex:3]; | |
63 | |
64 NSString *expected = [NSString stringWithFormat:@"setValue:%d forKey:@\"
%@\"", TestInt, TestString]; | |
65 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
66 } | |
67 | |
68 - (void)testInvocationDescriptionWithArrayArgument | |
69 { | |
70 SEL selector = @selector(addObjectsFromArray:); | |
71 NSMethodSignature *signature = [[NSMutableArray array] methodSignatureFo
rSelector:selector]; | |
72 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
73 [invocation setSelector:selector]; | |
74 // Give it one argument (starts at index 2) | |
75 NSArray *argument = [NSArray arrayWithObject:TestString]; | |
76 [invocation setArgument:&argument atIndex:2]; | |
77 | |
78 NSString *expected = [NSString stringWithFormat:@"addObjectsFromArray:%@
", [argument description]]; | |
79 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
80 } | |
81 | |
82 - (void)testInvocationDescriptionWithIntArgument | |
83 { | |
84 SEL selector = @selector(initWithInt:); | |
85 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
86 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
87 [invocation setSelector:selector]; | |
88 // Give it an argument | |
89 int argumentOne = TestInt; | |
90 [invocation setArgument:&argumentOne atIndex:2]; | |
91 | |
92 NSString *expected = [NSString stringWithFormat:@"initWithInt:%d", TestI
nt]; | |
93 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
94 } | |
95 | |
96 - (void)testInvocationDescriptionWithUnsignedIntArgument | |
97 { | |
98 SEL selector = @selector(initWithUnsignedInt:); | |
99 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
100 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
101 [invocation setSelector:selector]; | |
102 // Give it an argument | |
103 unsigned int argumentOne = TestInt; | |
104 [invocation setArgument:&argumentOne atIndex:2]; | |
105 | |
106 NSString *expected = [NSString stringWithFormat:@"initWithUnsignedInt:%d
", TestInt]; | |
107 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
108 } | |
109 | |
110 - (void)testInvocationDescriptionWithBoolArgument | |
111 { | |
112 SEL selector = @selector(initWithBool:); | |
113 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
114 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
115 [invocation setSelector:selector]; | |
116 // Give it an argument | |
117 BOOL argumentOne = TRUE; | |
118 [invocation setArgument:&argumentOne atIndex:2]; | |
119 | |
120 NSString *expected = [NSString stringWithFormat:@"initWithBool:YES"]; | |
121 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
122 } | |
123 | |
124 - (void)testInvocationDescriptionWithCharArgument | |
125 { | |
126 SEL selector = @selector(initWithChar:); | |
127 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
128 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
129 [invocation setSelector:selector]; | |
130 // Give it an argument | |
131 char argumentOne = 'd'; | |
132 [invocation setArgument:&argumentOne atIndex:2]; | |
133 | |
134 NSString *expected = [NSString stringWithFormat:@"initWithChar:'%c'", ar
gumentOne]; | |
135 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
136 } | |
137 | |
138 - (void)testInvocationDescriptionWithUnsignedCharArgument | |
139 { | |
140 NSNumber *dummyNumber = [NSNumber alloc]; | |
141 | |
142 SEL selector = @selector(initWithUnsignedChar:); | |
143 NSMethodSignature *signature = [dummyNumber methodSignatureForSelector:s
elector]; | |
144 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
145 [invocation setSelector:selector]; | |
146 // Give it an argument | |
147 unsigned char argumentOne = 'd'; | |
148 [invocation setArgument:&argumentOne atIndex:2]; | |
149 | |
150 NSString *expected = [NSString stringWithFormat:@"initWithUnsignedChar:'
%c'", argumentOne]; | |
151 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
152 } | |
153 | |
154 - (void)testInvocationDescriptionWithDoubleArgument | |
155 { | |
156 SEL selector = @selector(initWithDouble:); | |
157 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
158 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
159 [invocation setSelector:selector]; | |
160 // Give it an argument | |
161 double argumentOne = 1; | |
162 [invocation setArgument:&argumentOne atIndex:2]; | |
163 | |
164 NSString *expected = [NSString stringWithFormat:@"initWithDouble:%f", ar
gumentOne]; | |
165 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
166 } | |
167 | |
168 - (void)testInvocationDescriptionWithFloatArgument | |
169 { | |
170 SEL selector = @selector(initWithFloat:); | |
171 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
172 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
173 [invocation setSelector:selector]; | |
174 // Give it an argument | |
175 float argumentOne = 1; | |
176 [invocation setArgument:&argumentOne atIndex:2]; | |
177 | |
178 NSString *expected = [NSString stringWithFormat:@"initWithFloat:%f", arg
umentOne]; | |
179 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
180 } | |
181 | |
182 - (void)testInvocationDescriptionWithLongArgument | |
183 { | |
184 SEL selector = @selector(initWithLong:); | |
185 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
186 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
187 [invocation setSelector:selector]; | |
188 // Give it an argument | |
189 long argumentOne = 1; | |
190 [invocation setArgument:&argumentOne atIndex:2]; | |
191 | |
192 NSString *expected = [NSString stringWithFormat:@"initWithLong:%d", argu
mentOne]; | |
193 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
194 } | |
195 | |
196 - (void)testInvocationDescriptionWithUnsignedLongArgument | |
197 { | |
198 SEL selector = @selector(initWithUnsignedLong:); | |
199 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
200 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
201 [invocation setSelector:selector]; | |
202 // Give it an argument | |
203 unsigned long argumentOne = 1; | |
204 [invocation setArgument:&argumentOne atIndex:2]; | |
205 | |
206 NSString *expected = [NSString stringWithFormat:@"initWithUnsignedLong:%
u", argumentOne]; | |
207 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
208 } | |
209 | |
210 - (void)testInvocationDescriptionWithLongLongArgument | |
211 { | |
212 SEL selector = @selector(initWithLongLong:); | |
213 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
214 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
215 [invocation setSelector:selector]; | |
216 // Give it an argument | |
217 long long argumentOne = 1; | |
218 [invocation setArgument:&argumentOne atIndex:2]; | |
219 | |
220 NSString *expected = [NSString stringWithFormat:@"initWithLongLong:%qi",
argumentOne]; | |
221 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
222 } | |
223 | |
224 - (void)testInvocationDescriptionWithUnsignedLongLongArgument | |
225 { | |
226 SEL selector = @selector(initWithUnsignedLongLong:); | |
227 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
228 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
229 [invocation setSelector:selector]; | |
230 // Give it an argument | |
231 unsigned long long argumentOne = 1; | |
232 [invocation setArgument:&argumentOne atIndex:2]; | |
233 | |
234 NSString *expected = [NSString stringWithFormat:@"initWithUnsignedLongLo
ng:%qu", argumentOne]; | |
235 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
236 } | |
237 | |
238 - (void)testInvocationDescriptionWithShortArgument | |
239 { | |
240 SEL selector = @selector(initWithShort:); | |
241 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
242 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
243 [invocation setSelector:selector]; | |
244 // Give it an argument | |
245 short argumentOne = 1; | |
246 [invocation setArgument:&argumentOne atIndex:2]; | |
247 | |
248 NSString *expected = [NSString stringWithFormat:@"initWithShort:%hi", ar
gumentOne]; | |
249 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
250 } | |
251 | |
252 - (void)testInvocationDescriptionWithUnsignedShortArgument | |
253 { | |
254 SEL selector = @selector(initWithUnsignedShort:); | |
255 NSMethodSignature *signature = [[NSNumber alloc] methodSignatureForSelec
tor:selector]; | |
256 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
257 [invocation setSelector:selector]; | |
258 // Give it an argument | |
259 unsigned short argumentOne = 1; | |
260 [invocation setArgument:&argumentOne atIndex:2]; | |
261 | |
262 NSString *expected = [NSString stringWithFormat:@"initWithUnsignedShort:
%hu", argumentOne]; | |
263 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
264 } | |
265 /* | |
266 - (void)testInvocationDescriptionWithStructArgument | |
267 { | |
268 SEL selector = @selector(setFrameSize:); | |
269 NSMethodSignature *signature = [[[NSView alloc] init] methodSignatureFor
Selector:selector]; | |
270 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
271 [invocation setSelector:selector]; | |
272 // Give it an argument | |
273 NSSize size; | |
274 size.width = 1; | |
275 size.height = 1; | |
276 [invocation setArgument:&size atIndex:2]; | |
277 | |
278 NSString *expected = [NSString stringWithFormat:@"setFrameSize:(struct)"
]; | |
279 STAssertTrue(expected, [[invocation invocationDescription] rangeOfString
:description].length > 0, @""); | |
280 } | |
281 */ | |
282 /* | |
283 - (void)testInvocationDescriptionWithCStringArgument | |
284 { | |
285 SEL selector = @selector(initWithUTF8String:); | |
286 NSMethodSignature *signature = [[NSString alloc] methodSignatureForSelec
tor:selector]; | |
287 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
288 [invocation setSelector:selector]; | |
289 // Give it an argument | |
290 NSString *string = [NSString stringWithString:@"foo"]; | |
291 const char *cString = [string UTF8String]; | |
292 [invocation setArgument:&cString atIndex:2]; | |
293 | |
294 NSString *expected = @"initWithUTF8String:\"foo\""; | |
295 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
296 } | |
297 */ | |
298 - (void)testInvocationDescriptionWithSelectorArgument | |
299 { | |
300 SEL selector = @selector(respondsToSelector:); | |
301 NSMethodSignature *signature = [[NSString alloc] methodSignatureForSelec
tor:selector]; | |
302 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
303 [invocation setSelector:selector]; | |
304 // Give it an argument | |
305 SEL selectorValue = @selector(foo); | |
306 [invocation setArgument:&selectorValue atIndex:2]; | |
307 | |
308 NSString *expected = [NSString stringWithFormat:@"respondsToSelector:@se
lector(%@)", NSStringFromSelector(selectorValue)]; | |
309 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
310 } | |
311 | |
312 - (void)testInvocationDescriptionWithPointerArgument | |
313 { | |
314 SEL selector = @selector(initWithBytes:length:); | |
315 NSMethodSignature *signature = [[NSData alloc] methodSignatureForSelecto
r:selector]; | |
316 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
317 [invocation setSelector:selector]; | |
318 // Give it an argument | |
319 NSData *data = [@"foo" dataUsingEncoding:NSUTF8StringEncoding]; | |
320 const void *bytes = [[@"foo" dataUsingEncoding:NSUTF8StringEncoding] byt
es]; | |
321 NSUInteger length = [data length]; | |
322 [invocation setArgument:&bytes atIndex:2]; | |
323 [invocation setArgument:&length atIndex:3]; | |
324 | |
325 NSString *expected1 = [NSString stringWithFormat:@"initWithBytes:"]; | |
326 NSString *expected2 = [NSString stringWithFormat:@"length:%d", length]; | |
327 NSString *invocationDescription = [invocation invocationDescription]; | |
328 STAssertTrue([invocationDescription rangeOfString:expected1].length > 0,
@""); | |
329 STAssertTrue([invocationDescription rangeOfString:expected2].length > 0,
@""); | |
330 } | |
331 | |
332 - (void)testInvocationDescriptionWithNilArgument | |
333 { | |
334 SEL selector = @selector(initWithString:); | |
335 NSMethodSignature *signature = [[NSString alloc] methodSignatureForSelec
tor:selector]; | |
336 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | |
337 [invocation setSelector:selector]; | |
338 // Give it an argument | |
339 NSString *argString = nil; | |
340 [invocation setArgument:&argString atIndex:2]; | |
341 | |
342 NSString *expected = [NSString stringWithFormat:@"initWithString:nil"]; | |
343 STAssertEqualObjects(expected, [invocation invocationDescription], @""); | |
344 } | |
345 | |
346 @end | |
OLD | NEW |