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

Refactor using modern objective-c syntax #137

Merged
merged 2 commits into from Nov 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Classes-MacOSX/UI/GHTestApp.m
Expand Up @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions Classes-MacOSX/UI/GHTestOutlineViewModel.m
Expand Up @@ -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];
}
}

Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions Classes-iOS/GHUnitIOSAppDelegate.m
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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];
}


Expand Down
10 changes: 5 additions & 5 deletions Classes-iOS/GHUnitIOSTableViewDataSource.m
Expand Up @@ -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 {
Expand All @@ -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];
Expand Down
16 changes: 8 additions & 8 deletions Classes-iOS/GHUnitIOSTestViewController.m
Expand Up @@ -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_;
Expand All @@ -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];
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion Classes-iOS/GHUnitIOSView.m
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Classes-iOS/GHUnitIOSViewController.m
Expand Up @@ -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];
Expand Down
8 changes: 4 additions & 4 deletions Classes-iOS/GHViewTestCase.m
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"]];
Expand Down
8 changes: 4 additions & 4 deletions Classes/GHAsyncTestCase.m
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Classes/GHTest/GHTestRunner.m
Expand Up @@ -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"];
Expand Down
6 changes: 3 additions & 3 deletions Classes/GHTest/GHTestSuite.m
Expand Up @@ -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 {
Expand All @@ -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];
Expand Down
8 changes: 4 additions & 4 deletions Classes/GHTest/GHTesting.m
Expand Up @@ -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%@",
Expand All @@ -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];
}


Expand Down
12 changes: 4 additions & 8 deletions Classes/GHTest/NSException+GHTestFailureExceptions.m
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Classes/GHTestUtils.m
Expand Up @@ -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)) {
Expand Down
14 changes: 7 additions & 7 deletions Classes/SharedUI/GHTestViewModel.m
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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];
}

Expand Down Expand Up @@ -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;
Expand All @@ -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];
}
Expand Down
Binary file not shown.
Binary file not shown.