Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Prettified test case names as displayed in Xcode.
Browse files Browse the repository at this point in the history
Test class names have “Test” or “Tests” removed from the end if applicable.
Test method names have “test” removed from the start, and the rest split into words and lowercased.
E.g. “-[FooBarTests testIsExtremelyQuux]” becomes “FooBar is extremely quux”
  • Loading branch information
robrix committed Mar 1, 2010
1 parent f99c358 commit 11b00db
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
6 changes: 5 additions & 1 deletion RXAssertions.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
typedef BOOL (*RXAssertionHelperComparisonFunction)(const void *aRef, const void *bRef);
typedef NSString *(*RXAssertionHelperDescriptionFunction)(const void *ref);


@interface RXAssertionHelper : NSObject

+(void)registerComparisonFunction:(RXAssertionHelperComparisonFunction)comparator forObjCType:(const char *)type;
Expand All @@ -59,4 +60,7 @@ typedef NSString *(*RXAssertionHelperDescriptionFunction)(const void *ref);
+(double)floatingPointComparisonAccuracy;
+(void)setFloatingPointComparisonAccuracy:(double)epsilon;

@end
// returns a nicely formatted name for the test case selector
+(NSString *)humanReadableNameForTestCaseSelector:(SEL)selector;

@end
40 changes: 39 additions & 1 deletion RXAssertions.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,42 @@ +(void)setFloatingPointComparisonAccuracy:(double)epsilon {
RXAssertionHelperFloatingPointComparisonAccuracy = epsilon;
}

@end

+(NSString *)humanReadableNameForTestCaseSelector:(SEL)selector {
NSMutableArray *words = [NSMutableArray array];
NSScanner *scanner = [NSScanner scannerWithString: NSStringFromSelector(selector)];
[scanner scanString: @"test" intoString: nil]; // skip "test"
while(!scanner.isAtEnd) {
NSString *up = nil, *lo = nil;
unsigned cursor = scanner.scanLocation;
up = [scanner.string substringWithRange: NSMakeRange(cursor, 1)]; // grab the first character
scanner.scanLocation = cursor + 1;
[scanner scanCharactersFromSet: [NSCharacterSet lowercaseLetterCharacterSet] intoString: &lo];
[words addObject: [NSString stringWithFormat: @"%@%@", [up lowercaseString], lo ?: @""]];
}
return [words componentsJoinedByString: @" "];
}

+(NSString *)humanReadableNameForTestSuiteClass:(Class)klass {
NSString *name = NSStringFromClass(klass);
NSString *result = name;
NSRange testsRange = [name rangeOfString: @"Tests" options: NSBackwardsSearch | NSAnchoredSearch];
NSRange testRange = [name rangeOfString: @"Test" options: NSBackwardsSearch | NSAnchoredSearch];
if(testsRange.location != NSNotFound) {
result = [name substringToIndex: testsRange.location];
} else if(testRange.location != NSNotFound) {
result = [name substringToIndex: testRange.location];
}
return result;
}

@end


@implementation SenTestCase (RXAssertionsPrettierNamesForTestCases)

-(NSString *)name {
return [NSString stringWithFormat: @"%@ %@", [RXAssertionHelper humanReadableNameForTestSuiteClass: self.class], [RXAssertionHelper humanReadableNameForTestCaseSelector: self.invocation.selector]];
}

@end
2 changes: 1 addition & 1 deletion RXAssertions.pch
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
#endif

0 comments on commit 11b00db

Please sign in to comment.