From 7e3d52fb92cb6294da9b92dbcb591889c6848d56 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Sat, 18 Jun 2016 06:20:29 +0300 Subject: [PATCH] add tests --- Sources/TableDirector.swift | 6 +++ Tests/TableKitTests.swift | 89 +++++++++---------------------------- 2 files changed, 26 insertions(+), 69 deletions(-) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 170e06d..5c1021c 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -223,6 +223,12 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate return self } + public func append(rows rows: [Row]) -> Self { + + append(section: TableSection(rows: rows)) + return self + } + public func clear() -> Self { sections.removeAll() diff --git a/Tests/TableKitTests.swift b/Tests/TableKitTests.swift index a3e3ea3..7a601e1 100644 --- a/Tests/TableKitTests.swift +++ b/Tests/TableKitTests.swift @@ -63,7 +63,7 @@ class TestTableViewCell: UITableViewCell, ConfigurableCell { } func raiseAction() { - //Action(key: TestTableViewCellOptions.CellAction, sender: self, userInfo: [TestTableViewCellOptions.CellActionUserInfoKey: TestTableViewCellOptions.CellActionUserInfoValue]).invoke() + TableCellAction(key: TestTableViewCellOptions.CellAction, sender: self, userInfo: nil).invoke() } } @@ -112,7 +112,7 @@ class TabletTests: XCTestCase { let data = [TestData(title: "1"), TestData(title: "2"), TestData(title: "3")] - let rows: [TableRow] = data.map { TableRow(item: $0) } + let rows: [Row] = data.map({ TableRow(item: $0) }) testController.tableDirector += rows testController.tableView.reloadData() @@ -128,64 +128,18 @@ class TabletTests: XCTestCase { } } - - - - - /*func testSimpleRowBuilderCreatesRowsAndSection() { - - let source = ["1", "2", "3"] - - let rows = TableBaseRowBuilder(items: source) - .action(.configure) { data -> Void in - - XCTAssertNotNil(data.cell, "Action should have a cell") - data.cell?.textLabel?.text = "\(data.item)" - } + func testTableSectionCreatesSectionWithHeaderAndFooterTitles() { - testController.view.hidden = false - testController.tableDirector += rows - testController.tableView.reloadData() - - XCTAssertTrue(testController.tableView.dataSource?.numberOfSectionsInTableView?(testController.tableView) == 1, "Table view should have a section") - XCTAssertTrue(testController.tableView.dataSource?.tableView(testController.tableView, numberOfRowsInSection: 0) == source.count, "Table view should have certain number of rows in a section") + let row = TableRow(item: TestData(title: "title")) - for (index, element) in source.enumerate() { - - let cell = testController.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) - - XCTAssertNotNil(cell) - XCTAssertTrue(cell?.textLabel?.text == element) - } - } - - func testConfigurableRowBuilderCreatesRowsAndSection() { - - let testData = TestData(title: "title") - - testController.view.hidden = false - testController.tableDirector += TableRowBuilder(item: testData) - testController.tableView.reloadData() - - let cell = testController.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as? TestTableViewCell - - XCTAssertNotNil(cell, "Cell should exists and should be TestTableViewCell") - XCTAssertTrue(cell?.textLabel?.text == testData.title, "Cell's textLabel.text should equal to testData's title") - } - - func testSectionBuilderCreatesSectionWithHeaderAndFooterTitles() { - - let row = TableRowBuilder(items: [TestData(title: "title")]) - let sectionHeaderTitle = "Header Title" let sectionFooterTitle = "Footer Title" - - let section = TableSectionBuilder(headerTitle: sectionHeaderTitle, footerTitle: sectionFooterTitle, rows: [row]) - - testController.view.hidden = false + + let section = TableSection(headerTitle: sectionHeaderTitle, footerTitle: sectionFooterTitle, rows: [row]) + testController.tableDirector += section testController.tableView.reloadData() - + XCTAssertTrue(testController.tableView.dataSource?.numberOfSectionsInTableView?(testController.tableView) == 1, "Table view should have a section") XCTAssertTrue(testController.tableView.dataSource?.tableView(testController.tableView, numberOfRowsInSection: 0) == 1, "Table view should have certain number of rows in a section") @@ -193,17 +147,16 @@ class TabletTests: XCTestCase { XCTAssertTrue(testController.tableView.dataSource?.tableView?(testController.tableView, titleForFooterInSection: 0) == sectionFooterTitle) } - func testSectionBuilderCreatesSectionWithHeaderAndFooterViews() { - - let row = TableRowBuilder(items: [TestData(title: "title")]) + func testTableSectionCreatesSectionWithHeaderAndFooterViews() { + + let row = TableRow(item: TestData(title: "title")) let sectionHeaderView = UIView() let sectionFooterView = UIView() - - let section = TableSectionBuilder(headerView: sectionHeaderView, footerView: sectionFooterView, rows: nil) + + let section = TableSection(headerView: sectionHeaderView, footerView: sectionFooterView, rows: nil) section += row - testController.view.hidden = false testController.tableDirector += section testController.tableView.reloadData() @@ -213,20 +166,18 @@ class TabletTests: XCTestCase { XCTAssertTrue(testController.tableView.delegate?.tableView?(testController.tableView, viewForHeaderInSection: 0) == sectionHeaderView) XCTAssertTrue(testController.tableView.delegate?.tableView?(testController.tableView, viewForFooterInSection: 0) == sectionFooterView) } - + func testRowBuilderCustomActionInvokedAndSentUserInfo() { let expectation = expectationWithDescription("cell action") - let row = TableRowBuilder(items: [TestData(title: "title")]) - .action(TestTableViewCellOptions.CellAction) { data -> Void in - + let row = TableRow(item: TestData(title: "title")) + .action(TableRowAction(.custom(TestTableViewCellOptions.CellAction)) { (data) in + XCTAssertNotNil(data.cell, "Action data should have a cell") - XCTAssertNotNil(data.userInfo, "Action data should have a user info dictionary") - XCTAssertTrue(data.userInfo?[TestTableViewCellOptions.CellActionUserInfoKey] as? String == TestTableViewCellOptions.CellActionUserInfoValue, "UserInfo should have correct value for key") - + expectation.fulfill() - } + }) testController.view.hidden = false testController.tableDirector += row @@ -239,5 +190,5 @@ class TabletTests: XCTestCase { cell?.raiseAction() waitForExpectationsWithTimeout(1.0, handler: nil) - }*/ + } } \ No newline at end of file