Skip to content

Commit

Permalink
Closes #13 with a high number of unit tests, still missing one or two…
Browse files Browse the repository at this point in the history
… classes
  • Loading branch information
vittoriom committed Nov 10, 2013
1 parent c1cf5eb commit b1cf067
Show file tree
Hide file tree
Showing 7 changed files with 719 additions and 124 deletions.
2 changes: 1 addition & 1 deletion VMDInstrumenter.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'VMDInstrumenter'
s.version = '0.6.0'
s.version = '0.7'
s.summary = 'A simple Objective-C singleton to instrument, trace, and suppress selectors at runtime.'
s.author = {
'Vittorio Monaco' => 'vittorio.monaco1@gmail.com'
Expand Down
18 changes: 18 additions & 0 deletions VMInstrumenter/VMDInstrumenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ - (IMP) VMDImplementationForReturnType:(char)returnType withTestBlock:(BOOL (^)(
implementationBlock = [self VMDImplementationBlockForIntegerReturnTypeWithTestBlock:testBlock beforeBlock:executeBefore afterBlock:executeAfter forInstrumentedSelector:instrumentedSelector andArgsCount:argsCount];
break;
case 'f':
implementationBlock = [self VMDImplementationBlockForFloatReturnTypeWithTestBlock:testBlock beforeBlock:executeBefore afterBlock:executeAfter forInstrumentedSelector:instrumentedSelector andArgsCount:argsCount];
break;
case 'd':
implementationBlock = [self VMDImplementationBlockForDoubleReturnTypeWithTestBlock:testBlock beforeBlock:executeBefore afterBlock:executeAfter forInstrumentedSelector:instrumentedSelector andArgsCount:argsCount];
break;
Expand Down Expand Up @@ -391,6 +393,22 @@ - (NSInvocation *) preprocessAndPostprocessCallWithTestBlock:(BOOL (^)(id))testB
};
}

- (float (^)(id realSelf,...)) VMDImplementationBlockForFloatReturnTypeWithTestBlock:(BOOL (^)(id))testBlock beforeBlock:(void (^)(id))executeBefore afterBlock:(void (^)(id))executeAfter forInstrumentedSelector:(SEL)instrumentedSelector andArgsCount:(NSInteger)argsCount
{
return (id)^(id realSelf,...)
{
va_list args;
va_start(args, realSelf);
NSInvocation *invocation = [self preprocessAndPostprocessCallWithTestBlock:testBlock beforeBlock:executeBefore afterBlock:executeAfter forInstrumentedSelector:instrumentedSelector withArgs:args argsCount:argsCount onRealSelf:realSelf];
va_end(args);

float result = .0;
[invocation getReturnValue:&result];

return result;
};
}

- (SEL (^)(id realSelf,...)) VMDImplementationBlockForSELReturnTypeWithTestBlock:(BOOL (^)(id))testBlock beforeBlock:(void (^)(id))executeBefore afterBlock:(void (^)(id))executeAfter forInstrumentedSelector:(SEL)instrumentedSelector andArgsCount:(NSInteger)argsCount
{
return (id)^(id realSelf,...)
Expand Down
75 changes: 72 additions & 3 deletions VMInstrumenter_Tests/NSInvocationVMDInstrumenter_Tests.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <Kiwi/Kiwi.h>
#import <XCTest/XCTest.h>
#import "VMTestsHelper.h"
#import "NSInvocation+VMDInstrumenter.h"

@interface NSInvocationVMDInstrumenterHelper : NSObject
Expand All @@ -17,7 +18,17 @@ - (void) doFoo {}
SPEC_BEGIN(NSInvocationVMDInstrumenterTests)

describe(@"NSInvocation categrory", ^{
__block VMTestsHelper *helper;

context(@"when creating the NSInvocation object", ^{
beforeEach(^{
helper = [VMTestsHelper new];
});

afterEach(^{
helper = nil;
});

it(@"should correctly work with multiple arguments", ^{

});
Expand Down Expand Up @@ -46,15 +57,73 @@ - (void) doFoo {}
});

context(@"when invoking the NSInvocation other than returning it", ^{
it(@"it should contain the correct return value", ^{
it(@"it should contain the correct return value if void", ^{

});

it(@"it should contain the correct return value if primitive", ^{
it(@"it should contain the correct return value if object", ^{

});

//@TODO all primitive types
it(@"it should contain the correct return value if char", ^{

});

it(@"it should contain the correct return value if unsigned char", ^{

});

it(@"it should contain the correct return value if bool", ^{

});

it(@"it should contain the correct return value if selector", ^{

});

it(@"it should contain the correct return value if Class", ^{

});

it(@"it should contain the correct return value if float", ^{

});

it(@"it should contain the correct return value if double", ^{

});

it(@"it should contain the correct return value if int", ^{

});

it(@"it should contain the correct return value if unsigned int", ^{

});

it(@"it should contain the correct return value if short", ^{

});

it(@"it should contain the correct return value if unsigned short", ^{

});

it(@"it should contain the correct return value if long", ^{

});

it(@"it should contain the correct return value if unsigned long", ^{

});

it(@"it should contain the correct return value if long long", ^{

});

it(@"it should contain the correct return value if unsigned long long", ^{

});

it(@"should raise an exception if the selector doesn't exist", ^{
[[theBlock(^{
Expand Down
44 changes: 22 additions & 22 deletions VMInstrumenter_Tests/NSMethodSignatureVMDInstrumenter_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
describe(@"NSMethodSignature category",^{
context(@"when returning const char signature", ^{
it(@"should correctly return signature 1", ^{
const char * signature = [NSMethodSignature constCharSignatureForSelector:@selector(alwaysReturn3) ofClass:[VMTestsHelper class]];
const char * signature = [NSMethodSignature constCharSignatureForSelector:@selector(alwaysReturnMinus3231) ofClass:[VMTestsHelper class]];
NSString * signatureAsObject = [NSString stringWithUTF8String:signature];
[[[signatureAsObject substringToIndex:1] should] equal:@"i"];
});
Expand All @@ -20,32 +20,32 @@
});

it(@"should correctly return signature 3", ^{
const char * signature3 = [NSMethodSignature constCharSignatureForSelector:@selector(doFoo:withMoreThanOneParameter:) ofClass:[VMTestsHelper class]];
const char * signature3 = [NSMethodSignature constCharSignatureForSelector:@selector(voidNoArgs) ofClass:[VMTestsHelper class]];
NSString * signatureAsObject3 = [NSString stringWithUTF8String:signature3];
[[[signatureAsObject3 substringToIndex:1] should] equal:@"v"];
});

it(@"should correctly return signature 4", ^{
const char * signature = [NSMethodSignature constCharSignatureForSelector:@selector(alwaysReturn3) ofClass:[VMTestsHelper class]];
const char * signature = [NSMethodSignature constCharSignatureForSelector:@selector(alwaysReturnMinus3231) ofClass:[VMTestsHelper class]];
NSString * signatureAsObject = [NSString stringWithUTF8String:signature];
[[signatureAsObject should] equal:@"i@:"];
});

it(@"should correctly return signature 5", ^{
const char * signature3 = [NSMethodSignature constCharSignatureForSelector:@selector(doFoo:withMoreThanOneParameter:) ofClass:[VMTestsHelper class]];
const char * signature3 = [NSMethodSignature constCharSignatureForSelector:@selector(alwaysReturn:specifiedLong:) ofClass:[VMTestsHelper class]];
NSString * signatureAsObject3 = [NSString stringWithUTF8String:signature3];
[[signatureAsObject3 should] equal:@"v@:@@"];
[[signatureAsObject3 should] equal:@"l@:@l"];
});

it(@"should correctly return signature 6", ^{
const char * signature3 = [NSMethodSignature constCharSignatureForSelector:@selector(floatTest) ofClass:[VMTestsHelper class]];
const char * signature3 = [NSMethodSignature constCharSignatureForSelector:@selector(alwaysReturn5dot2) ofClass:[VMTestsHelper class]];
NSString * signatureAsObject3 = [NSString stringWithUTF8String:signature3];
[[signatureAsObject3 should] equal:@"f@:"];
});

it(@"should raise an exception if the selector doesn't exist", ^{
[[theBlock(^{
[NSMethodSignature constCharSignatureForSelector:@selector(floatTest) ofClass:[self class]];
[NSMethodSignature constCharSignatureForSelector:@selector(alwaysReturn5dot2) ofClass:[self class]];
}) should] raise];
});
});
Expand All @@ -57,12 +57,12 @@
});

it(@"should work with 1 argument", ^{
NSInteger number = [NSMethodSignature numberOfArgumentsForSelector:@selector(doAndReturnPrimitiveValue:) ofClass:[VMTestsHelper class]];
NSInteger number = [NSMethodSignature numberOfArgumentsForSelector:@selector(alwaysReturnSpecifiedClass:) ofClass:[VMTestsHelper class]];
[[theValue(number) should] equal:theValue(1)];
});

it(@"should work with more than 1 argument", ^{
NSInteger number = [NSMethodSignature numberOfArgumentsForSelector:@selector(doFoo:withMoreThanOneParameter:) ofClass:[VMTestsHelper class]];
NSInteger number = [NSMethodSignature numberOfArgumentsForSelector:@selector(alwaysReturn:specifiedUnsignedChar:) ofClass:[VMTestsHelper class]];
[[theValue(number) should] equal:theValue(2)];
});

Expand All @@ -75,63 +75,63 @@

context(@"when returning the NSMethodSignature of a selector", ^{
it(@"should work with selector 1", ^{
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(floatTest) ofClass:[VMTestsHelper class]];
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(alwaysReturn5dot2) ofClass:[VMTestsHelper class]];
[[theValue([signature numberOfArguments]) should] equal:theValue(2)];
[[[[NSString stringWithUTF8String:[signature methodReturnType]] substringToIndex:1] should] equal:@"f"];
});

it(@"should work with selector 2", ^{
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(doFoo:withMoreThanOneParameter:) ofClass:[VMTestsHelper class]];
[[theValue([signature numberOfArguments]) should] equal:theValue(4)];
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(voidNoArgs) ofClass:[VMTestsHelper class]];
[[theValue([signature numberOfArguments]) should] equal:theValue(2)];
[[[[NSString stringWithUTF8String:[signature methodReturnType]] substringToIndex:1] should] equal:@"v"];
});

it(@"should work with selector 3", ^{
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(doAndReturnValue:) ofClass:[VMTestsHelper class]];
[[theValue([signature numberOfArguments]) should] equal:theValue(3)];
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(alwaysReturn:specifiedString:) ofClass:[VMTestsHelper class]];
[[theValue([signature numberOfArguments]) should] equal:theValue(4)];
[[[[NSString stringWithUTF8String:[signature methodReturnType]] substringToIndex:1] should] equal:@"@"];
});

it(@"should work with selector 4", ^{
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(doAndReturnPrimitiveValue:) ofClass:[VMTestsHelper class]];
[[theValue([signature numberOfArguments]) should] equal:theValue(3)];
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(alwaysReturn:specifiedInt:) ofClass:[VMTestsHelper class]];
[[theValue([signature numberOfArguments]) should] equal:theValue(4)];
[[[[NSString stringWithUTF8String:[signature methodReturnType]] substringToIndex:1] should] equal:@"i"];
});

it(@"should raise an exception if the selector doesn't exist", ^{
[[theBlock(^{
[NSMethodSignature methodSignatureForSelector:@selector(doFoo:withMoreThanOneParameter:) ofClass:[self class]];
[NSMethodSignature methodSignatureForSelector:@selector(alwaysReturn:specifiedInt:) ofClass:[self class]];
}) should] raise];
});
});

context(@"when returning the type of the argument in a selector", ^{
it(@"should work with the self implicit argument", ^{
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(doFoo:withMoreThanOneParameter:) ofClass:[VMTestsHelper class]];
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(alwaysReturn:specifiedUnsignedLongLong:) ofClass:[VMTestsHelper class]];
char type = [NSMethodSignature typeOfArgumentInSignature:signature atIndex:0];
NSString *typeString = [NSString stringWithFormat:@"%c",type];

[[typeString should] equal:@"@"];
});

it(@"should work with the _cmd implicit argument", ^{
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(doFoo:withMoreThanOneParameter:) ofClass:[VMTestsHelper class]];
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(alwaysReturn:specifiedString:) ofClass:[VMTestsHelper class]];
char type = [NSMethodSignature typeOfArgumentInSignature:signature atIndex:1];
NSString *typeString = [NSString stringWithFormat:@"%c",type];

[[typeString should] equal:@":"];
});

it(@"should work with object arguments", ^{
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(doFoo:withMoreThanOneParameter:) ofClass:[VMTestsHelper class]];
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(alwaysReturnSpecifiedString:) ofClass:[VMTestsHelper class]];
char type = [NSMethodSignature typeOfArgumentInSignature:signature atIndex:2];
NSString *typeString = [NSString stringWithFormat:@"%c",type];

[[typeString should] equal:@"@"];
});

it(@"should work with primitive arguments", ^{
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(doAndReturnPrimitiveValue:) ofClass:[VMTestsHelper class]];
NSMethodSignature *signature = [NSMethodSignature methodSignatureForSelector:@selector(alwaysReturnSpecifiedInt:) ofClass:[VMTestsHelper class]];
char type = [NSMethodSignature typeOfArgumentInSignature:signature atIndex:2];
NSString *typeString = [NSString stringWithFormat:@"%c",type];

Expand All @@ -141,7 +141,7 @@

context(@"when getting Method type", ^{
it(@"should correctly distinguish between instance and class methods (instance)", ^{
VMDMethodType type = [NSMethodSignature typeOfMethodForSelector:@selector(doFoo:withMoreThanOneParameter:) ofClass:[VMTestsHelper class]];
VMDMethodType type = [NSMethodSignature typeOfMethodForSelector:@selector(alwaysReturn:specifiedInt:) ofClass:[VMTestsHelper class]];
[[theValue(type) should] equal:theValue(VMDInstanceMethodType)];
});

Expand Down
Loading

0 comments on commit b1cf067

Please sign in to comment.