Defaults/Tests/DefaultsTests/DefaultsCollectionTests.swift

171 lines
6.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()
struct Bag<Element: Defaults.Serializable>: Collection {
var items: [Element]
init(items: [Element]) {
self.items = items
}
var startIndex: Int {
items.startIndex
}
var endIndex: Int {
items.endIndex
}
mutating func insert(element: Element, at: Int) {
items.insert(element, at: at)
}
func index(after index: Int) -> Int {
items.index(after: index)
}
subscript(position: Int) -> Element {
items[position]
}
}
extension Bag: Defaults.CollectionSerializable {
init(_ elements: [Element]) {
self.items = elements
}
}
private let fixtureCollection = ["Juice", "Apple", "Banana"]
extension Defaults.Keys {
2024-09-26 05:17:26 +08:00
fileprivate static let collection = Key<Bag<String>>("collection", default: Bag(items: fixtureCollection), suite: suite_)
fileprivate static let collectionArray = Key<[Bag<String>]>("collectionArray", default: [Bag(items: fixtureCollection)], suite: suite_)
fileprivate static let collectionDictionary = Key<[String: Bag<String>]>("collectionDictionary", default: ["0": Bag(items: fixtureCollection)], suite: suite_)
}
2024-09-26 05:17:26 +08:00
@Suite(.serialized)
final class DefaultsCollectionTests {
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<Bag<String>>("independentCollectionKey", default: Bag(items: fixtureCollection), suite: suite_)
Defaults[key].insert(element: "123", at: 0)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0] == "123")
}
2024-09-26 05:17:26 +08:00
@Test
func testOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<Bag<String>?>("independentCollectionOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = Bag(items: [])
Defaults[key]?.insert(element: fixtureCollection[0], at: 0)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?[0] == fixtureCollection[0])
Defaults[key]?.insert(element: fixtureCollection[1], at: 1)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?[1] == fixtureCollection[1])
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[Bag<String>]>("independentCollectionArrayKey", default: [Bag(items: [fixtureCollection[0]])], suite: suite_)
Defaults[key].append(Bag(items: [fixtureCollection[1]]))
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][1][0] == fixtureCollection[1])
Defaults[key][0].insert(element: fixtureCollection[2], at: 1)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0][1] == fixtureCollection[2])
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[Bag<String>]?>("independentCollectionArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = [Bag(items: [fixtureCollection[0]])]
Defaults[key]?.append(Bag(items: [fixtureCollection[1]]))
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?[1][0] == fixtureCollection[1])
Defaults[key]?[0].insert(element: fixtureCollection[2], at: 1)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?[0][1] == fixtureCollection[2])
}
2024-09-26 05:17:26 +08:00
@Test
func testNestedArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[[Bag<String>]]>("independentCollectionNestedArrayKey", default: [[Bag(items: [fixtureCollection[0]])]], suite: suite_)
Defaults[key][0].append(Bag(items: [fixtureCollection[1]]))
Defaults[key].append([Bag(items: [fixtureCollection[2]])])
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0][0][0] == fixtureCollection[0])
#expect(Defaults[key][0][1][0] == fixtureCollection[1])
#expect(Defaults[key][1][0][0] == fixtureCollection[2])
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayDictionaryKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[[String: Bag<String>]]>("independentCollectionArrayDictionaryKey", default: [["0": Bag(items: [fixtureCollection[0]])]], suite: suite_)
Defaults[key][0]["1"] = Bag(items: [fixtureCollection[1]])
Defaults[key].append(["0": Bag(items: [fixtureCollection[2]])])
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0]["0"]?[0] == fixtureCollection[0])
#expect(Defaults[key][0]["1"]?[0] == fixtureCollection[1])
#expect(Defaults[key][1]["0"]?[0] == fixtureCollection[2])
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: Bag<String>]>("independentCollectionDictionaryKey", default: ["0": Bag(items: [fixtureCollection[0]])], suite: suite_)
Defaults[key]["0"]?.insert(element: fixtureCollection[1], at: 1)
Defaults[key]["1"] = Bag(items: [fixtureCollection[2]])
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["0"]?[0] == fixtureCollection[0])
#expect(Defaults[key]["0"]?[1] == fixtureCollection[1])
#expect(Defaults[key]["1"]?[0] == fixtureCollection[2])
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: Bag<String>]?>("independentCollectionDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = ["0": Bag(items: [fixtureCollection[0]])]
Defaults[key]?["0"]?.insert(element: fixtureCollection[1], at: 1)
Defaults[key]?["1"] = Bag(items: [fixtureCollection[2]])
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?["0"]?[0] == fixtureCollection[0])
#expect(Defaults[key]?["0"]?[1] == fixtureCollection[1])
#expect(Defaults[key]?["1"]?[0] == fixtureCollection[2])
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: [Bag<String>]]>("independentCollectionDictionaryArrayKey", default: ["0": [Bag(items: [fixtureCollection[0]])]], suite: suite_)
Defaults[key]["0"]?[0].insert(element: fixtureCollection[1], at: 1)
Defaults[key]["1"] = [Bag(items: [fixtureCollection[2]])]
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["0"]?[0][0] == fixtureCollection[0])
#expect(Defaults[key]["0"]?[0][1] == fixtureCollection[1])
#expect(Defaults[key]["1"]?[0][0] == fixtureCollection[2])
}
2024-09-26 05:17:26 +08:00
@Test
func testType() {
Defaults[.collection].insert(element: "123", at: 0)
2024-09-26 05:17:26 +08:00
#expect(Defaults[.collection][0] == "123")
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayType() {
Defaults[.collectionArray].append(Bag(items: [fixtureCollection[0]]))
Defaults[.collectionArray][0].insert(element: "123", at: 0)
2024-09-26 05:17:26 +08:00
#expect(Defaults[.collectionArray][0][0] == "123")
#expect(Defaults[.collectionArray][1][0] == fixtureCollection[0])
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryType() {
Defaults[.collectionDictionary]["1"] = Bag(items: [fixtureCollection[0]])
Defaults[.collectionDictionary]["0"]?.insert(element: "123", at: 0)
2024-09-26 05:17:26 +08:00
#expect(Defaults[.collectionDictionary]["0"]?[0] == "123")
#expect(Defaults[.collectionDictionary]["1"]?[0] == fixtureCollection[0])
}
}