Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests, move DefaultButtonStyle to TokamakCore #234

Merged
merged 1 commit into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Sources/TokamakCore/Styles/ButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public struct ButtonStyleConfiguration {
public let isPressed: Bool
}

public struct DefaultButtonStyle: ButtonStyle {
public init() {}
public func makeBody(configuration: ButtonStyleConfiguration) -> some View {
configuration.label
}
}

/// This is a helper class that works around absence of "package private" access control in Swift
public struct _ButtonStyleConfigurationProxy {
public struct Label {
Expand Down Expand Up @@ -75,7 +82,7 @@ public struct _AnyButtonStyle: ButtonStyle {

public enum _ButtonStyleKey: EnvironmentKey {
public static var defaultValue: _AnyButtonStyle {
fatalError("\(self) must have a renderer-provided default value")
_AnyButtonStyle(DefaultButtonStyle())
}
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/TokamakCore/Views/Buttons/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
/// Button("\(counter)", action: { counter += 1 })
/// }
public struct Button<Label>: View where Label: View {
let button: _Button<Label>
let implementation: _Button<Label>

public init(action: @escaping () -> (), @ViewBuilder label: () -> Label) {
button = _Button(action: action, label: label())
implementation = _Button(action: action, label: label())
}

public var body: some View {
button
implementation
}
}

Expand Down Expand Up @@ -73,6 +73,6 @@ extension Button where Label == Text {

extension Button: ParentView {
public var children: [AnyView] {
(button.label as? GroupView)?.children ?? [AnyView(button.label)]
(implementation.label as? GroupView)?.children ?? [AnyView(implementation.label)]
}
}
1 change: 0 additions & 1 deletion Sources/TokamakDOM/DOMRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ extension EnvironmentValues {
static var defaultEnvironment: Self {
var environment = EnvironmentValues()
environment[_ToggleStyleKey] = _AnyToggleStyle(DefaultToggleStyle())
environment[_ButtonStyleKey] = _AnyButtonStyle(DefaultButtonStyle())
environment._defaultAppStorage = LocalStorage.standard
_DefaultSceneStorageProvider.default = SessionStorage.standard

Expand Down
6 changes: 0 additions & 6 deletions Sources/TokamakDOM/Styles/ButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

import TokamakCore

public struct DefaultButtonStyle: ButtonStyle {
public func makeBody(configuration: ButtonStyleConfiguration) -> some View {
configuration.label
}
}

extension ButtonStyleConfiguration.Label: ViewDeferredToRenderer {
public var deferredBody: AnyView {
_ButtonStyleConfigurationProxy.Label(self).content
Expand Down
4 changes: 3 additions & 1 deletion Sources/TokamakTestRenderer/TestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public final class TestView: Target {
/// Parent `TestView` instance that owns this instance as a child
private weak var parent: TestView?

public var view: AnyView

/** Initialize a new test view. */
init<V: View>(_ view: V,
_ subviews: [TestView] = []) {
self.subviews = subviews
super.init(view)
self.view = AnyView(view)
}

/** Add a subview to this test view.
Expand Down
20 changes: 10 additions & 10 deletions Tests/TokamakTests/ReconcilerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class ReconcilerTests: XCTestCase {
)
let stack = conditional.subviews[0]
XCTAssertEqual(stack.subviews.count, 2)
XCTAssertTrue(stack.subviews[0].view.view is Button<Text>)
XCTAssertTrue(stack.subviews[0].view.view is _Button<Text>)
XCTAssertTrue(stack.subviews[1].view.view is Text)
XCTAssertEqual((stack.subviews[1].view.view as? Text)?.verbatim, "42")
}
Expand All @@ -68,7 +68,7 @@ final class ReconcilerTests: XCTestCase {
let root = renderer.rootTarget
let stack = root.subviews[0].subviews[0]

guard let button = stack.subviews[0].view.view as? Button<Text> else {
guard let button = stack.subviews[0].view.view as? _Button<Text> else {
XCTAssert(false, "counter has no button")
return
}
Expand All @@ -86,7 +86,7 @@ final class ReconcilerTests: XCTestCase {
XCTAssert(stack === newStack)
XCTAssertTrue(stack.view.view is VStack<TupleView<(Button<Text>, Text)>>)
XCTAssertEqual(stack.subviews.count, 2)
XCTAssertTrue(stack.subviews[0].view.view is Button<Text>)
XCTAssertTrue(stack.subviews[0].view.view is _Button<Text>)
XCTAssertTrue(stack.subviews[1].view.view is Text)
XCTAssertTrue(originalLabel === newStack.subviews[1])
XCTAssertEqual((stack.subviews[1].view.view as? Text)?.verbatim, "43")
Expand All @@ -102,7 +102,7 @@ final class ReconcilerTests: XCTestCase {
let root = renderer.rootTarget
let stack = root.subviews[0].subviews[0]

guard let button = stack.subviews[0].view.view as? Button<Text> else {
guard let button = stack.subviews[0].view.view as? _Button<Text> else {
XCTAssert(false, "counter has no button")
return
}
Expand All @@ -120,12 +120,12 @@ final class ReconcilerTests: XCTestCase {
XCTAssert(stack === newStack)
XCTAssertTrue(stack.view.view is VStack<TupleView<(Button<Text>, Text)>>)
XCTAssertEqual(stack.subviews.count, 2)
XCTAssertTrue(stack.subviews[0].view.view is Button<Text>)
XCTAssertTrue(stack.subviews[0].view.view is _Button<Text>)
XCTAssertTrue(stack.subviews[1].view.view is Text)
XCTAssertTrue(originalLabel === newStack.subviews[1])
XCTAssertEqual((stack.subviews[1].view.view as? Text)?.verbatim, "43")

guard let button = stack.subviews[0].view.view as? Button<Text> else {
guard let button = stack.subviews[0].view.view as? _Button<Text> else {
XCTAssert(false, "counter has no button")
return
}
Expand All @@ -139,7 +139,7 @@ final class ReconcilerTests: XCTestCase {
XCTAssert(stack === newStack)
XCTAssertTrue(stack.view.view is VStack<TupleView<(Button<Text>, Text)>>)
XCTAssertEqual(stack.subviews.count, 2)
XCTAssertTrue(stack.subviews[0].view.view is Button<Text>)
XCTAssertTrue(stack.subviews[0].view.view is _Button<Text>)
XCTAssertTrue(stack.subviews[1].view.view is Text)
XCTAssertTrue(originalLabel === newStack.subviews[1])
XCTAssertEqual((stack.subviews[1].view.view as? Text)?.verbatim, "44")
Expand All @@ -156,7 +156,7 @@ final class ReconcilerTests: XCTestCase {
let root = renderer.rootTarget

let stack = root.subviews[0].subviews[0]
guard let button = stack.subviews[0].view.view as? Button<Text> else {
guard let button = stack.subviews[0].view.view as? _Button<Text> else {
XCTAssert(false, "counter has no button")
return
}
Expand All @@ -167,15 +167,15 @@ final class ReconcilerTests: XCTestCase {

testScheduler {
// rerender completed here, schedule another one
guard let button = stack.subviews[0].view.view as? Button<Text> else {
guard let button = stack.subviews[0].view.view as? _Button<Text> else {
XCTAssert(false, "counter has no button")
return
}

button.action()

testScheduler {
guard let button = stack.subviews[0].view.view as? Button<Text> else {
guard let button = stack.subviews[0].view.view as? _Button<Text> else {
XCTAssert(false, "counter has no button")
return
}
Expand Down