Skip to content

Commit

Permalink
Follow up tweaks for #239 and #242
Browse files Browse the repository at this point in the history
  • Loading branch information
nalexn committed May 2, 2023
1 parent c58271d commit 7aafe74
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 32 deletions.
34 changes: 14 additions & 20 deletions Sources/ViewInspector/SwiftUI/Picker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,21 @@ public extension InspectableView where View == ViewType.Picker {

func select<SelectionValue>(value: SelectionValue) throws where SelectionValue: Hashable {
try guardIsResponsive()
var bindings = try Inspector.attribute(path: "selection", value: content.view)
if let single = bindings as? Binding<SelectionValue> {
bindings = [single]
}
let typeName = Inspector.typeName(value: bindings)
guard let casted = bindings as? [Binding<SelectionValue>] else {
var endIndex = typeName.index(before: typeName.endIndex)
if typeName.hasPrefix("Array") {
endIndex = typeName.index(before: endIndex)
}
let expected = typeName[..<endIndex]
.replacingOccurrences(of: "Array<Binding<", with: "")
.replacingOccurrences(of: "Binding<", with: "")
let factual = Inspector.typeName(type: SelectionValue.self)
throw InspectionError
.notSupported("select(value:) expects a value of type \(expected) but received \(factual)")
let bindings = try valueBindings(SelectionValue.self)
bindings.forEach { $0.wrappedValue = value }
}

func selectedValue<SelectionValue>(_ type: SelectionValue.Type) throws -> SelectionValue {
let bindings = try valueBindings(SelectionValue.self)
guard let value = bindings.first else {
throw InspectionError.attributeNotFound(
label: "binding", type: Inspector.typeName(type: SelectionValue.self))
}
casted.forEach { $0.wrappedValue = value }
return value.wrappedValue
}

func selectedValue<SelectionValue>() throws -> SelectionValue {
private func valueBindings<SelectionValue>(_ type: SelectionValue.Type, caller: StaticString = #function
) throws -> [Binding<SelectionValue>] {
var bindings = try Inspector.attribute(path: "selection", value: content.view)
if let single = bindings as? Binding<SelectionValue> {
bindings = [single]
Expand All @@ -92,9 +86,9 @@ public extension InspectableView where View == ViewType.Picker {
.replacingOccurrences(of: "Binding<", with: "")
let factual = Inspector.typeName(type: SelectionValue.self)
throw InspectionError
.notSupported("selectedValue() expected a value of type \(expected) but received \(factual)")
.notSupported("\(caller) expected a value of type \(expected) but received \(factual)")
}
return casted.first!.wrappedValue
return casted
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/ViewInspector/SwiftUI/PopoverContent.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import SwiftUI

@available(macOS 10.15, *)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
internal extension ViewType {
struct PopoverContent { }
}

// MARK: - Content Extraction

@available(macOS 10.15, *)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
extension ViewType.PopoverContent: SingleViewContent {

static func child(_ content: Content) throws -> Content {
Expand Down
8 changes: 4 additions & 4 deletions Tests/ViewInspectorTests/SwiftUI/PickerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class PickerTests: XCTestCase {
}
XCTAssertNil(binding.wrappedValue)
XCTAssertThrows(try view.inspect().picker().select(value: 1),
"select(value:) expects a value of type Optional<Int> but received Int")
"select(value:) expected a value of type Optional<Int> but received Int")
try view.inspect().picker().select(value: Int?(1))
XCTAssertEqual(binding.wrappedValue, 1)
}
Expand Down Expand Up @@ -104,9 +104,9 @@ final class PickerTests: XCTestCase {
}
try view.inspect().picker().select(value: "Second Option")

XCTAssertThrows(try view.inspect().picker().selectedValue() as Int,
"selectedValue() expected a value of type String but received Int")
XCTAssertEqual("Second Option", try view.inspect().picker().selectedValue())
XCTAssertThrows(try view.inspect().picker().selectedValue(Int.self),
"selectedValue(_:) expected a value of type String but received Int")
XCTAssertEqual("Second Option", try view.inspect().picker().selectedValue(String.self))
}
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/ViewInspectorTests/SwiftUI/PopoverContentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import XCTest

#if os(macOS)

fileprivate protocol AnyHostingView {
private protocol AnyHostingView {
var anyRootView: AnyView { get }
}

extension NSHostingView: AnyHostingView {
fileprivate var anyRootView: AnyView { AnyView(rootView) }
}

fileprivate class TestModel: ObservableObject {
private class TestModel: ObservableObject {
@Published var showPopover = false
}

fileprivate struct WindowRootView: View {
private struct WindowRootView: View {
@ObservedObject var model: TestModel

var body: some View {
Expand Down
13 changes: 12 additions & 1 deletion ViewInspector.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
5250764D265185A800ADE4E7 /* ActionSheetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5250764C265185A800ADE4E7 /* ActionSheetTests.swift */; };
52510DCE25E1486200A84CB8 /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52510DC825E1485300A84CB8 /* SwiftUI.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
52510DD325E1487200A84CB8 /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52510DC825E1485300A84CB8 /* SwiftUI.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
527C81EA2A016DCD00BCA8BC /* PopoverContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 527C81E92A016DCD00BCA8BC /* PopoverContent.swift */; };
529633862969672A00EC6C81 /* NavigationStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 529633852969672A00EC6C81 /* NavigationStack.swift */; };
52963388296967ED00EC6C81 /* NavigationStackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52963387296967ED00EC6C81 /* NavigationStackTests.swift */; };
5296338A29697C7D00EC6C81 /* NavigationSplitView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5296338929697C7D00EC6C81 /* NavigationSplitView.swift */; };
Expand Down Expand Up @@ -329,6 +330,7 @@
5250764A2651800800ADE4E7 /* ActionSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionSheet.swift; sourceTree = "<group>"; };
5250764C265185A800ADE4E7 /* ActionSheetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionSheetTests.swift; sourceTree = "<group>"; };
52510DC825E1485300A84CB8 /* SwiftUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftUI.framework; path = System/Library/Frameworks/SwiftUI.framework; sourceTree = SDKROOT; };
527C81E92A016DCD00BCA8BC /* PopoverContent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PopoverContent.swift; sourceTree = "<group>"; };
529633852969672A00EC6C81 /* NavigationStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationStack.swift; sourceTree = "<group>"; };
52963387296967ED00EC6C81 /* NavigationStackTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationStackTests.swift; sourceTree = "<group>"; };
5296338929697C7D00EC6C81 /* NavigationSplitView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationSplitView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -702,6 +704,7 @@
F6684BF123AA554500DECCB3 /* PasteButton.swift */,
F6D933B62385EDCB00358E0E /* Picker.swift */,
F605E76425606AA300DE9B07 /* Popover.swift */,
527C81E92A016DCD00BCA8BC /* PopoverContent.swift */,
F6C15AA2254DA270000240F1 /* ProgressView.swift */,
F6684BFD23AA863400DECCB3 /* RadialGradient.swift */,
52A4A7632621F4AE0063E00B /* Overlay.swift */,
Expand Down Expand Up @@ -1001,8 +1004,9 @@
OBJ_1 /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = YES;
LastSwiftMigration = 9999;
LastUpgradeCheck = 1400;
LastUpgradeCheck = 1430;
TargetAttributes = {
"ViewInspector::ViewInspector" = {
LastSwiftMigration = 1320;
Expand Down Expand Up @@ -1143,6 +1147,7 @@
5250764B2651800800ADE4E7 /* ActionSheet.swift in Sources */,
F60EEBCD2382EED0007DB53A /* Inspector.swift in Sources */,
F64105C4257BF9A70033D82D /* ViewSearch.swift in Sources */,
527C81EA2A016DCD00BCA8BC /* PopoverContent.swift in Sources */,
F6D933BF2385F5CB00358E0E /* TextField.swift in Sources */,
F6DFD87C2382FB510028E84D /* EditButton.swift in Sources */,
52A3B51A26591F59001FE17E /* Sheet.swift in Sources */,
Expand Down Expand Up @@ -1433,6 +1438,7 @@
DEFINES_MODULE = YES;
DEVELOPMENT_ASSET_PATHS = "";
ENABLE_BITCODE = NO;
ENABLE_MODULE_VERIFIER = YES;
ENABLE_TESTABILITY = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -1444,6 +1450,8 @@
"$(inherited)",
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
);
MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++";
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++14";
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand All @@ -1466,6 +1474,7 @@
DEFINES_MODULE = YES;
DEVELOPMENT_ASSET_PATHS = "";
ENABLE_BITCODE = NO;
ENABLE_MODULE_VERIFIER = YES;
ENABLE_TESTABILITY = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -1477,6 +1486,8 @@
"$(inherited)",
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
);
MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++";
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++14";
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1400"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion readiness.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ This document reflects the current status of the [ViewInspector](https://github.
|:white_check_mark:| Optional | `contained view` |
|:white_check_mark:| OutlineGroup | `leaf view`, `source data` |
|:white_check_mark:| PasteButton | `supportedTypes: [String]`|
|:white_check_mark:| Picker | `contained view`, `label view`, `select(value: Hashable)` |
|:white_check_mark:| Picker | `contained view`, `label view`, `select(value: Hashable)`, `selectedValue()` |
|:white_check_mark:| Popover | `contained view`, `attachmentAnchor: PopoverAttachmentAnchor`, `arrowEdge: Edge`, `dismiss()` |
|:white_check_mark:| PrimitiveButtonStyleConfiguration.Label | |
|:white_check_mark:| ProgressView | `label view`, `currentValueLabel view`, `fractionCompleted: Double?`, `progress: Progress` |
Expand Down

0 comments on commit 7aafe74

Please sign in to comment.