2018-04-16 17:19:45 +08:00
|
|
|
import Foundation
|
2019-10-30 19:57:31 +08:00
|
|
|
import CoreData
|
2020-01-21 00:41:13 +08:00
|
|
|
import Combine
|
2020-01-21 15:29:57 +08:00
|
|
|
import XCTest
|
|
|
|
import Defaults
|
2018-04-12 01:19:30 +08:00
|
|
|
|
2018-10-17 18:22:10 +08:00
|
|
|
let fixtureURL = URL(string: "https://sindresorhus.com")!
|
|
|
|
let fixtureURL2 = URL(string: "https://example.com")!
|
2018-04-12 01:19:30 +08:00
|
|
|
|
|
|
|
enum FixtureEnum: String, Codable {
|
|
|
|
case tenMinutes = "10 Minutes"
|
|
|
|
case halfHour = "30 Minutes"
|
|
|
|
case oneHour = "1 Hour"
|
|
|
|
}
|
|
|
|
|
2018-10-17 18:22:10 +08:00
|
|
|
let fixtureDate = Date()
|
|
|
|
|
2019-10-30 20:47:54 +08:00
|
|
|
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
|
2019-10-30 19:57:31 +08:00
|
|
|
final class ExamplePersistentHistory: NSPersistentHistoryToken {
|
|
|
|
let value: String
|
|
|
|
|
|
|
|
init(value: String) {
|
|
|
|
self.value = value
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
self.value = coder.decodeObject(forKey: "value") as! String
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func encode(with coder: NSCoder) {
|
|
|
|
coder.encode(value, forKey: "value")
|
|
|
|
}
|
|
|
|
|
2020-01-21 15:29:57 +08:00
|
|
|
override class var supportsSecureCoding: Bool { true }
|
2019-10-30 19:57:31 +08:00
|
|
|
}
|
|
|
|
|
2018-04-12 01:19:30 +08:00
|
|
|
extension Defaults.Keys {
|
2018-10-17 18:22:10 +08:00
|
|
|
static let key = Key<Bool>("key", default: false)
|
|
|
|
static let url = Key<URL>("url", default: fixtureURL)
|
|
|
|
static let `enum` = Key<FixtureEnum>("enum", default: .oneHour)
|
2019-07-25 09:53:47 +08:00
|
|
|
static let data = Key<Data>("data", default: Data([]))
|
2018-10-17 18:22:10 +08:00
|
|
|
static let date = Key<Date>("date", default: fixtureDate)
|
2019-10-30 19:57:31 +08:00
|
|
|
|
|
|
|
// NSSecureCoding
|
2019-10-30 20:47:54 +08:00
|
|
|
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
|
2019-10-30 19:57:31 +08:00
|
|
|
static let persistentHistoryValue = ExamplePersistentHistory(value: "ExampleToken")
|
2019-10-30 20:47:54 +08:00
|
|
|
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
|
2019-10-30 19:57:31 +08:00
|
|
|
static let persistentHistory = NSSecureCodingKey<ExamplePersistentHistory>("persistentHistory", default: persistentHistoryValue)
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
final class DefaultsTests: XCTestCase {
|
|
|
|
override func setUp() {
|
|
|
|
super.setUp()
|
2019-09-11 15:32:18 +08:00
|
|
|
Defaults.removeAll()
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 18:22:10 +08:00
|
|
|
override func tearDown() {
|
|
|
|
super.setUp()
|
2019-09-11 15:32:18 +08:00
|
|
|
Defaults.removeAll()
|
2018-10-17 18:22:10 +08:00
|
|
|
}
|
|
|
|
|
2018-04-16 17:00:33 +08:00
|
|
|
func testKey() {
|
2018-10-17 18:22:10 +08:00
|
|
|
let key = Defaults.Key<Bool>("independentKey", default: false)
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertFalse(Defaults[key])
|
|
|
|
Defaults[key] = true
|
|
|
|
XCTAssertTrue(Defaults[key])
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testOptionalKey() {
|
2020-01-21 15:29:57 +08:00
|
|
|
let key = Defaults.Key<Bool?>("independentOptionalKey")
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertNil(Defaults[key])
|
|
|
|
Defaults[key] = true
|
|
|
|
XCTAssertTrue(Defaults[key]!)
|
|
|
|
Defaults[key] = nil
|
|
|
|
XCTAssertNil(Defaults[key])
|
|
|
|
Defaults[key] = false
|
|
|
|
XCTAssertFalse(Defaults[key]!)
|
2018-10-17 18:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testKeyRegistersDefault() {
|
|
|
|
let keyName = "registersDefault"
|
|
|
|
XCTAssertEqual(UserDefaults.standard.bool(forKey: keyName), false)
|
|
|
|
_ = Defaults.Key<Bool>(keyName, default: true)
|
|
|
|
XCTAssertEqual(UserDefaults.standard.bool(forKey: keyName), true)
|
|
|
|
|
2019-09-11 15:56:11 +08:00
|
|
|
// Test that it works with multiple keys with `Defaults`.
|
2018-10-17 18:22:10 +08:00
|
|
|
let keyName2 = "registersDefault2"
|
|
|
|
_ = Defaults.Key<String>(keyName2, default: keyName2)
|
|
|
|
XCTAssertEqual(UserDefaults.standard.string(forKey: keyName2), keyName2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testKeyWithUserDefaultSubscript() {
|
|
|
|
let key = Defaults.Key<Bool>("keyWithUserDeaultSubscript", default: false)
|
|
|
|
XCTAssertFalse(UserDefaults.standard[key])
|
2018-04-12 01:19:30 +08:00
|
|
|
UserDefaults.standard[key] = true
|
2018-10-17 18:22:10 +08:00
|
|
|
XCTAssertTrue(UserDefaults.standard[key])
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testKeys() {
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertFalse(Defaults[.key])
|
|
|
|
Defaults[.key] = true
|
|
|
|
XCTAssertTrue(Defaults[.key])
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 20:47:54 +08:00
|
|
|
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
|
2019-10-30 19:57:31 +08:00
|
|
|
func testNSSecureCodingKeys() {
|
|
|
|
XCTAssertEqual(Defaults.Keys.persistentHistoryValue.value, Defaults[.persistentHistory].value)
|
|
|
|
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
|
|
|
|
Defaults[.persistentHistory] = newPersistentHistory
|
|
|
|
XCTAssertEqual(newPersistentHistory.value, Defaults[.persistentHistory].value)
|
|
|
|
}
|
|
|
|
|
2018-04-12 01:19:30 +08:00
|
|
|
func testUrlType() {
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertEqual(Defaults[.url], fixtureURL)
|
2018-04-12 01:19:30 +08:00
|
|
|
|
|
|
|
let newUrl = URL(string: "https://twitter.com")!
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[.url] = newUrl
|
|
|
|
XCTAssertEqual(Defaults[.url], newUrl)
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testEnumType() {
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertEqual(Defaults[.enum], FixtureEnum.oneHour)
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
|
|
|
|
2018-04-16 02:09:53 +08:00
|
|
|
func testDataType() {
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertEqual(Defaults[.data], Data([]))
|
2018-04-16 02:09:53 +08:00
|
|
|
|
2019-07-25 09:53:47 +08:00
|
|
|
let newData = Data([0xFF])
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[.data] = newData
|
|
|
|
XCTAssertEqual(Defaults[.data], newData)
|
2018-04-16 02:09:53 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 18:22:10 +08:00
|
|
|
func testDateType() {
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertEqual(Defaults[.date], fixtureDate)
|
2018-10-17 18:22:10 +08:00
|
|
|
|
|
|
|
let newDate = Date()
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[.date] = newDate
|
|
|
|
XCTAssertEqual(Defaults[.date], newDate)
|
2018-10-17 18:22:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-11 15:32:18 +08:00
|
|
|
func testRemoveAll() {
|
|
|
|
let key = Defaults.Key<Bool>("removeAll", default: false)
|
|
|
|
let key2 = Defaults.Key<Bool>("removeAll2", default: false)
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key] = true
|
2019-09-11 15:32:18 +08:00
|
|
|
Defaults[key2] = true
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertTrue(Defaults[key])
|
2019-09-11 15:32:18 +08:00
|
|
|
XCTAssertTrue(Defaults[key2])
|
|
|
|
Defaults.removeAll()
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertFalse(Defaults[key])
|
2019-09-11 15:32:18 +08:00
|
|
|
XCTAssertFalse(Defaults[key2])
|
2018-10-17 18:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCustomSuite() {
|
|
|
|
let customSuite = UserDefaults(suiteName: "com.sindresorhus.customSuite")!
|
|
|
|
let key = Defaults.Key<Bool>("customSuite", default: false, suite: customSuite)
|
|
|
|
XCTAssertFalse(customSuite[key])
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertFalse(Defaults[key])
|
|
|
|
Defaults[key] = true
|
2018-10-17 18:22:10 +08:00
|
|
|
XCTAssertTrue(customSuite[key])
|
2019-09-11 12:59:28 +08:00
|
|
|
XCTAssertTrue(Defaults[key])
|
2019-09-11 15:32:18 +08:00
|
|
|
Defaults.removeAll(suite: customSuite)
|
2018-10-17 18:22:10 +08:00
|
|
|
}
|
|
|
|
|
2020-01-21 00:41:13 +08:00
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObserveKeyCombine() {
|
|
|
|
let key = Defaults.Key<Bool>("observeKey", default: false)
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
|
|
|
let publisher = Defaults
|
2020-04-13 13:14:55 +08:00
|
|
|
.publisher(key, options: [])
|
2020-01-21 00:41:13 +08:00
|
|
|
.map { ($0.oldValue, $0.newValue) }
|
|
|
|
.collect(2)
|
|
|
|
|
|
|
|
let cancellable = publisher.sink { tuples in
|
|
|
|
for (i, expected) in [(false, true), (true, false)].enumerated() {
|
|
|
|
XCTAssertEqual(expected.0, tuples[i].0)
|
|
|
|
XCTAssertEqual(expected.1, tuples[i].1)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
Defaults[key] = true
|
|
|
|
Defaults.reset(key)
|
|
|
|
cancellable.cancel()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObserveNSSecureCodingKeyCombine() {
|
|
|
|
let key = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey", default: ExamplePersistentHistory(value: "TestValue"))
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
|
|
|
let publisher = Defaults
|
2020-04-13 13:14:55 +08:00
|
|
|
.publisher(key, options: [])
|
2020-01-21 00:41:13 +08:00
|
|
|
.map { ($0.oldValue.value, $0.newValue.value) }
|
|
|
|
.collect(3)
|
|
|
|
|
|
|
|
let expectedValues = [
|
|
|
|
("TestValue", "NewTestValue"),
|
|
|
|
("NewTestValue", "NewTestValue2"),
|
|
|
|
("NewTestValue2", "TestValue")
|
|
|
|
]
|
|
|
|
|
|
|
|
let cancellable = publisher.sink { actualValues in
|
|
|
|
for (expected, actual) in zip(expectedValues, actualValues) {
|
|
|
|
XCTAssertEqual(expected.0, actual.0)
|
|
|
|
XCTAssertEqual(expected.1, actual.1)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
Defaults[key] = ExamplePersistentHistory(value: "NewTestValue")
|
|
|
|
Defaults[key] = ExamplePersistentHistory(value: "NewTestValue2")
|
|
|
|
Defaults.reset(key)
|
|
|
|
cancellable.cancel()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObserveOptionalKeyCombine() {
|
2020-01-21 15:29:57 +08:00
|
|
|
let key = Defaults.Key<Bool?>("observeOptionalKey")
|
2020-01-21 00:41:13 +08:00
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
|
|
|
let publisher = Defaults
|
2020-04-13 13:14:55 +08:00
|
|
|
.publisher(key, options: [])
|
2020-01-21 00:41:13 +08:00
|
|
|
.map { ($0.oldValue, $0.newValue) }
|
|
|
|
.collect(3)
|
|
|
|
|
|
|
|
let expectedValues: [(Bool?, Bool?)] = [(nil, true), (true, false), (false, nil)]
|
|
|
|
|
|
|
|
let cancellable = publisher.sink { actualValues in
|
|
|
|
for (expected, actual) in zip(expectedValues, actualValues) {
|
|
|
|
XCTAssertEqual(expected.0, actual.0)
|
|
|
|
XCTAssertEqual(expected.1, actual.1)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
Defaults[key] = true
|
|
|
|
Defaults[key] = false
|
|
|
|
Defaults.reset(key)
|
|
|
|
cancellable.cancel()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObserveNSSecureCodingOptionalKeyCombine() {
|
|
|
|
let key = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("observeNSSecureCodingOptionalKey")
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
|
|
|
let publisher = Defaults
|
2020-04-13 13:14:55 +08:00
|
|
|
.publisher(key, options: [])
|
2020-01-21 00:41:13 +08:00
|
|
|
.map { ($0.oldValue?.value, $0.newValue?.value) }
|
|
|
|
.collect(3)
|
|
|
|
|
|
|
|
let expectedValues: [(String?, String?)] = [
|
|
|
|
(nil, "NewTestValue"),
|
|
|
|
("NewTestValue", "NewTestValue2"),
|
|
|
|
("NewTestValue2", nil)
|
|
|
|
]
|
|
|
|
|
|
|
|
let cancellable = publisher.sink { actualValues in
|
|
|
|
for (expected, actual) in zip(expectedValues, actualValues) {
|
|
|
|
XCTAssertEqual(expected.0, actual.0)
|
|
|
|
XCTAssertEqual(expected.1, actual.1)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertNil(Defaults[key])
|
|
|
|
Defaults[key] = ExamplePersistentHistory(value: "NewTestValue")
|
|
|
|
Defaults[key] = ExamplePersistentHistory(value: "NewTestValue2")
|
|
|
|
Defaults.reset(key)
|
|
|
|
cancellable.cancel()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObserveMultipleKeysCombine() {
|
2020-04-18 14:19:38 +08:00
|
|
|
let key1 = Defaults.Key<String>("observeKey1", default: "x")
|
2020-01-21 00:41:13 +08:00
|
|
|
let key2 = Defaults.Key<Bool>("observeKey2", default: true)
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 13:14:55 +08:00
|
|
|
let publisher = Defaults.publisher(keys: key1, key2, options: []).collect(2)
|
2020-01-21 00:41:13 +08:00
|
|
|
|
|
|
|
let cancellable = publisher.sink { _ in
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-04-18 14:19:38 +08:00
|
|
|
Defaults[key1] = "y"
|
2020-01-21 00:41:13 +08:00
|
|
|
Defaults[key2] = false
|
|
|
|
cancellable.cancel()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObserveMultipleNSSecureKeysCombine() {
|
|
|
|
let key1 = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey1", default: ExamplePersistentHistory(value: "TestValue"))
|
|
|
|
let key2 = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey2", default: ExamplePersistentHistory(value: "TestValue"))
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 13:14:55 +08:00
|
|
|
let publisher = Defaults.publisher(keys: key1, key2, options: []).collect(2)
|
2020-01-21 00:41:13 +08:00
|
|
|
|
|
|
|
let cancellable = publisher.sink { _ in
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
Defaults[key1] = ExamplePersistentHistory(value: "NewTestValue1")
|
|
|
|
Defaults[key2] = ExamplePersistentHistory(value: "NewTestValue2")
|
|
|
|
cancellable.cancel()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-01-21 01:07:30 +08:00
|
|
|
|
2020-01-21 00:41:13 +08:00
|
|
|
|
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObserveMultipleOptionalKeysCombine() {
|
2020-04-18 14:19:38 +08:00
|
|
|
let key1 = Defaults.Key<String?>("observeOptionalKey1")
|
2020-01-21 15:29:57 +08:00
|
|
|
let key2 = Defaults.Key<Bool?>("observeOptionalKey2")
|
2020-01-21 00:41:13 +08:00
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 13:14:55 +08:00
|
|
|
let publisher = Defaults.publisher(keys: key1, key2, options: []).collect(2)
|
2020-01-21 00:41:13 +08:00
|
|
|
|
|
|
|
let cancellable = publisher.sink { _ in
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-04-18 14:19:38 +08:00
|
|
|
Defaults[key1] = "x"
|
2020-01-21 00:41:13 +08:00
|
|
|
Defaults[key2] = false
|
|
|
|
cancellable.cancel()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObserveMultipleNSSecureOptionalKeysCombine() {
|
|
|
|
let key1 = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("observeNSSecureCodingKey1")
|
|
|
|
let key2 = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("observeNSSecureCodingKey2")
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 13:14:55 +08:00
|
|
|
let publisher = Defaults.publisher(keys: key1, key2, options: []).collect(2)
|
2020-01-21 00:41:13 +08:00
|
|
|
|
|
|
|
let cancellable = publisher.sink { _ in
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
Defaults[key1] = ExamplePersistentHistory(value: "NewTestValue1")
|
|
|
|
Defaults[key2] = ExamplePersistentHistory(value: "NewTestValue2")
|
|
|
|
cancellable.cancel()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:56:59 +08:00
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testReceiveValueBeforeSubscriptionCombine() {
|
|
|
|
let key = Defaults.Key<String>("receiveValueBeforeSubscription", default: "hello")
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
2020-06-07 02:14:12 +08:00
|
|
|
|
2020-04-05 22:56:59 +08:00
|
|
|
let publisher = Defaults
|
2020-04-13 13:14:55 +08:00
|
|
|
.publisher(key)
|
2020-08-29 04:38:56 +08:00
|
|
|
.map(\.newValue)
|
2020-04-05 22:56:59 +08:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
.collect(2)
|
|
|
|
|
|
|
|
let cancellable = publisher.sink { values in
|
|
|
|
XCTAssertEqual(["hello", "world"], values)
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-06-07 02:14:12 +08:00
|
|
|
Defaults[key] = "world"
|
2020-04-05 22:56:59 +08:00
|
|
|
cancellable.cancel()
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
2018-10-17 18:22:10 +08:00
|
|
|
func testObserveKey() {
|
|
|
|
let key = Defaults.Key<Bool>("observeKey", default: false)
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 12:31:54 +08:00
|
|
|
var observation: Defaults.Observation!
|
2020-04-13 13:14:55 +08:00
|
|
|
observation = Defaults.observe(key, options: []) { change in
|
2018-10-17 18:22:10 +08:00
|
|
|
XCTAssertFalse(change.oldValue)
|
|
|
|
XCTAssertTrue(change.newValue)
|
|
|
|
observation.invalidate()
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key] = true
|
2018-10-17 18:22:10 +08:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
2019-10-30 20:47:54 +08:00
|
|
|
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
|
2019-10-30 19:57:31 +08:00
|
|
|
func testObserveNSSecureCodingKey() {
|
|
|
|
let key = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey", default: ExamplePersistentHistory(value: "TestValue"))
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 12:31:54 +08:00
|
|
|
var observation: Defaults.Observation!
|
2020-04-13 13:14:55 +08:00
|
|
|
observation = Defaults.observe(key, options: []) { change in
|
2019-10-30 19:57:31 +08:00
|
|
|
XCTAssertEqual(change.oldValue.value, "TestValue")
|
|
|
|
XCTAssertEqual(change.newValue.value, "NewTestValue")
|
|
|
|
observation.invalidate()
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
Defaults[key] = ExamplePersistentHistory(value: "NewTestValue")
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
2018-10-17 18:22:10 +08:00
|
|
|
func testObserveOptionalKey() {
|
2020-01-21 15:29:57 +08:00
|
|
|
let key = Defaults.Key<Bool?>("observeOptionalKey")
|
2018-10-17 18:22:10 +08:00
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 12:31:54 +08:00
|
|
|
var observation: Defaults.Observation!
|
2020-04-13 13:14:55 +08:00
|
|
|
observation = Defaults.observe(key, options: []) { change in
|
2018-10-17 18:22:10 +08:00
|
|
|
XCTAssertNil(change.oldValue)
|
|
|
|
XCTAssertTrue(change.newValue!)
|
|
|
|
observation.invalidate()
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key] = true
|
2018-10-17 18:22:10 +08:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
2019-10-30 20:47:54 +08:00
|
|
|
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
|
2019-10-30 19:57:31 +08:00
|
|
|
func testObserveNSSecureCodingOptionalKey() {
|
|
|
|
let key = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("observeNSSecureCodingOptionalKey")
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 12:31:54 +08:00
|
|
|
var observation: Defaults.Observation!
|
2020-04-13 13:14:55 +08:00
|
|
|
observation = Defaults.observe(key, options: []) { change in
|
2019-10-30 19:57:31 +08:00
|
|
|
XCTAssertNil(change.oldValue)
|
|
|
|
XCTAssertEqual(change.newValue?.value, "NewOptionalValue")
|
|
|
|
observation.invalidate()
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
Defaults[key] = ExamplePersistentHistory(value: "NewOptionalValue")
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
func testObserveMultipleKeys() {
|
|
|
|
let key1 = Defaults.Key<String>("observeKey1", default: "x")
|
|
|
|
let key2 = Defaults.Key<Bool>("observeKey2", default: true)
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
var observation: Defaults.Observation!
|
|
|
|
var counter = 0
|
|
|
|
observation = Defaults.observe(keys: key1, key2, options: []) {
|
|
|
|
counter += 1
|
|
|
|
if counter == 2 {
|
|
|
|
expect.fulfill()
|
|
|
|
} else if counter > 2 {
|
|
|
|
XCTFail()
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
Defaults[key1] = "y"
|
|
|
|
Defaults[key2] = false
|
|
|
|
observation.invalidate()
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
|
|
|
|
func testObserveMultipleNSSecureKeys() {
|
|
|
|
let key1 = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey1", default: ExamplePersistentHistory(value: "TestValue"))
|
|
|
|
let key2 = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey2", default: ExamplePersistentHistory(value: "TestValue"))
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
var observation: Defaults.Observation!
|
|
|
|
var counter = 0
|
|
|
|
observation = Defaults.observe(keys: key1, key2, options: []) {
|
|
|
|
counter += 1
|
|
|
|
if counter == 2 {
|
|
|
|
expect.fulfill()
|
|
|
|
} else if counter > 2 {
|
|
|
|
XCTFail()
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
Defaults[key1] = ExamplePersistentHistory(value: "NewTestValue1")
|
|
|
|
Defaults[key2] = ExamplePersistentHistory(value: "NewTestValue2")
|
|
|
|
observation.invalidate()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2019-10-30 19:57:31 +08:00
|
|
|
|
2018-10-17 18:22:10 +08:00
|
|
|
func testObserveKeyURL() {
|
|
|
|
let fixtureURL = URL(string: "https://sindresorhus.com")!
|
|
|
|
let fixtureURL2 = URL(string: "https://example.com")!
|
|
|
|
let key = Defaults.Key<URL>("observeKeyURL", default: fixtureURL)
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 12:31:54 +08:00
|
|
|
var observation: Defaults.Observation!
|
2020-04-13 13:14:55 +08:00
|
|
|
observation = Defaults.observe(key, options: []) { change in
|
2018-10-17 18:22:10 +08:00
|
|
|
XCTAssertEqual(change.oldValue, fixtureURL)
|
|
|
|
XCTAssertEqual(change.newValue, fixtureURL2)
|
|
|
|
observation.invalidate()
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key] = fixtureURL2
|
2018-10-17 18:22:10 +08:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testObserveKeyEnum() {
|
|
|
|
let key = Defaults.Key<FixtureEnum>("observeKeyEnum", default: .oneHour)
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 12:31:54 +08:00
|
|
|
var observation: Defaults.Observation!
|
2020-04-13 13:14:55 +08:00
|
|
|
observation = Defaults.observe(key, options: []) { change in
|
2018-10-17 18:22:10 +08:00
|
|
|
XCTAssertEqual(change.oldValue, .oneHour)
|
|
|
|
XCTAssertEqual(change.newValue, .tenMinutes)
|
|
|
|
observation.invalidate()
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key] = .tenMinutes
|
2018-10-17 18:22:10 +08:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
func testObservePreventPropagation() {
|
|
|
|
let key1 = Defaults.Key<Bool?>("preventPropagation0", default: nil)
|
|
|
|
let expect = expectation(description: "No infinite recursion")
|
|
|
|
|
|
|
|
var observation: Defaults.Observation!
|
|
|
|
var wasInside = false
|
|
|
|
observation = Defaults.observe(key1, options: []) { _ in
|
|
|
|
XCTAssertFalse(wasInside)
|
|
|
|
wasInside = true
|
|
|
|
Defaults.withoutPropagation {
|
|
|
|
Defaults[key1] = true
|
|
|
|
}
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
Defaults[key1] = false
|
|
|
|
observation.invalidate()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
func testObservePreventPropagationMultipleKeys() {
|
|
|
|
let key1 = Defaults.Key<Bool?>("preventPropagation1", default: nil)
|
|
|
|
let key2 = Defaults.Key<Bool?>("preventPropagation2", default: nil)
|
|
|
|
let expect = expectation(description: "No infinite recursion")
|
|
|
|
|
|
|
|
var observation: Defaults.Observation!
|
|
|
|
var wasInside = false
|
|
|
|
observation = Defaults.observe(keys: key1, key2, options: []) {
|
|
|
|
XCTAssertFalse(wasInside)
|
|
|
|
wasInside = true
|
|
|
|
Defaults.withoutPropagation {
|
|
|
|
Defaults[key1] = true
|
|
|
|
}
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
Defaults[key1] = false
|
|
|
|
observation.invalidate()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
|
|
|
// This checks if the callback is still being called if the value is changed on a second thread while the initial thread is doing some long running task.
|
2020-08-29 04:12:29 +08:00
|
|
|
func testObservePreventPropagationMultipleThreads() {
|
|
|
|
let key1 = Defaults.Key<Int?>("preventPropagation3", default: nil)
|
|
|
|
let expect = expectation(description: "No infinite recursion")
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
var observation: Defaults.Observation!
|
|
|
|
observation = Defaults.observe(key1, options: []) { _ in
|
|
|
|
Defaults.withoutPropagation {
|
|
|
|
Defaults[key1]! += 1
|
|
|
|
}
|
|
|
|
print("--- Main Thread: \(Thread.isMainThread)")
|
|
|
|
if !Thread.isMainThread {
|
|
|
|
XCTAssert(Defaults[key1]! == 4)
|
|
|
|
expect.fulfill()
|
|
|
|
} else {
|
|
|
|
usleep(100000)
|
|
|
|
print("--- Release: \(Thread.isMainThread)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DispatchQueue.global().asyncAfter(deadline: .now() + 0.05) {
|
|
|
|
Defaults[key1]! += 1
|
|
|
|
}
|
|
|
|
Defaults[key1] = 1
|
|
|
|
observation.invalidate()
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
|
|
|
// Check if propagation prevention works across multiple observations.
|
2020-08-29 04:12:29 +08:00
|
|
|
func testObservePreventPropagationMultipleObservations() {
|
|
|
|
let key1 = Defaults.Key<Bool?>("preventPropagation4", default: nil)
|
|
|
|
let key2 = Defaults.Key<Bool?>("preventPropagation5", default: nil)
|
|
|
|
let expect = expectation(description: "No infinite recursion")
|
|
|
|
|
|
|
|
let observation1 = Defaults.observe(key2, options: []) { _ in
|
|
|
|
XCTFail()
|
|
|
|
}
|
|
|
|
let observation2 = Defaults.observe(keys: key1, key2, options: []) {
|
|
|
|
Defaults.withoutPropagation {
|
|
|
|
Defaults[key2] = true
|
|
|
|
}
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
Defaults[key1] = false
|
|
|
|
observation1.invalidate()
|
|
|
|
observation2.invalidate()
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObservePreventPropagationCombine() {
|
|
|
|
let key1 = Defaults.Key<Bool?>("preventPropagation6", default: nil)
|
|
|
|
let expect = expectation(description: "No infinite recursion")
|
|
|
|
|
|
|
|
var wasInside = false
|
|
|
|
let cancellable = Defaults.publisher(key1, options: []).sink { _ in
|
|
|
|
XCTAssertFalse(wasInside)
|
|
|
|
wasInside = true
|
|
|
|
Defaults.withoutPropagation {
|
|
|
|
Defaults[key1] = true
|
|
|
|
}
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
Defaults[key1] = false
|
|
|
|
cancellable.cancel()
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObservePreventPropagationMultipleKeysCombine() {
|
|
|
|
let key1 = Defaults.Key<Bool?>("preventPropagation7", default: nil)
|
|
|
|
let key2 = Defaults.Key<Bool?>("preventPropagation8", default: nil)
|
|
|
|
let expect = expectation(description: "No infinite recursion")
|
|
|
|
|
|
|
|
var wasInside = false
|
|
|
|
let cancellable = Defaults.publisher(keys: key1, key2, options: []).sink { _ in
|
|
|
|
XCTAssertFalse(wasInside)
|
|
|
|
wasInside = true
|
|
|
|
Defaults.withoutPropagation {
|
|
|
|
Defaults[key1] = true
|
|
|
|
}
|
|
|
|
expect.fulfill()
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
Defaults[key2] = false
|
|
|
|
cancellable.cancel()
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
|
|
|
func testObservePreventPropagationModifiersCombine() {
|
|
|
|
let key1 = Defaults.Key<Bool?>("preventPropagation9", default: nil)
|
|
|
|
let expect = expectation(description: "No infinite recursion")
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
var wasInside = false
|
|
|
|
var cancellable: AnyCancellable!
|
|
|
|
cancellable = Defaults.publisher(key1, options: [])
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.delay(for: 0.5, scheduler: DispatchQueue.global())
|
|
|
|
.sink { _ in
|
|
|
|
XCTAssertFalse(wasInside)
|
|
|
|
wasInside = true
|
|
|
|
Defaults.withoutPropagation {
|
|
|
|
Defaults[key1] = true
|
|
|
|
}
|
|
|
|
expect.fulfill()
|
|
|
|
cancellable.cancel()
|
|
|
|
}
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
Defaults[key1] = false
|
2020-08-29 04:38:56 +08:00
|
|
|
|
2020-08-29 04:12:29 +08:00
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
2020-01-21 01:07:30 +08:00
|
|
|
|
2019-09-11 15:09:12 +08:00
|
|
|
func testResetKey() {
|
2020-04-17 22:32:44 +08:00
|
|
|
let defaultFixture1 = "foo1"
|
|
|
|
let defaultFixture2 = 0
|
|
|
|
let defaultFixture3 = "foo3"
|
|
|
|
let newFixture1 = "bar1"
|
|
|
|
let newFixture2 = 1
|
|
|
|
let newFixture3 = "bar3"
|
|
|
|
let key1 = Defaults.Key<String>("key1", default: defaultFixture1)
|
|
|
|
let key2 = Defaults.Key<Int>("key2", default: defaultFixture2)
|
|
|
|
Defaults[key1] = newFixture1
|
|
|
|
Defaults[key2] = newFixture2
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults.reset(key1)
|
2020-04-17 22:32:44 +08:00
|
|
|
XCTAssertEqual(Defaults[key1], defaultFixture1)
|
|
|
|
XCTAssertEqual(Defaults[key2], newFixture2)
|
2019-10-30 19:57:31 +08:00
|
|
|
|
2019-10-30 20:47:54 +08:00
|
|
|
if #available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) {
|
2020-04-17 22:32:44 +08:00
|
|
|
let key3 = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("key3", default: ExamplePersistentHistory(value: defaultFixture3))
|
|
|
|
Defaults[key3] = ExamplePersistentHistory(value: newFixture3)
|
2019-10-30 19:57:31 +08:00
|
|
|
Defaults.reset(key3)
|
|
|
|
|
2020-04-17 22:32:44 +08:00
|
|
|
XCTAssertEqual(Defaults[key3].value, defaultFixture3)
|
2019-10-30 19:57:31 +08:00
|
|
|
}
|
2019-09-11 15:09:12 +08:00
|
|
|
}
|
2020-01-21 01:07:30 +08:00
|
|
|
|
2020-04-17 22:32:44 +08:00
|
|
|
func testResetMultipleKeys() {
|
|
|
|
let defaultFxiture1 = "foo1"
|
|
|
|
let defaultFixture2 = 0
|
|
|
|
let defaultFixture3 = "foo3"
|
|
|
|
let newFixture1 = "bar1"
|
|
|
|
let newFixture2 = 1
|
|
|
|
let newFixture3 = "bar3"
|
|
|
|
let key1 = Defaults.Key<String>("akey1", default: defaultFxiture1)
|
|
|
|
let key2 = Defaults.Key<Int>("akey2", default: defaultFixture2)
|
|
|
|
let key3 = Defaults.Key<String>("akey3", default: defaultFixture3)
|
|
|
|
Defaults[key1] = newFixture1
|
|
|
|
Defaults[key2] = newFixture2
|
|
|
|
Defaults[key3] = newFixture3
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults.reset(key1, key2)
|
2020-04-17 22:32:44 +08:00
|
|
|
XCTAssertEqual(Defaults[key1], defaultFxiture1)
|
|
|
|
XCTAssertEqual(Defaults[key2], defaultFixture2)
|
|
|
|
XCTAssertEqual(Defaults[key3], newFixture3)
|
2019-09-11 15:09:12 +08:00
|
|
|
}
|
2020-01-21 01:07:30 +08:00
|
|
|
|
2019-09-11 15:09:12 +08:00
|
|
|
func testResetOptionalKey() {
|
|
|
|
let newString1 = "bar1"
|
|
|
|
let newString2 = "bar2"
|
2019-10-30 19:57:31 +08:00
|
|
|
let newString3 = "bar3"
|
2020-01-21 15:29:57 +08:00
|
|
|
let key1 = Defaults.Key<String?>("optionalKey1")
|
|
|
|
let key2 = Defaults.Key<String?>("optionalKey2")
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key1] = newString1
|
|
|
|
Defaults[key2] = newString2
|
|
|
|
Defaults.reset(key1)
|
|
|
|
XCTAssertEqual(Defaults[key1], nil)
|
|
|
|
XCTAssertEqual(Defaults[key2], newString2)
|
2019-10-30 19:57:31 +08:00
|
|
|
|
2019-10-30 20:47:54 +08:00
|
|
|
if #available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) {
|
2019-10-30 19:57:31 +08:00
|
|
|
let key3 = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("optionalKey3")
|
|
|
|
Defaults[key3] = ExamplePersistentHistory(value: newString3)
|
|
|
|
Defaults.reset(key3)
|
|
|
|
XCTAssertEqual(Defaults[key3], nil)
|
|
|
|
}
|
2019-09-11 15:09:12 +08:00
|
|
|
}
|
2020-01-21 01:07:30 +08:00
|
|
|
|
2020-04-17 22:32:44 +08:00
|
|
|
func testResetMultipleOptionalKeys() {
|
|
|
|
let newFixture1 = "bar1"
|
|
|
|
let newFixture2 = 1
|
|
|
|
let newFixture3 = "bar3"
|
2020-01-21 15:29:57 +08:00
|
|
|
let key1 = Defaults.Key<String?>("aoptionalKey1")
|
2020-04-17 22:32:44 +08:00
|
|
|
let key2 = Defaults.Key<Int?>("aoptionalKey2")
|
2020-01-21 15:29:57 +08:00
|
|
|
let key3 = Defaults.Key<String?>("aoptionalKey3")
|
2020-04-17 22:32:44 +08:00
|
|
|
Defaults[key1] = newFixture1
|
|
|
|
Defaults[key2] = newFixture2
|
|
|
|
Defaults[key3] = newFixture3
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults.reset(key1, key2)
|
|
|
|
XCTAssertEqual(Defaults[key1], nil)
|
|
|
|
XCTAssertEqual(Defaults[key2], nil)
|
2020-04-17 22:32:44 +08:00
|
|
|
XCTAssertEqual(Defaults[key3], newFixture3)
|
2019-09-11 15:09:12 +08:00
|
|
|
}
|
2019-10-30 19:50:24 +08:00
|
|
|
|
|
|
|
func testObserveWithLifetimeTie() {
|
|
|
|
let key = Defaults.Key<Bool>("lifetimeTie", default: false)
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
2020-04-13 12:31:54 +08:00
|
|
|
weak var observation: Defaults.Observation!
|
2020-06-07 02:14:12 +08:00
|
|
|
observation = Defaults.observe(key, options: []) { _ in
|
2019-10-30 19:50:24 +08:00
|
|
|
observation.invalidate()
|
|
|
|
expect.fulfill()
|
|
|
|
}.tieToLifetime(of: self)
|
|
|
|
|
|
|
|
Defaults[key] = true
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testObserveWithLifetimeTieManualBreak() {
|
|
|
|
let key = Defaults.Key<Bool>("lifetimeTieManualBreak", default: false)
|
|
|
|
|
2020-04-13 12:31:54 +08:00
|
|
|
weak var observation: Defaults.Observation? = Defaults.observe(key, options: []) { _ in }.tieToLifetime(of: self)
|
2019-10-30 19:50:24 +08:00
|
|
|
observation!.removeLifetimeTie()
|
|
|
|
|
|
|
|
for i in 1...10 {
|
|
|
|
if observation == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
if i == 10 {
|
|
|
|
XCTFail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|