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

Refactor tests #51

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions Dwifft.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
041EBBB21B8967C300E113B3 /* Dwifft+UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 041EBBAF1B8967C300E113B3 /* Dwifft+UIKit.swift */; };
041EBBB31B8967C300E113B3 /* Dwifft+UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 041EBBAF1B8967C300E113B3 /* Dwifft+UIKit.swift */; };
04AC329F1E88AEB000EF63DD /* SwiftCheck.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 04AC329E1E88AEB000EF63DD /* SwiftCheck.framework */; };
D297A6641E968160008B9FD5 /* TableViewDiffTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D297A6631E968160008B9FD5 /* TableViewDiffTests.swift */; };
D297A6661E9681B8008B9FD5 /* CollectionViewDiffTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D297A6651E9681B8008B9FD5 /* CollectionViewDiffTests.swift */; };
D297A6681E9699CA008B9FD5 /* ExpectedAssertions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D297A6671E9699CA008B9FD5 /* ExpectedAssertions.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -37,6 +40,9 @@
041EBBAE1B8967C300E113B3 /* Dwifft.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Dwifft.swift; sourceTree = "<group>"; };
041EBBAF1B8967C300E113B3 /* Dwifft+UIKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Dwifft+UIKit.swift"; sourceTree = "<group>"; };
04AC329E1E88AEB000EF63DD /* SwiftCheck.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftCheck.framework; path = Carthage/Build/iOS/SwiftCheck.framework; sourceTree = SOURCE_ROOT; };
D297A6631E968160008B9FD5 /* TableViewDiffTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewDiffTests.swift; sourceTree = "<group>"; };
D297A6651E9681B8008B9FD5 /* CollectionViewDiffTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewDiffTests.swift; sourceTree = "<group>"; };
D297A6671E9699CA008B9FD5 /* ExpectedAssertions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpectedAssertions.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -100,6 +106,9 @@
isa = PBXGroup;
children = (
041EBBA41B89679200E113B3 /* DwifftTests.swift */,
D297A6651E9681B8008B9FD5 /* CollectionViewDiffTests.swift */,
D297A6631E968160008B9FD5 /* TableViewDiffTests.swift */,
D297A6671E9699CA008B9FD5 /* ExpectedAssertions.swift */,
04AC329E1E88AEB000EF63DD /* SwiftCheck.framework */,
041EBBA21B89679200E113B3 /* Supporting Files */,
);
Expand Down Expand Up @@ -252,9 +261,12 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D297A6681E9699CA008B9FD5 /* ExpectedAssertions.swift in Sources */,
041EBBB11B8967C300E113B3 /* Dwifft.swift in Sources */,
041EBBA51B89679200E113B3 /* DwifftTests.swift in Sources */,
D297A6641E968160008B9FD5 /* TableViewDiffTests.swift in Sources */,
041EBBB31B8967C300E113B3 /* Dwifft+UIKit.swift in Sources */,
D297A6661E9681B8008B9FD5 /* CollectionViewDiffTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
30 changes: 30 additions & 0 deletions DwifftExample/DwifftExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
045502231B898C6600F5614D /* Dwifft.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0455021E1B898C5400F5614D /* Dwifft.framework */; };
045502241B898C6600F5614D /* Dwifft.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 0455021E1B898C5400F5614D /* Dwifft.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
0455022B1B898CF000F5614D /* StuffTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0455022A1B898CF000F5614D /* StuffTableViewController.swift */; };
D297A65C1E93FB78008B9FD5 /* SwiftCheck.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D297A65B1E93FB78008B9FD5 /* SwiftCheck.framework */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -70,13 +71,15 @@
045502021B898C3200F5614D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
045502181B898C5400F5614D /* Dwifft.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Dwifft.xcodeproj; path = ../Dwifft.xcodeproj; sourceTree = "<group>"; };
0455022A1B898CF000F5614D /* StuffTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StuffTableViewController.swift; sourceTree = "<group>"; };
D297A65B1E93FB78008B9FD5 /* SwiftCheck.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftCheck.framework; path = ../Carthage/Build/iOS/SwiftCheck.framework; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
045501F01B898C3200F5614D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D297A65C1E93FB78008B9FD5 /* SwiftCheck.framework in Frameworks */,
045502231B898C6600F5614D /* Dwifft.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -90,6 +93,7 @@
045502181B898C5400F5614D /* Dwifft.xcodeproj */,
045501F51B898C3200F5614D /* DwifftExample */,
045501F41B898C3200F5614D /* Products */,
D297A65A1E93FB78008B9FD5 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -131,6 +135,14 @@
name = Products;
sourceTree = "<group>";
};
D297A65A1E93FB78008B9FD5 /* Frameworks */ = {
isa = PBXGroup;
children = (
D297A65B1E93FB78008B9FD5 /* SwiftCheck.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -142,6 +154,7 @@
045501F01B898C3200F5614D /* Frameworks */,
045501F11B898C3200F5614D /* Resources */,
045502271B898C6600F5614D /* Embed Frameworks */,
D297A65E1E93FB97008B9FD5 /* ShellScript */,
);
buildRules = (
);
Expand Down Expand Up @@ -224,6 +237,23 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
D297A65E1E93FB97008B9FD5 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"$(SRCROOT)/Carthage/Build/iOS/SwiftCheck.framework",
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "/usr/local/bin/carthage copy-frameworks";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
045501EF1B898C3200F5614D /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down
96 changes: 96 additions & 0 deletions DwifftTests/CollectionViewDiffTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// CollectionViewDiffTests.swift
// Dwifft
//
// Created by Alessandro on 06/04/2017.
// Copyright © 2017 jflinter. All rights reserved.
//

import UIKit
import XCTest

final class CollectionViewDiffTests: XCTestCase {
func testCollectionViewDiffCalculator() {
let expectedIndexesForInsertions = [0, 3, 4, 5]
let expectedIndexesForDeletions = [4, 2, 1, 0]

let collectionView = TestCollectionView(insertionAssertions: equalAssertions(for: expectedIndexesForInsertions),
deletionAssertions: equalAssertions(for: expectedIndexesForDeletions))
let viewController = TestCollectionViewController(collectionView: collectionView, rows: [0, 1, 2, 5, 8, 9, 0])

viewController.rows = [4, 5, 9, 8, 3, 1, 0]

waitForExpectations(timeout: 1.0, handler: nil)
}
}

final class TestCollectionView: UICollectionView {
let insertionAssertions: [ExpectedAssertion<Int>]
let deletionAssertions: [ExpectedAssertion<Int>]

init(insertionAssertions: [ExpectedAssertion<Int>], deletionAssertions: [ExpectedAssertion<Int>]) {
self.insertionAssertions = insertionAssertions
self.deletionAssertions = deletionAssertions
super.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
}

required init?(coder aDecoder: NSCoder) {
fatalError("not implemented")
}

override func insertItems(at indexPaths: [IndexPath]) {
super.insertItems(at: indexPaths)

indexPaths.enumerated().forEach { index, indexPath in
let assertion = insertionAssertions[index]
assertion.assertion(indexPath.item)
assertion.expectation.fulfill()
}
}

override func deleteItems(at indexPaths: [IndexPath]) {
super.deleteItems(at: indexPaths)

indexPaths.enumerated().forEach { index, indexPath in
let assertion = deletionAssertions[index]
assertion.assertion(indexPath.item)
assertion.expectation.fulfill()
}
}
}

final class TestCollectionViewController: UIViewController, UICollectionViewDataSource {
let testCollectionView: TestCollectionView
let diffCalculator: CollectionViewDiffCalculator<Int>

var rows: [Int] {
didSet {
diffCalculator.rows = rows
}
}

init(collectionView: TestCollectionView, rows: [Int]) {
self.testCollectionView = collectionView
self.diffCalculator = CollectionViewDiffCalculator<Int>(collectionView: collectionView, initialRows: rows)
self.rows = rows
super.init(nibName: nil, bundle: nil)
setupCollectionView()
}

required init?(coder aDecoder: NSCoder) {
fatalError("not implemented")
}

private func setupCollectionView() {
testCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "TestCell")
testCollectionView.dataSource = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return rows.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: "TestCell", for: indexPath)
}
}
Loading