// The order a pick-one list offers records in. // // Every name here is invented. The ranking is a pure function and this file // is its exhaustive statement: each of the three ranks, the fold that makes // case and accents not matter, the ties, and the suggestion order before // anyone types. import XCTest @testable import CottonUI final class PickerRankingTests: XCTestCase { private struct Row: Equatable { var id: String var name: String } private func names(_ rows: [Row]) -> [String] { rows.map(\.name) } private func search(_ rows: [Row], _ query: String) -> [String] { names(PickerRanking.search(rows, matching: query, name: \.name)) } // MARK: One name against one query /// RULE: A picker search ranks a name that starts with the typed text first, then a name in which a word starts with it, then any other name containing it. /// /// Before this the search was an unranked substring filter in the /// snapshot's order, so a name holding the letters mid-word could sit /// above one that begins with them — the person being looked for was /// somewhere down a list of accidental matches. func testTheThreeRanks() { XCTAssertEqual(PickerRanking.matchRank("Yamuna Test", query: "yam"), 0, "the name starts with it") XCTAssertEqual(PickerRanking.matchRank("Test Yamuna", query: "yam"), 1, "a word starts with it") XCTAssertEqual(PickerRanking.matchRank("Priyam Test", query: "yam"), 2, "only inside a word") XCTAssertNil(PickerRanking.matchRank("Test Zulu", query: "yam"), "not in the name at all") } func testAWordStartsAfterAnythingThatIsNotALetterOrADigit() { XCTAssertEqual(PickerRanking.matchRank("Test-Zulu", query: "zu"), 1) XCTAssertEqual(PickerRanking.matchRank("Test O'Zulu", query: "zu"), 1) XCTAssertEqual(PickerRanking.matchRank("Test (Zulu)", query: "zu"), 1) XCTAssertEqual(PickerRanking.matchRank("Test9Zulu", query: "zu"), 2, "a digit is part of the word") } /// The best occurrence counts, not the first: "Banna Annex" holds "ann" /// mid-word first and at a word start second, and "Anna Test Annex" /// starts with it however many times it recurs. func testTheBestOccurrenceIsTheOneThatCounts() { XCTAssertEqual(PickerRanking.matchRank("Banna Annex", query: "ann"), 1) XCTAssertEqual(PickerRanking.matchRank("Anna Test Annex", query: "ann"), 0) // Overlapping occurrences are looked at too: "a a" in "Aa a a" is // mid-word at the second letter and at a word start two letters on, // inside the first match. XCTAssertEqual(PickerRanking.matchRank("Aa a a", query: "a a"), 1) } func testCaseAndAccentsDoNotMatterOnEitherSide() { XCTAssertEqual(PickerRanking.matchRank("ÉMILE Test", query: "emi"), 0) XCTAssertEqual(PickerRanking.matchRank("Test Émile", query: "ÉMI"), 1) XCTAssertEqual(PickerRanking.matchRank("Test Zoë", query: "zoe"), 1) XCTAssertEqual(PickerRanking.matchRank("Test Şule", query: "sul"), 1) } func testTheQueryIsTrimmedAndABlankOneMatchesNothing() { XCTAssertEqual(PickerRanking.matchRank("Test Zulu", query: " zu "), 1) XCTAssertNil(PickerRanking.matchRank("Test Zulu", query: "")) XCTAssertNil(PickerRanking.matchRank("Test Zulu", query: " ")) } func testAQueryOfTwoWordsIsMatchedAsItStands() { XCTAssertEqual(PickerRanking.matchRank("Test Zulu Alpha", query: "zulu al"), 1) XCTAssertNil(PickerRanking.matchRank("Test Zulu Alpha", query: "alpha zulu")) } // MARK: A list against one query func testTheListReadsRankByRankAndAlphabeticallyWithinARank() { let rows = [ Row(id: "1", name: "Priyam Test"), Row(id: "2", name: "Test Yamuna"), Row(id: "3", name: "Yamuna Test"), Row(id: "4", name: "Ayam Test"), Row(id: "5", name: "Alpha Yam"), Row(id: "6", name: "Yam Bravo"), Row(id: "7", name: "Test Zulu"), ] XCTAssertEqual( search(rows, "yam"), [ "Yam Bravo", "Yamuna Test", // start with it "Alpha Yam", "Test Yamuna", // a word starts with it "Ayam Test", "Priyam Test", // inside a word ] ) } /// Alphabetical as the fold reads it, so an accent or a capital does not /// move a name to the end of its rank. func testTheAlphabeticalOrderIgnoresCaseAndAccentsToo() { let rows = [ Row(id: "1", name: "test zulu"), Row(id: "2", name: "Test Émile"), Row(id: "3", name: "TEST ALPHA"), ] XCTAssertEqual(search(rows, "test"), ["TEST ALPHA", "Test Émile", "test zulu"]) } /// Two rows with the same name keep the order they arrived in — the /// snapshot's — so a picker does not shuffle namesakes between keystrokes. func testTwoRowsWithOneNameKeepTheirOrder() { let rows = [ Row(id: "second", name: "Test Alpha"), Row(id: "first", name: "Test Alpha"), Row(id: "other", name: "Test Aleph"), ] XCTAssertEqual( PickerRanking.search(rows, matching: "test", name: \.name).map(\.id), ["other", "second", "first"] ) } func testNothingMatchingIsNothing() { XCTAssertEqual(search([Row(id: "1", name: "Test Alpha")], "zulu"), []) XCTAssertEqual(search([], "zulu"), []) } // MARK: Before anyone types private func suggest(_ rows: [Row], chosen: [String] = [], limit: Int = 12) -> [String] { PickerRanking.suggestions(rows, chosen: chosen, limit: limit, id: \.id).map(\.id) } /// RULE: Before anyone types, a picker suggests the ids already chosen, in their own order, then the rest in the app's order, each once. func testChosenThenTheAppsOrder() { let rows = [ Row(id: "head-1", name: "Test Alpha"), Row(id: "head-2", name: "Test Bravo"), Row(id: "linked", name: "Test Charlie"), ] XCTAssertEqual(suggest(rows, chosen: ["linked"]), ["linked", "head-1", "head-2"]) } func testTheLimitCutsAcrossTheGroups() { let rows = [ Row(id: "head", name: "Test Alpha"), Row(id: "next", name: "Test Bravo"), Row(id: "chosen", name: "Test Charlie"), ] XCTAssertEqual(suggest(rows, chosen: ["chosen"], limit: 2), ["chosen", "head"]) XCTAssertEqual(suggest(rows, chosen: ["chosen"], limit: 1), ["chosen"]) XCTAssertEqual(suggest(rows, chosen: ["chosen"], limit: 0), []) } func testAChosenIdTheListNoLongerCarriesIsSkipped() { let rows = [Row(id: "here", name: "Test Alpha")] XCTAssertEqual(suggest(rows, chosen: ["gone", "here"]), ["here"]) } /// Nothing chosen still offers somebody: the app's order. func testWithNothingChosenTheAppsOrderIsOffered() { let rows = (1...20).map { Row(id: "p\($0)", name: "Test Person \($0)") } XCTAssertEqual(suggest(rows), (1...12).map { "p\($0)" }) } }