Permalink
Browse files

Merge pull request #137 from x2on/modern-objective-c-syntax

* Refactor using modern objective-c syntax
* Fixes #131
  • Loading branch information...
2 parents 9011d1c + 4441306 commit 160be554a7720d8b3113c30831c61164b23cad2c @x2on x2on committed Nov 3, 2013
@@ -15,7 +15,7 @@ - (id)init {
windowController_ = [[GHTestWindowController alloc] init];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
topLevelObjects_ = [[NSMutableArray alloc] init];
- NSDictionary *externalNameTable = [NSDictionary dictionaryWithObjectsAndKeys:self, @"NSOwner", topLevelObjects_, @"NSTopLevelObjects", nil];
+ NSDictionary *externalNameTable = @{@"NSOwner": self, @"NSTopLevelObjects": topLevelObjects_};
[bundle loadNibFile:@"GHTestApp" externalNameTable:externalNameTable withZone:nil];
}
return self;
@@ -18,7 +18,7 @@ - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id
if (!item) {
return [self root];
} else {
- return [[item children] objectAtIndex:index];
+ return [item children][index];
}
}
@@ -63,10 +63,8 @@ - (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTab
if (self.isEditing) {
[cell setState:[item isSelected] ? NSOnState : NSOffState];
- NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
- textColor, NSForegroundColorAttributeName,
- [cell font], NSFontAttributeName,
- nil];
+ NSDictionary *attributes = @{NSForegroundColorAttributeName: textColor,
+ NSFontAttributeName: [cell font]};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:[item name] attributes:attributes];
[cell setAttributedTitle:attributedString];
@@ -40,7 +40,7 @@ @implementation GHUnitIOSAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
char *stderrRedirect = getenv("GHUNIT_STDERR_REDIRECT");
if (stderrRedirect) {
- NSString *stderrRedirectPath = [NSString stringWithUTF8String:stderrRedirect];
+ NSString *stderrRedirectPath = @(stderrRedirect);
freopen([stderrRedirectPath fileSystemRepresentation], "a", stderr);
}
@@ -69,7 +69,7 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application {
- (void)applicationWillTerminate:(UIApplication *)application {
// Called only graceful terminate; Closing simulator won't trigger this
- [[[navigationController_ viewControllers] objectAtIndex:0] saveDefaults];
+ [[navigationController_ viewControllers][0] saveDefaults];
}
@@ -32,8 +32,8 @@
@implementation GHUnitIOSTableViewDataSource
- (GHTestNode *)nodeForIndexPath:(NSIndexPath *)indexPath {
- GHTestNode *sectionNode = [[[self root] children] objectAtIndex:indexPath.section];
- return [[sectionNode children] objectAtIndex:indexPath.row];
+ GHTestNode *sectionNode = [[self root] children][indexPath.section];
+ return [sectionNode children][indexPath.row];
}
- (void)setSelectedForAllNodes:(BOOL)selected {
@@ -59,13 +59,13 @@ - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)sec
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *children = [[self root] children];
if ([children count] == 0) return nil;
- GHTestNode *sectionNode = [children objectAtIndex:section];
+ GHTestNode *sectionNode = children[section];
return sectionNode.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- GHTestNode *sectionNode = [[[self root] children] objectAtIndex:indexPath.section];
- GHTestNode *node = [[sectionNode children] objectAtIndex:indexPath.row];
+ GHTestNode *sectionNode = [[self root] children][indexPath.section];
+ GHTestNode *node = [sectionNode children][indexPath.row];
static NSString *CellIdentifier = @"ReviewFeedViewItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
@@ -71,9 +71,9 @@ - (void)_runTest {
- (void)_showImageDiff {
if (!imageDiffView_) imageDiffView_ = [[GHImageDiffView alloc] initWithFrame:CGRectZero];
- UIImage *savedImage = [testNode_.test.exception.userInfo objectForKey:@"SavedImage"];
- UIImage *renderedImage = [testNode_.test.exception.userInfo objectForKey:@"RenderedImage"];
- UIImage *diffImage = [testNode_.test.exception.userInfo objectForKey:@"DiffImage"];
+ UIImage *savedImage = (testNode_.test.exception.userInfo)[@"SavedImage"];
+ UIImage *renderedImage = (testNode_.test.exception.userInfo)[@"RenderedImage"];
+ UIImage *diffImage = (testNode_.test.exception.userInfo)[@"DiffImage"];
[imageDiffView_ setSavedImage:savedImage renderedImage:renderedImage diffImage:diffImage];
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view = imageDiffView_;
@@ -89,12 +89,12 @@ - (NSString *)updateTestView {
if (stackTrace) [text appendFormat:@"\n%@\n", stackTrace];
if ([testNode_.test.exception.name isEqualToString:@"GHViewChangeException"]) {
NSDictionary *exceptionUserInfo = testNode_.test.exception.userInfo;
- UIImage *savedImage = [exceptionUserInfo objectForKey:@"SavedImage"];
- UIImage *renderedImage = [exceptionUserInfo objectForKey:@"RenderedImage"];
+ UIImage *savedImage = exceptionUserInfo[@"SavedImage"];
+ UIImage *renderedImage = exceptionUserInfo[@"RenderedImage"];
[testView_ setSavedImage:savedImage renderedImage:renderedImage text:text];
} else if ([testNode_.test.exception.name isEqualToString:@"GHViewUnavailableException"]) {
NSDictionary *exceptionUserInfo = testNode_.test.exception.userInfo;
- UIImage *renderedImage = [exceptionUserInfo objectForKey:@"RenderedImage"];
+ UIImage *renderedImage = exceptionUserInfo[@"RenderedImage"];
[testView_ setSavedImage:nil renderedImage:renderedImage text:text];
} else {
[testView_ setText:text];
@@ -125,8 +125,8 @@ - (void)testViewDidSelectRenderedImage:(GHUnitIOSTestView *)testView {
- (void)testViewDidApproveChange:(GHUnitIOSTestView *)testView {
// Save new image as the approved version
- NSString *imageFilename = [testNode_.test.exception.userInfo objectForKey:@"ImageFilename"];
- UIImage *renderedImage = [testNode_.test.exception.userInfo objectForKey:@"RenderedImage"];
+ NSString *imageFilename = (testNode_.test.exception.userInfo)[@"ImageFilename"];
+ UIImage *renderedImage = (testNode_.test.exception.userInfo)[@"RenderedImage"];
[GHViewTestCase saveApprovedViewTestImage:renderedImage filename:imageFilename];
testNode_.test.status = GHTestStatusSucceeded;
[self _runTest];
@@ -63,7 +63,7 @@ - (id)initWithFrame:(CGRect)frame {
[self addSubview:footerView_];
runToolbar_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 36)];
- filterControl_ = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"All", @"Failed", nil]];
+ filterControl_ = [[UISegmentedControl alloc] initWithItems:@[@"All", @"Failed"]];
filterControl_.frame = CGRectMake(20, 6, 280, 24);
filterControl_.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
filterControl_.segmentedControlStyle = UISegmentedControlStyleBar;
@@ -207,8 +207,8 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[view_.tableView reloadData];
} else {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
- GHTestNode *sectionNode = [[[dataSource_ root] children] objectAtIndex:indexPath.section];
- GHTestNode *testNode = [[sectionNode children] objectAtIndex:indexPath.row];
+ GHTestNode *sectionNode = [[dataSource_ root] children][indexPath.section];
+ GHTestNode *testNode = [sectionNode children][indexPath.row];
GHUnitIOSTestViewController *testViewController = [[GHUnitIOSTestViewController alloc] init];
[testViewController setTest:testNode.test];
@@ -42,7 +42,7 @@ + (NSString *)documentsDirectory {
return @(getenv("GHUNIT_DOCS_DIR"));
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- return [paths objectAtIndex:0];
+ return paths[0];
}
+ (NSString *)approvedTestImagesDirectory {
@@ -279,16 +279,16 @@ - (void)verifyView:(UIView *)view filename:(NSString *)filename lineNumber:(int)
NSMutableDictionary *exceptionDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
newViewImage, @"RenderedImage",
imageFilename, @"ImageFilename",
- [NSNumber numberWithInteger:lineNumber], GHTestLineNumberKey,
+ @(lineNumber), GHTestLineNumberKey,
filename, GHTestFilenameKey,
nil];
if (!originalViewImage) {
GHUDebug(@"No image available for filename %@", filename);
[[NSException exceptionWithName:@"GHViewUnavailableException" reason:@"No image saved for view" userInfo:exceptionDictionary] raise];
} else if (![[self class] compareImage:originalViewImage withRenderedImage:newViewImage]) {
UIImage *diffImage = [[self class] diffWithImage:originalViewImage renderedImage:newViewImage];
- if (diffImage) [exceptionDictionary setObject:diffImage forKey:@"DiffImage"];
- if (originalViewImage) [exceptionDictionary setObject:originalViewImage forKey:@"SavedImage"];
+ if (diffImage) exceptionDictionary[@"DiffImage"] = diffImage;
+ if (originalViewImage) exceptionDictionary[@"SavedImage"] = originalViewImage;
// Save new and diff images
[[self class] saveFailedViewTestImage:diffImage filename:[imageFilenamePrefix stringByAppendingString:@"-diff.png"]];
[[self class] saveFailedViewTestImage:newViewImage filename:[imageFilenamePrefix stringByAppendingString:@"-new.png"]];
@@ -76,14 +76,14 @@ - (GHUnitAsyncError)_waitFor:(NSInteger)status timeout:(NSTimeInterval)timeout {
waitForStatus_ = status;
if (!_runLoopModes)
- _runLoopModes = [NSArray arrayWithObjects:NSDefaultRunLoopMode, NSRunLoopCommonModes, nil];
+ _runLoopModes = @[NSDefaultRunLoopMode, NSRunLoopCommonModes];
NSTimeInterval checkEveryInterval = 0.05;
NSDate *runUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
BOOL timedOut = NO;
NSInteger runIndex = 0;
while(notifiedStatus_ == kGHUnitWaitStatusUnknown) {
- NSString *mode = [_runLoopModes objectAtIndex:(runIndex++ % [_runLoopModes count])];
+ NSString *mode = _runLoopModes[(runIndex++ % [_runLoopModes count])];
[lock_ unlock];
@autoreleasepool {
@@ -139,12 +139,12 @@ - (void)runForInterval:(NSTimeInterval)interval {
NSDate *runUntilDate = [NSDate dateWithTimeIntervalSinceNow:interval];
if (!_runLoopModes)
- _runLoopModes = [NSArray arrayWithObjects:NSDefaultRunLoopMode, NSRunLoopCommonModes, nil];
+ _runLoopModes = @[NSDefaultRunLoopMode, NSRunLoopCommonModes];
NSInteger runIndex = 0;
while ([runUntilDate compare:[NSDate dateWithTimeIntervalSinceNow:0]] == NSOrderedDescending) {
- NSString *mode = [_runLoopModes objectAtIndex:(runIndex++ % [_runLoopModes count])];
+ NSString *mode = _runLoopModes[(runIndex++ % [_runLoopModes count])];
[lock_ unlock];
@autoreleasepool {
@@ -281,7 +281,7 @@ - (void)_notifyFinished {
char *resultsDirStr = getenv("JUNIT_XML_DIR");
if (resultsDirStr) {
- resultsDir = [NSString stringWithUTF8String:resultsDirStr];
+ resultsDir = @(resultsDirStr);
} else {
NSString *tmpDir = NSTemporaryDirectory();
resultsDir = [tmpDir stringByAppendingPathComponent:@"test-results"];
@@ -96,14 +96,14 @@ + (GHTestSuite *)suiteWithTestFilter:(NSString *)testFilterString {
for(NSString *testFilter in testFilters) {
NSArray *components = [testFilter componentsSeparatedByString:@"/"];
if ([components count] == 2) {
- NSString *testCaseClassName = [components objectAtIndex:0];
+ NSString *testCaseClassName = components[0];
Class testCaseClass = NSClassFromString(testCaseClassName);
id testCase = [[testCaseClass alloc] init];
if (!testCase) {
NSLog(@"Couldn't find test: %@", testCaseClassName);
continue;
}
- NSString *methodName = [components objectAtIndex:1];
+ NSString *methodName = components[1];
GHTestGroup *group = [[GHTestGroup alloc] initWithTestCase:testCase selector:NSSelectorFromString(methodName) delegate:nil];
[testSuite addTestGroup:group];
} else {
@@ -123,7 +123,7 @@ + (GHTestSuite *)suiteWithTestFilter:(NSString *)testFilterString {
+ (GHTestSuite *)suiteFromEnv {
const char* cTestFilter = getenv("TEST");
if (cTestFilter) {
- NSString *testFilter = [NSString stringWithUTF8String:cTestFilter];
+ NSString *testFilter = @(cTestFilter);
return [GHTestSuite suiteWithTestFilter:testFilter];
} else {
if (GHUnitTest != NULL) return [GHTestSuite suiteWithTestFilter:GHUnitTest];
@@ -130,9 +130,9 @@ - (void)registerClassName:(NSString *)className {
}
+ (NSString *)descriptionForException:(NSException *)exception {
- NSNumber *lineNumber = [[exception userInfo] objectForKey:GHTestLineNumberKey];
+ NSNumber *lineNumber = [exception userInfo][GHTestLineNumberKey];
NSString *lineDescription = (lineNumber ? [lineNumber description] : @"Unknown");
- NSString *filename = [[[[exception userInfo] objectForKey:GHTestFilenameKey] stringByStandardizingPath] stringByAbbreviatingWithTildeInPath];
+ NSString *filename = [[[exception userInfo][GHTestFilenameKey] stringByStandardizingPath] stringByAbbreviatingWithTildeInPath];
NSString *filenameDescription = (filename ? filename : @"Unknown");
return [NSString stringWithFormat:@"\n\tName: %@\n\tFile: %@\n\tLine: %@\n\tReason: %@\n\n%@",
@@ -144,11 +144,11 @@ + (NSString *)descriptionForException:(NSException *)exception {
}
+ (NSString *)exceptionFilenameForTest:(id<GHTest>)test {
- return [[[[[test exception] userInfo] objectForKey:GHTestFilenameKey] stringByStandardizingPath] stringByAbbreviatingWithTildeInPath];
+ return [[[[test exception] userInfo][GHTestFilenameKey] stringByStandardizingPath] stringByAbbreviatingWithTildeInPath];
}
+ (NSInteger)exceptionLineNumberForTest:(id<GHTest>)test {
- return [[[[test exception] userInfo] objectForKey:GHTestLineNumberKey] integerValue];
+ return [[[test exception] userInfo][GHTestLineNumberKey] integerValue];
}
@@ -70,10 +70,8 @@ + (NSException *)ghu_failureInFile:(NSString *)filename
atLine:(int)lineNumber
reason:(NSString *)reason {
NSDictionary *userInfo =
- [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithInteger:lineNumber], GHTestLineNumberKey,
- filename, GHTestFilenameKey,
- nil];
+ @{GHTestLineNumberKey: @(lineNumber),
+ GHTestFilenameKey: filename};
return [self exceptionWithName:GHTestFailureException
reason:reason
@@ -249,10 +247,8 @@ + (NSException *)ghu_failureWithName:(NSString *)name
atLine:(int)lineNumber
reason:(NSString *)reason {
NSDictionary *userInfo =
- [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithInteger:lineNumber], GHTestLineNumberKey,
- filename, GHTestFilenameKey,
- nil];
+ @{GHTestLineNumberKey: @(lineNumber),
+ GHTestFilenameKey: filename};
return [self exceptionWithName:name
reason:reason
View
@@ -39,7 +39,7 @@ void GHRunForInterval(CFTimeInterval interval) {
NSArray *runLoopModes = @[(NSString *)kCFRunLoopDefaultMode, (NSString *)kCFRunLoopCommonModes];
NSInteger runIndex = 0;
while (CACurrentMediaTime() < runUntilTime) {
- NSString *mode = [runLoopModes objectAtIndex:(runIndex++ % [runLoopModes count])];
+ NSString *mode = runLoopModes[(runIndex++ % [runLoopModes count])];
@autoreleasepool {
SInt32 runLoopStatus = CFRunLoopRunInMode((__bridge CFStringRef)mode, checkEveryInterval, false);
if (!mode || (runLoopStatus == kCFRunLoopRunFinished)) {
@@ -49,7 +49,7 @@ - (id)initWithIdentifier:(NSString *)identifier suite:(GHTestSuite *)suite {
- (void)dealloc {
// Clear delegates
for(NSString *identifier in map_)
- [[map_ objectForKey:identifier] setDelegate:nil];
+ [map_[identifier] setDelegate:nil];
[runner_ cancel];
runner_.delegate = nil;
@@ -68,12 +68,12 @@ - (NSString *)statusString:(NSString *)prefix {
}
- (void)registerNode:(GHTestNode *)node {
- [map_ setObject:node forKey:node.identifier];
+ map_[node.identifier] = node;
node.delegate = self;
}
- (GHTestNode *)findTestNodeForTest:(id<GHTest>)test {
- return [map_ objectForKey:[test identifier]];
+ return map_[[test identifier]];
}
- (GHTestNode *)findFailure {
@@ -96,7 +96,7 @@ - (NSInteger)numberOfGroups {
- (NSInteger)numberOfTestsInGroup:(NSInteger)group {
NSArray *children = [root_ children];
if ([children count] == 0) return 0;
- GHTestNode *groupNode = [children objectAtIndex:group];
+ GHTestNode *groupNode = children[group];
return [[groupNode children] count];
}
@@ -127,12 +127,12 @@ - (NSString *)_defaultsPath {
if ([paths count] == 0) return nil;
NSString *identifier = identifier_;
if (!identifier) identifier = @"Tests";
- return [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"GHUnit-%@.tests", identifier]];
+ return [paths[0] stringByAppendingPathComponent:[NSString stringWithFormat:@"GHUnit-%@.tests", identifier]];
}
- (void)_updateTestNodeWithDefaults:(GHTestNode *)node {
id<GHTest> test = node.test;
- id<GHTest> testDefault = [defaults_ objectForKey:test.identifier];
+ id<GHTest> testDefault = defaults_[test.identifier];
if (testDefault) {
test.status = testDefault.status;
test.interval = testDefault.interval;
@@ -146,7 +146,7 @@ - (void)_updateTestNodeWithDefaults:(GHTestNode *)node {
}
- (void)_saveTestNodeToDefaults:(GHTestNode *)node {
- [defaults_ setObject:node.test forKey:node.test.identifier];
+ defaults_[node.test.identifier] = node.test;
for(GHTestNode *childNode in [node children])
[self _saveTestNodeToDefaults:childNode];
}

0 comments on commit 160be55

Please sign in to comment.