|
| 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 | + guard let store = UserDefaults(suiteName: #function) else { |
| 68 | + Issue.record("Failed to create UserDefaults") |
| 69 | + return |
| 70 | + } |
| 71 | + defer { store.removePersistentDomain(forName: #function) } |
| 72 | + |
| 73 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 74 | + let storage = AppStorage( |
| 75 | + wrappedValue: testDate, |
| 76 | + for: TestDateStored.self, |
| 77 | + store: store |
| 78 | + ) |
| 79 | + |
| 80 | + #expect(storage.wrappedValue == testDate) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + internal func testNonOptionalDateUpdate() async throws { |
| 85 | + guard let store = UserDefaults(suiteName: #function) else { |
| 86 | + Issue.record("Failed to create UserDefaults") |
| 87 | + return |
| 88 | + } |
| 89 | + defer { store.removePersistentDomain(forName: #function) } |
| 90 | + |
| 91 | + let initialDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 92 | + var storage = AppStorage( |
| 93 | + wrappedValue: initialDate, |
| 94 | + for: TestDateStored.self, |
| 95 | + store: store |
| 96 | + ) |
| 97 | + |
| 98 | + #expect(storage.wrappedValue == initialDate) |
| 99 | + |
| 100 | + let newDate = Date(timeIntervalSince1970: 1_735_689_600) |
| 101 | + storage.wrappedValue = newDate |
| 102 | + |
| 103 | + #expect(storage.wrappedValue == newDate) |
| 104 | + #expect(store.object(forKey: TestDateStored.key) != nil) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + internal func testNonOptionalDateWithReflectingKey() async throws { |
| 109 | + guard let store = UserDefaults(suiteName: #function) else { |
| 110 | + Issue.record("Failed to create UserDefaults") |
| 111 | + return |
| 112 | + } |
| 113 | + defer { store.removePersistentDomain(forName: #function) } |
| 114 | + |
| 115 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 116 | + let storage = AppStorage( |
| 117 | + wrappedValue: testDate, |
| 118 | + for: TestReflectingDateStored.self, |
| 119 | + store: store |
| 120 | + ) |
| 121 | + |
| 122 | + #expect(storage.wrappedValue == testDate) |
| 123 | + |
| 124 | + let key = TestReflectingDateStored.key |
| 125 | + #expect(key.contains("TestReflectingDateStored")) |
| 126 | + } |
| 127 | + |
| 128 | + // MARK: - Optional Date Tests |
| 129 | + |
| 130 | + @Test |
| 131 | + internal func testOptionalDateStorageWithValue() async throws { |
| 132 | + guard let store = UserDefaults(suiteName: #function) else { |
| 133 | + Issue.record("Failed to create UserDefaults") |
| 134 | + return |
| 135 | + } |
| 136 | + defer { store.removePersistentDomain(forName: #function) } |
| 137 | + |
| 138 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 139 | + var storage = AppStorage( |
| 140 | + for: TestOptionalDateStored.self, |
| 141 | + store: store |
| 142 | + ) |
| 143 | + |
| 144 | + #expect(storage.wrappedValue == nil) |
| 145 | + |
| 146 | + storage.wrappedValue = testDate |
| 147 | + |
| 148 | + #expect(storage.wrappedValue == testDate) |
| 149 | + #expect(store.object(forKey: TestOptionalDateStored.key) != nil) |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + internal func testOptionalDateStorageWithNil() async throws { |
| 154 | + guard let store = UserDefaults(suiteName: #function) else { |
| 155 | + Issue.record("Failed to create UserDefaults") |
| 156 | + return |
| 157 | + } |
| 158 | + defer { store.removePersistentDomain(forName: #function) } |
| 159 | + |
| 160 | + let storage = AppStorage( |
| 161 | + for: TestOptionalDateStored.self, |
| 162 | + store: store |
| 163 | + ) |
| 164 | + |
| 165 | + #expect(storage.wrappedValue == nil) |
| 166 | + |
| 167 | + let retrievedValue = store.object( |
| 168 | + forKey: TestOptionalDateStored.key |
| 169 | + ) |
| 170 | + #expect(retrievedValue == nil) |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + internal func testOptionalDateSetToNil() async throws { |
| 175 | + guard let store = UserDefaults(suiteName: #function) else { |
| 176 | + Issue.record("Failed to create UserDefaults") |
| 177 | + return |
| 178 | + } |
| 179 | + defer { store.removePersistentDomain(forName: #function) } |
| 180 | + |
| 181 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 182 | + var storage = AppStorage( |
| 183 | + for: TestOptionalDateStored.self, |
| 184 | + store: store |
| 185 | + ) |
| 186 | + |
| 187 | + storage.wrappedValue = testDate |
| 188 | + #expect(storage.wrappedValue == testDate) |
| 189 | + |
| 190 | + storage.wrappedValue = nil |
| 191 | + #expect(storage.wrappedValue == nil) |
| 192 | + |
| 193 | + let retrievedValue = store.object( |
| 194 | + forKey: TestOptionalDateStored.key |
| 195 | + ) |
| 196 | + #expect(retrievedValue == nil) |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + internal func testOptionalDateWithReflectingKey() async throws { |
| 201 | + guard let store = UserDefaults(suiteName: #function) else { |
| 202 | + Issue.record("Failed to create UserDefaults") |
| 203 | + return |
| 204 | + } |
| 205 | + defer { store.removePersistentDomain(forName: #function) } |
| 206 | + |
| 207 | + let testDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 208 | + var storage = AppStorage( |
| 209 | + for: TestReflectingOptionalDateStored.self, |
| 210 | + store: store |
| 211 | + ) |
| 212 | + |
| 213 | + storage.wrappedValue = testDate |
| 214 | + #expect(storage.wrappedValue == testDate) |
| 215 | + |
| 216 | + let key = TestReflectingOptionalDateStored.key |
| 217 | + #expect(key.contains("TestReflectingOptionalDateStored")) |
| 218 | + #expect(store.object(forKey: key) != nil) |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | +#endif |
0 commit comments