Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug1 #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions BrowseOverflow/Question.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,16 @@ - (NSArray *)answers {
return [[answerSet allObjects] sortedArrayUsingSelector: @selector(compare:)];
}

- (BOOL)isEqual:(id)object {
if ([object isKindOfClass: [Question class]]) {
Question *other = (Question *)object;
return (self.questionID == other.questionID);
} else {
return [super isEqual: object];
}
}

- (NSUInteger)hash {
return (NSUInteger)self.questionID;
}
@end
4 changes: 1 addition & 3 deletions BrowseOverflow/Topic.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
/**
* A class representing a particular subject on Stack Overflow.
*/
@interface Topic : NSObject {
NSArray *questions;
}
@interface Topic : NSObject
/**
* A name for this topic, suitable for displaying in the UI.
*/
Expand Down
16 changes: 10 additions & 6 deletions BrowseOverflow/Topic.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,34 @@ - (NSArray *)sortQuestionsLatestFirst: (NSArray *)questionList;
@end

@implementation Topic
{
NSSet *questions;
}
@synthesize name;
@synthesize tag;

- (id)initWithName:(NSString *)newName tag: (NSString *)newTag {
if ((self = [super init])) {
name = [newName copy];
tag = [newTag copy];
questions = [[NSArray alloc] init];
questions = [[NSSet alloc] init];
}
return self;
}


- (void)addQuestion: (Question *)question {
NSArray *newQuestions = [questions arrayByAddingObject: question];
NSSet *newQuestions = [questions setByAddingObject: question];
NSArray *latestQuestions = [newQuestions allObjects];
if ([newQuestions count] > 20) {
newQuestions = [self sortQuestionsLatestFirst: newQuestions];
newQuestions = [newQuestions subarrayWithRange: NSMakeRange(0, 20)];
latestQuestions = [self sortQuestionsLatestFirst: latestQuestions];
latestQuestions = [latestQuestions subarrayWithRange: NSMakeRange(0, 20)];
}
questions = newQuestions;
questions = [NSSet setWithArray: latestQuestions];
}

- (NSArray *)recentQuestions {
return [self sortQuestionsLatestFirst: questions];
return [self sortQuestionsLatestFirst: [questions allObjects]];
}

- (NSArray *)sortQuestionsLatestFirst: (NSArray *)questionList {
Expand Down
2 changes: 2 additions & 0 deletions BrowseOverflowTests/QuestionListTableDataSourceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ - (void)setUp {
firstCell = [NSIndexPath indexPathForRow: 0 inSection: 0];
question1 = [[Question alloc] init];
question1.title = @"Question One";
question1.questionID = 1;
question1.score = 2;
question2 = [[Question alloc] init];
question2.questionID = 2;
question2.title = @"Question Two";

asker1 = [[Person alloc] initWithName: @"Graham Lee" avatarLocation: @"http://www.gravatar.com/avatar/563290c0c1b776a315b36e863b388a0c"];
Expand Down
29 changes: 25 additions & 4 deletions BrowseOverflowTests/TopicTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
#import "Question.h"

@implementation TopicTests
{
Question *questionWithID123;
}

- (void)setUp {
topic = [[Topic alloc] initWithName: @"iPhone" tag: @"iphone"];
questionWithID123 = [[Question alloc] init];
questionWithID123.questionID = 123;
}

- (void)tearDown {
Expand All @@ -40,16 +45,17 @@ - (void)testForInitiallyEmptyQuestionList {
}

- (void)testAddingAQuestionToTheList {
Question *question = [[Question alloc] init];
[topic addQuestion: question];
[topic addQuestion: questionWithID123];
STAssertEquals([[topic recentQuestions] count], (NSUInteger)1, @"Add a question, and the count of questions should go up");
}

- (void)testQuestionsAreListedChronologically {
Question *q1 = [[Question alloc] init];
q1.questionID = 1;
q1.date = [NSDate distantPast];

Question *q2 = [[Question alloc] init];
q2.questionID = 2;
q2.date = [NSDate distantFuture];

[topic addQuestion: q1];
Expand All @@ -63,11 +69,26 @@ - (void)testQuestionsAreListedChronologically {
}

- (void)testLimitOfTwentyQuestions {
Question *q1 = [[Question alloc] init];
for (NSInteger i = 0; i < 25; i++) {
[topic addQuestion: q1];
Question *q = [[Question alloc] init];
q.questionID = i;
[topic addQuestion: q];
}
STAssertTrue([[topic recentQuestions] count] < 21, @"There should never be more than twenty questions");
}

- (void)testThatTheSameQuestionCannotBeAddedTwiceToTheList {
for (NSInteger i = 0; i < 2; i++) {
[topic addQuestion: questionWithID123];
}
STAssertEquals([[topic recentQuestions] count], (NSUInteger)1, @"Adding the same question twice should only yield one entry");
}

- (void)testThatDifferentQuestionsWithTheSameValueCannotBothBeInTheList {
Question *alsoQ123 = [[Question alloc] init];
alsoQ123.questionID = 123;
[topic addQuestion: questionWithID123];
[topic addQuestion: alsoQ123];
STAssertEquals([[topic recentQuestions] count], (NSUInteger)1, @"Adding questions with the same value should only yield one entry");
}
@end