Permalink
Browse files

End of chapter 6: data model finished

  • Loading branch information...
1 parent 3f6a6bf commit b4ba19ad3ff2ad166a9afb79673482db606d092e @grosshei committed Nov 28, 2012
View
@@ -15,7 +15,7 @@
@property NSString *text;
@property Person *person;
@property NSInteger score;
-@property BOOL accepted;
+@property (getter=isAccepted) BOOL accepted;
- (NSComparisonResult)compare:(Answer *)otherAnswer;
View
@@ -7,11 +7,19 @@
#import <Foundation/Foundation.h>
+@class Answer;
-@interface Question : NSObject
+
+@interface Question : NSObject{
+ NSMutableSet *answerSet;
+}
@property NSDate *date;
-@property NSString *title;
+@property (copy) NSString *title;
@property NSInteger score;
+@property (readonly) NSArray *answers;
+
+- (void)addAnswer:(Answer *)answer;
+
@end
View
@@ -6,10 +6,26 @@
#import "Question.h"
+#import "Answer.h"
@implementation Question
@synthesize date, title, score;
+-(id)init{
+ if(self = [super init]){
+ answerSet = [[NSMutableSet alloc] init];
+ }
+ return self;
+}
+
+-(void)addAnswer:(Answer *)answer{
+ [answerSet addObject:answer];
+}
+
+-(NSArray *)answers{
+ return [[answerSet allObjects] sortedArrayUsingSelector:@selector(compare:)];
+}
+
@end
@@ -9,10 +9,13 @@
#import <SenTestingKit/SenTestingKit.h>
@class Question;
+@class Answer;
@interface QuestionTests : SenTestCase
{
Question *question;
+ Answer *lowScore;
+ Answer *highScore;
}
@end
@@ -8,6 +8,7 @@
#import "QuestionTests.h"
#import "Question.h"
+#import "Answer.h"
@implementation QuestionTests
@@ -17,11 +18,26 @@ -(void)setUp
question.date = [NSDate distantPast];
question.title = @"Do iPhones also dream of electric sheep?";
question.score = 42;
+
+ Answer *accepted = [[Answer alloc] init];
+ accepted.score = 1;
+ accepted.accepted = YES;
+ [question addAnswer:accepted];
+
+ lowScore = [[Answer alloc] init];
+ lowScore.score = -4;
+ [question addAnswer: lowScore];
+
+ highScore = [[Answer alloc] init];
+ highScore.score = 4;
+ [question addAnswer: highScore];
}
-(void)tearDown
{
question = nil;
+ lowScore = nil;
+ highScore = nil;
}
-(void)testQuestionHasADate{
@@ -39,4 +55,20 @@ -(void)testQuestionHasATitle{
@"Do iPhones also dream of electric sheep?", @"Question should know its title");
}
+-(void)testQuestionCanHaveAnswersAdded{
+ Answer *answer = [[Answer alloc] init];
+ STAssertNoThrow([question addAnswer:answer], @"Must be able to add answers");
+}
+
+-(void)testAcceptedAnswerIsFirst{
+ STAssertTrue([[question.answers objectAtIndex:0] isAccepted], @"Accepted answer comes first");
+}
+
+-(void)testHighScoreAnswerBeforeLow{
+ NSArray *answers = question.answers;
+ NSInteger highIndex = [answers indexOfObject:highScore];
+ NSInteger lowIndex = [answers indexOfObject:lowScore];
+ STAssertTrue(highIndex < lowIndex, @"High-scoring answer comes first");
+}
+
@end

0 comments on commit b4ba19a

Please sign in to comment.