|
| 1 | +// |
| 2 | +// AppStorageDateTests+NonOptional.swift |
| 3 | +// RadiantKit |
| 4 | +// |
| 5 | +// Created by Leo Dion. |
| 6 | +// Copyright © 2025 BrightDigit. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the “Software”), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or |
| 13 | +// sell copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +// |
| 29 | + |
| 30 | +import Foundation |
| 31 | +import RadiantKit |
| 32 | +import Testing |
| 33 | + |
| 34 | +#if canImport(SwiftUI) |
| 35 | + import SwiftUI |
| 36 | +#endif |
| 37 | + |
| 38 | +extension AppStorageDateTests { |
| 39 | + @Suite("Non-Optional Date Storage Tests") |
| 40 | + @MainActor |
| 41 | + internal struct NonOptionalDateTests { |
| 42 | + @Test(.enabled(if: AppStorageDateTests.isAppStorageDateAvailable)) |
| 43 | + internal func testNonOptionalDateStorage() async throws { |
| 44 | + #if canImport(SwiftUI) |
| 45 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 46 | + guard let store = UserDefaults(suiteName: #function) else { |
| 47 | + Issue.record("Failed to create UserDefaults") |
| 48 | + return |
| 49 | + } |
| 50 | + defer { store.removePersistentDomain(forName: #function) } |
| 51 | + |
| 52 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 53 | + let storage = AppStorage( |
| 54 | + wrappedValue: testDate, |
| 55 | + for: TestDateStored.self, |
| 56 | + store: store |
| 57 | + ) |
| 58 | + |
| 59 | + #expect(storage.wrappedValue == testDate) |
| 60 | + } else { |
| 61 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 62 | + } |
| 63 | + #else |
| 64 | + Issue.record("SwiftUI is not available on this platform") |
| 65 | + #endif |
| 66 | + } |
| 67 | + |
| 68 | + @Test(.enabled(if: AppStorageDateTests.isAppStorageDateAvailable)) |
| 69 | + internal func testNonOptionalDateUpdate() async throws { |
| 70 | + #if canImport(SwiftUI) |
| 71 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 72 | + guard let store = UserDefaults(suiteName: #function) else { |
| 73 | + Issue.record("Failed to create UserDefaults") |
| 74 | + return |
| 75 | + } |
| 76 | + defer { store.removePersistentDomain(forName: #function) } |
| 77 | + |
| 78 | + let initialDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 79 | + let storage = AppStorage( |
| 80 | + wrappedValue: initialDate, |
| 81 | + for: TestDateStored.self, |
| 82 | + store: store |
| 83 | + ) |
| 84 | + |
| 85 | + #expect(storage.wrappedValue == initialDate) |
| 86 | + |
| 87 | + let newDate = Date(timeIntervalSince1970: 1_735_689_600) |
| 88 | + storage.wrappedValue = newDate |
| 89 | + |
| 90 | + #expect(storage.wrappedValue == newDate) |
| 91 | + #expect(store.object(forKey: TestDateStored.key) != nil) |
| 92 | + } else { |
| 93 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 94 | + } |
| 95 | + #else |
| 96 | + Issue.record("SwiftUI is not available on this platform") |
| 97 | + #endif |
| 98 | + } |
| 99 | + |
| 100 | + @Test(.enabled(if: AppStorageDateTests.isAppStorageDateAvailable)) |
| 101 | + internal func testNonOptionalDateWithReflectingKey() async throws { |
| 102 | + #if canImport(SwiftUI) |
| 103 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 104 | + guard let store = UserDefaults(suiteName: #function) else { |
| 105 | + Issue.record("Failed to create UserDefaults") |
| 106 | + return |
| 107 | + } |
| 108 | + defer { store.removePersistentDomain(forName: #function) } |
| 109 | + |
| 110 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 111 | + let storage = AppStorage( |
| 112 | + wrappedValue: testDate, |
| 113 | + for: TestReflectingDateStored.self, |
| 114 | + store: store |
| 115 | + ) |
| 116 | + |
| 117 | + #expect(storage.wrappedValue == testDate) |
| 118 | + |
| 119 | + let key = TestReflectingDateStored.key |
| 120 | + #expect(key.contains("TestReflectingDateStored")) |
| 121 | + } else { |
| 122 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 123 | + } |
| 124 | + #else |
| 125 | + Issue.record("SwiftUI is not available on this platform") |
| 126 | + #endif |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments