Skip to content

Commit

Permalink
Generalize API of Changeset model and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Richter committed Feb 5, 2015
1 parent 31d7638 commit 098dbbf
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ObjectiveGoal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
0FA2AE021A228F79002801D2 /* OpenSans-Light.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 0FA2ADFF1A228F79002801D2 /* OpenSans-Light.ttf */; };
0FA2AE031A228F79002801D2 /* OpenSans-Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 0FA2AE001A228F79002801D2 /* OpenSans-Regular.ttf */; };
0FA2AE041A228F79002801D2 /* OpenSans-Semibold.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 0FA2AE011A228F79002801D2 /* OpenSans-Semibold.ttf */; };
0FBA66641A835BDB0089A3E8 /* ChangesetTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 0FBA66631A835BDB0089A3E8 /* ChangesetTests.m */; };
0FC0BFE71A229D45006A6695 /* PlayerCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 0FC0BFE61A229D45006A6695 /* PlayerCell.m */; };
0FC0BFEA1A22A25C006A6695 /* Player.m in Sources */ = {isa = PBXBuildFile; fileRef = 0FC0BFE91A22A25C006A6695 /* Player.m */; };
0FDBE03F1A1FD9F5001CA0ED /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 0FDBE03E1A1FD9F5001CA0ED /* main.m */; };
Expand Down Expand Up @@ -88,6 +89,7 @@
0FA2ADFF1A228F79002801D2 /* OpenSans-Light.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "OpenSans-Light.ttf"; sourceTree = "<group>"; };
0FA2AE001A228F79002801D2 /* OpenSans-Regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "OpenSans-Regular.ttf"; sourceTree = "<group>"; };
0FA2AE011A228F79002801D2 /* OpenSans-Semibold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "OpenSans-Semibold.ttf"; sourceTree = "<group>"; };
0FBA66631A835BDB0089A3E8 /* ChangesetTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ChangesetTests.m; sourceTree = "<group>"; };
0FC0BFE51A229D45006A6695 /* PlayerCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = PlayerCell.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
0FC0BFE61A229D45006A6695 /* PlayerCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = PlayerCell.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
0FC0BFE81A22A25C006A6695 /* Player.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = Player.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
Expand Down Expand Up @@ -210,6 +212,7 @@
children = (
0FA2ADFD1A224772002801D2 /* TestHelper.h */,
0FA2ADFB1A22475F002801D2 /* TestHelper.m */,
0FBA66631A835BDB0089A3E8 /* ChangesetTests.m */,
0FDCB2A51A1FFC0900656AF2 /* MatchesViewModelTests.m */,
0FA2ADE71A2139C8002801D2 /* EditMatchViewModelTests.m */,
0FA2ADF91A224631002801D2 /* ManagePlayersViewModelTests.m */,
Expand Down Expand Up @@ -494,6 +497,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0FBA66641A835BDB0089A3E8 /* ChangesetTests.m in Sources */,
0FA2ADE81A2139C8002801D2 /* EditMatchViewModelTests.m in Sources */,
0F9E47411A2E6A880022AA38 /* RankedPlayersViewModelTests.m in Sources */,
0FA2ADFA1A224631002801D2 /* ManagePlayersViewModelTests.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion ObjectiveGoal/Model/Changeset.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
@property (nonatomic, copy, readonly) NSArray *deletions;
@property (nonatomic, copy, readonly) NSArray *insertions;

+ (Changeset *)changesetFromMatches:(NSArray *)oldMatches toMatches:(NSArray *)newMatches;
+ (Changeset *)changesetOfIndexPathsFromItems:(NSArray *)oldItems toItems:(NSArray *)newItems;

@end
11 changes: 6 additions & 5 deletions ObjectiveGoal/Model/Changeset.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ - (instancetype)initWithDeletions:(NSArray *)deletions insertions:(NSArray *)ins

#pragma mark - Match Changesets

+ (Changeset *)changesetFromMatches:(NSArray *)oldMatches toMatches:(NSArray *)newMatches {
+ (Changeset *)changesetOfIndexPathsFromItems:(NSArray *)oldItems toItems:(NSArray *)newItems
{
NSMutableArray *mutableDeletions = [NSMutableArray array];
NSMutableArray *mutableInsertions = [NSMutableArray array];

[oldMatches enumerateObjectsUsingBlock:^(NSObject *match, NSUInteger index, BOOL *stop) {
if (![newMatches containsObject:match]) {
[oldItems enumerateObjectsUsingBlock:^(NSObject *item, NSUInteger index, BOOL *stop) {
if (![newItems containsObject:item]) {
[mutableDeletions addObject:[NSIndexPath indexPathForRow:index inSection:0]];
}
}];

[newMatches enumerateObjectsUsingBlock:^(NSObject *match, NSUInteger index, BOOL *stop) {
if (![oldMatches containsObject:match]) {
[newItems enumerateObjectsUsingBlock:^(NSObject *item, NSUInteger index, BOOL *stop) {
if (![oldItems containsObject:item]) {
[mutableInsertions addObject:[NSIndexPath indexPathForRow:index inSection:0]];
}
}];
Expand Down
2 changes: 1 addition & 1 deletion ObjectiveGoal/ViewModel/MatchesViewModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ - (instancetype)initWithAPIClient:(APIClient *)apiClient {

_contentChangesSignal = [updatedContentSignal
combinePreviousWithStart:@[] reduce:^(NSArray *previousMatches, NSArray *currentMatches) {
return [Changeset changesetFromMatches:previousMatches toMatches:currentMatches];
return [Changeset changesetOfIndexPathsFromItems:previousMatches toItems:currentMatches];
}];

_refreshIndicatorVisibleSignal = [RACSignal merge:@[
Expand Down
102 changes: 102 additions & 0 deletions ObjectiveGoalTests/ChangesetTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// ChangesetTests.m
// ObjectiveGoal
//
// Created by Martin Richter on 05/02/15.
// Copyright (c) 2015 Martin Richter. All rights reserved.
//

#import <XCTest/XCTest.h>
#import <UIKit/UITableView.h>
#import "Changeset.h"

@interface ChangesetTests : XCTestCase

@end

@implementation ChangesetTests

- (void)testChangesetBetweenEmptyArrays
{
Changeset *changeset = [Changeset changesetOfIndexPathsFromItems:@[] toItems:@[]];

XCTAssertEqual(changeset.deletions.count, 0);
XCTAssertEqual(changeset.insertions.count, 0);
}

- (void)testChangesetFromEmptyToNonEmptyArray
{
Changeset *changeset = [Changeset changesetOfIndexPathsFromItems:@[] toItems:@[[NSObject new]]];

XCTAssertEqual(changeset.deletions.count, 0);
XCTAssertEqual(changeset.insertions.count, 1);
XCTAssertEqual(changeset.insertions.firstObject, [NSIndexPath indexPathForRow:0 inSection:0]);
}

- (void)testChangesetFromNonEmptyToEmptyArray
{
Changeset *changeset = [Changeset changesetOfIndexPathsFromItems:@[[NSObject new]] toItems:@[]];

XCTAssertEqual(changeset.deletions.count, 1);
XCTAssertEqual(changeset.deletions.firstObject, [NSIndexPath indexPathForRow:0 inSection:0]);
XCTAssertEqual(changeset.insertions.count, 0);
}

- (void)testChangesetWithInsertedItems
{
NSObject *firstItem = [NSObject new];
NSObject *secondItem = [NSObject new];
NSObject *firstInsertedItem = [NSObject new];
NSObject *secondInsertedItem = [NSObject new];

Changeset *changeset = [Changeset
changesetOfIndexPathsFromItems:@[firstItem, secondItem]
toItems:@[firstItem, firstInsertedItem, secondItem, secondInsertedItem]];

XCTAssertEqual(changeset.deletions.count, 0);
XCTAssertEqual(changeset.insertions.count, 2);
XCTAssertEqual(changeset.insertions[0], [NSIndexPath indexPathForRow:1 inSection:0]);
XCTAssertEqual(changeset.insertions[1], [NSIndexPath indexPathForRow:3 inSection:0]);
}

- (void)testChangesetWithDeletedItems
{
NSObject *firstItem = [NSObject new];
NSObject *secondItem = [NSObject new];
NSObject *thirdItem = [NSObject new];
NSObject *fourthItem = [NSObject new];

Changeset *changeset = [Changeset
changesetOfIndexPathsFromItems:@[firstItem, secondItem, thirdItem, fourthItem]
toItems:@[firstItem, thirdItem]];

XCTAssertEqual(changeset.deletions.count, 2);
XCTAssertEqual(changeset.deletions[0], [NSIndexPath indexPathForRow:1 inSection:0]);
XCTAssertEqual(changeset.deletions[1], [NSIndexPath indexPathForRow:3 inSection:0]);
XCTAssertEqual(changeset.insertions.count, 0);
}

- (void)testChangesetWithDeletedAndInsertedItems
{
NSObject *firstItem = [NSObject new];
NSObject *secondItem = [NSObject new];
NSObject *thirdItem = [NSObject new];
NSObject *fourthItem = [NSObject new];

NSObject *firstInsertedItem = [NSObject new];
NSObject *secondInsertedItem = [NSObject new];

Changeset *changeset = [Changeset
changesetOfIndexPathsFromItems:@[firstItem, secondItem, thirdItem, fourthItem]
toItems:@[firstInsertedItem, firstItem, secondInsertedItem, thirdItem]];

XCTAssertEqual(changeset.deletions.count, 2);
XCTAssertEqual(changeset.deletions[0], [NSIndexPath indexPathForRow:1 inSection:0]);
XCTAssertEqual(changeset.deletions[1], [NSIndexPath indexPathForRow:3 inSection:0]);

XCTAssertEqual(changeset.insertions.count, 2);
XCTAssertEqual(changeset.insertions[0], [NSIndexPath indexPathForRow:0 inSection:0]);
XCTAssertEqual(changeset.insertions[1], [NSIndexPath indexPathForRow:2 inSection:0]);
}

@end

0 comments on commit 098dbbf

Please sign in to comment.