2021-05-16 20:21:17 +08:00
|
|
|
import XCTest
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
import Defaults
|
|
|
|
|
2021-10-16 20:01:16 +08:00
|
|
|
#if os(macOS)
|
|
|
|
typealias NativeColor = NSColor
|
|
|
|
#else
|
|
|
|
typealias NativeColor = UIColor
|
|
|
|
#endif
|
|
|
|
|
|
|
|
@available(macOS 11.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
|
2021-05-16 20:21:17 +08:00
|
|
|
extension Defaults.Keys {
|
|
|
|
fileprivate static let hasUnicorn = Key<Bool>("swiftui_hasUnicorn", default: false)
|
|
|
|
fileprivate static let user = Key<User>("swiftui_user", default: User(username: "Hank", password: "123456"))
|
|
|
|
fileprivate static let setInt = Key<Set<Int>>("swiftui_setInt", default: Set(1...3))
|
2021-10-16 20:01:16 +08:00
|
|
|
fileprivate static let color = Key<Color>("swiftui_color", default: .black)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2021-10-16 20:01:16 +08:00
|
|
|
@available(macOS 11.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
|
2021-05-16 20:21:17 +08:00
|
|
|
struct ContentView: View {
|
|
|
|
@Default(.hasUnicorn) var hasUnicorn
|
|
|
|
@Default(.user) var user
|
|
|
|
@Default(.setInt) var setInt
|
2021-10-16 20:01:16 +08:00
|
|
|
@Default(.color) var color
|
2021-05-16 20:21:17 +08:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Text("User \(user.username) has Unicorn: \(String(hasUnicorn))")
|
2021-10-16 20:01:16 +08:00
|
|
|
.foregroundColor(color)
|
2021-05-16 20:21:17 +08:00
|
|
|
Toggle("Toggle Unicorn", isOn: $hasUnicorn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 20:01:16 +08:00
|
|
|
@available(macOS 11.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
|
2021-05-16 20:21:17 +08:00
|
|
|
final class DefaultsSwiftUITests: XCTestCase {
|
|
|
|
override func setUp() {
|
|
|
|
super.setUp()
|
|
|
|
Defaults.removeAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDown() {
|
|
|
|
super.tearDown()
|
|
|
|
Defaults.removeAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSwiftUIObserve() {
|
|
|
|
let view = ContentView()
|
|
|
|
XCTAssertFalse(view.hasUnicorn)
|
|
|
|
XCTAssertEqual(view.user.username, "Hank")
|
|
|
|
XCTAssertEqual(view.setInt.count, 3)
|
2021-10-16 20:01:16 +08:00
|
|
|
XCTAssertEqual(NativeColor(view.color), NativeColor(Color.black))
|
2021-05-16 20:21:17 +08:00
|
|
|
view.user = User(username: "Chen", password: "123456")
|
|
|
|
view.hasUnicorn.toggle()
|
|
|
|
view.setInt.insert(4)
|
2021-10-16 20:01:16 +08:00
|
|
|
view.color = Color(.sRGB, red: 100, green: 100, blue: 100, opacity: 1)
|
2021-05-16 20:21:17 +08:00
|
|
|
XCTAssertTrue(view.hasUnicorn)
|
|
|
|
XCTAssertEqual(view.user.username, "Chen")
|
|
|
|
XCTAssertEqual(view.setInt, Set(1...4))
|
2021-06-18 14:15:13 +08:00
|
|
|
XCTAssertFalse(Default(.hasUnicorn).defaultValue)
|
|
|
|
XCTAssertFalse(Default(.hasUnicorn).isDefaultValue)
|
2021-10-16 20:01:16 +08:00
|
|
|
XCTAssertNotEqual(NativeColor(view.color), NativeColor(Color.black))
|
|
|
|
XCTAssertEqual(NativeColor(view.color), NativeColor(Color(.sRGB, red: 100, green: 100, blue: 100, opacity: 1)))
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|