Permalink
Browse files

Answer through compare answer

  • Loading branch information...
1 parent 7915c19 commit 3f6a6bfa6ffceeccb5afb40f6efb3e0e25c34599 @grosshei committed Nov 27, 2012
@@ -7,11 +7,14 @@
objects = {
/* Begin PBXBuildFile section */
+ 1E13C2E82B9D0DDE53F78895 /* Answer.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E13C452D647C262E3E6CAE8 /* Answer.m */; };
1E13C432325ECF9A4BF696E6 /* PersonTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E13CDE0D945AF9838139F5F /* PersonTests.m */; };
1E13C98A10FE6EF954E81D6D /* Question.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E13C668BB194139649A1575 /* Question.m */; };
1E13CA01EF9FC6BBF7BE8313 /* Person.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E13C4FA4EA96FC59063F3D4 /* Person.m */; };
+ 1E13CA15F3BE5B789BD7D3EB /* AnswerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E13C741C53B2DD66FA89C39 /* AnswerTests.m */; };
1E13CB5B54FD3022E65BD66B /* Question.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E13C668BB194139649A1575 /* Question.m */; };
1E13CE3518E22DCEEFFF0132 /* Person.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E13C4FA4EA96FC59063F3D4 /* Person.m */; };
+ 1E13CF6F992D3396163C53F0 /* Answer.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E13C452D647C262E3E6CAE8 /* Answer.m */; };
7B06FA7616460FD7001B35F9 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B06FA7516460FD7001B35F9 /* UIKit.framework */; };
7B06FA7816460FD7001B35F9 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B06FA7716460FD7001B35F9 /* Foundation.framework */; };
7B06FA7A16460FD7001B35F9 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B06FA7916460FD7001B35F9 /* CoreGraphics.framework */; };
@@ -46,12 +49,16 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 1E13C452D647C262E3E6CAE8 /* Answer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Answer.m; sourceTree = "<group>"; };
1E13C498B10A812C2A0B9AEF /* Question.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Question.h; sourceTree = "<group>"; };
1E13C4FA4EA96FC59063F3D4 /* Person.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Person.m; sourceTree = "<group>"; };
1E13C65AFFEEDDE8B71285F5 /* Person.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Person.h; sourceTree = "<group>"; };
1E13C668BB194139649A1575 /* Question.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Question.m; sourceTree = "<group>"; };
+ 1E13C741C53B2DD66FA89C39 /* AnswerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AnswerTests.m; sourceTree = "<group>"; };
1E13CA507F1ABF4B32015C99 /* PersonTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PersonTests.h; sourceTree = "<group>"; };
1E13CDE0D945AF9838139F5F /* PersonTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PersonTests.m; sourceTree = "<group>"; };
+ 1E13CE6269976BC3CD4914E4 /* AnswerTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AnswerTests.h; sourceTree = "<group>"; };
+ 1E13CFDEDAD9B0856FD956F0 /* Answer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Answer.h; sourceTree = "<group>"; };
7B06FA7116460FD7001B35F9 /* BrowseOverflow.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BrowseOverflow.app; sourceTree = BUILT_PRODUCTS_DIR; };
7B06FA7516460FD7001B35F9 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
7B06FA7716460FD7001B35F9 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -155,6 +162,8 @@
1E13C498B10A812C2A0B9AEF /* Question.h */,
1E13C4FA4EA96FC59063F3D4 /* Person.m */,
1E13C65AFFEEDDE8B71285F5 /* Person.h */,
+ 1E13C452D647C262E3E6CAE8 /* Answer.m */,
+ 1E13CFDEDAD9B0856FD956F0 /* Answer.h */,
);
path = BrowseOverflow;
sourceTree = "<group>";
@@ -183,6 +192,8 @@
7B5D0C731652EC7F00D0920E /* QuestionTests.m */,
1E13CA507F1ABF4B32015C99 /* PersonTests.h */,
1E13CDE0D945AF9838139F5F /* PersonTests.m */,
+ 1E13CE6269976BC3CD4914E4 /* AnswerTests.h */,
+ 1E13C741C53B2DD66FA89C39 /* AnswerTests.m */,
);
path = BrowseOverflowTests;
sourceTree = "<group>";
@@ -313,6 +324,7 @@
7BB720551646123500C5D1C4 /* Topic.m in Sources */,
1E13CB5B54FD3022E65BD66B /* Question.m in Sources */,
1E13CA01EF9FC6BBF7BE8313 /* Person.m in Sources */,
+ 1E13C2E82B9D0DDE53F78895 /* Answer.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -326,6 +338,8 @@
1E13C98A10FE6EF954E81D6D /* Question.m in Sources */,
1E13C432325ECF9A4BF696E6 /* PersonTests.m in Sources */,
1E13CE3518E22DCEEFFF0132 /* Person.m in Sources */,
+ 1E13CA15F3BE5B789BD7D3EB /* AnswerTests.m in Sources */,
+ 1E13CF6F992D3396163C53F0 /* Answer.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -0,0 +1,23 @@
+//
+// Created by grosshei on 11/27/12.
+//
+// To change the template use AppCode | Preferences | File Templates.
+//
+
+
+#import <Foundation/Foundation.h>
+
+@class Person;
+
+
+@interface Answer : NSObject
+
+@property NSString *text;
+@property Person *person;
+@property NSInteger score;
+@property BOOL accepted;
+
+- (NSComparisonResult)compare:(Answer *)otherAnswer;
+
+
+@end
View
@@ -0,0 +1,32 @@
+//
+// Created by grosshei on 11/27/12.
+//
+// To change the template use AppCode | Preferences | File Templates.
+//
+
+
+#import "Answer.h"
+#import "Person.h"
+
+
+@implementation Answer
+
+@synthesize text, person, score, accepted;
+
+-(NSComparisonResult)compare:(Answer *)otherAnswer{
+ if (accepted && !(otherAnswer.accepted)){
+ return NSOrderedAscending;
+ } else if (!accepted && otherAnswer.accepted){
+ return NSOrderedDescending;
+ }
+
+ if(score > otherAnswer.score){
+ return NSOrderedAscending;
+ } else if (score < otherAnswer.score){
+ return NSOrderedDescending;
+ }else{
+ return NSOrderedSame;
+ }
+}
+
+@end
@@ -0,0 +1,18 @@
+//
+// AnswerTests.h
+// BrowseOverflow
+//
+// Created by Mike Grossheim on 11/27/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import <SenTestingKit/SenTestingKit.h>
+
+@class Answer;
+
+@interface AnswerTests : SenTestCase{
+ Answer *answer;
+ Answer *otherAnswer;
+}
+
+@end
@@ -0,0 +1,71 @@
+//
+// AnswerTests.m
+// BrowseOverflow
+//
+// Created by Mike Grossheim on 11/27/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import "AnswerTests.h"
+#import "Person.h"
+#import "Answer.h"
+
+@implementation AnswerTests
+
+-(void)setUp
+{
+ answer = [[Answer alloc] init];
+ answer.text = @"The answer is 42";
+ answer.person = [[Person alloc] initWithName:@"Graham Lee" avatarLocation:@"http://example.com/avatar.png"];
+ answer.score = 42;
+ otherAnswer = [[Answer alloc] init];
+ otherAnswer.text = @"I have the answer you need";
+ otherAnswer.score = 42;
+}
+
+-(void)tearDown
+{
+ answer = nil;
+ otherAnswer = nil;
+}
+
+-(void)testAnswerHasSomeText{
+ STAssertEqualObjects(answer.text, @"The answer is 42", @"Answers need to contain some text");
+}
+
+-(void)testSomeoneProvidedTheAnswer{
+ STAssertTrue([answer.person isKindOfClass:[Person class]], @"A Person gave this Answer");
+}
+
+-(void)testAnswersNotAcceptedByDefault{
+ STAssertFalse(answer.accepted, @"Answer not accepted by default");
+}
+
+-(void)testAnswerCanBeAccepted{
+ STAssertNoThrow(answer.accepted = YES, @"It is possible to accept an answer");
+}
+
+-(void)testAnswerHasAScore{
+ STAssertTrue(answer.score == 42, @"Answer's score can be retrieved");
+}
+
+-(void)testAcceptedAnswerComesBeforeUnaccepted{
+ otherAnswer.accepted = YES;
+ otherAnswer.score = answer.score + 10;
+
+ STAssertEquals([answer compare:otherAnswer], NSOrderedDescending, @"Accepted answer should come first");
+ STAssertEquals([otherAnswer compare:answer], NSOrderedAscending, @"Unaccepted answer should come last");
+}
+
+-(void)testAnswersWithEqualScoresCompareEqually{
+ STAssertEquals([answer compare:otherAnswer], NSOrderedSame, @"Both answers of equal rank");
+ STAssertEquals([otherAnswer compare:answer], NSOrderedSame, @"Each answer has the same rank");
+}
+
+-(void)testLowerScoringAnswerComesAfterHigher{
+ otherAnswer.score = answer.score + 10;
+ STAssertEquals([answer compare:otherAnswer], NSOrderedDescending, @"Higher score comes first");
+ STAssertEquals([otherAnswer compare:answer], NSOrderedAscending, @"Lower score comes last");
+}
+
+@end

0 comments on commit 3f6a6bf

Please sign in to comment.