Skip to content

Commit

Permalink
r
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar Otasevic committed Sep 10, 2024
1 parent 6da8ad8 commit f7accd5
Show file tree
Hide file tree
Showing 24 changed files with 42 additions and 56 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 0 additions & 27 deletions Sources/ViewInspection/Elements/AnyInspectable.swift

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import SwiftUI

@available(tvOS 16.0, *)
extension InspectableType._OnTap {
extension InspectableType._onTapGesture {
func tap() throws {
try node.one(.closureWithVoidParam).castValue(())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI

extension InspectableType._Refreshable {
extension InspectableType._refreshable {
func refresh() async throws {
try await node.one(.asyncClosure).castValue()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extension AnyInspectable {
@available(tvOS 16.0, *)
static var onTapGesture: AnyInspectable<InspectableType._onTapGesture> { .some }

static var refreshable: AnyInspectable<InspectableType._refreshable> { .some }
}
6 changes: 6 additions & 0 deletions Sources/ViewInspection/Inspection/AnyInspectable+Values.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extension AnyInspectable {
static var closure: AnyInspectable<ClosureElement<() -> Void>> { .some }
static var closureWithVoidParam: AnyInspectable<ClosureElement<(()) -> Void>> { .some }
static var asyncClosure: AnyInspectable<ClosureElement<() async -> Void>> { .some }
static var string: AnyInspectable<SameTypeElement<String>> { .some }
}
3 changes: 3 additions & 0 deletions Sources/ViewInspection/Inspection/AnyInspectable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum AnyInspectable<T: InspectionElement> {
case some
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import SwiftUI

extension InspectableType {
struct _Refreshable: ModifierDerivedElement {
struct _refreshable: ModifierDerivedElement {
let node: ReflectionNode
static func makeModifiedContent() -> Any { EmptyView().refreshable {} }
}

@available(tvOS 16.0, *)
struct _OnTap: ModifierDerivedElement {
struct _onTapGesture: ModifierDerivedElement {
let node: ReflectionNode
static func makeModifiedContent() -> Any { EmptyView().onTapGesture {} }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import SwiftUI

enum InspectableType {}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
protocol ReflectionElement {
protocol InspectionElement {
var node: ReflectionNode { get }
init(node: ReflectionNode)
static func isValid(_ node: ReflectionNode) -> Bool
}

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

protocol TypeDerivedElement: ReflectionElement {
protocol TypeDerivedElement: InspectionElement {
associatedtype RelatedType
}

Expand All @@ -35,7 +35,7 @@ struct SameBaseElement<T>: TypeDerivedElement {
static func isValid(_ node: ReflectionNode) -> Bool { node.typeInfo.baseTypename == typeInfo.baseTypename }
}

protocol ModifierDerivedElement: ReflectionElement {
protocol ModifierDerivedElement: InspectionElement {
static func makeModifiedContent() -> Any
}

Expand All @@ -58,7 +58,7 @@ struct ClosureElement<T>: CastableTypeDerivedElement {
static func isValid(_ node: ReflectionNode) -> Bool { node.typeInfo.typename.hasSuffix(typeInfo.typename) }
}

struct SomeClosureElement: ReflectionElement {
struct SomeClosureElement: InspectionElement {
let node: ReflectionNode
static func isValid(_ node: ReflectionNode) -> Bool {
node.typeInfo.typename.contains("->")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum ViewInspectionError: Error {
enum InspectionError: Error {
case wrongNumberOfItems
case wrongType
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ final class ReflectionNode: @unchecked Sendable {
// MARK: - Utilities

extension ReflectionNode {
var tree: [ReflectionNode] {
children.reduce([self]) { $0 + $1.tree }
var nodes: [ReflectionNode] {
children.reduce([self]) { $0 + $1.nodes }
}

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

@discardableResult func one<CP: ReflectionElement>(_ t: AnyInspectable<CP> = .some, _ filter: (CP) -> Bool = { _ in true }) throws -> CP {
@discardableResult func one<CP: InspectionElement>(_ t: AnyInspectable<CP> = .some, _ filter: (CP) -> Bool = { _ in true }) throws -> CP {
let items = all(t, filter)
if items.count != 1 {
throw ViewInspectionError.wrongNumberOfItems
throw InspectionError.wrongNumberOfItems
}
return items[0]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import SwiftUI
@testable import ViewInspection
import XCTest

@MainActor final class Test_OnTap: XCTestCase {
@MainActor final class Test_onTapGesture: XCTestCase {
@available(iOS 13.0, macOS 10.15, tvOS 16.0, watchOS 6.0, *)
func test_OnTap() throws {
func test_onTapGesture() throws {
var x = 0
try EmptyView().onTapGesture { x = 1 }.snap.one(.onTap).tap()
try EmptyView().onTapGesture { x = 1 }.snap.one(.onTapGesture).tap()
XCTAssert(x == 1)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import SwiftUI
@testable import ViewInspection
import XCTest

@MainActor final class Test_Refreshable: XCTestCase {
@MainActor final class Test_refreshable: XCTestCase {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
func test_Refreshable() async throws {
func test_refreshable() async throws {
var x = 0
try await EmptyView().refreshable { x = 1 }.snap.one(.refreshable).refresh()
XCTAssert(x == 1)
}
}
}

0 comments on commit f7accd5

Please sign in to comment.