From 5887740a2cec07305cb45650a82752f920551383 Mon Sep 17 00:00:00 2001 From: Daniel Thorpe Date: Sun, 10 Apr 2016 22:13:53 +0100 Subject: [PATCH] [TAY-44]: Adds TableView tests --- TaylorSource.xcodeproj/project.pbxproj | 4 + Tests/TableViewTests.swift | 264 +++++++++++++++++++++++++ 2 files changed, 268 insertions(+) create mode 100644 Tests/TableViewTests.swift diff --git a/TaylorSource.xcodeproj/project.pbxproj b/TaylorSource.xcodeproj/project.pbxproj index 12dec1e..a970aec 100644 --- a/TaylorSource.xcodeproj/project.pbxproj +++ b/TaylorSource.xcodeproj/project.pbxproj @@ -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 */ @@ -70,6 +71,7 @@ 65E3FC761CBAAA2900479E0C /* CellDataSourceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CellDataSourceTests.swift; sourceTree = ""; }; 65E3FC781CBABC0D00479E0C /* ConformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConformanceTests.swift; sourceTree = ""; }; 65E3FC7A1CBAC4F000479E0C /* TestTableViewHeader.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TestTableViewHeader.xib; sourceTree = ""; }; + 65E3FC7C1CBADCD900479E0C /* TableViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -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 */, @@ -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; }; diff --git a/Tests/TableViewTests.swift b/Tests/TableViewTests.swift new file mode 100644 index 0000000..a978392 --- /dev/null +++ b/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: DataSourceProviderType { + + let dataSource: DataSource + let editor = NoEditor() + + init(_ dataSource: DataSource) { + self.dataSource = dataSource + } +} + +class ReadonlyTableViewDataSourceProviderTests: DataSourceTests { + + typealias DataSourceProvider = NoEditorDataSourceProvider + + var dataSourceProvider: DataSourceProvider! + var tableViewDataSourceProvider: TableViewDataSourceProvider! + 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: DataSourceProviderType { + + let dataSource: DataSource + let editor: Editor + + init(_ dataSource: DataSource, editor: Editor) { + self.dataSource = dataSource + self.editor = editor + } +} + +class InsertDeleteTableViewDataSourceProviderTests: DataSourceTests { + + typealias DataSourceProvider = EditableDataSourceProvider + + var editor: Editor! + var dataSourceProvider: DataSourceProvider! + var tableViewDataSourceProvider: TableViewDataSourceProvider! + 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 + + var editor: Editor! + var dataSourceProvider: DataSourceProvider! + var tableViewDataSourceProvider: TableViewDataSourceProvider! + 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)) + } +} + +