Created
February 10, 2013 18:44
-
-
Save justinwyer/4750573 to your computer and use it in GitHub Desktop.
Card equals tests and evolving implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FIRST TEST | |
- (void)testShouldKnowCardsWithSameContentsAreEqual { | |
Card *card = [[Card alloc] initWithContents:@"Test Card"]; | |
Card *equalCard = [[Card alloc] initWithContents:@"Test Card"]; | |
STAssertEqualObjects(card, equalCard, @"Cards with the same content should be equal"); | |
} | |
// RED isEqual is not yet overriden | |
// GREEN returns YES | |
- (BOOL)isEqual:(id)other { | |
return YES; | |
} | |
// SECOND TEST | |
- (void)testShouldKnowCardCannotBeEqualToADifferentObject { | |
Card *card = [[Card alloc] initWithContents:@"Test Card"]; | |
STAssertFalse([card isEqual:@"Not a Card"], @"Cards with the same content should be equal"); | |
} | |
// RED isEqual always returns YES | |
// GREEN isEqual checks type and returns NO | |
- (BOOL)isEqual:(id)other { | |
if (![[other class] isEqual:[self class]]) | |
return NO; | |
return YES; | |
} | |
// THIRD TEST | |
- (void)testShouldKnowCardsWithDifferContentsAreNotEqual { | |
Card *card = [[Card alloc] initWithContents:@"Test Card"]; | |
Card *differentCard = [[Card alloc] initWithContents:@"Different Test Card"]; | |
STAssertFalse([card isEqual: differentCard], @"Cards with the different content should not be equal"); | |
} | |
// RED always returns YES if other is a card | |
// GREEN delegates isEqual to NSString *contents | |
- (BOOL)isEqual:(id)other { | |
if (![[other class] isEqual:[self class]]) | |
return NO; | |
return [self.contents isEqualToString:((Card *)other).contents]; | |
} | |
// FORTH TEST - Hash needs to be overriden too | |
- (void)testShouldKnowCardHashEqualsContentsHash { | |
Card *card = [[Card alloc] initWithContents:@"Test Card"]; | |
STAssertEquals([card hash], [card.contents hash] , @"Card hash must equal its contents hash"); | |
} | |
// RED hash is not yet overriden | |
// GREEN delegate to NSString *contents | |
- (NSUInteger)hash { | |
return [self.contents hash]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment