2021-05-16 20:21:17 +08:00
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
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-10-16 20:01:16 +08:00
|
|
|
#if os(macOS)
|
2023-10-18 17:56:02 +08:00
|
|
|
typealias XColor = NSColor
|
2021-10-16 20:01:16 +08:00
|
|
|
#else
|
2023-10-18 17:56:02 +08:00
|
|
|
typealias XColor = UIColor
|
2021-10-16 20:01:16 +08:00
|
|
|
#endif
|
|
|
|
|
2021-05-16 20:21:17 +08:00
|
|
|
extension Defaults.Keys {
|
2024-09-26 05:17:26 +08:00
|
|
|
fileprivate static let hasUnicorn = Key<Bool>("swiftui_hasUnicorn", default: false, suite: suite_)
|
|
|
|
fileprivate static let user = Key<User>("swiftui_user", default: User(username: "Hank", password: "123456"), suite: suite_)
|
|
|
|
fileprivate static let setInt = Key<Set<Int>>("swiftui_setInt", default: Set(1...3), suite: suite_)
|
|
|
|
fileprivate static let color = Key<Color>("swiftui_color", default: .black, suite: suite_)
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Suite(.serialized)
|
|
|
|
final class DefaultsSwiftUITests {
|
|
|
|
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 testSwiftUIObserve() {
|
|
|
|
let view = ContentView()
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(!view.hasUnicorn)
|
|
|
|
#expect(view.user.username == "Hank")
|
|
|
|
#expect(view.setInt.count == 3)
|
|
|
|
#expect(XColor(view.color) == XColor(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)
|
2024-09-26 05:17:26 +08:00
|
|
|
|
|
|
|
#expect(view.hasUnicorn)
|
|
|
|
#expect(view.user.username == "Chen")
|
|
|
|
#expect(view.setInt == Set(1...4))
|
|
|
|
#expect(!Default(.hasUnicorn).defaultValue)
|
|
|
|
#expect(!Default(.hasUnicorn).isDefaultValue)
|
|
|
|
#expect(XColor(view.color) != XColor(Color.black))
|
|
|
|
#expect(XColor(view.color) == XColor(Color(.sRGB, red: 100, green: 100, blue: 100, opacity: 1)))
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|