Permalink
Browse files

Merge pull request #8 from twobitlabs/master

Added OCUnit exception formatting to support line-number highlighting in Xcode
  • Loading branch information...
2 parents cbedf32 + 301cc5a commit cde47bd426917e315becf1eccdd686d649621982 @petejkim petejkim committed Dec 6, 2011
Showing with 30 additions and 2 deletions.
  1. +1 −0 src/ExpectaSupport.h
  2. +29 −2 src/ExpectaSupport.m
View
@@ -8,6 +8,7 @@ id _EXPObjectify(char *type, ...);
EXPExpect *_EXP_expect(id testCase, int lineNumber, char *fileName, EXPIdBlock actualBlock);
void EXPFail(id testCase, int lineNumber, char *fileName, NSString *message);
+NSException* EXPOCUnitException(const char* fileName, int lineNumber, NSString *description);
NSString *EXPDescribeObject(id obj);
void EXP_prerequisite(EXPBoolBlock block);
View
@@ -9,6 +9,13 @@
#import "EXPFloatTuple.h"
#import "EXPDoubleTuple.h"
+#if TARGET_OS_IPHONE
+ #import <objc/runtime.h>
+#else
+ #import <objc/objc-class.h>
+#endif
+
+
typedef void (^EXPBasicBlock)();
id _EXPObjectify(char *type, ...) {
@@ -93,15 +100,35 @@ id _EXPObjectify(char *type, ...) {
}
void EXPFail(id testCase, int lineNumber, char *fileName, NSString *message) {
- NSString *reason = [NSString stringWithFormat:@"%s:%d %@", fileName, lineNumber, message];
- NSException *exception = [NSException exceptionWithName:@"Expecta Error" reason:reason userInfo:nil];
if(testCase && [testCase respondsToSelector:@selector(failWithException:)]) {
+ NSException *exception = EXPOCUnitException(fileName, lineNumber, message);
[testCase failWithException:exception];
} else {
+ NSString *reason = [NSString stringWithFormat:@"%s:%d %@", fileName, lineNumber, message];
+ NSException *exception = [NSException exceptionWithName:@"Expecta Error" reason:reason userInfo:nil];
[exception raise];
}
}
+NSException* EXPOCUnitException(const char* fileName, int lineNumber, NSString *description) {
+ NSException *exception = nil;
+
+ SEL selector = @selector(failureInFile:atLine:withDescription:);
+ NSMethodSignature *signature = [[NSException class]->isa instanceMethodSignatureForSelector:selector];
+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
+ [invocation setTarget:[NSException class]];
+ [invocation setSelector:selector];
+
+ id fileArg = [NSString stringWithUTF8String:fileName];
+ [invocation setArgument:&fileArg atIndex:2];
+ [invocation setArgument:&lineNumber atIndex:3];
+ [invocation setArgument:&description atIndex:4];
+
+ [invocation invoke];
+ [invocation getReturnValue:&exception];
+ return exception;
+}
+
NSString *EXPDescribeObject(id obj) {
if(obj == nil) {
return @"nil/null";

0 comments on commit cde47bd

Please sign in to comment.