Skip to content

Commit

Permalink
Error handling follows Cocoa idioms
Browse files Browse the repository at this point in the history
  • Loading branch information
idoru committed Jan 17, 2014
1 parent 331144a commit fc9a85f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 52 deletions.
35 changes: 16 additions & 19 deletions Source/CDRSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,22 @@ - (void)markAsFocusedClosestToLineNumber:(NSUInteger)lineNumber {
// - also __LINE__ is unrolled from the outermost block
// which causes incorrect values
NSError *error = nil;
[self.symbolicator symbolicateAddresses:addresses error:&error];

if (error.domain == kCDRSymbolicatorErrorDomain) {
if ([self.symbolicator symbolicateAddresses:addresses error:&error]) {
NSUInteger bestAddressIndex = [children indexOfObject:self.rootGroup];

// Matches closest example/group located on or below specified line number
// (only takes into account start of an example/group)
for (NSInteger i = 0, shortestDistance = -1; i < addresses.count; i++) {
NSInteger address = [[addresses objectAtIndex:i] integerValue];
NSInteger distance = lineNumber - [self.symbolicator lineNumberForStackAddress:address];

if (distance >= 0 && (distance < shortestDistance || shortestDistance == -1) ) {
bestAddressIndex = i;
shortestDistance = distance;
}
}
[[children objectAtIndex:bestAddressIndex] setFocused:YES];
} else if (error.domain == kCDRSymbolicatorErrorDomain) {
if (error.code == kCDRSymbolicatorErrorNotAvailable) {
printf("Spec location symbolication is not available.\n");
} else if (error.code == kCDRSymbolicatorErrorNotSuccessful) {
Expand All @@ -158,23 +171,7 @@ - (void)markAsFocusedClosestToLineNumber:(NSUInteger)lineNumber {
} else {
printf("Spec location symbolication failed.\n");
}
return;
}

NSUInteger bestAddressIndex = [children indexOfObject:self.rootGroup];

// Matches closest example/group located on or below specified line number
// (only takes into account start of an example/group)
for (NSInteger i = 0, shortestDistance = -1; i < addresses.count; i++) {
NSInteger address = [[addresses objectAtIndex:i] integerValue];
NSInteger distance = lineNumber - [self.symbolicator lineNumberForStackAddress:address];

if (distance >= 0 && (distance < shortestDistance || shortestDistance == -1) ) {
bestAddressIndex = i;
shortestDistance = distance;
}
}
[[children objectAtIndex:bestAddressIndex] setFocused:YES];
}

- (NSArray *)allChildren {
Expand Down
42 changes: 20 additions & 22 deletions Source/CDRSpecFailure.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,31 @@ - (NSString *)description {
- (NSString *)callStackSymbolicatedSymbols:(NSError **)error {
if (!self.callStackReturnAddresses) return nil;

CDRSymbolicator *symbolicator =
[[[CDRSymbolicator alloc] init] autorelease];
CDRSymbolicator *symbolicator = [[[CDRSymbolicator alloc] init] autorelease];

NSMutableString *result = nil;
NSError *symbolicationError = nil;
[symbolicator symbolicateAddresses:self.callStackReturnAddresses error:&symbolicationError];
if (symbolicationError) {
if ([symbolicator symbolicateAddresses:self.callStackReturnAddresses error:&symbolicationError]) {
result = [NSMutableString stringWithString:@"Call stack:\n"];
BOOL previousAddressWasUnknown = NO;

for (int i=0; i < self.callStackReturnAddresses.count; i++) {
NSUInteger address = [[self.callStackReturnAddresses objectAtIndex:i] unsignedIntegerValue];
NSString *fileName = [symbolicator fileNameForStackAddress:address];
unsigned long lineNumber = [symbolicator lineNumberForStackAddress:address];

if (fileName) {
previousAddressWasUnknown = NO;
[result appendFormat:@" *%@:%lu\n", fileName, lineNumber];
} else if (!previousAddressWasUnknown) {
previousAddressWasUnknown = YES;
[result appendString:@" ...\n"];
}
}
} else if (error && symbolicationError) {
*error = symbolicationError;
return nil;
}

NSMutableString *result =
[NSMutableString stringWithString:@"Call stack:\n"];
BOOL previousAddressWasUnknown = NO;

for (int i=0; i < self.callStackReturnAddresses.count; i++) {
NSUInteger address = [[self.callStackReturnAddresses objectAtIndex:i] unsignedIntegerValue];
NSString *fileName = [symbolicator fileNameForStackAddress:address];
unsigned long lineNumber = [symbolicator lineNumberForStackAddress:address];

if (fileName) {
previousAddressWasUnknown = NO;
[result appendFormat:@" *%@:%lu\n", fileName, lineNumber];
} else if (!previousAddressWasUnknown) {
previousAddressWasUnknown = YES;
[result appendString:@" ...\n"];
}
}
return result;
}

Expand Down
22 changes: 15 additions & 7 deletions Source/CDRSymbolicator.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ - (NSUInteger)lineNumberForStackAddress:(NSUInteger)address {
return (index == NSNotFound) ? 0 : [[self.lineNumbers objectAtIndex:index] unsignedIntegerValue];
}

- (void)symbolicateAddresses:(NSArray *)addresses error:(NSError **)error {
- (BOOL)symbolicateAddresses:(NSArray *)addresses error:(NSError **)error {
#if __arm__
*error = self.buildNotAvailableError;
return;
if (error) {
*error = self.buildNotAvailableError;
}
return NO;
#else
NSArray *validAddresses = [self.class validAddresses:addresses];
if (validAddresses.count == 0) {
*error = self.buildNoAddressesError;
return;
if (error) {
*error = self.buildNoAddressesError;
}
return NO;
}

CDRAtosTask *atosTask = [CDRAtosTask taskForCurrentTestExecutable];
Expand All @@ -99,9 +103,13 @@ - (void)symbolicateAddresses:(NSArray *)addresses error:(NSError **)error {
}

if (!atLeastOneSuccessfulSymbolication) {
*error = self.buildNotSuccessfulError;
return;
if (error) {
*error = self.buildNotSuccessfulError;
}
return NO;
}

return YES;
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Headers/CDRSymbolicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef enum {
} kCDRSymbolicatorError;

@interface CDRSymbolicator : NSObject
- (void)symbolicateAddresses:(NSArray *)addresses error:(NSError **)error;
- (BOOL)symbolicateAddresses:(NSArray *)addresses error:(NSError **)error;
- (NSString *)fileNameForStackAddress:(NSUInteger)address;
- (NSUInteger)lineNumberForStackAddress:(NSUInteger)address;
@end
Expand Down
2 changes: 1 addition & 1 deletion Spec/CDRSpecSpec.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
spec = [[[CDRSpec alloc] init] autorelease];

spy_on(spec.symbolicator);
spec.symbolicator stub_method("symbolicateAddresses:error:");
spec.symbolicator stub_method("symbolicateAddresses:error:").and_return(YES);
spec.symbolicator stub_method("lineNumberForStackAddress:").and_do(^(NSInvocation *i){
NSUInteger lineNumber;
[i getArgument:&lineNumber atIndex:2];
Expand Down
4 changes: 2 additions & 2 deletions Spec/CDRSymbolicatorSpec.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
NSArray *addresses = [NSArray arrayWithObject:address];

NSError *error = nil;
[symbolicator symbolicateAddresses:addresses error:&error];
[symbolicator symbolicateAddresses:addresses error:&error] should be_truthy;
error should be_nil;

[[symbolicator fileNameForStackAddress:b.stackAddress] hasSuffix:fileName] should be_truthy;
Expand Down Expand Up @@ -108,7 +108,7 @@

subjectAction(^{
error = nil;
[symbolicator symbolicateAddresses:addresses error:&error];
[symbolicator symbolicateAddresses:addresses error:&error] should be_falsy;
});

it(@"does not return filename or line number", ^{
Expand Down

0 comments on commit fc9a85f

Please sign in to comment.