|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +import XCTest |
| 13 | +import Algorithms |
| 14 | + |
| 15 | +final class KeyedTests: XCTestCase { |
| 16 | + private class SampleError: Error {} |
| 17 | + |
| 18 | + func testUniqueKeys() { |
| 19 | + let d = ["Apple", "Banana", "Cherry"].keyed(by: { $0.first! }) |
| 20 | + XCTAssertEqual(d.count, 3) |
| 21 | + XCTAssertEqual(d["A"]!, "Apple") |
| 22 | + XCTAssertEqual(d["B"]!, "Banana") |
| 23 | + XCTAssertEqual(d["C"]!, "Cherry") |
| 24 | + XCTAssertNil(d["D"]) |
| 25 | + } |
| 26 | + |
| 27 | + func testEmpty() { |
| 28 | + let d = EmptyCollection<String>().keyed(by: { $0.first! }) |
| 29 | + XCTAssertEqual(d.count, 0) |
| 30 | + } |
| 31 | + |
| 32 | + func testNonUniqueKeys() throws { |
| 33 | + let d = ["Apple", "Avocado", "Banana", "Cherry"].keyed(by: { $0.first! }) |
| 34 | + XCTAssertEqual(d.count, 3) |
| 35 | + XCTAssertEqual(d["A"]!, "Avocado", "On a key-collision, keyed(by:) should take the latest value.") |
| 36 | + XCTAssertEqual(d["B"]!, "Banana") |
| 37 | + XCTAssertEqual(d["C"]!, "Cherry") |
| 38 | + } |
| 39 | + |
| 40 | + func testNonUniqueKeysWithMergeFunction() { |
| 41 | + var resolveCallHistory = [(key: Character, current: String, new: String)]() |
| 42 | + let expectedCallHistory = [ |
| 43 | + (key: "A", current: "Apple", new: "Avocado"), |
| 44 | + (key: "C", current: "Cherry", new: "Coconut"), |
| 45 | + ] |
| 46 | + |
| 47 | + let d = ["Apple", "Avocado", "Banana", "Cherry", "Coconut"].keyed( |
| 48 | + by: { $0.first! }, |
| 49 | + resolvingConflictsWith: { key, older, newer in |
| 50 | + resolveCallHistory.append((key, older, newer)) |
| 51 | + return "\(older)-\(newer)" |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | + XCTAssertEqual(d.count, 3) |
| 56 | + XCTAssertEqual(d["A"]!, "Apple-Avocado") |
| 57 | + XCTAssertEqual(d["B"]!, "Banana") |
| 58 | + XCTAssertEqual(d["C"]!, "Cherry-Coconut") |
| 59 | + XCTAssertNil(d["D"]) |
| 60 | + |
| 61 | + XCTAssertEqual( |
| 62 | + resolveCallHistory.map(String.init(describing:)), // quick/dirty workaround: tuples aren't Equatable |
| 63 | + expectedCallHistory.map(String.init(describing:)) |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + func testThrowingFromKeyFunction() { |
| 68 | + let input = ["Apple", "Banana", "Cherry"] |
| 69 | + let error = SampleError() |
| 70 | + |
| 71 | + XCTAssertThrowsError( |
| 72 | + try input.keyed(by: { (_: String) -> Character in throw error }) |
| 73 | + ) { thrownError in |
| 74 | + XCTAssertIdentical(error, thrownError as? SampleError) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + func testThrowingFromCombineFunction() { |
| 79 | + let input = ["Apple", "Avocado", "Banana", "Cherry"] |
| 80 | + let error = SampleError() |
| 81 | + |
| 82 | + XCTAssertThrowsError( |
| 83 | + try input.keyed(by: { $0.first! }, resolvingConflictsWith: { _, _, _ in throw error }) |
| 84 | + ) { thrownError in |
| 85 | + XCTAssertIdentical(error, thrownError as? SampleError) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments