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
|
|
|
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_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Suite(.serialized)
|
|
|
|
final class DefaultsCollectionTests {
|
|
|
|
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<Bag<String>>("independentCollectionKey", default: Bag(items: fixtureCollection), suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key].insert(element: "123", at: 0)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0] == "123")
|
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<Bag<String>?>("independentCollectionOptionalKey", suite: suite_)
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
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])
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key]?.insert(element: fixtureCollection[1], at: 1)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?[1] == fixtureCollection[1])
|
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<[Bag<String>]>("independentCollectionArrayKey", default: [Bag(items: [fixtureCollection[0]])], suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key].append(Bag(items: [fixtureCollection[1]]))
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][1][0] == fixtureCollection[1])
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key][0].insert(element: fixtureCollection[2], at: 1)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0][1] == fixtureCollection[2])
|
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<[Bag<String>]?>("independentCollectionArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
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])
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key]?[0].insert(element: fixtureCollection[2], at: 1)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?[0][1] == fixtureCollection[2])
|
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<[[Bag<String>]]>("independentCollectionNestedArrayKey", default: [[Bag(items: [fixtureCollection[0]])]], suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
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])
|
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: Bag<String>]]>("independentCollectionArrayDictionaryKey", default: [["0": Bag(items: [fixtureCollection[0]])]], suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
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])
|
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: Bag<String>]>("independentCollectionDictionaryKey", default: ["0": Bag(items: [fixtureCollection[0]])], suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
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])
|
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: Bag<String>]?>("independentCollectionDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
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])
|
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: [Bag<String>]]>("independentCollectionDictionaryArrayKey", default: ["0": [Bag(items: [fixtureCollection[0]])]], suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
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])
|
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() {
|
|
|
|
Defaults[.collection].insert(element: "123", at: 0)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.collection][0] == "123")
|
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() {
|
|
|
|
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])
|
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() {
|
|
|
|
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])
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|