diff --git a/Sources/CollectionView/CellFactory.swift b/Sources/CollectionView/CellFactory.swift index 10f463710160b5e98da94b122602977e4c645ced..a828584d4530531310ddb99969c7aeffda88a983 100644 --- a/Sources/CollectionView/CellFactory.swift +++ b/Sources/CollectionView/CellFactory.swift @@ -52,9 +52,12 @@ extension CellFactory { factories.forEach { $0.register(in: collectionView) } }, build: .init { model, collectionView, indexPath in - factories.lazy - .compactMap { $0.build(for: model, in: collectionView, at: indexPath) } - .first + for factory in factories { + if let cell = factory.build(for: model, in: collectionView, at: indexPath) { + return cell + } + } + return nil } ) } diff --git a/Tests/CollectionViewTests/CellFactoryTests.swift b/Tests/CollectionViewTests/CellFactoryTests.swift index c9b052dded1f5bab417fea256aabd5eed33fdd4c..0fc5063570ee1d2f615d24e5cc2d38292e0ab003 100644 --- a/Tests/CollectionViewTests/CellFactoryTests.swift +++ b/Tests/CollectionViewTests/CellFactoryTests.swift @@ -1,46 +1,46 @@ +import CustomDump import XCTest @testable import CollectionView final class CellFactoryTests: XCTestCase { func testCombined() { + struct Cell: Equatable { + var model: Int + var collectionView: UICollectionView + var indexPath: IndexPath + } + var didRegisterFirst = [UICollectionView]() var didRegisterSecond = [UICollectionView]() var didRegisterThird = [UICollectionView]() - class Cell: UICollectionViewCell { - var collectionView: UICollectionView? - var indexPath: IndexPath? - } + var didBuildFirst = [Cell]() + var didBuildSecond = [Cell]() + var didBuildThird = [Cell]() let factory = CellFactory<Int>.combined( .init( register: .init { didRegisterFirst.append($0) }, build: .init { model, collectionView, indexPath in guard model == 1 else { return nil } - let cell = Cell() - cell.collectionView = collectionView - cell.indexPath = indexPath - return cell + didBuildFirst.append(Cell(model: model, collectionView: collectionView, indexPath: indexPath)) + return UICollectionViewCell() } ), .init( register: .init { didRegisterSecond.append($0) }, build: .init { model, collectionView, indexPath in guard model == 2 else { return nil } - let cell = Cell() - cell.collectionView = collectionView - cell.indexPath = indexPath - return cell + didBuildSecond.append(Cell(model: model, collectionView: collectionView, indexPath: indexPath)) + return UICollectionViewCell() } ), .init( register: .init { didRegisterThird.append($0) }, build: .init { model, collectionView, indexPath in guard model == 3 else { return nil } - let cell = Cell() - cell.collectionView = collectionView - cell.indexPath = indexPath - return cell + didBuildThird.append(Cell(model: model, collectionView: collectionView, indexPath: indexPath)) + return UICollectionViewCell() } ) ) @@ -53,23 +53,53 @@ final class CellFactoryTests: XCTestCase { XCTAssertEqual(didRegisterSecond, [collectionView]) XCTAssertEqual(didRegisterThird, [collectionView]) - let firstCell = factory.build(for: 1, in: collectionView, at: IndexPath(item: 0, section: 1)) as? Cell + let firstCell = factory.build(for: 1, in: collectionView, at: IndexPath(item: 0, section: 1)) - XCTAssertEqual(firstCell?.collectionView, collectionView) - XCTAssertEqual(firstCell?.indexPath, IndexPath(item: 0, section: 1)) + XCTAssertNotNil(firstCell) + XCTAssertNoDifference(didBuildFirst, [Cell( + model: 1, + collectionView: collectionView, + indexPath: IndexPath(row: 0, section: 1) + )]) + XCTAssertNoDifference(didBuildSecond, []) + XCTAssertNoDifference(didBuildThird, []) - let secondCell = factory.build(for: 2, in: collectionView, at: IndexPath(item: 2, section: 3)) as? Cell + didBuildFirst = [] + didBuildSecond = [] + didBuildThird = [] + let secondCell = factory.build(for: 2, in: collectionView, at: IndexPath(item: 2, section: 3)) - XCTAssertEqual(secondCell?.collectionView, collectionView) - XCTAssertEqual(secondCell?.indexPath, IndexPath(item: 2, section: 3)) + XCTAssertNotNil(secondCell) + XCTAssertNoDifference(didBuildFirst, []) + XCTAssertNoDifference(didBuildSecond, [Cell( + model: 2, + collectionView: collectionView, + indexPath: IndexPath(row: 2, section: 3) + )]) + XCTAssertNoDifference(didBuildThird, []) - let thirdCell = factory.build(for: 3, in: collectionView, at: IndexPath(item: 4, section: 5)) as? Cell + didBuildFirst = [] + didBuildSecond = [] + didBuildThird = [] + let thirdCell = factory.build(for: 3, in: collectionView, at: IndexPath(item: 4, section: 5)) - XCTAssertEqual(thirdCell?.collectionView, collectionView) - XCTAssertEqual(thirdCell?.indexPath, IndexPath(item: 4, section: 5)) + XCTAssertNotNil(thirdCell) + XCTAssertNoDifference(didBuildFirst, []) + XCTAssertNoDifference(didBuildSecond, []) + XCTAssertNoDifference(didBuildThird, [Cell( + model: 3, + collectionView: collectionView, + indexPath: IndexPath(row: 4, section: 5) + )]) + didBuildFirst = [] + didBuildSecond = [] + didBuildThird = [] let otherCell = factory.build(for: 4, in: collectionView, at: IndexPath(item: 0, section: 0)) XCTAssertNil(otherCell) + XCTAssertNoDifference(didBuildFirst, []) + XCTAssertNoDifference(didBuildSecond, []) + XCTAssertNoDifference(didBuildThird, []) } }