aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMObjC2RuntimeTest.m
blob: 2e6362c1bdc661498c134b8438505aa5c34b92ae (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//
//  GTMObjC2RuntimeTest.m
//
//  Copyright 2007-2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file 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 "GTMObjC2Runtime.h"
#import "GTMSenTestCase.h"
#import "GTMSystemVersion.h"
#import "GTMTypeCasting.h"

#import <string.h>

@protocol GTMObjC2Runtime_TestProtocol
@end

@protocol GTMObjC2Runtime_Test2Protocol
AT_OPTIONAL
- (NSString*)optional;
AT_REQUIRED
- (NSString*)required;
AT_OPTIONAL
+ (NSString*)class_optional;
AT_REQUIRED
+ (NSString*)class_required;
@end

@interface GTMObjC2RuntimeTest : GTMTestCase {
  Class cls_;
}
@end

@interface GTMObjC2Runtime_TestClass : NSObject <GTMObjC2Runtime_TestProtocol>
- (NSString*)kwyjibo;

@end

@interface GTMObjC2Runtime_TestClass (GMObjC2Runtime_TestClassCategory)
- (NSString*)eatMyShorts;
@end

@implementation GTMObjC2Runtime_TestClass

+ (NSString*)dontHaveACow {
  return @"dontHaveACow";
}

- (NSString*)kwyjibo {
  return @"kwyjibo";
}
@end

@implementation GTMObjC2Runtime_TestClass (GMObjC2Runtime_TestClassCategory)
- (NSString*)eatMyShorts {
  return @"eatMyShorts";
}

+ (NSString*)brokeHisBrain {
  return @"brokeHisBrain";
}

@end

@interface GTMObjC2NotificationWatcher : NSObject
- (void)startedTest:(NSNotification *)notification;
@end

@implementation GTMObjC2NotificationWatcher
- (id)init {
  if ((self = [super init])) {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    // We release ourselves when we are notified.
    [self retain];
    [nc addObserver:self
           selector:@selector(startedTest:)
               name:SenTestSuiteDidStartNotification
             object:nil];

  }
  return self;
}

- (void)startedTest:(NSNotification *)notification {
  // Logs if we are testing on Tiger or Leopard runtime.
  SenTestSuiteRun *suiteRun = GTM_STATIC_CAST(SenTestSuiteRun,
                                              [notification object]);
  NSString *testName = [[suiteRun test] name];
  NSString *className = NSStringFromClass([GTMObjC2RuntimeTest class]);
  if ([testName isEqualToString:className]) {
    NSString *runtimeString;
#ifndef OBJC2_UNAVAILABLE
    runtimeString = @"ObjC1";
#else
    runtimeString = @"ObjC2";
#endif
    NSLog(@"Running GTMObjC2RuntimeTests using %@ runtime.", runtimeString);
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self];
    [self autorelease];
  }
}
@end

@implementation GTMObjC2RuntimeTest

+ (void)initialize {
  // This allows us to track which runtime we are actually testing.
  [[[GTMObjC2NotificationWatcher alloc] init] autorelease];
}

- (void)setUp {
  cls_ = [[GTMObjC2Runtime_TestClass class] retain];
}

- (void)tearDown {
  [cls_ release];
}

- (void)test_object_getClass {
  // Nil Checks
  STAssertNil(object_getClass(nil), nil);

  // Standard use check
  GTMObjC2Runtime_TestClass *test = [[[cls_ alloc] init] autorelease];
  Class cls = object_getClass(test);
  STAssertEqualObjects(cls, cls_, nil);
}

- (void)test_class_getName {
  // Nil Checks
  const char *name = class_getName(nil);
  STAssertEqualCStrings(name, "nil", nil);

  // Standard use check
  STAssertEqualCStrings(class_getName(cls_), "GTMObjC2Runtime_TestClass", nil);
}

- (void)test_class_conformsToProtocol {
  // Nil Checks
  STAssertFalse(class_conformsToProtocol(cls_, @protocol(NSObject)), nil);
  STAssertFalse(class_conformsToProtocol(cls_, nil), nil);
  // The following two tests intentionally commented out as they fail on
  // Leopard with a crash, so we fail on Tiger intentionally as well.
  // STAssertFalse(class_conformsToProtocol(nil, @protocol(NSObject)), nil);
  // STAssertFalse(class_conformsToProtocol(nil, nil), nil);

  // Standard use check
  STAssertTrue(class_conformsToProtocol(cls_,
                                        @protocol(GTMObjC2Runtime_TestProtocol)),
               nil);
}

- (void)test_class_respondsToSelector {
  // Nil Checks
  STAssertFalse(class_respondsToSelector(cls_, @selector(setUp)), nil);
  STAssertFalse(class_respondsToSelector(cls_, nil), nil);

  // Standard use check
  STAssertTrue(class_respondsToSelector(cls_, @selector(kwyjibo)), nil);
}

- (void)test_class_getSuperclass {
  // Nil Checks
  STAssertNil(class_getSuperclass(nil), nil);

  // Standard use check
  STAssertEqualObjects(class_getSuperclass(cls_), [NSObject class], nil);
}

- (void)test_class_copyMethodList {
  // Nil Checks
  Method *list = class_copyMethodList(nil, nil);
  STAssertNULL(list, nil);

  // Standard use check
  list = class_copyMethodList(cls_, nil);
  STAssertNotNULL(list, nil);
  free(list);
  unsigned int count = 0;
  list = class_copyMethodList(cls_, &count);
  STAssertNotNULL(list, nil);
  STAssertEquals(count, 2U, nil);
  STAssertNULL(list[count], nil);
  free(list);

  // Now test meta class
  count = 0;
  list = class_copyMethodList((Class)objc_getMetaClass(class_getName(cls_)),
                              &count);
  STAssertNotNULL(list, nil);
  STAssertEquals(count, 2U, nil);
  STAssertNULL(list[count], nil);
  free(list);
}

- (void)test_method_getName {
  // Nil Checks
  STAssertNULL(method_getName(nil), nil);

  // Standard use check
  Method *list = class_copyMethodList(cls_, nil);
  STAssertNotNULL(list, nil);
  const char* selName1 = sel_getName(method_getName(list[0]));
  const char* selName2 = sel_getName(@selector(kwyjibo));
  const char* selName3 = sel_getName(@selector(eatMyShorts));
  BOOL isGood = ((strcmp(selName1, selName2)) == 0 || (strcmp(selName1, selName3) == 0));
  STAssertTrue(isGood, nil);
  free(list);
}

- (void)test_method_exchangeImplementations {
  // nil checks
  method_exchangeImplementations(nil, nil);

  // Standard use check
  GTMObjC2Runtime_TestClass *test = [[GTMObjC2Runtime_TestClass alloc] init];
  STAssertNotNil(test, nil);

  // Get initial values
  NSString *val1 = [test kwyjibo];
  STAssertNotNil(val1, nil);
  NSString *val2 = [test eatMyShorts];
  STAssertNotNil(val2, nil);
  NSString *val3 = [GTMObjC2Runtime_TestClass dontHaveACow];
  STAssertNotNil(val3, nil);
  NSString *val4 = [GTMObjC2Runtime_TestClass brokeHisBrain];
  STAssertNotNil(val4, nil);

  // exchange the imps
  Method *list = class_copyMethodList(cls_, nil);
  STAssertNotNULL(list, nil);
  method_exchangeImplementations(list[0], list[1]);

  // test against initial values
  NSString *val5 = [test kwyjibo];
  STAssertNotNil(val5, nil);
  NSString *val6 = [test eatMyShorts];
  STAssertNotNil(val6, nil);
  STAssertEqualStrings(val1, val6, nil);
  STAssertEqualStrings(val2, val5, nil);

  // Check that other methods not affected
  STAssertEqualStrings([GTMObjC2Runtime_TestClass dontHaveACow], val3, nil);
  STAssertEqualStrings([GTMObjC2Runtime_TestClass brokeHisBrain], val4, nil);

  // exchange the imps back
  method_exchangeImplementations(list[0], list[1]);

  // and test against initial values again
  NSString *val7 = [test kwyjibo];
  STAssertNotNil(val7, nil);
  NSString *val8 = [test eatMyShorts];
  STAssertNotNil(val8, nil);
  STAssertEqualStrings(val1, val7, nil);
  STAssertEqualStrings(val2, val8, nil);

  method_exchangeImplementations(list[0], nil);
  method_exchangeImplementations(nil, list[0]);

  val7 = [test kwyjibo];
  STAssertNotNil(val7, nil);
  val8 = [test eatMyShorts];
  STAssertNotNil(val8, nil);
  STAssertEqualStrings(val1, val7, nil);
  STAssertEqualStrings(val2, val8, nil);

  free(list);
  [test release];
}

- (void)test_method_getImplementation {
  // Nil Checks
  STAssertNULL(method_getImplementation(nil), nil);

  // Standard use check
  Method *list = class_copyMethodList(cls_, nil);
  STAssertNotNULL(list, nil);
  STAssertNotNULL(method_getImplementation(list[0]), nil);
  free(list);
}

- (void)test_method_setImplementation {
  // Standard use check
  GTMObjC2Runtime_TestClass *test = [[GTMObjC2Runtime_TestClass alloc] init];
  Method *list = class_copyMethodList(cls_, nil);

  // Get initial value
  NSString *str1 = objc_msgSend(test, method_getName(list[0]));
  STAssertNotNil(str1, nil);

  // set the imp to something else
  IMP oldImp = method_setImplementation(list[0], method_getImplementation(list[1]));
  STAssertNotNULL(oldImp, nil);

  // make sure they are different
  NSString *str2 = objc_msgSend(test,method_getName(list[0]));
  STAssertNotNil(str2, nil);
  STAssertNotEqualStrings(str1, str2, nil);

  // reset the imp
  IMP newImp = method_setImplementation(list[0], oldImp);
  STAssertNotEquals(oldImp, newImp, nil);

  // test nils
  // Apparently it was a bug that we could call setImplementation with a nil
  // so we now test to make sure that setting to nil works as expected on
  // all systems.
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  // Built for less then leopard gives us the behaviors we defined...
  // (doesn't take nil)
  IMP nullImp = method_setImplementation(list[0], nil);
  STAssertNULL(nullImp, nil);
  IMP testImp = method_setImplementation(list[0], newImp);
  STAssertEquals(testImp, oldImp, nil);
#else
  // Built for leopard or later means we get the os runtime behavior...
  if ([GTMSystemVersion isLeopard]) {
    // (takes nil)
    oldImp = method_setImplementation(list[0], nil);
    STAssertNotNULL(oldImp, nil);
    newImp = method_setImplementation(list[0], oldImp);
    STAssertNULL(newImp, nil);
  } else {
    // (doesn't take nil)
    IMP nullImp = method_setImplementation(list[0], nil);
    STAssertNULL(nullImp, nil);
    IMP testImp = method_setImplementation(list[0], newImp);
    STAssertEquals(testImp, oldImp, nil);
  }
#endif

  // This case intentionally not tested. Passing nil to method_setImplementation
  // on Leopard crashes. It does on Tiger as well. Half works on SnowLeopard.
  // We made our Tiger implementation the same as the SnowLeopard
  // implementation.
  // Logged as radar 5572981.
  if (![GTMSystemVersion isLeopardOrGreater]) {
    STAssertNULL(method_setImplementation(nil, nil), nil);
  }
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  if ([GTMSystemVersion isSnowLeopardOrGreater]) {
    STAssertNULL(method_setImplementation(nil, newImp), nil);
  }
#endif

  [test release];
  free(list);
}

- (void)test_protocol_getMethodDescription {
  // Check nil cases
  struct objc_method_description desc = protocol_getMethodDescription(nil, nil,
                                                                      YES, YES);
  STAssertNULL(desc.name, nil);
  desc = protocol_getMethodDescription(nil, @selector(optional), YES, YES);
  STAssertNULL(desc.name, nil);
  desc = protocol_getMethodDescription(@protocol(GTMObjC2Runtime_Test2Protocol),
                                       nil, YES, YES);
  STAssertNULL(desc.name, nil);

  // Instance Methods
  // Check Required case. Only OBJC2 supports required.
  desc = protocol_getMethodDescription(@protocol(GTMObjC2Runtime_Test2Protocol),
                                       @selector(optional), YES, YES);
#if OBJC_API_VERSION >= 2
  STAssertNULL(desc.name, nil);
#else
  STAssertNotNULL(desc.name, nil);
#endif

  // Check Required case. Only OBJC2 supports required.
  desc = protocol_getMethodDescription(@protocol(GTMObjC2Runtime_Test2Protocol),
                                       @selector(required), YES, YES);

  STAssertNotNULL(desc.name, nil);

  // Check Optional case. Only OBJC2 supports optional.
  desc = protocol_getMethodDescription(@protocol(GTMObjC2Runtime_Test2Protocol),
                                       @selector(optional), NO, YES);

  STAssertNotNULL(desc.name, nil);

  // Check Optional case. Only OBJC2 supports optional.
  desc = protocol_getMethodDescription(@protocol(GTMObjC2Runtime_Test2Protocol),
                                       @selector(required), NO, YES);
#if OBJC_API_VERSION >= 2
  STAssertNULL(desc.name, nil);
#else
  STAssertNotNULL(desc.name, nil);
#endif

  // Class Methods
  // Check Required case. Only OBJC2 supports required.
  desc = protocol_getMethodDescription(@protocol(GTMObjC2Runtime_Test2Protocol),
                                       @selector(class_optional), YES, NO);
#if OBJC_API_VERSION >= 2
  STAssertNULL(desc.name, nil);
#else
  STAssertNotNULL(desc.name, nil);
#endif

  // Check Required case. Only OBJC2 supports required.
  desc = protocol_getMethodDescription(@protocol(GTMObjC2Runtime_Test2Protocol),
                                       @selector(class_required), YES, NO);

  STAssertNotNULL(desc.name, nil);

  // Check Optional case. Only OBJC2 supports optional.
  desc = protocol_getMethodDescription(@protocol(GTMObjC2Runtime_Test2Protocol),
                                       @selector(class_optional), NO, NO);

  STAssertNotNULL(desc.name, nil);

  // Check Optional case. Only OBJC2 supports optional.
  desc = protocol_getMethodDescription(@protocol(GTMObjC2Runtime_Test2Protocol),
                                       @selector(class_required), NO, NO);
#if OBJC_API_VERSION >= 2
  STAssertNULL(desc.name, nil);
#else
  STAssertNotNULL(desc.name, nil);
#endif

}

- (void)test_sel_isEqual {
  STAssertTrue(sel_isEqual(@selector(kwyjibo), @selector(kwyjibo)), nil);
  STAssertFalse(sel_isEqual(@selector(kwyjibo), @selector(dontHaveACow)), nil);
  STAssertTrue(sel_isEqual(_cmd, @selector(test_sel_isEqual)), nil);
  STAssertTrue(sel_isEqual(_cmd, _cmd), nil);
  STAssertFalse(sel_isEqual(_cmd, @selector(kwyjibo)), nil);
}

@end