Permalink
Browse files

Finished topic for now

  • Loading branch information...
grosshei committed Nov 27, 2012
1 parent ac2ab8a commit 7915c19452245886f62df3c7fdea89a3b81b0e41
Showing with 50 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +28 −0 BrowseOverflowTests/TopicTests.m
  3. +5 −0 Topic.h
  4. +16 −2 Topic.m
View
@@ -13,3 +13,4 @@ build/*
xcuserdata
profile
*.moved-aside
+.idea
@@ -8,6 +8,7 @@
#import "TopicTests.h"
#import "Topic.h"
+#import "Question.h"
@implementation TopicTests
@@ -36,4 +37,31 @@ -(void)testForAListOfQuestions{
@"Topics should provide a list of recent questions");
}
+-(void)testQuestionsAreListedChronologically{
+ Question *q1 = [[Question alloc] init];
+ q1.date = [NSDate distantPast];
+
+ Question *q2 = [[Question alloc] init];
+ q2.date = [NSDate distantFuture];
+
+ [topic addQuestion:q1];
+ [topic addQuestion:q2];
+
+ NSArray *questions = [topic recentQuestions];
+
+ Question *listedFirst = [questions objectAtIndex:0];
+ Question *listedSecond = [questions objectAtIndex:1];
+
+ STAssertEqualObjects([listedFirst.date laterDate:listedSecond.date], listedFirst.date, @"The later question should appear first in the list");
+
+}
+
+-(void)testLimitOfTwentyQuestions{
+ Question *q1 = [[Question alloc] init];
+ for (NSUInteger i = 0; i < 25; i++){
+ [topic addQuestion:q1];
+ }
+ STAssertTrue([[topic recentQuestions] count] < 21, @"There should never be more than twenty quesetions");
+}
+
@end
View
@@ -8,6 +8,8 @@
#import <Foundation/Foundation.h>
+@class Question;
+
@interface Topic : NSObject {
NSArray *questions;
}
@@ -16,6 +18,9 @@
@property (readonly) NSString *tag;
-(id)initWithName:(NSString *)newName tag:newTag;
+
+- (void)addQuestion:(Question *)question;
+
-(NSArray *)recentQuestions;
@end
View
18 Topic.m
@@ -23,11 +23,25 @@ -(id)initWithName:(NSString *)newName tag:newTag{
}
-(void)addQuestion: (Question *)question{
- questions = [questions arrayByAddingObject: question];
+ NSArray *newQuestions = [questions arrayByAddingObject: question];
+ if ([newQuestions count] > 20){
+ newQuestions = [self sortQuestionsLatestFirst:newQuestions];
+ newQuestions = [newQuestions subarrayWithRange:NSMakeRange(0, 20)];
+ }
+ questions = newQuestions;
+}
+
+-(NSArray *)sortQuestionsLatestFirst:questionList{
+ return [questionList sortedArrayUsingComparator:^(id obj1, id obj2){
+ Question *q1 = (Question *)obj1;
+ Question *q2 = (Question *)obj2;
+
+ return [q2.date compare:q1.date];
+ }];
}
-(NSArray *)recentQuestions{
- return questions;
+ return [self sortQuestionsLatestFirst:questions];
}
@end

0 comments on commit 7915c19

Please sign in to comment.