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 more tests for Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
danthorpe committed Apr 3, 2016
1 parent d5c7dc1 commit 34133b4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
31 changes: 27 additions & 4 deletions Tests/FactoryTests.swift
Expand Up @@ -9,7 +9,7 @@
import XCTest
@testable import TaylorSource

typealias TestableFactory = Factory<String, TestCell, UITableViewHeaderFooterView, TestableTable, NSIndexPath, NSIndexPath>
typealias TestableFactory = Factory<String, UITableViewCell, UITableViewHeaderFooterView, TestableTable, NSIndexPath, NSIndexPath>

class FactoryTests: XCTestCase {

Expand Down Expand Up @@ -90,9 +90,17 @@ class FactoryCellVendorTypeTests: FactoryTests {
XCTAssertThrowsError(try factory.cellForItem(item, inView: tableView, atIndex: indexPath), TestableFactory.Error.NoCellRegisteredAtIndex(indexPath))
}

func test__cellForItem__incorrect_cell_type_registered__throws_error() {
factory.registerCell(.ClassWithIdentifier(UITableViewCell.self, identifier), inView: tableView) { _, _, _ in }
XCTAssertThrowsError(try factory.cellForItem(item, inView: tableView, atIndex: indexPath), TestableFactory.Error.InvalidCellRegisteredAtIndexWithIdentifier(indexPath, identifier))
// func test__cellForItem__incorrect_cell_type_registered__throws_error() {
// factory.registerCell(.ClassWithIdentifier(UITableViewCell.self, "Another Identifier"), inView: tableView) { _, _, _ in }
// XCTAssertThrowsError(try factory.cellForItem(item, inView: tableView, atIndex: indexPath), TestableFactory.Error.InvalidCellRegisteredAtIndexWithIdentifier(indexPath, identifier))
// }

func test__cellForItem__configureBlockReceivesCell() {
factory.registerCell(.ClassWithIdentifier(TestableFactory.CellType.self, identifier), inView: tableView) { cell, item, index in
cell.textLabel!.text = item
}
let cell = XCTAssertNoThrows(try factory.cellForItem(item, inView: tableView, atIndex: indexPath))
XCTAssertEqual(cell.textLabel?.text ?? "Not Correct", item)
}
}

Expand All @@ -109,6 +117,12 @@ class FactorySupplementaryViewRegistrarTypeTests: FactoryTests {
XCTAssertEqual(index.kind, kind)
}

func test__registerSupplementaryView_multipleTimes() {
factory.registerSupplementaryView(.ClassWithIdentifier(UITableViewHeaderFooterView.self, identifier), kind: .Header, inView: tableView) { _, _ in }
factory.registerSupplementaryView(.ClassWithIdentifier(UITableViewHeaderFooterView.self, identifier), kind: .Footer, inView: tableView) { _, _ in }
XCTAssertEqual(factory.views.count, 2)
}

func test__registerSupplementaryView() {
var didExecuteConfiguration = false

Expand All @@ -126,6 +140,15 @@ class FactorySupplementaryViewRegistrarTypeTests: FactoryTests {
}
}

class FactorySupplementaryTextRegistrarTypeTests: FactoryTests {

func test__registerSupplementaryTextWithKind_multipleTimes() {
factory.registerSupplementaryTextWithKind(.Header) { "Header text with index: \($0)" }
factory.registerSupplementaryTextWithKind(.Footer) { "Footer text with index: \($0)" }
XCTAssertEqual(factory.texts.count, 2)
}
}

class SupplementaryElementKindTests: XCTestCase {

var kind: SupplementaryElementKind!
Expand Down
18 changes: 17 additions & 1 deletion Tests/Helpers.swift
Expand Up @@ -22,4 +22,20 @@ func XCTAssertThrowsError<E, T where E: ErrorType, E: Equatable>(@autoclosure ex
XCTFail("Incorrect error type thrown", file: file, line: line)
}
XCTAssertTrue(didCatchCorrectError, message, file: file, line: line)
}
}

func XCTAssertNoThrows<T>(@autoclosure expression: () throws -> T, @autoclosure _ message: () -> String = "", file: StaticString = #file, line: UInt = #line) -> T! {

do {
return try expression()
}
catch {
XCTFail(message(), file: file, line: line)
}
return nil
}





0 comments on commit 34133b4

Please sign in to comment.