Skip to content

Commit

Permalink
renamed something
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar Otasevic committed Sep 8, 2024
1 parent ff0ecf9 commit 687dffc
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 65 deletions.
12 changes: 6 additions & 6 deletions Sources/ViewInspection/Elements/InspectableModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ enum InspectableModifier {

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension InspectableModifier._Refreshable {
func doRefresh() async {
await node.one(.asyncClosure)?.castValue()
func doRefresh() async throws {
try await node.one(.asyncClosure).castValue()
}
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension InspectableModifier._Task {
func doTask() async {
await node.one(.asyncClosure)?.castValue()
func doTask() async throws {
try await node.one(.asyncClosure).castValue()
}
}

@available(iOS 13.0, macOS 10.15, tvOS 16.0, watchOS 6.0, *)
extension InspectableModifier._OnTap {
func doTap() {
node.one(.closureWithVoidParam)?.castValue(())
func doTap() throws {
try node.one(.closureWithVoidParam).castValue(())
}
}
48 changes: 28 additions & 20 deletions Sources/ViewInspection/Elements/InspectableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,26 +413,34 @@ enum InspectableView {
}

extension InspectableView._Button {
func tap() {
node.one(.closure)?.castValue()
func tap() throws {
try node.one(.closure).castValue()
}
}

extension InspectableView._Text {
var string: String? {
node.one(.String)?.castValue
var string: String {
get throws {
try node.one(.String).castValue
}
}
}

extension InspectableView._Image {
var name: String? {
node.all(.String).first { $0.node.label == "name" }?.castValue
var name: String {
get throws {
try node.one(.String) {
$0.node.label == "name"
}.castValue
}
}
}

extension InspectableView._TextField {
var text: Binding<String> {
node.one(.Binding)?.tryCast() ?? .constant("")
get throws {
try node.one(.Binding).tryCast()
}
}
}

Expand All @@ -443,21 +451,21 @@ extension InspectableView._Toggle {
}

var isOn: Binding<Bool> {
guard let binding = node.one(.Binding) else {
return .constant(true)
}
if let boolBinding = binding.node.object as? Binding<Bool> {
return boolBinding
}
let dummyBinding = CastingUtils.memoryCast(binding.node.object, Binding<DummyEnum>.self)
return Binding {
dummyBinding.wrappedValue == .case0
} set: {
dummyBinding.wrappedValue = $0 ? .case0 : .case1
get throws {
let binding = try node.one(.Binding)
if let boolBinding = binding.node.object as? Binding<Bool> {
return boolBinding
}
let dummyBinding = CastingUtils.memoryCast(binding.node.object, Binding<DummyEnum>.self)
return Binding {
dummyBinding.wrappedValue == .case0
} set: {
dummyBinding.wrappedValue = $0 ? .case0 : .case1
}
}
}

func toggle() {
isOn.wrappedValue.toggle()
func toggle() throws {
try isOn.wrappedValue.toggle()
}
}
7 changes: 5 additions & 2 deletions Sources/ViewInspection/Elements/ReflectionElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ protocol ReflectionElement {
}

extension ReflectionElement {
func tryCast<T>(_ t: T.Type = T.self) -> T? {
node.object as? T
func tryCast<T>(_ t: T.Type = T.self) throws -> T {
guard let obj = node.object as? T else {
throw ViewInspectionError.wrongType
}
return obj
}
}

Expand Down
18 changes: 11 additions & 7 deletions Sources/ViewInspection/Reflection/ReflectionNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ final class ReflectionNode: @unchecked Sendable {
// MARK: - Utilities

extension ReflectionNode {
var allNodes: [ReflectionNode] {
private var allNodes: [ReflectionNode] {
children.reduce([self]) { $0 + $1.allNodes }
}
func all<CP: ReflectionElement>(_ t: Inspectable<CP> = .some) -> [CP] {
allNodes.filter(CP.isValid).map(CP.init)

func all<CP: ReflectionElement>(_ t: Inspectable<CP> = .some, _ filter: (CP) -> Bool = { _ in true }) -> [CP] {
allNodes.filter(CP.isValid).map(CP.init).filter(filter)
}

func one<CP: ReflectionElement>(_ t: Inspectable<CP> = .some) -> CP? {
all(t).first

func one<CP: ReflectionElement>(_ t: Inspectable<CP> = .some, _ filter: (CP) -> Bool = { _ in true }) throws -> CP {
let items = all(t, filter)
if items.count != 1 {
throw ViewInspectionError.wrongNumberOfItems
}
return items[0]
}
}
2 changes: 1 addition & 1 deletion Sources/ViewInspection/ViewInspection.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI

extension View {
var inspect: ReflectionNode {
var snap: ReflectionNode {
ReflectionNode(object: self)
}
}
4 changes: 4 additions & 0 deletions Sources/ViewInspection/ViewInspectionError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum ViewInspectionError: Error {
case wrongNumberOfItems
case wrongType
}
14 changes: 7 additions & 7 deletions Tests/ViewInspectionTests/InteractiveViewElementsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import XCTest
final class InteractiveViewElementsTests: XCTestCase {}

@MainActor extension InteractiveViewElementsTests {
func test_Toggle() {
func test_Toggle() throws {
let b = Binding<Bool>.variable(false)
Toggle("", isOn: b).inspect.one(.Toggle)?.toggle()
try Toggle("", isOn: b).snap.one(.Toggle).toggle()
XCTAssertEqual(b.wrappedValue, true)
}

func test_Button() {
func test_Button() throws {
var value = 0
Button("") { value = 1 }.inspect.one(.Button)?.tap()
try Button("") { value = 1 }.snap.one(.Button).tap()
XCTAssertEqual(value, 1)
}

func test_TextField() {
func test_TextField() throws {
let textFieldBinding = Binding<String>.variable("")
let textField = TextField("", text: textFieldBinding).inspect.one(.TextField)
let textField = try TextField("", text: textFieldBinding).snap.one(.TextField)
XCTAssertEqual(textFieldBinding.wrappedValue, "")
textField?.text.wrappedValue = "a"
try textField.text.wrappedValue = "a"
XCTAssertEqual(textFieldBinding.wrappedValue, "a")
}
}
12 changes: 6 additions & 6 deletions Tests/ViewInspectionTests/ModifierElementsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ final class ModifierElementsTests: XCTestCase {}

@MainActor extension ModifierElementsTests {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
func test_Refreshable() async {
func test_Refreshable() async throws {
var x = 0
await EmptyView().refreshable { x = 1 }.inspect.one(.Refreshable)?.doRefresh()
try await EmptyView().refreshable { x = 1 }.snap.one(.Refreshable).doRefresh()
XCTAssert(x == 1)
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
func test_Task() async {
func test_Task() async throws {
var x = 0
await EmptyView().task { x = 1 }.inspect.one(.Task)?.doTask()
try await EmptyView().task { x = 1 }.snap.one(.Task).doTask()
XCTAssert(x == 1)
}

@available(tvOS 16.0, *)
func test_OnTap() {
func test_OnTap() throws {
var x = 0
EmptyView().onTapGesture { x = 1 }.inspect.one(.OnTap)?.doTap()
try EmptyView().onTapGesture { x = 1 }.snap.one(.OnTap).doTap()
XCTAssert(x == 1)
}
}
6 changes: 3 additions & 3 deletions Tests/ViewInspectionTests/PropertyWrappersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class PropertyWrappersTests: XCTestCase {}
@Environment(\.colorScheme) private var colorScheme
let body = EmptyView()
}
let t = Dummy().inspect.all(.Environment)
let t = Dummy().snap.all(.Environment)
XCTAssertEqual(t.count, 2)
XCTAssert(t[1].node.parent === t[0].node)
}
Expand All @@ -20,14 +20,14 @@ final class PropertyWrappersTests: XCTestCase {}
@State private var x = 0
let body = EmptyView()
}
XCTAssertEqual(Dummy().inspect.all(.State).count, 1)
XCTAssertEqual(Dummy().snap.all(.State).count, 1)
}

func testBinding() {
struct Dummy: View {
@Binding var x: Int
let body = EmptyView()
}
XCTAssertEqual(Dummy(x: .constant(1)).inspect.all(.Binding).count, 1)
XCTAssertEqual(Dummy(x: .constant(1)).snap.all(.Binding).count, 1)
}
}
26 changes: 13 additions & 13 deletions Tests/ViewInspectionTests/StaticViewElementsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import XCTest
final class StaticViewElementsTests: XCTestCase {}

@MainActor extension StaticViewElementsTests {
func test_Text() {
func test_Text() throws {
XCTAssertEqual(
Text("a").inspect.one(.Text)?.string,
try Text("a").snap.one(.Text).string,
"a"
)
}

func test_Image() {
let ref = Image(systemName: "circle").inspect.one(.Image)
XCTAssertEqual(ref?.name, "circle")
func test_Image() throws {
let ref = try Image(systemName: "circle").snap.one(.Image)
XCTAssertEqual(try ref.name, "circle")
}

@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
func test_NavigationStack() {
func test_NavigationStack() throws {
XCTAssertEqual(
NavigationStack { Text("a") }
.inspect
.one(.NavigationStack)?
try NavigationStack { Text("a") }
.snap
.one(.NavigationStack)
.node
.one(.Text)?
.one(.Text)
.string,
"a"
)
Expand All @@ -33,7 +33,7 @@ final class StaticViewElementsTests: XCTestCase {}
func test_GeometryReader() {
XCTAssertEqual(
GeometryReader { _ in }
.inspect
.snap
.all(.GeometryReader)
.count,
1
Expand All @@ -45,7 +45,7 @@ final class StaticViewElementsTests: XCTestCase {}
ForEach(Array(0 ... 1), id: \.self) {
Text($0.description)
}
.inspect
.snap
.all(.ForEach)
.count,
2
Expand All @@ -54,7 +54,7 @@ final class StaticViewElementsTests: XCTestCase {}
ForEach(Array(0 ... 1), id: \.self) {
Text($0.description)
}
.inspect
.snap
.all(.Text)
.count,
0
Expand Down

0 comments on commit 687dffc

Please sign in to comment.