<commit_before>//
//  HomepageViewControllerTests.swift
//  Peekazoo
//
//  Created by <NAME> on 14/05/2017.
//  Copyright © 2017 Peekazoo. All rights reserved.
//

@testable import Peekazoo
import XCTest

class HomepageViewControllerTests: XCTestCase {

    var homepageViewController: HomepageViewController!

    override func setUp() {
        super.setUp()

        homepageViewController = HomepageViewController()
        homepageViewController.loadViewIfNeeded()
    }

    private func updateInterface(viewModel: HomepageInterfaceViewModel, applyingDifferences diffs: [Difference]) {
        homepageViewController.updateInterface(viewModel: viewModel, applyingDifferences: [])
        homepageViewController.collectionView.layoutIfNeeded()
    }

    func testHasHomeAsTheTitle() {
        XCTAssertEqual("Home", homepageViewController.title)
    }

    func testHasCollectionView() {
        XCTAssertNotNil(homepageViewController.collectionView)
    }

    func testHaveZeroSectionsInTheCollectionViewBeforeLoading() {
        XCTAssertEqual(0, homepageViewController.collectionView?.numberOfSections)
    }

    func testUpdatingInterfaceUpdatesNumberOfSectionsToOne() {
        let item = StubHomepageInterfaceItemViewModel()
        let viewModel = StubHomepageInterfaceViewModel(items: [item])
        updateInterface(viewModel: viewModel, applyingDifferences: [])

        XCTAssertEqual(1, homepageViewController.collectionView?.numberOfSections)
    }

    func testUpdatingInterfaceUpdatesNumberOfItemsToCountFromViewModel() {
        let count = Int.random(upperLimit: 100)
        let items = Array(repeating: StubHomepageInterfaceItemViewModel(), count: count)
        let insertions = (0..<count).map({ Difference.insertion(index: $0) })
        let viewModel = StubHomepageInterfaceViewModel(items: items)
        updateInterface(viewModel: viewModel, applyingDifferences: insertions)

        XCTAssertEqual(count, homepageViewController.collectionView?.numberOfItems(inSection: 0))
    }

    func testDequeueingCellAfterSupplyingContentShouldProvideHomepageItemCell() {
        let item = StubHomepageInterfaceItemViewModel()
        let viewModel = StubHomepageInterfaceViewModel(items: [item])
        updateInterface(viewModel: viewModel, applyingDifferences: [])
        let cell = homepageViewController.collectionView.cellForItem(at: IndexPath(item: 0, section: 0))

        XCTAssertTrue(cell is HomepageItemCollectionViewCell)
    }

    func testDequeuedHomepageItemCellsShouldHaveLabels() {
        let item = StubHomepageInterfaceItemViewModel()
        let viewModel = StubHomepageInterfaceViewModel(items: [item])
        updateInterface(viewModel: viewModel, applyingDifferences: [])
        let cell = homepageViewController.collectionView.cellForItem(at: IndexPath(item: 0, section: 0)) as? HomepageItemCollectionViewCell

        XCTAssertNotNil(cell?.itemTitleLabel)
    }

    func testDequeuedItemCellShouldBeConfiguredWithTheTitleFromTheViewModel() {
        let item = StubHomepageInterfaceItemViewModel(title: "Some title")
        let viewModel = StubHomepageInterfaceViewModel(items: [item])
        updateInterface(viewModel: viewModel, applyingDifferences: [])
        let cell = homepageViewController.collectionView.cellForItem(at: IndexPath(item: 0, section: 0)) as? HomepageItemCollectionViewCell

        XCTAssertEqual(item.title, cell?.itemTitleLabel.text)
    }

    func testDequeuedItemsShouldBeProvidedWithCorrectTitlesFromViewModel() {
        let firstItem = StubHomepageInterfaceItemViewModel(title: "Some title")
        let secondItem = StubHomepageInterfaceItemViewModel(title: "Some other title")
        let viewModel = StubHomepageInterfaceViewModel(items: [firstItem, secondItem])
        updateInterface(viewModel: viewModel, applyingDifferences: [])
        let cell = homepageViewController.collectionView.cellForItem(at: IndexPath(item: 1, section: 0)) as? HomepageItemCollectionViewCell

        XCTAssertEqual(secondItem.title, cell?.itemTitleLabel.text)
    }
<commit_msg>Extract common cell access from tests<commit_after>//
//  HomepageViewControllerTests.swift
//  Peekazoo
//
//  Created by <NAME> on 14/05/2017.
//  Copyright © 2017 Peekazoo. All rights reserved.
//

@testable import Peekazoo
import XCTest

class HomepageViewControllerTests: XCTestCase {

    var homepageViewController: HomepageViewController!

    override func setUp() {
        super.setUp()

        homepageViewController = HomepageViewController()
        homepageViewController.loadViewIfNeeded()
    }

    private func updateInterface(viewModel: HomepageInterfaceViewModel, applyingDifferences diffs: [Difference]) {
        homepageViewController.updateInterface(viewModel: viewModel, applyingDifferences: [])
        homepageViewController.collectionView.layoutIfNeeded()
    }

    private func cellForItem(at index: Int) -> HomepageItemCollectionViewCell? {
        let indexPath = IndexPath(item: index, section: 0)
        let cell = homepageViewController.collectionView.cellForItem(at: indexPath)
        return cell as? HomepageItemCollectionViewCell
    }

    func testHasHomeAsTheTitle() {
        XCTAssertEqual("Home", homepageViewController.title)
    }

    func testHasCollectionView() {
        XCTAssertNotNil(homepageViewController.collectionView)
    }

    func testHaveZeroSectionsInTheCollectionViewBeforeLoading() {
        XCTAssertEqual(0, homepageViewController.collectionView?.numberOfSections)
    }

    func testUpdatingInterfaceUpdatesNumberOfSectionsToOne() {
        let item = StubHomepageInterfaceItemViewModel()
        let viewModel = StubHomepageInterfaceViewModel(items: [item])
        updateInterface(viewModel: viewModel, applyingDifferences: [])

        XCTAssertEqual(1, homepageViewController.collectionView?.numberOfSections)
    }

    func testUpdatingInterfaceUpdatesNumberOfItemsToCountFromViewModel() {
        let count = Int.random(upperLimit: 100)
        let items = Array(repeating: StubHomepageInterfaceItemViewModel(), count: count)
        let insertions = (0..<count).map({ Difference.insertion(index: $0) })
        let viewModel = StubHomepageInterfaceViewModel(items: items)
        updateInterface(viewModel: viewModel, applyingDifferences: insertions)

        XCTAssertEqual(count, homepageViewController.collectionView?.numberOfItems(inSection: 0))
    }

    func testDequeueingCellAfterSupplyingContentShouldProvideHomepageItemCell() {
        let item = StubHomepageInterfaceItemViewModel()
        let viewModel = StubHomepageInterfaceViewModel(items: [item])
        updateInterface(viewModel: viewModel, applyingDifferences: [])

        XCTAssertTrue((cellForItem(at: 0) as Any) is HomepageItemCollectionViewCell)
    }

    func testDequeuedHomepageItemCellsShouldHaveLabels() {
        let item = StubHomepageInterfaceItemViewModel()
        let viewModel = StubHomepageInterfaceViewModel(items: [item])
        updateInterface(viewModel: viewModel, applyingDifferences: [])

        XCTAssertNotNil(cellForItem(at: 0)?.itemTitleLabel)
    }

    func testDequeuedItemCellShouldBeConfiguredWithTheTitleFromTheViewModel() {
        let item = StubHomepageInterfaceItemViewModel(title: "Some title")
        let viewModel = StubHomepageInterfaceViewModel(items: [item])
        updateInterface(viewModel: viewModel, applyingDifferences: [])

        XCTAssertEqual(item.title, cellForItem(at: 0)?.itemTitleLabel.text)
    }

    func testDequeuedItemsShouldBeProvidedWithCorrectTitlesFromViewModel() {
        let firstItem = StubHomepageInterfaceItemViewModel(title: "Some title")
        let secondItem = StubHomepageInterfaceItemViewModel(title: "Some other title")
        let viewModel = StubHomepageInterfaceViewModel(items: [firstItem, secondItem])
        updateInterface(viewModel: viewModel, applyingDifferences: [])

        XCTAssertEqual(secondItem.title, cellForItem(at: 1)?.itemTitleLabel.text)
    }

{'fraction_non_alphanumeric': 0.08115577889447236, 'fraction_numerical': 0.006658291457286432, 'mean_word_length': 4.426721199727335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4736809', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2077, 'n_words': 393}