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
Prev Previous commit
Next Next commit
Move classes nested in test functions out of the function scope
  • Loading branch information
AleLudovici committed Apr 6, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit b5c31509511335d1624e6900b5ef80773adbe1b4
4 changes: 2 additions & 2 deletions Dwifft.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -104,10 +104,10 @@
isa = PBXGroup;
children = (
041EBBA41B89679200E113B3 /* DwifftTests.swift */,
D297A6651E9681B8008B9FD5 /* CollectionViewDiffTests.swift */,
D297A6631E968160008B9FD5 /* TableViewDiffTests.swift */,
04AC329E1E88AEB000EF63DD /* SwiftCheck.framework */,
041EBBA21B89679200E113B3 /* Supporting Files */,
D297A6631E968160008B9FD5 /* TableViewDiffTests.swift */,
D297A6651E9681B8008B9FD5 /* CollectionViewDiffTests.swift */,
);
path = DwifftTests;
sourceTree = "<group>";
130 changes: 65 additions & 65 deletions DwifftTests/CollectionViewDiffTests.swift
Original file line number Diff line number Diff line change
@@ -12,70 +12,6 @@ import XCTest
final class CollectionViewDiffTests: XCTestCase {
func testCollectionViewDiffCalculator() {

class TestCollectionView: UICollectionView {

let insertionExpectations: [Int: XCTestExpectation]
let deletionExpectations: [Int: XCTestExpectation]

init(insertionExpectations: [Int: XCTestExpectation], deletionExpectations: [Int: XCTestExpectation]) {
self.insertionExpectations = insertionExpectations
self.deletionExpectations = deletionExpectations
super.init(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
}

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

override func insertItems(at indexPaths: [IndexPath]) {
super.insertItems(at: indexPaths)
for indexPath in indexPaths {
self.insertionExpectations[(indexPath as NSIndexPath).item]!.fulfill()
}
}

override func deleteItems(at indexPaths: [IndexPath]) {
super.deleteItems(at: indexPaths)
for indexPath in indexPaths {
self.deletionExpectations[(indexPath as NSIndexPath).item]!.fulfill()
}
}

}

class TestViewController: UIViewController, UICollectionViewDataSource {

let testCollectionView: TestCollectionView
let diffCalculator: CollectionViewDiffCalculator<Int>
var rows: [Int] {
didSet {
self.diffCalculator.rows = rows
}
}

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

collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "TestCell")
collectionView.dataSource = self
}

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

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

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

}

var insertionExpectations: [Int: XCTestExpectation] = [:]
for i in [0, 3, 4, 5] {
@@ -90,8 +26,72 @@ final class CollectionViewDiffTests: XCTestCase {
}

let collectionView = TestCollectionView(insertionExpectations: insertionExpectations, deletionExpectations: deletionExpectations)
let viewController = TestViewController(collectionView: collectionView, rows: [0, 1, 2, 5, 8, 9, 0])
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 insertionExpectations: [Int: XCTestExpectation]
let deletionExpectations: [Int: XCTestExpectation]

init(insertionExpectations: [Int: XCTestExpectation], deletionExpectations: [Int: XCTestExpectation]) {
self.insertionExpectations = insertionExpectations
self.deletionExpectations = deletionExpectations
super.init(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
}

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

override func insertItems(at indexPaths: [IndexPath]) {
super.insertItems(at: indexPaths)
for indexPath in indexPaths {
self.insertionExpectations[(indexPath as NSIndexPath).item]!.fulfill()
}
}

override func deleteItems(at indexPaths: [IndexPath]) {
super.deleteItems(at: indexPaths)
for indexPath in indexPaths {
self.deletionExpectations[(indexPath as NSIndexPath).item]!.fulfill()
}
}

}

final class TestCollectionViewController: UIViewController, UICollectionViewDataSource {

let testCollectionView: TestCollectionView
let diffCalculator: CollectionViewDiffCalculator<Int>
var rows: [Int] {
didSet {
self.diffCalculator.rows = rows
}
}

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

collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "TestCell")
collectionView.dataSource = self
}

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

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

@objc func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: "TestCell", for: indexPath)
}
}
131 changes: 64 additions & 67 deletions DwifftTests/TableViewDiffTests.swift
Original file line number Diff line number Diff line change
@@ -11,71 +11,6 @@ import XCTest

final class TableViewDiffTests: XCTestCase {
func testTableViewDiffCalculator() {

class TestTableView: UITableView {

let insertionExpectations: [Int: XCTestExpectation]
let deletionExpectations: [Int: XCTestExpectation]

init(insertionExpectations: [Int: XCTestExpectation], deletionExpectations: [Int: XCTestExpectation]) {
self.insertionExpectations = insertionExpectations
self.deletionExpectations = deletionExpectations
super.init(frame: CGRect.zero, style: UITableViewStyle.plain)
}

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

override func insertRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation) {
XCTAssertEqual(animation, UITableViewRowAnimation.left, "incorrect insertion animation")
for indexPath in indexPaths {
self.insertionExpectations[(indexPath as NSIndexPath).row]!.fulfill()
}
}

override func deleteRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation) {
XCTAssertEqual(animation, UITableViewRowAnimation.right, "incorrect insertion animation")
for indexPath in indexPaths {
self.deletionExpectations[(indexPath as NSIndexPath).row]!.fulfill()
}
}

}

class TestViewController: UIViewController, UITableViewDataSource {

let tableView: TestTableView
let diffCalculator: TableViewDiffCalculator<Int>
var rows: [Int] {
didSet {
self.diffCalculator.rows = rows
}
}

init(tableView: TestTableView, rows: [Int]) {
self.tableView = tableView
self.diffCalculator = TableViewDiffCalculator<Int>(tableView: tableView, initialRows: rows)
self.diffCalculator.insertionAnimation = .left
self.diffCalculator.deletionAnimation = .right
self.rows = rows
super.init(nibName: nil, bundle: nil)
}

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

@objc func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}

@objc func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows.count
}

}

var insertionExpectations: [Int: XCTestExpectation] = [:]
for i in [0, 3, 4, 5] {
let x: XCTestExpectation = expectation(description: "+\(i)")
@@ -89,9 +24,71 @@ final class TableViewDiffTests: XCTestCase {
}

let tableView = TestTableView(insertionExpectations: insertionExpectations, deletionExpectations: deletionExpectations)
let viewController = TestViewController(tableView: tableView, rows: [0, 1, 2, 5, 8, 9, 0])
let viewController = TestTableViewController(tableView: tableView, 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 TestTableView: UITableView {

let insertionExpectations: [Int: XCTestExpectation]
let deletionExpectations: [Int: XCTestExpectation]

init(insertionExpectations: [Int: XCTestExpectation], deletionExpectations: [Int: XCTestExpectation]) {
self.insertionExpectations = insertionExpectations
self.deletionExpectations = deletionExpectations
super.init(frame: CGRect.zero, style: UITableViewStyle.plain)
}

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

override func insertRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation) {
XCTAssertEqual(animation, UITableViewRowAnimation.left, "incorrect insertion animation")
for indexPath in indexPaths {
self.insertionExpectations[(indexPath as NSIndexPath).row]!.fulfill()
}
}

override func deleteRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation) {
XCTAssertEqual(animation, UITableViewRowAnimation.right, "incorrect insertion animation")
for indexPath in indexPaths {
self.deletionExpectations[(indexPath as NSIndexPath).row]!.fulfill()
}
}

}

final class TestTableViewController: UIViewController, UITableViewDataSource {

let tableView: TestTableView
let diffCalculator: TableViewDiffCalculator<Int>
var rows: [Int] {
didSet {
self.diffCalculator.rows = rows
}
}

init(tableView: TestTableView, rows: [Int]) {
self.tableView = tableView
self.diffCalculator = TableViewDiffCalculator<Int>(tableView: tableView, initialRows: rows)
self.diffCalculator.insertionAnimation = .left
self.diffCalculator.deletionAnimation = .right
self.rows = rows
super.init(nibName: nil, bundle: nil)
}

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

@objc func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}

@objc func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows.count
}
}