Skip to content

Commit

Permalink
Add some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tokorom committed Jul 7, 2013
1 parent bb3027a commit afa83d9
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 37 deletions.
8 changes: 0 additions & 8 deletions BlockInjection/BILib.h
Expand Up @@ -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
Expand Down
26 changes: 0 additions & 26 deletions BlockInjection/BILib.m
Expand Up @@ -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)
Expand Down
Expand Up @@ -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 */; };
Expand All @@ -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 = "<group>"; };
06584EEB16DEFAF800CB2D68 /* BILib.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BILib.m; sourceTree = "<group>"; };
066CDB0717898A490024EDB8 /* EtcTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EtcTest.h; sourceTree = "<group>"; };
066CDB0817898A490024EDB8 /* EtcTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EtcTest.m; sourceTree = "<group>"; };
06941C0E16E7344800EDAA36 /* BILibArg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BILibArg.h; sourceTree = "<group>"; };
06941C0F16E7344800EDAA36 /* BILibArg.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BILibArg.m; sourceTree = "<group>"; };
06EE077A1723C573004EBE60 /* BILibUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BILibUtils.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -143,6 +146,8 @@
06F03DD8177FB42A00745436 /* ReturnValueTest.m */,
06F03DD9177FB42A00745436 /* SkippingTest.h */,
06F03DDA177FB42A00745436 /* SkippingTest.m */,
066CDB0717898A490024EDB8 /* EtcTest.h */,
066CDB0817898A490024EDB8 /* EtcTest.m */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
};
Expand Down
13 changes: 13 additions & 0 deletions 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 <SenTestingKit/SenTestingKit.h>

@interface EtcTest : SenTestCase

@end
103 changes: 103 additions & 0 deletions 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 <objc/runtime.h>
#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
18 changes: 18 additions & 0 deletions BlockInjectionTest/Tests/ReplaceTest.m
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit afa83d9

Please sign in to comment.