|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Async Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 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 AsyncAlgorithms |
| 14 | + |
| 15 | +final class TestDictionary: XCTestCase { |
| 16 | + func test_uniqueKeysAndValues() async { |
| 17 | + let source = [(1, "a"), (2, "b"), (3, "c")] |
| 18 | + let expected = Dictionary(uniqueKeysWithValues: source) |
| 19 | + let actual = await Dictionary(uniqueKeysWithValues: source.async) |
| 20 | + XCTAssertEqual(expected, actual) |
| 21 | + } |
| 22 | + |
| 23 | + func test_throwing_uniqueKeysAndValues() async { |
| 24 | + let source = Array([1, 2, 3, 4, 5, 6]) |
| 25 | + let input = source.async.map { (value: Int) async throws -> (Int, Int) in |
| 26 | + if value == 4 { throw NSError(domain: NSCocoaErrorDomain, code: -1, userInfo: nil) } |
| 27 | + return (value, value) |
| 28 | + } |
| 29 | + do { |
| 30 | + _ = try await Dictionary(uniqueKeysWithValues: input) |
| 31 | + XCTFail() |
| 32 | + } catch { |
| 33 | + XCTAssertEqual((error as NSError).code, -1) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + func test_uniquingWith() async { |
| 38 | + let source = [("a", 1), ("b", 2), ("a", 3), ("b", 4)] |
| 39 | + let expected = Dictionary(source) { first, _ in first } |
| 40 | + let actual = await Dictionary(source.async) { first, _ in first } |
| 41 | + XCTAssertEqual(expected, actual) |
| 42 | + } |
| 43 | + |
| 44 | + func test_throwing_uniquingWith() async { |
| 45 | + let source = Array([1, 2, 3, 4, 5, 6]) |
| 46 | + let input = source.async.map { (value: Int) async throws -> (Int, Int) in |
| 47 | + if value == 4 { throw NSError(domain: NSCocoaErrorDomain, code: -1, userInfo: nil) } |
| 48 | + return (value, value) |
| 49 | + } |
| 50 | + do { |
| 51 | + _ = try await Dictionary(input) { first, _ in first } |
| 52 | + XCTFail() |
| 53 | + } catch { |
| 54 | + XCTAssertEqual((error as NSError).code, -1) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + func test_grouping() async { |
| 59 | + let source = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"] |
| 60 | + let expected = Dictionary(grouping: source, by: { $0.first! }) |
| 61 | + let actual = await Dictionary(grouping: source.async, by: { $0.first! }) |
| 62 | + XCTAssertEqual(expected, actual) |
| 63 | + } |
| 64 | + |
| 65 | + func test_throwing_grouping() async { |
| 66 | + let source = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"] |
| 67 | + let input = source.async.map { (value: String) async throws -> String in |
| 68 | + if value == "Kweku" { throw NSError(domain: NSCocoaErrorDomain, code: -1, userInfo: nil) } |
| 69 | + return value |
| 70 | + } |
| 71 | + do { |
| 72 | + _ = try await Dictionary(grouping: input, by: { $0.first! }) |
| 73 | + XCTFail() |
| 74 | + } catch { |
| 75 | + XCTAssertEqual((error as NSError).code, -1) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments