diff --git a/BlockInjection/BILib.h b/BlockInjection/BILib.h index d25dd23..283ea47 100644 --- a/BlockInjection/BILib.h +++ b/BlockInjection/BILib.h @@ -28,14 +28,6 @@ + (void)clear; -/** - * Deprecated - */ -+ (BOOL)injectToSelector:(SEL)sel forClass:(Class)class preprocess:(id)preprocess __deprecated; -+ (BOOL)injectToSelector:(SEL)sel forClass:(Class)class postprocess:(id)postprocess __deprecated; -+ (BOOL)injectToSelectorWithMethodName:(NSString*)methodName forClassName:(NSString*)className preprocess:(id)preprocess __deprecated; -+ (BOOL)injectToSelectorWithMethodName:(NSString*)methodName forClassName:(NSString*)className postprocess:(id)postprocess __deprecated; - @end #pragma mark - Inline methods diff --git a/BlockInjection/BILib.m b/BlockInjection/BILib.m index 73fa31d..c8099e7 100644 --- a/BlockInjection/BILib.m +++ b/BlockInjection/BILib.m @@ -341,32 +341,6 @@ + (id)replaceBlockForStructWithSize:(NSUInteger)size withItem:(BIItem*)item } } -#pragma mark - Deprecated Methods - -+ (BOOL)injectToSelector:(SEL)sel forClass:(Class)class preprocess:(id)preprocess -{ - return [BILib injectToSelector:sel forClass:class preprocess:preprocess postprocess:nil]; -} - -+ (BOOL)injectToSelector:(SEL)sel forClass:(Class)class postprocess:(id)postprocess -{ - return [BILib injectToSelector:sel forClass:class preprocess:nil postprocess:postprocess]; -} - -+ (BOOL)injectToSelectorWithMethodName:(NSString*)methodName forClassName:(NSString*)className preprocess:(id)preprocess -{ - Class class = objc_getClass([className UTF8String]); - SEL sel = sel_getUid([methodName UTF8String]); - return [BILib injectToSelector:sel forClass:class preprocess:preprocess]; -} - -+ (BOOL)injectToSelectorWithMethodName:(NSString*)methodName forClassName:(NSString*)className postprocess:(id)postprocess -{ - Class class = objc_getClass([className UTF8String]); - SEL sel = sel_getUid([methodName UTF8String]); - return [BILib injectToSelector:sel forClass:class postprocess:postprocess]; -} - #pragma mark - Inline methods inline NSRegularExpression* BIRegex(NSString* regexString) diff --git a/BlockInjectionTest/BlockInjectionTest.xcodeproj/project.pbxproj b/BlockInjectionTest/BlockInjectionTest.xcodeproj/project.pbxproj index 86fe9b3..05bf67a 100644 --- a/BlockInjectionTest/BlockInjectionTest.xcodeproj/project.pbxproj +++ b/BlockInjectionTest/BlockInjectionTest.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 06584ED216DEFA6000CB2D68 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 06584EB216DEFA6000CB2D68 /* UIKit.framework */; }; 06584ED316DEFA6000CB2D68 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 06584EB416DEFA6000CB2D68 /* Foundation.framework */; }; 06584EF016DEFC8D00CB2D68 /* BILib.m in Sources */ = {isa = PBXBuildFile; fileRef = 06584EEB16DEFAF800CB2D68 /* BILib.m */; }; + 066CDB0917898A490024EDB8 /* EtcTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 066CDB0817898A490024EDB8 /* EtcTest.m */; }; 06941C1016E7344800EDAA36 /* BILibArg.m in Sources */ = {isa = PBXBuildFile; fileRef = 06941C0F16E7344800EDAA36 /* BILibArg.m */; }; 06EE077C1723C573004EBE60 /* BILibUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 06EE077B1723C573004EBE60 /* BILibUtils.m */; }; 06F03DDB177FB42A00745436 /* BasicTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 06F03DCB177FB42A00745436 /* BasicTest.m */; }; @@ -39,6 +40,8 @@ 06584ED016DEFA6000CB2D68 /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; }; 06584EEA16DEFAF800CB2D68 /* BILib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BILib.h; sourceTree = ""; }; 06584EEB16DEFAF800CB2D68 /* BILib.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BILib.m; sourceTree = ""; }; + 066CDB0717898A490024EDB8 /* EtcTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EtcTest.h; sourceTree = ""; }; + 066CDB0817898A490024EDB8 /* EtcTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EtcTest.m; sourceTree = ""; }; 06941C0E16E7344800EDAA36 /* BILibArg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BILibArg.h; sourceTree = ""; }; 06941C0F16E7344800EDAA36 /* BILibArg.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BILibArg.m; sourceTree = ""; }; 06EE077A1723C573004EBE60 /* BILibUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BILibUtils.h; sourceTree = ""; }; @@ -143,6 +146,8 @@ 06F03DD8177FB42A00745436 /* ReturnValueTest.m */, 06F03DD9177FB42A00745436 /* SkippingTest.h */, 06F03DDA177FB42A00745436 /* SkippingTest.m */, + 066CDB0717898A490024EDB8 /* EtcTest.h */, + 066CDB0817898A490024EDB8 /* EtcTest.m */, ); path = Tests; sourceTree = ""; @@ -238,6 +243,7 @@ 06F03DE1177FB42A00745436 /* ReplaceTest.m in Sources */, 06F03DE2177FB42A00745436 /* ReturnValueTest.m in Sources */, 06F03DE3177FB42A00745436 /* SkippingTest.m in Sources */, + 066CDB0917898A490024EDB8 /* EtcTest.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/BlockInjectionTest/Tests/EtcTest.h b/BlockInjectionTest/Tests/EtcTest.h new file mode 100644 index 0000000..28165b1 --- /dev/null +++ b/BlockInjectionTest/Tests/EtcTest.h @@ -0,0 +1,13 @@ +// +// EtcTest.h +// BlockInjectionTest +// +// Created by ytokoro on 7/7/13. +// Copyright (c) 2013 tokorom. All rights reserved. +// + +#import + +@interface EtcTest : SenTestCase + +@end diff --git a/BlockInjectionTest/Tests/EtcTest.m b/BlockInjectionTest/Tests/EtcTest.m new file mode 100644 index 0000000..1498516 --- /dev/null +++ b/BlockInjectionTest/Tests/EtcTest.m @@ -0,0 +1,103 @@ +// +// EtcTest.m +// BlockInjectionTest +// +// Created by ytokoro on 7/7/13. +// Copyright (c) 2013 tokorom. All rights reserved. +// + +#import "EtcTest.h" +#import "BILib.h" +#import "BIItemManager.h" +#import "BIItem.h" +#import +#import "BILibUtils.h" + +typedef struct SuperBigStruct { + char buff[1096]; + bool b; +} SuperBig; + +@interface ClassForEtc : NSObject +@end + +@implementation ClassForEtc +- (SuperBig)superBig { + SuperBig bigStruct; + bigStruct.b = false; + return bigStruct; +} +- (int)intValue { + return 0; +} +@end + +@implementation EtcTest + +- (void)setUp +{ + [super setUp]; + [BILib clear]; +} + +- (void)tearDown +{ + [super tearDown]; +} + +- (void)testCopyBIItemManager +{ + BIItemManager *manager1 = [BIItemManager sharedInstance]; + BIItemManager *manager2 = [manager1 copy]; + + STAssertEqualObjects(manager1, manager2, @"manager1 and manager2 is different"); +} + +- (void)testReturnSuperBigStruct +{ + [BILib injectToClassWithName:@"ClassForEtc" methodName:@"superBig" preprocess:^SuperBig(id target){ + SuperBig st; + st.b = true; + return st; + }]; + + SuperBig ret = [[ClassForEtc new] superBig]; + + STAssertEquals(ret.b, (bool)false, @"ret is invalid."); +} + +- (void)testDeallocBIItem +{ + BIItem* item = [BIItem new]; + int i = 100; + [item prepareWithInvocation:[self.class invocation]]; + [item skipAfterProcessesWithReturnValue:&i]; + + STAssertNotNil(item, @"item is nil"); +} + +- (void)testBIItemOthers +{ + BIItem* item = [BIItem new]; + int i = 100; + [item prepareWithInvocation:[self.class invocation]]; + [item skipAfterProcessesWithReturnValue:&i]; + int i2 = 200; + [item skipAfterProcessesWithReturnValue:&i2]; + [item prepareWithInvocation:nil]; + + STAssertNotNil(item, @"item is nil"); +} + +#pragma mark - Private Methods + ++ (NSInvocation*)invocation +{ + BOOL isClassMethod = NO; + Method method = [BILibUtils getMethodInClass:[ClassForEtc class] selector:@selector(intValue) isClassMethod:&isClassMethod]; + NSMethodSignature* signature = [NSMethodSignature signatureWithObjCTypes:method_getTypeEncoding(method)]; + NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature]; + return invocation; +} + +@end diff --git a/BlockInjectionTest/Tests/ReplaceTest.m b/BlockInjectionTest/Tests/ReplaceTest.m index f251352..52f53b4 100644 --- a/BlockInjectionTest/Tests/ReplaceTest.m +++ b/BlockInjectionTest/Tests/ReplaceTest.m @@ -11,6 +11,10 @@ #pragma mark - SubjectForReplace +@interface SubjectForDummy : NSObject +- (void)instanceMethod2:(id)arg; +@end + @interface SubjectForReplace : NSObject - (void)instanceMethod:(id)arg; + (void)classMethod:(id)arg; @@ -59,6 +63,20 @@ - (void)testReplaceImplementation STAssertEquals(i, 1, @"i is invalid."); } +- (void)testReplaceImplementationForNoMethods +{ + __block int i = 0; + [BILib replaceImplementationForClass:[SubjectForReplace class] selector:@selector(instanceMethod2:) block:^{ + ++i; + }]; + + STAssertEquals(i, 0, @"i is invalid."); + + [[SubjectForReplace new] instanceMethod:@"hello!"]; + + STAssertEquals(i, 0, @"i is invalid."); +} + - (void)testReplaceImplementationWithArg { __block NSString* got = nil; diff --git a/BlockInjectionTest/Tests/ReturnValueTest.m b/BlockInjectionTest/Tests/ReturnValueTest.m index bd13fac..104b0dc 100644 --- a/BlockInjectionTest/Tests/ReturnValueTest.m +++ b/BlockInjectionTest/Tests/ReturnValueTest.m @@ -8,6 +8,7 @@ #import "ReturnValueTest.h" #import "BILib.h" +#import "BILibDummyStruct.h" static int ia[2] = {1, 2}; @@ -52,10 +53,35 @@ - (struct ReturnValueBigStruct)bigStructValue { bigStruct.f = 0.255; return bigStruct; } +- (BILibStruct1)struct1 { BILibStruct1 st; return st; } +- (BILibStruct2)struct2 { BILibStruct2 st; return st; } +- (BILibStruct3)struct3 { BILibStruct3 st; return st; } +- (BILibStruct4)struct4 { BILibStruct4 st; return st; } +- (BILibStruct5)struct5 { BILibStruct5 st; return st; } +- (BILibStruct6)struct6 { BILibStruct6 st; return st; } +- (BILibStruct7)struct7 { BILibStruct7 st; return st; } +- (BILibStruct8)struct8 { BILibStruct8 st; return st; } +- (BILibStruct9)struct9 { BILibStruct9 st; return st; } +- (BILibStruct10)struct10 { BILibStruct10 st; return st; } +- (BILibStruct20)struct20 { BILibStruct20 st; return st; } +- (BILibStruct30)struct30 { BILibStruct30 st; return st; } +- (BILibStruct40)struct40 { BILibStruct40 st; return st; } +- (BILibStruct50)struct50 { BILibStruct50 st; return st; } +- (BILibStruct60)struct60 { BILibStruct60 st; return st; } +- (BILibStruct70)struct70 { BILibStruct70 st; return st; } +- (BILibStruct80)struct80 { BILibStruct80 st; return st; } +- (BILibStruct90)struct90 { BILibStruct90 st; return st; } +- (BILibStruct100)struct100 { BILibStruct100 st; return st; } +- (BILibStruct200)struct200 { BILibStruct200 st; return st; } +- (BILibStruct300)struct300 { BILibStruct300 st; return st; } +- (BILibStruct400)struct400 { BILibStruct400 st; return st; } +- (BILibStruct500)struct500 { BILibStruct500 st; return st; } +- (BILibStruct600)struct600 { BILibStruct600 st; return st; } +- (BILibStruct700)struct700 { BILibStruct700 st; return st; } +- (BILibStruct800)struct800 { BILibStruct800 st; return st; } +- (BILibStruct900)struct900 { BILibStruct900 st; return st; } @end -#pragma mark - Private Methods - @implementation ReturnValueTest - (void)setUp @@ -332,4 +358,145 @@ - (void)testReturnBigStruct STAssertEquals(ret.f, (CGFloat)0.255, @"ret is invalid."); } +- (void)testReturnSomeStruct +{ + __block int count = 0; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct1" preprocess:^BILibStruct1(id target){ + ++count; BILibStruct1 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct2" preprocess:^BILibStruct2(id target){ + ++count; BILibStruct2 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct3" preprocess:^BILibStruct3(id target){ + ++count; BILibStruct3 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct4" preprocess:^BILibStruct4(id target){ + ++count; BILibStruct4 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct5" preprocess:^BILibStruct5(id target){ + ++count; BILibStruct5 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct6" preprocess:^BILibStruct6(id target){ + ++count; BILibStruct6 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct7" preprocess:^BILibStruct7(id target){ + ++count; BILibStruct7 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct8" preprocess:^BILibStruct8(id target){ + ++count; BILibStruct8 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct9" preprocess:^BILibStruct9(id target){ + ++count; BILibStruct9 st; return st; + }]; + + STAssertEquals(count, (int)0, @"count is invalid."); + + ClassForReturnValue* c = [ClassForReturnValue new]; + [c struct1]; + [c struct2]; + [c struct3]; + [c struct4]; + [c struct5]; + [c struct6]; + [c struct7]; + [c struct8]; + [c struct9]; + + STAssertEquals(count, (int)9, @"count is invalid."); +} + +- (void)testReturnSomeStruct10 +{ + __block int count = 0; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct10" preprocess:^BILibStruct10(id target){ + ++count; BILibStruct10 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct20" preprocess:^BILibStruct20(id target){ + ++count; BILibStruct20 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct30" preprocess:^BILibStruct30(id target){ + ++count; BILibStruct30 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct40" preprocess:^BILibStruct40(id target){ + ++count; BILibStruct40 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct50" preprocess:^BILibStruct50(id target){ + ++count; BILibStruct50 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct60" preprocess:^BILibStruct60(id target){ + ++count; BILibStruct60 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct70" preprocess:^BILibStruct70(id target){ + ++count; BILibStruct70 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct80" preprocess:^BILibStruct80(id target){ + ++count; BILibStruct80 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct90" preprocess:^BILibStruct90(id target){ + ++count; BILibStruct90 st; return st; + }]; + + STAssertEquals(count, (int)0, @"count is invalid."); + + ClassForReturnValue* c = [ClassForReturnValue new]; + [c struct10]; + [c struct20]; + [c struct30]; + [c struct40]; + [c struct50]; + [c struct60]; + [c struct70]; + [c struct80]; + [c struct90]; + + STAssertEquals(count, (int)9, @"count is invalid."); +} + +- (void)testReturnSomeStruct100 +{ + __block int count = 0; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct100" preprocess:^BILibStruct100(id target){ + ++count; BILibStruct100 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct200" preprocess:^BILibStruct200(id target){ + ++count; BILibStruct200 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct300" preprocess:^BILibStruct300(id target){ + ++count; BILibStruct300 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct400" preprocess:^BILibStruct400(id target){ + ++count; BILibStruct400 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct500" preprocess:^BILibStruct500(id target){ + ++count; BILibStruct500 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct600" preprocess:^BILibStruct600(id target){ + ++count; BILibStruct600 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct700" preprocess:^BILibStruct700(id target){ + ++count; BILibStruct700 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct800" preprocess:^BILibStruct800(id target){ + ++count; BILibStruct800 st; return st; + }]; + [BILib injectToClassWithName:@"ClassForReturnValue" methodName:@"struct900" preprocess:^BILibStruct900(id target){ + ++count; BILibStruct900 st; return st; + }]; + + STAssertEquals(count, (int)0, @"count is invalid."); + + ClassForReturnValue* c = [ClassForReturnValue new]; + [c struct100]; + [c struct200]; + [c struct300]; + [c struct400]; + [c struct500]; + [c struct600]; + [c struct700]; + [c struct800]; + [c struct900]; + + STAssertEquals(count, (int)9, @"count is invalid."); +} + @end diff --git a/BlockInjectionTest/Tests/SkippingTest.m b/BlockInjectionTest/Tests/SkippingTest.m index e4a8d09..fa8d06f 100644 --- a/BlockInjectionTest/Tests/SkippingTest.m +++ b/BlockInjectionTest/Tests/SkippingTest.m @@ -94,7 +94,7 @@ - (void)testSkippingPostprocess - (void)testOverrideReturnValueByPostprocess { - [BILib injectToClassWithName:@"SubjectForSkipping" methodName:@"intValue" preprocess:^{ + [BILib injectToClassWithName:@"SubjectForSkipping" methodName:@"intValue" postprocess:^{ int ret = 99; [BILib skipAfterProcessesWithReturnValue:&ret]; }];