|
| 1 | +import XCTest |
| 2 | + |
| 3 | +@testable import ComposableArchitecture |
| 4 | + |
| 5 | +final class IdentifiedArrayTests: XCTestCase { |
| 6 | + func testIdSubscript() { |
| 7 | + struct User: Equatable, Identifiable { |
| 8 | + let id: Int |
| 9 | + var name: String |
| 10 | + } |
| 11 | + |
| 12 | + var array: IdentifiedArray = [User(id: 1, name: "Blob")] |
| 13 | + |
| 14 | + XCTAssertEqual(array[id: 1], .some(User(id: 1, name: "Blob"))) |
| 15 | + |
| 16 | + array[id: 1] = nil |
| 17 | + XCTAssertEqual(array, []) |
| 18 | + } |
| 19 | + |
| 20 | + func testInsert() { |
| 21 | + struct User: Equatable, Identifiable { |
| 22 | + let id: Int |
| 23 | + var name: String |
| 24 | + } |
| 25 | + |
| 26 | + var array: IdentifiedArray = [User(id: 1, name: "Blob")] |
| 27 | + |
| 28 | + array.insert(User(id: 2, name: "Blob Jr."), at: 0) |
| 29 | + XCTAssertEqual(array, [User(id: 2, name: "Blob Jr."), User(id: 1, name: "Blob")]) |
| 30 | + } |
| 31 | + |
| 32 | + func testInsertContentsOf() { |
| 33 | + struct User: Equatable, Identifiable { |
| 34 | + let id: Int |
| 35 | + var name: String |
| 36 | + } |
| 37 | + |
| 38 | + var array: IdentifiedArray = [User(id: 1, name: "Blob")] |
| 39 | + |
| 40 | + array.insert(contentsOf: [User(id: 3, name: "Blob Sr."), User(id: 2, name: "Blob Jr.")], at: 0) |
| 41 | + XCTAssertEqual( |
| 42 | + array, |
| 43 | + [User(id: 3, name: "Blob Sr."), User(id: 2, name: "Blob Jr."), User(id: 1, name: "Blob")] |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + func testRemoveAt() { |
| 48 | + struct User: Equatable, Identifiable { |
| 49 | + let id: Int |
| 50 | + var name: String |
| 51 | + } |
| 52 | + |
| 53 | + var array: IdentifiedArray = [ |
| 54 | + User(id: 3, name: "Blob Sr."), |
| 55 | + User(id: 2, name: "Blob Jr."), |
| 56 | + User(id: 1, name: "Blob"), |
| 57 | + ] |
| 58 | + |
| 59 | + array.remove(at: 1) |
| 60 | + XCTAssertEqual(array, [User(id: 3, name: "Blob Sr."), User(id: 1, name: "Blob")]) |
| 61 | + } |
| 62 | + |
| 63 | + func testRemoveAllWhere() { |
| 64 | + struct User: Equatable, Identifiable { |
| 65 | + let id: Int |
| 66 | + var name: String |
| 67 | + } |
| 68 | + |
| 69 | + var array: IdentifiedArray = [ |
| 70 | + User(id: 3, name: "Blob Sr."), |
| 71 | + User(id: 2, name: "Blob Jr."), |
| 72 | + User(id: 1, name: "Blob"), |
| 73 | + ] |
| 74 | + |
| 75 | + array.removeAll(where: { $0.name.starts(with: "Blob ") }) |
| 76 | + XCTAssertEqual(array, [User(id: 1, name: "Blob")]) |
| 77 | + } |
| 78 | + |
| 79 | + func testRemoveAtOffsets() { |
| 80 | + struct User: Equatable, Identifiable { |
| 81 | + let id: Int |
| 82 | + var name: String |
| 83 | + } |
| 84 | + |
| 85 | + var array: IdentifiedArray = [ |
| 86 | + User(id: 3, name: "Blob Sr."), |
| 87 | + User(id: 2, name: "Blob Jr."), |
| 88 | + User(id: 1, name: "Blob"), |
| 89 | + ] |
| 90 | + |
| 91 | + array.remove(atOffsets: [0, 2]) |
| 92 | + XCTAssertEqual(array, [User(id: 2, name: "Blob Jr.")]) |
| 93 | + } |
| 94 | + |
| 95 | + func testMoveFromOffsets() { |
| 96 | + struct User: Equatable, Identifiable { |
| 97 | + let id: Int |
| 98 | + var name: String |
| 99 | + } |
| 100 | + |
| 101 | + var array: IdentifiedArray = [ |
| 102 | + User(id: 3, name: "Blob Sr."), |
| 103 | + User(id: 2, name: "Blob Jr."), |
| 104 | + User(id: 1, name: "Blob"), |
| 105 | + ] |
| 106 | + |
| 107 | + array.move(fromOffsets: [0], toOffset: 2) |
| 108 | + XCTAssertEqual( |
| 109 | + array, |
| 110 | + [User(id: 2, name: "Blob Jr."), User(id: 3, name: "Blob Sr."), User(id: 1, name: "Blob")] |
| 111 | + ) |
| 112 | + } |
| 113 | +} |
0 commit comments