Skip to content
This repository has been archived by the owner on Jun 19, 2018. It is now read-only.

Commit

Permalink
[TAY-44]: Adds TableView tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danthorpe committed Apr 10, 2016
1 parent fdc969a commit 5887740
Show file tree
Hide file tree
Showing 2 changed files with 268 additions and 0 deletions.
4 changes: 4 additions & 0 deletions TaylorSource.xcodeproj/project.pbxproj
Expand Up @@ -30,6 +30,7 @@
65E3FC771CBAAA2900479E0C /* CellDataSourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65E3FC761CBAAA2900479E0C /* CellDataSourceTests.swift */; };
65E3FC791CBABC0D00479E0C /* ConformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65E3FC781CBABC0D00479E0C /* ConformanceTests.swift */; };
65E3FC7B1CBAC4F000479E0C /* TestTableViewHeader.xib in Resources */ = {isa = PBXBuildFile; fileRef = 65E3FC7A1CBAC4F000479E0C /* TestTableViewHeader.xib */; };
65E3FC7D1CBADCD900479E0C /* TableViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65E3FC7C1CBADCD900479E0C /* TableViewTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -70,6 +71,7 @@
65E3FC761CBAAA2900479E0C /* CellDataSourceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CellDataSourceTests.swift; sourceTree = "<group>"; };
65E3FC781CBABC0D00479E0C /* ConformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConformanceTests.swift; sourceTree = "<group>"; };
65E3FC7A1CBAC4F000479E0C /* TestTableViewHeader.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TestTableViewHeader.xib; sourceTree = "<group>"; };
65E3FC7C1CBADCD900479E0C /* TableViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -144,6 +146,7 @@
6508498C1CB15C94005C7E6F /* DataSourceTests.swift */,
6508498D1CB15C94005C7E6F /* FactoryTests.swift */,
6590FABB1CB1743400694DAF /* Helpers.swift */,
65E3FC7C1CBADCD900479E0C /* TableViewTests.swift */,
6590FABD1CB17CAA00694DAF /* Testable.swift */,
6590FABF1CB1819A00694DAF /* TestCell.xib */,
65E3FC7A1CBAC4F000479E0C /* TestTableViewHeader.xib */,
Expand Down Expand Up @@ -324,6 +327,7 @@
6508498F1CB15C94005C7E6F /* DataSourceTests.swift in Sources */,
6590FAC21CB1A2C900694DAF /* ArrayDataSourceTests.swift in Sources */,
65E3FC791CBABC0D00479E0C /* ConformanceTests.swift in Sources */,
65E3FC7D1CBADCD900479E0C /* TableViewTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
264 changes: 264 additions & 0 deletions Tests/TableViewTests.swift
@@ -0,0 +1,264 @@
//
// TableViewTests.swift
// TaylorSource
//
// Created by Daniel Thorpe on 10/04/2016.
// Copyright © 2016 Daniel Thorpe. All rights reserved.
//

import XCTest
@testable import TaylorSource

class NoEditorDataSourceProvider<DataSource: DataSourceType>: DataSourceProviderType {

let dataSource: DataSource
let editor = NoEditor()

init(_ dataSource: DataSource) {
self.dataSource = dataSource
}
}

class ReadonlyTableViewDataSourceProviderTests: DataSourceTests {

typealias DataSourceProvider = NoEditorDataSourceProvider<DataSource>

var dataSourceProvider: DataSourceProvider!
var tableViewDataSourceProvider: TableViewDataSourceProvider<DataSourceProvider>!
var tableViewDataSource: UITableViewDataSource!
var indexPath: NSIndexPath!

override func setUp() {
super.setUp()
setUpAgain()
}

override func tearDown() {
indexPath = nil
tableViewDataSource = nil
tableViewDataSourceProvider = nil
dataSourceProvider = nil
super.tearDown()
}

func setUpAgain() {
dataSourceProvider = NoEditorDataSourceProvider(dataSource)
tableViewDataSourceProvider = TableViewDataSourceProvider(dataSourceProvider)
tableViewDataSource = tableViewDataSourceProvider.tableViewDataSource
indexPath = NSIndexPath(forRow: 0, inSection: 0)
}

func test__provider_dataSource_is_dataSource() {
XCTAssertNotNil(dataSource.identifier)
XCTAssertEqual(tableViewDataSourceProvider.dataSource.identifier ?? "Not Correct", dataSource.identifier)
}

func test__numberOfSectionsInTableView() {
XCTAssertEqual(tableViewDataSource.numberOfSectionsInTableView?(tableView) ?? 0, 1)
}

func test__numberOfRowsInSection() {
XCTAssertEqual(tableViewDataSource.tableView(tableView, numberOfRowsInSection: 0), 2)
}

func test__cellForRowAtIndexPath() {

var didConfigureCellWithItemAtIndex: (Cell, Item, NSIndexPath)? = .None
factory.registerCell(.ClassWithIdentifier(Cell.self, "cell identifier"), inView: tableView) { cell, item, index in
didConfigureCellWithItemAtIndex = (cell, item, index)
}
setUpAgain()

let _ = tableViewDataSource.tableView(tableView, cellForRowAtIndexPath: indexPath)

guard let (_, item, index) = didConfigureCellWithItemAtIndex else {
XCTFail("Did not configure cell"); return
}

XCTAssertEqual(item, "Hello")
XCTAssertEqual(indexPath, index)
}

func test__titleForHeaderInSection() {

var didConfigureSupplementaryText: Int? = .None
factory.registerSupplementaryTextWithKind(.Header) { section in
didConfigureSupplementaryText = section
return "Header text with index: \(section)"
}

setUpAgain()

let index = 0
let text = tableViewDataSource.tableView?(tableView, titleForHeaderInSection: index)

guard let section = didConfigureSupplementaryText else {
XCTFail("Did not configure cell"); return
}

XCTAssertNotNil(text)
XCTAssertEqual(text, "Header text with index: 0")
XCTAssertEqual(section, index)
}

func test__titleForFooterInSection() {

var didConfigureSupplementaryText: Int? = .None
factory.registerSupplementaryTextWithKind(.Footer) { section in
didConfigureSupplementaryText = section
return "Footer text with index: \(section)"
}

setUpAgain()

let index = 0
let text = tableViewDataSource.tableView?(tableView, titleForFooterInSection: 0)

guard let section = didConfigureSupplementaryText else {
XCTFail("Did not configure cell"); return
}

XCTAssertNotNil(text)
XCTAssertEqual(text, "Footer text with index: 0")
XCTAssertEqual(section, index)
}
}

class EditableDataSourceProvider<DataSource: DataSourceType>: DataSourceProviderType {

let dataSource: DataSource
let editor: Editor

init(_ dataSource: DataSource, editor: Editor) {
self.dataSource = dataSource
self.editor = editor
}
}

class InsertDeleteTableViewDataSourceProviderTests: DataSourceTests {

typealias DataSourceProvider = EditableDataSourceProvider<DataSource>

var editor: Editor!
var dataSourceProvider: DataSourceProvider!
var tableViewDataSourceProvider: TableViewDataSourceProvider<DataSourceProvider>!
var tableViewDataSource: UITableViewDataSource!
var indexPath: NSIndexPath!

var didCheckCanEditAtIndexPath: NSIndexPath? = .None
var didCommitEditActionAtIndexPath: (Edit.Action, NSIndexPath)? = .None

override func setUp() {
super.setUp()
setUpAgain()
}

override func tearDown() {
indexPath = nil
tableViewDataSource = nil
tableViewDataSourceProvider = nil
dataSourceProvider = nil
super.tearDown()
}

func setUpAgain() {
editor = Editor(
canEdit: { [unowned self] indexPath in
self.didCheckCanEditAtIndexPath = indexPath
return true
},
commitEdit: { [unowned self] action, indexPath in
self.didCommitEditActionAtIndexPath = (action, indexPath)
},
editAction: { _ in .Insert },
canMove: .None, move: .None)
dataSourceProvider = EditableDataSourceProvider(dataSource, editor: editor)
tableViewDataSourceProvider = TableViewDataSourceProvider(dataSourceProvider)
tableViewDataSource = tableViewDataSourceProvider.tableViewDataSource
indexPath = NSIndexPath(forRow: 0, inSection: 0)
}

func test__canEditRowAtIndexPath() {
XCTAssertTrue(tableViewDataSource.tableView?(tableView, canEditRowAtIndexPath: indexPath) ?? false)
XCTAssertEqual(didCheckCanEditAtIndexPath ?? NSIndexPath(forRow: 1, inSection: 1), indexPath)
}

func test__commitEditingStyleForRowAtIndexPath() {
tableViewDataSource.tableView?(tableView, commitEditingStyle: .Delete, forRowAtIndexPath: indexPath)
guard let (action, index) = didCommitEditActionAtIndexPath else {
XCTFail("Did not commit editing."); return
}

XCTAssertEqual(action, Edit.Action.Delete)
XCTAssertEqual(index, indexPath)
}
}

class EditableTableViewDataSourceProviderTests: DataSourceTests {

typealias DataSourceProvider = EditableDataSourceProvider<DataSource>

var editor: Editor!
var dataSourceProvider: DataSourceProvider!
var tableViewDataSourceProvider: TableViewDataSourceProvider<DataSourceProvider>!
var tableViewDataSource: UITableViewDataSource!
var indexPath: NSIndexPath!

var didCheckCanMoveAtIndexPath: NSIndexPath? = .None
var didMoveIndexPathToIndexPath: (NSIndexPath, NSIndexPath)? = .None

override func setUp() {
super.setUp()
setUpAgain()
}

override func tearDown() {
indexPath = nil
tableViewDataSource = nil
tableViewDataSourceProvider = nil
dataSourceProvider = nil
super.tearDown()
}

func setUpAgain() {
editor = Editor(
canEdit: { _ in true },
commitEdit: { _, _ in },
editAction: { _ in .Insert },
canMove: .None,
move: .None)

editor = Editor(
canEdit: { _ in true },
commitEdit: { _, _ in },
editAction: { _ in .Insert },
canMove: { [unowned self] indexPath in
self.didCheckCanMoveAtIndexPath = indexPath
return true
},
move: { [unowned self] from, to in
self.didMoveIndexPathToIndexPath = (from, to)
})
dataSourceProvider = EditableDataSourceProvider(dataSource, editor: editor)
tableViewDataSourceProvider = TableViewDataSourceProvider(dataSourceProvider)
tableViewDataSource = tableViewDataSourceProvider.tableViewDataSource
indexPath = NSIndexPath(forRow: 0, inSection: 0)
}

func test__canMoveRowAtIndexPath() {
XCTAssertTrue(tableViewDataSource.tableView?(tableView, canMoveRowAtIndexPath: indexPath) ?? false)
XCTAssertEqual(didCheckCanMoveAtIndexPath ?? NSIndexPath(forRow: 1, inSection: 1), indexPath)
}

func test__moveRowAtIndexPathToIndexPath() {
tableViewDataSource.tableView?(tableView, moveRowAtIndexPath: indexPath, toIndexPath: NSIndexPath(forRow: 1, inSection: 0))
guard let (from, to) = didMoveIndexPathToIndexPath else {
XCTFail("Did not move row."); return
}

XCTAssertEqual(from, indexPath)
XCTAssertEqual(to, NSIndexPath(forRow: 1, inSection: 0))
}
}


0 comments on commit 5887740

Please sign in to comment.