This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
/
EXTSwizzleTest.m
96 lines (76 loc) · 4.11 KB
/
EXTSwizzleTest.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
//
// EXTSwizzleTest.m
// libextobjc
//
// Created by Justin Spahr-Summers on 2010-08-15.
// Copyright (C) 2012 Justin Spahr-Summers.
// Released under the MIT license.
//
#import "EXTSwizzleTest.h"
@protocol JunkForCompilerMethodPrototypeInfo
@optional
- (void)oldDealloc;
- (id)oldSelf;
+ (Class)oldClass;
@end
/* Test category for macro manipulation */
@interface NSURLRequest (SwizzleTestCategory)
- (id)replacementSelf;
+ (Class)replacementClass;
@end
@implementation NSURLRequest (SwizzleTestCategory)
- (void)replacementDealloc {
[(id)self oldDealloc];
}
- (id)replacementSelf {
return [NSString string];
}
+ (Class)replacementClass {
return [NSString class];
}
@end
/* End test category for macro manipulation */
@implementation EXTSwizzleTests
- (void)testInstanceMethodSwap {
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
STAssertNotNil(request, @"could not initialize NSURLRequest test object");
STAssertTrue([request respondsToSelector:@selector(replacementSelf)], @"NSURLRequest category did not load at test case startup");
STAssertTrueNoThrow([[request replacementSelf] isKindOfClass:[NSString class]], @"expected -replacementSelf of NSURLRequest to return a string");
STAssertEquals(request, [request self], @"expected -self of NSURLRequest object to match itself");
EXT_SWIZZLE_INSTANCE_METHODS(
NSURLRequest,
self,
replacementSelf,
oldSelf
);
STAssertTrueNoThrow([[request self] isKindOfClass:[NSString class]], @"expected -self of NSURLRequest object to return a string after swapping implementation");
STAssertTrue([request respondsToSelector:@selector(oldSelf)], @"expected NSURLRequest to respond to -oldSelf after renaming -self");
STAssertEquals([(id)request oldSelf], request, @"expected -oldSelf of NSURLRequest object to match itself after swapping implementation");
request = nil;
// ensure that superclass implementations are intact
NSObject *obj = [[NSObject alloc] init];
STAssertNotNil(obj, @"could not initialize NSObject test object");
STAssertFalse([obj respondsToSelector:@selector(replacementSelf)], @"NSURLRequest category is available on NSObject, when it shouldn't be");
STAssertFalse([obj respondsToSelector:@selector(oldSelf)], @"expected NSObject not to respond to -oldSelf even after renaming subclass method");
STAssertEquals(obj, [obj self], @"expected -self of NSObject object to match itself even after renaming subclass method");
obj = nil;
}
- (void)testClassMethodSwap {
STAssertEqualObjects([NSURLRequest class], NSClassFromString(@"NSURLRequest"), @"expected [NSURLRequest class] to equal NSURLRequest");
STAssertTrue([NSURLRequest respondsToSelector:@selector(replacementClass)], @"NSURLRequest category did not load at test case startup");
STAssertEqualObjects([NSURLRequest replacementClass], [NSString class], @"expected [NSURLRequest replacementClass] to equal [NSString class]");
EXT_SWIZZLE_CLASS_METHODS(
NSURLRequest,
class,
replacementClass,
oldClass
);
STAssertEqualObjects([NSURLRequest class], [NSString class], @"expected [NSURLRequest class] to equal [NSString class] after swapping out implementation");
STAssertTrue([NSURLRequest respondsToSelector:@selector(oldClass)], @"expected NSURLRequest to respond to +oldClass after renaming +class");
STAssertTrueNoThrow([[NSClassFromString(@"NSURLRequest") oldClass] isEqual:NSClassFromString(@"NSURLRequest")], @"expected [NSURLRequest oldClass] to be a valid method that equals NSURLRequest");
// ensure that superclass implemenations are intact
STAssertEqualObjects([NSObject class], NSClassFromString(@"NSObject"), @"expected [NSObject class] to equal NSObject");
STAssertFalse([NSObject respondsToSelector:@selector(replacementClass)], @"NSURLRequest category is available on NSObject, when it shouldn't be");
STAssertFalse([NSObject respondsToSelector:@selector(oldClass)], @"expected NSObject not to respond to +oldClass after renaming +class in subclass");
}
@end