Last active
April 21, 2016 00:22
-
-
Save alanf/827798bce11b09f501179b48325a2743 to your computer and use it in GitHub Desktop.
XCTestCase (Debugging)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <objc/runtime.h> | |
@implementation XCTestCase (Debugging) | |
+ (XCTestSuite *)debug_defaultTestSuite { | |
NSString *className = NSStringFromClass(self); | |
// Only run tests that start with SQR. | |
if ([className rangeOfString:@"SQR"].location != 0) { | |
return nil; | |
} | |
// Calls the original method. | |
return [self debug_defaultTestSuite]; | |
} | |
+ (void)load { | |
// Swizzle defaultTestSuite | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
Class class = object_getClass((id)self); | |
SEL originalSelector = @selector(defaultTestSuite); | |
SEL swizzledSelector = @selector(debug_defaultTestSuite); | |
Method originalMethod = class_getClassMethod(class, originalSelector); | |
Method swizzledMethod = class_getClassMethod(class, swizzledSelector); | |
BOOL didAddMethod = | |
class_addMethod(class, | |
originalSelector, | |
method_getImplementation(swizzledMethod), | |
method_getTypeEncoding(swizzledMethod)); | |
if (didAddMethod) { | |
class_replaceMethod(class, | |
swizzledSelector, | |
method_getImplementation(originalMethod), | |
method_getTypeEncoding(originalMethod)); | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod); | |
} | |
}); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment