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
|
|
|
|
|
|
|
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_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Suite(.serialized)
|
|
|
|
final class DefaultsEnumTests {
|
|
|
|
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<FixtureEnum>("independentEnumKey", 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<FixtureEnum?>("independentEnumOptionalKey", 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<[FixtureEnum]>("independentEnumArrayKey", default: [.tenMinutes], suite: suite_)
|
|
|
|
#expect(Defaults[key][0] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key].append(.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 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)
|
2021-05-16 20:21:17 +08:00
|
|
|
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)
|
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 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)
|
2021-05-16 20:21:17 +08:00
|
|
|
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)
|
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: FixtureEnum]]>("independentEnumArrayDictionaryKey", 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: FixtureEnum]>("independentEnumDictionaryKey", default: ["0": .tenMinutes], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
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 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)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = ["0": .tenMinutes]
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?["0"] == .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 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)
|
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"]?[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[.enum] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.enum] = .halfHour
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.enum] == .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 testArrayType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.enumArray][0] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.enumArray][0] = .oneHour
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.enumArray][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 testDictionaryType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.enumDictionary]["0"] == .tenMinutes)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.enumDictionary]["0"] = .halfHour
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.enumDictionary]["0"] == .halfHour)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|