2018-04-16 17:19:45 +08:00
|
|
|
import Foundation
|
2018-04-12 01:19:30 +08:00
|
|
|
import XCTest
|
2018-04-16 17:00:33 +08:00
|
|
|
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()
|
|
|
|
|
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)
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
final class DefaultsTests: XCTestCase {
|
|
|
|
override func setUp() {
|
|
|
|
super.setUp()
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults.clear()
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 18:22:10 +08:00
|
|
|
override func tearDown() {
|
|
|
|
super.setUp()
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults.clear()
|
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() {
|
2018-10-17 18:22:10 +08:00
|
|
|
let key = Defaults.OptionalKey<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 12:59:28 +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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-04-12 01:19:30 +08:00
|
|
|
func testClear() {
|
2018-10-17 18:22:10 +08:00
|
|
|
let key = Defaults.Key<Bool>("clear", default: false)
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key] = true
|
|
|
|
XCTAssertTrue(Defaults[key])
|
|
|
|
Defaults.clear()
|
|
|
|
XCTAssertFalse(Defaults[key])
|
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])
|
|
|
|
Defaults.clear(suite: customSuite)
|
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")
|
|
|
|
|
|
|
|
var observation: DefaultsObservation!
|
2019-09-11 12:59:28 +08:00
|
|
|
observation = Defaults.observe(key, options: [.old, .new]) { 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testObserveOptionalKey() {
|
|
|
|
let key = Defaults.OptionalKey<Bool>("observeOptionalKey")
|
|
|
|
let expect = expectation(description: "Observation closure being called")
|
|
|
|
|
|
|
|
var observation: DefaultsObservation!
|
2019-09-11 12:59:28 +08:00
|
|
|
observation = Defaults.observe(key, options: [.old, .new]) { 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
var observation: DefaultsObservation!
|
2019-09-11 12:59:28 +08:00
|
|
|
observation = Defaults.observe(key, options: [.old, .new]) { 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")
|
|
|
|
|
|
|
|
var observation: DefaultsObservation!
|
2019-09-11 12:59:28 +08:00
|
|
|
observation = Defaults.observe(key, options: [.old, .new]) { 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
|
|
|
}
|
2019-09-11 15:09:12 +08:00
|
|
|
|
|
|
|
func testResetKey() {
|
|
|
|
let defaultString1 = "foo1"
|
|
|
|
let defaultString2 = "foo2"
|
|
|
|
let newString1 = "bar1"
|
|
|
|
let newString2 = "bar2"
|
|
|
|
let key1 = Defaults.Key<String>("key1", default: defaultString1)
|
|
|
|
let key2 = Defaults.Key<String>("key2", default: defaultString2)
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key1] = newString1
|
|
|
|
Defaults[key2] = newString2
|
|
|
|
Defaults.reset(key1)
|
|
|
|
XCTAssertEqual(Defaults[key1], defaultString1)
|
|
|
|
XCTAssertEqual(Defaults[key2], newString2)
|
2019-09-11 15:09:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testResetKeyArray() {
|
|
|
|
let defaultString1 = "foo1"
|
|
|
|
let defaultString2 = "foo2"
|
|
|
|
let defaultString3 = "foo3"
|
|
|
|
let newString1 = "bar1"
|
|
|
|
let newString2 = "bar2"
|
|
|
|
let newString3 = "bar3"
|
|
|
|
let key1 = Defaults.Key<String>("akey1", default: defaultString1)
|
|
|
|
let key2 = Defaults.Key<String>("akey2", default: defaultString2)
|
|
|
|
let key3 = Defaults.Key<String>("akey3", default: defaultString3)
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key1] = newString1
|
|
|
|
Defaults[key2] = newString2
|
|
|
|
Defaults[key3] = newString3
|
|
|
|
Defaults.reset(key1, key2)
|
|
|
|
XCTAssertEqual(Defaults[key1], defaultString1)
|
|
|
|
XCTAssertEqual(Defaults[key2], defaultString2)
|
|
|
|
XCTAssertEqual(Defaults[key3], newString3)
|
2019-09-11 15:09:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testResetOptionalKey() {
|
|
|
|
let newString1 = "bar1"
|
|
|
|
let newString2 = "bar2"
|
|
|
|
let key1 = Defaults.OptionalKey<String>("optionalKey1")
|
|
|
|
let key2 = Defaults.OptionalKey<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-09-11 15:09:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testResetOptionalKeyArray() {
|
|
|
|
let newString1 = "bar1"
|
|
|
|
let newString2 = "bar2"
|
|
|
|
let newString3 = "bar3"
|
|
|
|
let key1 = Defaults.OptionalKey<String>("aoptionalKey1")
|
|
|
|
let key2 = Defaults.OptionalKey<String>("aoptionalKey2")
|
|
|
|
let key3 = Defaults.OptionalKey<String>("aoptionalKey3")
|
2019-09-11 12:59:28 +08:00
|
|
|
Defaults[key1] = newString1
|
|
|
|
Defaults[key2] = newString2
|
|
|
|
Defaults[key3] = newString3
|
|
|
|
Defaults.reset(key1, key2)
|
|
|
|
XCTAssertEqual(Defaults[key1], nil)
|
|
|
|
XCTAssertEqual(Defaults[key2], nil)
|
|
|
|
XCTAssertEqual(Defaults[key3], newString3)
|
2019-09-11 15:09:12 +08:00
|
|
|
}
|
2018-04-12 01:19:30 +08:00
|
|
|
}
|