// // TableSectionDataDiffingTableViewTests.swift // Fabric // // Created by Javier Soto on 1/10/16. // Copyright © 2016 Fabric. All rights reserved. // import Foundation import Quick import Nimble @testable import FabricApp private final class TestTableViewDataSource: NSObject, UITableViewDataSource { var sections: [TestSection] = [] @objc private func numberOfSectionsInTableView(tableView: UITableView) -> Int { return self.sections.count } @objc private func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.sections[section].rows.count } @objc private func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // Doesn't really matter return UITableViewCell(style: .Default, reuseIdentifier: nil) } } class TableSectionDataDiffingTableViewTests: QuickSpec { override func spec() { describe("Applying table update operations") { struct TableSectionDiff { let old: [TestSection] let new: [TestSection] } let diffs: [TableSectionDiff] = [ TableSectionDiff( old: [.Section1([.Row1]), .Section2([])], new: [.Section1([.Row1]), .Section2([])] ), TableSectionDiff( old: [], new: [.Section1([])] ), TableSectionDiff( old: [.Section1([])], new: [] ), TableSectionDiff( old: [.Section1([])], new: [.Section1([])] ), TableSectionDiff( old: [.Section1([])], new: [.Section2([])] ), TableSectionDiff( old: [.Section1([])], new: [.Section1([.Row1])] ), TableSectionDiff( old: [.Section1([])], new: [.Section2([.Row1])] ), TableSectionDiff( old: [.Section1([.Row1])], new: [.Section2([.Row1])] ), TableSectionDiff( old: [.Section1([.Row1])], new: [.Section1([.Row1, .Row3])] ), TableSectionDiff( old: [.Section1([.Row1])], new: [.Section1([])] ), TableSectionDiff( old: [.Section1([.Row1])], new: [.Section1([.Row2])] ), ] for (index, diff) in diffs.enumerate() { it("TableView doesn't raise exception (check \(index + 1)") { let tableViewController = UITableViewController() let dataSource = TestTableViewDataSource() let tableView = tableViewController.tableView dataSource.sections = diff.old tableView.dataSource = dataSource tableView.reloadData() dataSource.sections = diff.new let operations = TableSectionDataDiffing.tableOperationsToUpdateFromSections(sections: diff.old, toSections: diff.new) expect(tableView.applyTableOperations(operations)).toNot(raiseException()) } } } } }