Defaults/Tests/DefaultsTests/DefaultsEnumTests.swift

132 lines
4.1 KiB
Swift
Raw Normal View History

import Foundation
2024-09-26 05:17:26 +08:00
import Testing
import Defaults
2024-09-26 05:17:26 +08:00
private let suite_ = createSuite()
private enum FixtureEnum: String, Defaults.Serializable {
case tenMinutes = "10 Minutes"
case halfHour = "30 Minutes"
case oneHour = "1 Hour"
}
extension Defaults.Keys {
2024-09-26 05:17:26 +08:00
fileprivate static let `enum` = Key<FixtureEnum>("enum", default: .tenMinutes, suite: suite_)
fileprivate static let enumArray = Key<[FixtureEnum]>("array_enum", default: [.tenMinutes], suite: suite_)
fileprivate static let enumDictionary = Key<[String: FixtureEnum]>("dictionary_enum", default: ["0": .tenMinutes], suite: suite_)
}
2024-09-26 05:17:26 +08:00
@Suite(.serialized)
final class DefaultsEnumTests {
init() {
Defaults.removeAll(suite: suite_)
}
2024-09-26 05:17:26 +08:00
deinit {
Defaults.removeAll(suite: suite_)
}
2024-09-26 05:17:26 +08:00
@Test
func testKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<FixtureEnum>("independentEnumKey", default: .tenMinutes, suite: suite_)
#expect(Defaults[key] == .tenMinutes)
Defaults[key] = .halfHour
2024-09-26 05:17:26 +08:00
#expect(Defaults[key] == .halfHour)
}
2024-09-26 05:17:26 +08:00
@Test
func testOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<FixtureEnum?>("independentEnumOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = .tenMinutes
2024-09-26 05:17:26 +08:00
#expect(Defaults[key] == .tenMinutes)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[FixtureEnum]>("independentEnumArrayKey", default: [.tenMinutes], suite: suite_)
#expect(Defaults[key][0] == .tenMinutes)
Defaults[key].append(.halfHour)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0] == .tenMinutes)
#expect(Defaults[key][1] == .halfHour)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[FixtureEnum]?>("independentEnumArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = [.tenMinutes]
Defaults[key]?.append(.halfHour)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?[0] == .tenMinutes)
#expect(Defaults[key]?[1] == .halfHour)
}
2024-09-26 05:17:26 +08:00
@Test
func testNestedArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[[FixtureEnum]]>("independentEnumNestedArrayKey", default: [[.tenMinutes]], suite: suite_)
#expect(Defaults[key][0][0] == .tenMinutes)
Defaults[key][0].append(.halfHour)
Defaults[key].append([.oneHour])
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0][1] == .halfHour)
#expect(Defaults[key][1][0] == .oneHour)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayDictionaryKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[[String: FixtureEnum]]>("independentEnumArrayDictionaryKey", default: [["0": .tenMinutes]], suite: suite_)
#expect(Defaults[key][0]["0"] == .tenMinutes)
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)
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: FixtureEnum]>("independentEnumDictionaryKey", default: ["0": .tenMinutes], suite: suite_)
#expect(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)
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: FixtureEnum]?>("independentEnumDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = ["0": .tenMinutes]
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?["0"] == .tenMinutes)
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: [FixtureEnum]]>("independentEnumDictionaryKey", default: ["0": [.tenMinutes]], suite: suite_)
#expect(Defaults[key]["0"]?[0] == .tenMinutes)
Defaults[key]["0"]?.append(.halfHour)
Defaults[key]["1"] = [.oneHour]
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["0"]?[1] == .halfHour)
#expect(Defaults[key]["1"]?[0] == .oneHour)
}
2024-09-26 05:17:26 +08:00
@Test
func testType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.enum] == .tenMinutes)
Defaults[.enum] = .halfHour
2024-09-26 05:17:26 +08:00
#expect(Defaults[.enum] == .halfHour)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.enumArray][0] == .tenMinutes)
Defaults[.enumArray][0] = .oneHour
2024-09-26 05:17:26 +08:00
#expect(Defaults[.enumArray][0] == .oneHour)
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.enumDictionary]["0"] == .tenMinutes)
Defaults[.enumDictionary]["0"] = .halfHour
2024-09-26 05:17:26 +08:00
#expect(Defaults[.enumDictionary]["0"] == .halfHour)
}
}