|
| 1 | +// |
| 2 | +// IndexedTests.swift |
| 3 | +// CodableDatastore |
| 4 | +// |
| 5 | +// Created by Dimitri Bouniol on 2023-05-31. |
| 6 | +// Copyright © 2023 Mochi Development, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +@testable import CodableDatastore |
| 11 | + |
| 12 | +final class IndexedTests: XCTestCase { |
| 13 | + func testIndexed() throws { |
| 14 | + struct NonCodable {} |
| 15 | + |
| 16 | + struct TestStruct { |
| 17 | + var id: UUID |
| 18 | + |
| 19 | + @Indexed |
| 20 | + var name: String |
| 21 | + |
| 22 | + @Indexed |
| 23 | + var age: Int = 1 |
| 24 | + |
| 25 | + var other: [Int] = [] |
| 26 | + |
| 27 | +// @Indexed |
| 28 | +// var nonCodable = NonCodable() // Not allowed! |
| 29 | + |
| 30 | + // Technically possible, but heavily discouraged: |
| 31 | + var composed: Indexed<String> { Indexed(wrappedValue: "\(name) \(age)") } |
| 32 | + } |
| 33 | + |
| 34 | + let myValue = TestStruct(id: UUID(), name: "Hello!") |
| 35 | + |
| 36 | + XCTAssertEqual("\(myValue[keyPath: \.age])", "1") |
| 37 | + XCTAssertEqual("\(myValue[keyPath: \.$age])", "Indexed<Int>(wrappedValue: 1)") |
| 38 | + XCTAssertEqual("\(myValue[keyPath: \.composed])", "Indexed<String>(wrappedValue: \"Hello! 1\")") |
| 39 | + |
| 40 | + // This did not work unfortunately: |
| 41 | +// withUnsafeTemporaryAllocation(of: TestStruct.self, capacity: 1) { pointer in |
| 42 | +//// print(Mirror(reflecting: pointer).children) |
| 43 | +// let value = pointer.first! |
| 44 | +// |
| 45 | +// let mirror = Mirror(reflecting: value) |
| 46 | +// var indexedProperties: [String] = [] |
| 47 | +// for child in mirror.children { |
| 48 | +// guard let label = child.label else { continue } |
| 49 | +// let childType = type(of: child.value) |
| 50 | +// guard childType is _Indexed.Type else { continue } |
| 51 | +// print("Child: \(label), type: \(childType)") |
| 52 | +// indexedProperties.append(label) |
| 53 | +// } |
| 54 | +// print("Indexable Children from type: \(indexedProperties)") |
| 55 | +// } |
| 56 | + |
| 57 | + |
| 58 | +// let mirror = Mirror(reflecting: TestStruct.self) // Doesn't work :( |
| 59 | + let mirror = Mirror(reflecting: myValue) |
| 60 | + var indexedProperties: [String] = [] |
| 61 | + for child in mirror.children { |
| 62 | + guard let label = child.label else { continue } |
| 63 | + let childType = type(of: child.value) |
| 64 | + guard childType is any _Indexed.Type else { continue } |
| 65 | + indexedProperties.append(label) |
| 66 | + } |
| 67 | + XCTAssertEqual(indexedProperties, ["_name", "_age"]) |
| 68 | + |
| 69 | + struct TestAccessor<T> { |
| 70 | + func load<V>(from keypath: KeyPath<T, Indexed<V>>) -> [T] { |
| 71 | + XCTAssertEqual(keypath, \TestStruct.$age) |
| 72 | + return [] |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + let accessor: TestAccessor<TestStruct> = TestAccessor() |
| 77 | +// let values = accessor.load(from: \.other) // not allowed! |
| 78 | +// let values = accessor.load(from: \.age) // not allowed! |
| 79 | + let values = accessor.load(from: \.$age) |
| 80 | + XCTAssertEqual("\(values)", "[]") |
| 81 | + XCTAssertEqual("\(type(of: values))", "Array<TestStruct>") |
| 82 | + } |
| 83 | +} |
0 commit comments