Defaults/Tests/DefaultsTests/DefaultsRangeTests.swift

229 lines
11 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 struct CustomDate {
let year: Int
let month: Int
let day: Int
}
extension CustomDate: Defaults.Serializable {
public struct CustomDateBridge: Defaults.Bridge {
public typealias Value = CustomDate
public typealias Serializable = [Int]
public func serialize(_ value: Value?) -> Serializable? {
2022-09-05 15:05:41 +08:00
guard let value else {
return nil
}
return [value.year, value.month, value.day]
}
public func deserialize(_ object: Serializable?) -> Value? {
2022-09-05 15:05:41 +08:00
guard let object else {
return nil
}
return .init(year: object[0], month: object[1], day: object[2])
}
}
public static let bridge = CustomDateBridge()
}
extension CustomDate: Comparable {
static func < (lhs: CustomDate, rhs: CustomDate) -> Bool {
if lhs.year != rhs.year {
2024-09-26 05:17:26 +08:00
return lhs.year < rhs.year
2023-09-03 14:39:57 +08:00
}
if lhs.month != rhs.month {
2024-09-26 05:17:26 +08:00
return lhs.month < rhs.month
}
2023-09-03 14:39:57 +08:00
return lhs.day < rhs.day
}
static func == (lhs: CustomDate, rhs: CustomDate) -> Bool {
lhs.year == rhs.year && lhs.month == rhs.month
2024-09-26 05:17:26 +08:00
&& lhs.day == rhs.day
}
}
// Fixtures:
private let fixtureRange = 0..<10
private let nextFixtureRange = 1..<20
private let fixtureDateRange = CustomDate(year: 2022, month: 4, day: 0)..<CustomDate(year: 2022, month: 5, day: 0)
private let nextFixtureDateRange = CustomDate(year: 2022, month: 6, day: 1)..<CustomDate(year: 2022, month: 7, day: 1)
private let fixtureClosedRange = 0...10
private let nextFixtureClosedRange = 1...20
private let fixtureDateClosedRange = CustomDate(year: 2022, month: 4, day: 0)...CustomDate(year: 2022, month: 5, day: 0)
private let nextFixtureDateClosedRange = CustomDate(year: 2022, month: 6, day: 1)...CustomDate(year: 2022, month: 7, day: 1)
2024-09-26 05:17:26 +08:00
@Suite(.serialized)
final class DefaultsClosedRangeTests {
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() {
// Test native support Range type
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<Range>("independentRangeKey", default: fixtureRange, suite: suite_)
#expect(fixtureRange.upperBound == Defaults[key].upperBound)
#expect(fixtureRange.lowerBound == Defaults[key].lowerBound)
Defaults[key] = nextFixtureRange
2024-09-26 05:17:26 +08:00
#expect(nextFixtureRange.upperBound == Defaults[key].upperBound)
#expect(nextFixtureRange.lowerBound == Defaults[key].lowerBound)
// Test serializable Range type
2024-09-26 05:17:26 +08:00
let dateKey = Defaults.Key<Range<CustomDate>>("independentRangeDateKey", default: fixtureDateRange, suite: suite_)
#expect(fixtureDateRange.upperBound == Defaults[dateKey].upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey].lowerBound)
Defaults[dateKey] = nextFixtureDateRange
2024-09-26 05:17:26 +08:00
#expect(nextFixtureDateRange.upperBound == Defaults[dateKey].upperBound)
#expect(nextFixtureDateRange.lowerBound == Defaults[dateKey].lowerBound)
// Test native support ClosedRange type
2024-09-26 05:17:26 +08:00
let closedKey = Defaults.Key<ClosedRange>("independentClosedRangeKey", default: fixtureClosedRange, suite: suite_)
#expect(fixtureClosedRange.upperBound == Defaults[closedKey].upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey].lowerBound)
Defaults[closedKey] = nextFixtureClosedRange
2024-09-26 05:17:26 +08:00
#expect(nextFixtureClosedRange.upperBound == Defaults[closedKey].upperBound)
#expect(nextFixtureClosedRange.lowerBound == Defaults[closedKey].lowerBound)
// Test serializable ClosedRange type
2024-09-26 05:17:26 +08:00
let closedDateKey = Defaults.Key<ClosedRange<CustomDate>>("independentClosedRangeDateKey", default: fixtureDateClosedRange, suite: suite_)
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey].upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey].lowerBound)
Defaults[closedDateKey] = nextFixtureDateClosedRange
2024-09-26 05:17:26 +08:00
#expect(nextFixtureDateClosedRange.upperBound == Defaults[closedDateKey].upperBound)
#expect(nextFixtureDateClosedRange.lowerBound == Defaults[closedDateKey].lowerBound)
}
2024-09-26 05:17:26 +08:00
@Test
func testOptionalKey() {
// Test native support Range type
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<Range<Int>?>("independentRangeOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = fixtureRange
2024-09-26 05:17:26 +08:00
#expect(fixtureRange.upperBound == Defaults[key]?.upperBound)
#expect(fixtureRange.lowerBound == Defaults[key]?.lowerBound)
// Test serializable Range type
2024-09-26 05:17:26 +08:00
let dateKey = Defaults.Key<Range<CustomDate>?>("independentRangeDateOptionalKey", suite: suite_)
#expect(Defaults[dateKey] == nil)
Defaults[dateKey] = fixtureDateRange
2024-09-26 05:17:26 +08:00
#expect(fixtureDateRange.upperBound == Defaults[dateKey]?.upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey]?.lowerBound)
// Test native support ClosedRange type
2024-09-26 05:17:26 +08:00
let closedKey = Defaults.Key<ClosedRange<Int>?>("independentClosedRangeOptionalKey", suite: suite_)
#expect(Defaults[closedKey] == nil)
Defaults[closedKey] = fixtureClosedRange
2024-09-26 05:17:26 +08:00
#expect(fixtureClosedRange.upperBound == Defaults[closedKey]?.upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey]?.lowerBound)
// Test serializable ClosedRange type
2024-09-26 05:17:26 +08:00
let closedDateKey = Defaults.Key<ClosedRange<CustomDate>?>("independentClosedRangeDateOptionalKey", suite: suite_)
#expect(Defaults[closedDateKey] == nil)
Defaults[closedDateKey] = fixtureDateClosedRange
2024-09-26 05:17:26 +08:00
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey]?.upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey]?.lowerBound)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayKey() {
// Test native support Range type
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[Range]>("independentRangeArrayKey", default: [fixtureRange], suite: suite_)
#expect(fixtureRange.upperBound == Defaults[key][0].upperBound)
#expect(fixtureRange.lowerBound == Defaults[key][0].lowerBound)
Defaults[key].append(nextFixtureRange)
2024-09-26 05:17:26 +08:00
#expect(fixtureRange.upperBound == Defaults[key][0].upperBound)
#expect(fixtureRange.lowerBound == Defaults[key][0].lowerBound)
#expect(nextFixtureRange.upperBound == Defaults[key][1].upperBound)
#expect(nextFixtureRange.lowerBound == Defaults[key][1].lowerBound)
// Test serializable Range type
2024-09-26 05:17:26 +08:00
let dateKey = Defaults.Key<[Range<CustomDate>]>("independentRangeDateArrayKey", default: [fixtureDateRange], suite: suite_)
#expect(fixtureDateRange.upperBound == Defaults[dateKey][0].upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey][0].lowerBound)
Defaults[dateKey].append(nextFixtureDateRange)
2024-09-26 05:17:26 +08:00
#expect(fixtureDateRange.upperBound == Defaults[dateKey][0].upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey][0].lowerBound)
#expect(nextFixtureDateRange.upperBound == Defaults[dateKey][1].upperBound)
#expect(nextFixtureDateRange.lowerBound == Defaults[dateKey][1].lowerBound)
// Test native support ClosedRange type
2024-09-26 05:17:26 +08:00
let closedKey = Defaults.Key<[ClosedRange]>("independentClosedRangeArrayKey", default: [fixtureClosedRange], suite: suite_)
#expect(fixtureClosedRange.upperBound == Defaults[closedKey][0].upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey][0].lowerBound)
Defaults[closedKey].append(nextFixtureClosedRange)
2024-09-26 05:17:26 +08:00
#expect(fixtureClosedRange.upperBound == Defaults[closedKey][0].upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey][0].lowerBound)
#expect(nextFixtureClosedRange.upperBound == Defaults[closedKey][1].upperBound)
#expect(nextFixtureClosedRange.lowerBound == Defaults[closedKey][1].lowerBound)
// Test serializable ClosedRange type
2024-09-26 05:17:26 +08:00
let closedDateKey = Defaults.Key<[ClosedRange<CustomDate>]>("independentClosedRangeDateArrayKey", default: [fixtureDateClosedRange], suite: suite_)
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey][0].upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey][0].lowerBound)
Defaults[closedDateKey].append(nextFixtureDateClosedRange)
2024-09-26 05:17:26 +08:00
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey][0].upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey][0].lowerBound)
#expect(nextFixtureDateClosedRange.upperBound == Defaults[closedDateKey][1].upperBound)
#expect(nextFixtureDateClosedRange.lowerBound == Defaults[closedDateKey][1].lowerBound)
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryKey() {
// Test native support Range type
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: Range]>("independentRangeDictionaryKey", default: ["0": fixtureRange], suite: suite_)
#expect(fixtureRange.upperBound == Defaults[key]["0"]?.upperBound)
#expect(fixtureRange.lowerBound == Defaults[key]["0"]?.lowerBound)
Defaults[key]["1"] = nextFixtureRange
2024-09-26 05:17:26 +08:00
#expect(fixtureRange.upperBound == Defaults[key]["0"]?.upperBound)
#expect(fixtureRange.lowerBound == Defaults[key]["0"]?.lowerBound)
#expect(nextFixtureRange.upperBound == Defaults[key]["1"]?.upperBound)
#expect(nextFixtureRange.lowerBound == Defaults[key]["1"]?.lowerBound)
// Test serializable Range type
2024-09-26 05:17:26 +08:00
let dateKey = Defaults.Key<[String: Range<CustomDate>]>("independentRangeDateDictionaryKey", default: ["0": fixtureDateRange], suite: suite_)
#expect(fixtureDateRange.upperBound == Defaults[dateKey]["0"]?.upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey]["0"]?.lowerBound)
Defaults[dateKey]["1"] = nextFixtureDateRange
2024-09-26 05:17:26 +08:00
#expect(fixtureDateRange.upperBound == Defaults[dateKey]["0"]?.upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey]["0"]?.lowerBound)
#expect(nextFixtureDateRange.upperBound == Defaults[dateKey]["1"]?.upperBound)
#expect(nextFixtureDateRange.lowerBound == Defaults[dateKey]["1"]?.lowerBound)
// Test native support ClosedRange type
2024-09-26 05:17:26 +08:00
let closedKey = Defaults.Key<[String: ClosedRange]>("independentClosedRangeDictionaryKey", default: ["0": fixtureClosedRange], suite: suite_)
#expect(fixtureClosedRange.upperBound == Defaults[closedKey]["0"]?.upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey]["0"]?.lowerBound)
Defaults[closedKey]["1"] = nextFixtureClosedRange
2024-09-26 05:17:26 +08:00
#expect(fixtureClosedRange.upperBound == Defaults[closedKey]["0"]?.upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey]["0"]?.lowerBound)
#expect(nextFixtureClosedRange.upperBound == Defaults[closedKey]["1"]?.upperBound)
#expect(nextFixtureClosedRange.lowerBound == Defaults[closedKey]["1"]?.lowerBound)
// Test serializable ClosedRange type
2024-09-26 05:17:26 +08:00
let closedDateKey = Defaults.Key<[String: ClosedRange<CustomDate>]>("independentClosedRangeDateDictionaryKey", default: ["0": fixtureDateClosedRange], suite: suite_)
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey]["0"]?.upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey]["0"]?.lowerBound)
Defaults[closedDateKey]["1"] = nextFixtureDateClosedRange
2024-09-26 05:17:26 +08:00
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey]["0"]?.upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey]["0"]?.lowerBound)
#expect(nextFixtureDateClosedRange.upperBound == Defaults[closedDateKey]["1"]?.upperBound)
#expect(nextFixtureDateClosedRange.lowerBound == Defaults[closedDateKey]["1"]?.lowerBound)
}
}