2021-05-16 20:21:17 +08:00
|
|
|
import Foundation
|
2024-09-26 05:17:26 +08:00
|
|
|
import Testing
|
2021-05-16 20:21:17 +08:00
|
|
|
import Defaults
|
2024-09-26 05:17:26 +08:00
|
|
|
|
|
|
|
private let suite_ = createSuite()
|
2021-05-16 20:21:17 +08:00
|
|
|
|
2022-10-22 01:04:15 +08:00
|
|
|
private enum FixtureCodableEnum: String, Hashable, Codable, Defaults.Serializable {
|
2021-05-16 20:21:17 +08:00
|
|
|
case tenMinutes = "10 Minutes"
|
|
|
|
case halfHour = "30 Minutes"
|
|
|
|
case oneHour = "1 Hour"
|
|
|
|
}
|
|
|
|
|
2021-10-12 15:03:34 +08:00
|
|
|
private enum FixtureCodableEnumPreferRawRepresentable: Int, Hashable, Codable, Defaults.Serializable, Defaults.PreferRawRepresentable {
|
|
|
|
case tenMinutes = 10
|
|
|
|
case halfHour = 30
|
|
|
|
case oneHour = 60
|
|
|
|
}
|
|
|
|
|
2021-05-16 20:21:17 +08:00
|
|
|
extension Defaults.Keys {
|
2024-09-26 05:17:26 +08:00
|
|
|
fileprivate static let codableEnum = Key<FixtureCodableEnum>("codable_enum", default: .oneHour, suite: suite_)
|
|
|
|
fileprivate static let codableEnumArray = Key<[FixtureCodableEnum]>("codable_enum", default: [.oneHour], suite: suite_)
|
|
|
|
fileprivate static let codableEnumDictionary = Key<[String: FixtureCodableEnum]>("codable_enum", default: ["0": .oneHour], suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Suite(.serialized)
|
|
|
|
final class DefaultsCodableEnumTests {
|
|
|
|
init() {
|
|
|
|
Defaults.removeAll(suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
deinit {
|
|
|
|
Defaults.removeAll(suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<FixtureCodableEnum>("independentCodableEnumKey", default: .tenMinutes, suite: suite_)
|
|
|
|
#expect(Defaults[key] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = .halfHour
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key] == .halfHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testOptionalKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<FixtureCodableEnum?>("independentCodableEnumOptionalKey", suite: suite_)
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = .tenMinutes
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testArrayKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[FixtureCodableEnum]>("independentCodableEnumArrayKey", default: [.tenMinutes], suite: suite_)
|
|
|
|
#expect(Defaults[key][0] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key][0] = .halfHour
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0] == .halfHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testArrayOptionalKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[FixtureCodableEnum]?>("independentCodableEnumArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = [.halfHour]
|
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testNestedArrayKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[[FixtureCodableEnum]]>("independentCodableEnumNestedArrayKey", default: [[.tenMinutes]], suite: suite_)
|
|
|
|
#expect(Defaults[key][0][0] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key].append([.halfHour])
|
|
|
|
Defaults[key][0].append(.oneHour)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][1][0] == .halfHour)
|
|
|
|
#expect(Defaults[key][0][1] == .oneHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testArrayDictionaryKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[[String: FixtureCodableEnum]]>("independentCodableEnumArrayDictionaryKey", default: [["0": .tenMinutes]], suite: suite_)
|
|
|
|
#expect(Defaults[key][0]["0"] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key][0]["1"] = .halfHour
|
|
|
|
Defaults[key].append(["0": .oneHour])
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0]["1"] == .halfHour)
|
|
|
|
#expect(Defaults[key][1]["0"] == .oneHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testDictionaryKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: FixtureCodableEnum]>("independentCodableEnumDictionaryKey", default: ["0": .tenMinutes], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key]["1"] = .halfHour
|
|
|
|
Defaults[key]["0"] = .oneHour
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["0"] == .oneHour)
|
|
|
|
#expect(Defaults[key]["1"] == .halfHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testDictionaryOptionalKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: FixtureCodableEnum]?>("independentCodableEnumDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = ["0": .tenMinutes]
|
|
|
|
Defaults[key]?["1"] = .halfHour
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?["0"] == .tenMinutes)
|
|
|
|
#expect(Defaults[key]?["1"] == .halfHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testDictionaryArrayKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: [FixtureCodableEnum]]>("independentCodableEnumDictionaryArrayKey", default: ["0": [.tenMinutes]], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"]?[0] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key]["0"]?.append(.halfHour)
|
|
|
|
Defaults[key]["1"] = [.oneHour]
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["0"]?[0] == .tenMinutes)
|
|
|
|
#expect(Defaults[key]["0"]?[1] == .halfHour)
|
|
|
|
#expect(Defaults[key]["1"]?[0] == .oneHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.codableEnum] == .oneHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.codableEnum] = .tenMinutes
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.codableEnum] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testArrayType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.codableEnumArray][0] == .oneHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.codableEnumArray].append(.halfHour)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.codableEnumArray][0] == .oneHour)
|
|
|
|
#expect(Defaults[.codableEnumArray][1] == .halfHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testDictionaryType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.codableEnumDictionary]["0"] == .oneHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.codableEnumDictionary]["1"] = .halfHour
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.codableEnumDictionary]["0"] == .oneHour)
|
|
|
|
#expect(Defaults[.codableEnumDictionary]["1"] == .halfHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-10-12 15:03:34 +08:00
|
|
|
func testFixtureCodableEnumPreferRawRepresentable() {
|
|
|
|
let fixture: FixtureCodableEnumPreferRawRepresentable = .tenMinutes
|
|
|
|
let keyName = "testFixtureCodableEnumPreferRawRepresentable"
|
2024-09-26 05:17:26 +08:00
|
|
|
_ = Defaults.Key<FixtureCodableEnumPreferRawRepresentable>(keyName, default: fixture, suite: suite_)
|
|
|
|
#expect(UserDefaults.standard.integer(forKey: keyName) != 0)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|