Defaults/Tests/DefaultsTests/DefaultsTests.swift

73 lines
1.7 KiB
Swift
Raw Normal View History

import Foundation
2018-04-12 01:19:30 +08:00
import XCTest
import Defaults
2018-04-12 01:19:30 +08:00
let fixtureUrl = URL(string: "https://sindresorhus.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"
}
extension Defaults.Keys {
static let key = Defaults.Key<Bool>("key", default: false)
static let url = Defaults.Key<URL>("url", default: fixtureUrl)
static let `enum` = Defaults.Key<FixtureEnum>("enum", default: .oneHour)
2018-04-16 02:09:53 +08:00
static let data = Defaults.Key<Data>("data", default: Data(bytes: []))
2018-04-12 01:19:30 +08:00
}
final class DefaultsTests: XCTestCase {
override func setUp() {
super.setUp()
defaults.clear()
}
func testKey() {
2018-04-12 01:19:30 +08:00
let key = Defaults.Key<Bool>("key", default: false)
XCTAssertFalse(UserDefaults.standard[key])
UserDefaults.standard[key] = true
XCTAssertTrue(UserDefaults.standard[key])
}
func testOptionalKey() {
let key = Defaults.OptionalKey<Bool>("key")
XCTAssertNil(UserDefaults.standard[key])
UserDefaults.standard[key] = true
XCTAssertTrue(UserDefaults.standard[key]!)
}
func testKeys() {
XCTAssertFalse(defaults[.key])
defaults[.key] = true
XCTAssertTrue(defaults[.key])
}
func testUrlType() {
XCTAssertEqual(defaults[.url], fixtureUrl)
let newUrl = URL(string: "https://twitter.com")!
defaults[.url] = newUrl
XCTAssertEqual(defaults[.url], newUrl)
}
func testEnumType() {
XCTAssertEqual(defaults[.enum], FixtureEnum.oneHour)
}
2018-04-16 02:09:53 +08:00
func testDataType() {
XCTAssertEqual(defaults[.data], Data(bytes: []))
let newData = Data(bytes: [0xFF])
defaults[.data] = newData
XCTAssertEqual(defaults[.data], newData)
}
2018-04-12 01:19:30 +08:00
func testClear() {
defaults[.key] = true
XCTAssertTrue(defaults[.key])
defaults.clear()
XCTAssertFalse(defaults[.key])
}
}