Permalink
Browse files

Prettified test case names as displayed in Xcode.

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...
1 parent f99c358 commit 11b00dbc07bea37324290dd9e9faa537720e3e55 @robrix committed Mar 1, 2010
Showing with 45 additions and 3 deletions.
  1. +5 −1 RXAssertions.h
  2. +39 −1 RXAssertions.m
  3. +1 −1 RXAssertions.pch
View
@@ -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;
@@ -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
View
@@ -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
View
@@ -4,4 +4,4 @@
#ifdef __OBJC__
#import <Foundation/Foundation.h>
-#endif
+#endif

0 comments on commit 11b00db

Please sign in to comment.