Defaults/Tests/DefaultsTests/DefaultsSetTests.swift

61 lines
1.6 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 let fixtureSet = Set(1...5)
extension Defaults.Keys {
2024-09-26 05:17:26 +08:00
fileprivate static let set = Key<Set<Int>>("setInt", default: fixtureSet, suite: suite_)
}
2024-09-26 05:17:26 +08:00
@Suite(.serialized)
final class DefaultsSetTests {
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<Set<Int>>("independentSetKey", default: fixtureSet, suite: suite_)
#expect(Defaults[key].count == fixtureSet.count)
Defaults[key].insert(6)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key] == Set(1...6))
}
2024-09-26 05:17:26 +08:00
@Test
func testOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<Set<Int>?>("independentSetOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = fixtureSet
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?.count == fixtureSet.count)
Defaults[key]?.insert(6)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key] == Set(1...6))
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[Set<Int>]>("independentSetArrayKey", default: [fixtureSet], suite: suite_)
#expect(Defaults[key][0].count == fixtureSet.count)
Defaults[key][0].insert(6)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0] == Set(1...6))
Defaults[key].append(Set(1...4))
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][1] == Set(1...4))
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: Set<Int>]>("independentSetArrayKey", default: ["0": fixtureSet], suite: suite_)
#expect(Defaults[key]["0"]?.count == fixtureSet.count)
Defaults[key]["0"]?.insert(6)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["0"] == Set(1...6))
Defaults[key]["1"] = Set(1...4)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["1"] == Set(1...4))
}
}