diff --git a/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing.xcodeproj/xcshareddata/xcbaselines/F3E26D942239409000AE182D.xcbaseline/122389AC-969F-420D-9EF1-1ED43478ED92.plist b/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing.xcodeproj/xcshareddata/xcbaselines/F3E26D942239409000AE182D.xcbaseline/122389AC-969F-420D-9EF1-1ED43478ED92.plist index 61ecbb8..443b6af 100644 --- a/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing.xcodeproj/xcshareddata/xcbaselines/F3E26D942239409000AE182D.xcbaseline/122389AC-969F-420D-9EF1-1ED43478ED92.plist +++ b/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing.xcodeproj/xcshareddata/xcbaselines/F3E26D942239409000AE182D.xcbaseline/122389AC-969F-420D-9EF1-1ED43478ED92.plist @@ -11,7 +11,7 @@ com.apple.XCTPerformanceMetric_WallClockTime baselineAverage - 0.391 + 0.556 baselineIntegrationDisplayName Local Baseline diff --git a/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing/Controllers/Home/HomeViewController.swift b/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing/Controllers/Home/HomeViewController.swift index 3eecfaa..0ef3401 100644 --- a/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing/Controllers/Home/HomeViewController.swift +++ b/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing/Controllers/Home/HomeViewController.swift @@ -10,10 +10,19 @@ import UIKit class HomeViewController: UITableViewController { lazy var playData = PlayData() + lazy var words = playData.filteredWords +} + + +// MARK: - Lifecycle + +extension HomeViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. + + playData.applyCustomFilter({ $0.lowercased().contains("swift") }) } } @@ -22,12 +31,12 @@ class HomeViewController: UITableViewController { extension HomeViewController { override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return playData.allWords.count + return words.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: StoryboardID.TableView.cell, for: indexPath) - let word = playData.allWords[indexPath.row] + let word = words[indexPath.row] cell.textLabel?.text = word cell.detailTextLabel?.text = "\(playData.wordCounts.count(for: word)) counts" diff --git a/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing/Models/PlayData.swift b/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing/Models/PlayData.swift index 5a5e139..e07cd44 100644 --- a/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing/Models/PlayData.swift +++ b/39-ios-unit-testing/iOS Unit Testing/iOS Unit Testing/Models/PlayData.swift @@ -10,21 +10,41 @@ import Foundation class PlayData { private lazy var pathToWords = Bundle.main.path(forResource: "plays", ofType: "txt") - - var wordCounts: NSCountedSet! - + + var allWords: [String] = [] + var wordsSortedByCount: [String] = [] + private(set) var filteredWords: [String] = [] + + var wordCounts: NSCountedSet! { + didSet { + wordCountsChanged() + } + } + + init() { loadWords() + filteredWords = allWords } } + +// MARK: - Core Methods + extension PlayData { - var allWords: [String] { - return wordCounts.allObjects as! [String] + func applyCustomFilter(_ predicate: (_ word: String) -> Bool) { + filteredWords = allWords.filter(predicate) + } + + func setCountThreshold(_ minimumCount: Int) { + filteredWords = allWords.filter { wordCounts.count(for: $0) >= minimumCount } } } + +// MARK: - Private Helper Methods + private extension PlayData { func loadWords() { guard let pathToWords = pathToWords else { @@ -44,5 +64,13 @@ private extension PlayData { print("Error while parsing plays file:\n\n\(error.localizedDescription)") } } + + func wordCountsChanged() { + allWords = wordCounts.allObjects as! [String] + + wordsSortedByCount = allWords.sorted(by: { + wordCounts.count(for: $0) > wordCounts.count(for: $1) + }) + } } diff --git a/39-ios-unit-testing/iOS Unit Testing/iOS Unit TestingTests/iOS_Unit_TestingTests.swift b/39-ios-unit-testing/iOS Unit Testing/iOS Unit TestingTests/iOS_Unit_TestingTests.swift index e70f7d9..1d08195 100644 --- a/39-ios-unit-testing/iOS Unit Testing/iOS Unit TestingTests/iOS_Unit_TestingTests.swift +++ b/39-ios-unit-testing/iOS Unit Testing/iOS Unit TestingTests/iOS_Unit_TestingTests.swift @@ -43,6 +43,23 @@ class iOS_Unit_TestingTests: XCTestCase { XCTAssertEqual(actual, expected, "\"\(word)\" did not have a count of \(count)") } } + + func testFilteredWords() { + let playData = PlayData() + + playData.applyCustomFilter({ $0.count >= 10 }) + + var expected = 2047 + var actual = playData.filteredWords.count + + XCTAssertEqual(actual, expected, "`filteredWords` did not have expeted count of \(expected)") + + playData.applyCustomFilter { $0.lowercased().contains("swift") } + + expected = 7 + actual = playData.filteredWords.count + XCTAssertEqual(actual, expected, "`filteredWords` did not have expeted count of \(expected)") + } }