|
| 1 | +// |
| 2 | +// AppStorageDateTests.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 | +#if canImport(SwiftUI) |
| 31 | + |
| 32 | + import Foundation |
| 33 | + import RadiantKit |
| 34 | + import SwiftUI |
| 35 | + import Testing |
| 36 | + |
| 37 | + // MARK: - Test Types |
| 38 | + |
| 39 | + internal enum TestDateStored: AppStored { |
| 40 | + internal typealias Value = Date |
| 41 | + internal static let keyType: KeyType = .describing |
| 42 | + } |
| 43 | + |
| 44 | + internal enum TestOptionalDateStored: AppStored { |
| 45 | + internal typealias Value = Date? |
| 46 | + internal static let keyType: KeyType = .describing |
| 47 | + } |
| 48 | + |
| 49 | + internal enum TestReflectingDateStored: AppStored { |
| 50 | + internal typealias Value = Date |
| 51 | + internal static let keyType: KeyType = .reflecting |
| 52 | + } |
| 53 | + |
| 54 | + internal enum TestReflectingOptionalDateStored: AppStored { |
| 55 | + internal typealias Value = Date? |
| 56 | + internal static let keyType: KeyType = .reflecting |
| 57 | + } |
| 58 | + |
| 59 | + // MARK: - Tests |
| 60 | + |
| 61 | + @MainActor |
| 62 | + internal struct AppStorageDateTests { |
| 63 | + // MARK: - Non-Optional Date Tests |
| 64 | + |
| 65 | + @Test |
| 66 | + internal func testNonOptionalDateStorage() async throws { |
| 67 | + #if canImport(SwiftUI) |
| 68 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 69 | + guard let store = UserDefaults(suiteName: #function) else { |
| 70 | + Issue.record("Failed to create UserDefaults") |
| 71 | + return |
| 72 | + } |
| 73 | + defer { store.removePersistentDomain(forName: #function) } |
| 74 | + |
| 75 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 76 | + let storage = AppStorage( |
| 77 | + wrappedValue: testDate, |
| 78 | + for: TestDateStored.self, |
| 79 | + store: store |
| 80 | + ) |
| 81 | + |
| 82 | + #expect(storage.wrappedValue == testDate) |
| 83 | + } else { |
| 84 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 85 | + } |
| 86 | + #else |
| 87 | + Issue.record("SwiftUI is not available on this platform") |
| 88 | + #endif |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + internal func testNonOptionalDateUpdate() async throws { |
| 93 | + #if canImport(SwiftUI) |
| 94 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 95 | + guard let store = UserDefaults(suiteName: #function) else { |
| 96 | + Issue.record("Failed to create UserDefaults") |
| 97 | + return |
| 98 | + } |
| 99 | + defer { store.removePersistentDomain(forName: #function) } |
| 100 | + |
| 101 | + let initialDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 102 | + let storage = AppStorage( |
| 103 | + wrappedValue: initialDate, |
| 104 | + for: TestDateStored.self, |
| 105 | + store: store |
| 106 | + ) |
| 107 | + |
| 108 | + #expect(storage.wrappedValue == initialDate) |
| 109 | + |
| 110 | + let newDate = Date(timeIntervalSince1970: 1_735_689_600) |
| 111 | + storage.wrappedValue = newDate |
| 112 | + |
| 113 | + #expect(storage.wrappedValue == newDate) |
| 114 | + #expect(store.object(forKey: TestDateStored.key) != nil) |
| 115 | + } else { |
| 116 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 117 | + } |
| 118 | + #else |
| 119 | + Issue.record("SwiftUI is not available on this platform") |
| 120 | + #endif |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + internal func testNonOptionalDateWithReflectingKey() async throws { |
| 125 | + #if canImport(SwiftUI) |
| 126 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 127 | + guard let store = UserDefaults(suiteName: #function) else { |
| 128 | + Issue.record("Failed to create UserDefaults") |
| 129 | + return |
| 130 | + } |
| 131 | + defer { store.removePersistentDomain(forName: #function) } |
| 132 | + |
| 133 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 134 | + let storage = AppStorage( |
| 135 | + wrappedValue: testDate, |
| 136 | + for: TestReflectingDateStored.self, |
| 137 | + store: store |
| 138 | + ) |
| 139 | + |
| 140 | + #expect(storage.wrappedValue == testDate) |
| 141 | + |
| 142 | + let key = TestReflectingDateStored.key |
| 143 | + #expect(key.contains("TestReflectingDateStored")) |
| 144 | + } else { |
| 145 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 146 | + } |
| 147 | + #else |
| 148 | + Issue.record("SwiftUI is not available on this platform") |
| 149 | + #endif |
| 150 | + } |
| 151 | + |
| 152 | + // MARK: - Optional Date Tests |
| 153 | + |
| 154 | + @Test |
| 155 | + internal func testOptionalDateStorageWithValue() async throws { |
| 156 | + #if canImport(SwiftUI) |
| 157 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 158 | + guard let store = UserDefaults(suiteName: #function) else { |
| 159 | + Issue.record("Failed to create UserDefaults") |
| 160 | + return |
| 161 | + } |
| 162 | + defer { store.removePersistentDomain(forName: #function) } |
| 163 | + |
| 164 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 165 | + let storage = AppStorage( |
| 166 | + for: TestOptionalDateStored.self, |
| 167 | + store: store |
| 168 | + ) |
| 169 | + |
| 170 | + #expect(storage.wrappedValue == nil) |
| 171 | + |
| 172 | + storage.wrappedValue = testDate |
| 173 | + |
| 174 | + #expect(storage.wrappedValue == testDate) |
| 175 | + #expect(store.object(forKey: TestOptionalDateStored.key) != nil) |
| 176 | + } else { |
| 177 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 178 | + } |
| 179 | + #else |
| 180 | + Issue.record("SwiftUI is not available on this platform") |
| 181 | + #endif |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + internal func testOptionalDateStorageWithNil() async throws { |
| 186 | + #if canImport(SwiftUI) |
| 187 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 188 | + guard let store = UserDefaults(suiteName: #function) else { |
| 189 | + Issue.record("Failed to create UserDefaults") |
| 190 | + return |
| 191 | + } |
| 192 | + defer { store.removePersistentDomain(forName: #function) } |
| 193 | + |
| 194 | + let storage = AppStorage( |
| 195 | + for: TestOptionalDateStored.self, |
| 196 | + store: store |
| 197 | + ) |
| 198 | + |
| 199 | + #expect(storage.wrappedValue == nil) |
| 200 | + |
| 201 | + let retrievedValue = store.object( |
| 202 | + forKey: TestOptionalDateStored.key |
| 203 | + ) |
| 204 | + #expect(retrievedValue == nil) |
| 205 | + } else { |
| 206 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 207 | + } |
| 208 | + #else |
| 209 | + Issue.record("SwiftUI is not available on this platform") |
| 210 | + #endif |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + internal func testOptionalDateSetToNil() async throws { |
| 215 | + #if canImport(SwiftUI) |
| 216 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 217 | + guard let store = UserDefaults(suiteName: #function) else { |
| 218 | + Issue.record("Failed to create UserDefaults") |
| 219 | + return |
| 220 | + } |
| 221 | + defer { store.removePersistentDomain(forName: #function) } |
| 222 | + |
| 223 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 224 | + let storage = AppStorage( |
| 225 | + for: TestOptionalDateStored.self, |
| 226 | + store: store |
| 227 | + ) |
| 228 | + |
| 229 | + storage.wrappedValue = testDate |
| 230 | + #expect(storage.wrappedValue == testDate) |
| 231 | + |
| 232 | + storage.wrappedValue = nil |
| 233 | + #expect(storage.wrappedValue == nil) |
| 234 | + |
| 235 | + let retrievedValue = store.object( |
| 236 | + forKey: TestOptionalDateStored.key |
| 237 | + ) |
| 238 | + #expect(retrievedValue == nil) |
| 239 | + } else { |
| 240 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 241 | + } |
| 242 | + #else |
| 243 | + Issue.record("SwiftUI is not available on this platform") |
| 244 | + #endif |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + internal func testOptionalDateWithReflectingKey() async throws { |
| 249 | + #if canImport(SwiftUI) |
| 250 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { |
| 251 | + guard let store = UserDefaults(suiteName: #function) else { |
| 252 | + Issue.record("Failed to create UserDefaults") |
| 253 | + return |
| 254 | + } |
| 255 | + defer { store.removePersistentDomain(forName: #function) } |
| 256 | + |
| 257 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 258 | + let storage = AppStorage( |
| 259 | + for: TestReflectingOptionalDateStored.self, |
| 260 | + store: store |
| 261 | + ) |
| 262 | + |
| 263 | + storage.wrappedValue = testDate |
| 264 | + #expect(storage.wrappedValue == testDate) |
| 265 | + |
| 266 | + let key = TestReflectingOptionalDateStored.key |
| 267 | + #expect(key.contains("TestReflectingOptionalDateStored")) |
| 268 | + #expect(store.object(forKey: key) != nil) |
| 269 | + } else { |
| 270 | + Issue.record("AppStorage Date support requires macOS 15.0+") |
| 271 | + } |
| 272 | + #else |
| 273 | + Issue.record("SwiftUI is not available on this platform") |
| 274 | + #endif |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | +#endif |
0 commit comments