From 7aafe74d8f3f46c07fffd9fb42bce540b7b3c911 Mon Sep 17 00:00:00 2001 From: Alexey Naumov Date: Tue, 2 May 2023 22:24:04 +0600 Subject: [PATCH] Follow up tweaks for #239 and #242 --- Sources/ViewInspector/SwiftUI/Picker.swift | 34 ++++++++----------- .../SwiftUI/PopoverContent.swift | 4 +-- .../SwiftUI/PickerTests.swift | 8 ++--- .../SwiftUI/PopoverContentTests.swift | 6 ++-- ViewInspector.xcodeproj/project.pbxproj | 13 ++++++- .../xcschemes/ViewInspector-Package.xcscheme | 2 +- readiness.md | 2 +- 7 files changed, 37 insertions(+), 32 deletions(-) diff --git a/Sources/ViewInspector/SwiftUI/Picker.swift b/Sources/ViewInspector/SwiftUI/Picker.swift index 3ee5c98b..e663a768 100644 --- a/Sources/ViewInspector/SwiftUI/Picker.swift +++ b/Sources/ViewInspector/SwiftUI/Picker.swift @@ -56,27 +56,21 @@ public extension InspectableView where View == ViewType.Picker { func select(value: SelectionValue) throws where SelectionValue: Hashable { try guardIsResponsive() - var bindings = try Inspector.attribute(path: "selection", value: content.view) - if let single = bindings as? Binding { - bindings = [single] - } - let typeName = Inspector.typeName(value: bindings) - guard let casted = bindings as? [Binding] else { - var endIndex = typeName.index(before: typeName.endIndex) - if typeName.hasPrefix("Array") { - endIndex = typeName.index(before: endIndex) - } - let expected = typeName[..(_ 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() throws -> SelectionValue { + private func valueBindings(_ type: SelectionValue.Type, caller: StaticString = #function + ) throws -> [Binding] { var bindings = try Inspector.attribute(path: "selection", value: content.view) if let single = bindings as? Binding { bindings = [single] @@ -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 } } diff --git a/Sources/ViewInspector/SwiftUI/PopoverContent.swift b/Sources/ViewInspector/SwiftUI/PopoverContent.swift index c9d92664..e581975e 100644 --- a/Sources/ViewInspector/SwiftUI/PopoverContent.swift +++ b/Sources/ViewInspector/SwiftUI/PopoverContent.swift @@ -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 { diff --git a/Tests/ViewInspectorTests/SwiftUI/PickerTests.swift b/Tests/ViewInspectorTests/SwiftUI/PickerTests.swift index 16b19223..dcd8a5d8 100644 --- a/Tests/ViewInspectorTests/SwiftUI/PickerTests.swift +++ b/Tests/ViewInspectorTests/SwiftUI/PickerTests.swift @@ -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 but received Int") + "select(value:) expected a value of type Optional but received Int") try view.inspect().picker().select(value: Int?(1)) XCTAssertEqual(binding.wrappedValue, 1) } @@ -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)) } } diff --git a/Tests/ViewInspectorTests/SwiftUI/PopoverContentTests.swift b/Tests/ViewInspectorTests/SwiftUI/PopoverContentTests.swift index c01296eb..cc116524 100644 --- a/Tests/ViewInspectorTests/SwiftUI/PopoverContentTests.swift +++ b/Tests/ViewInspectorTests/SwiftUI/PopoverContentTests.swift @@ -6,7 +6,7 @@ import XCTest #if os(macOS) -fileprivate protocol AnyHostingView { +private protocol AnyHostingView { var anyRootView: AnyView { get } } @@ -14,11 +14,11 @@ 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 { diff --git a/ViewInspector.xcodeproj/project.pbxproj b/ViewInspector.xcodeproj/project.pbxproj index 34a6704c..e9f44717 100644 --- a/ViewInspector.xcodeproj/project.pbxproj +++ b/ViewInspector.xcodeproj/project.pbxproj @@ -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 */; }; @@ -329,6 +330,7 @@ 5250764A2651800800ADE4E7 /* ActionSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionSheet.swift; sourceTree = ""; }; 5250764C265185A800ADE4E7 /* ActionSheetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionSheetTests.swift; sourceTree = ""; }; 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 = ""; }; 529633852969672A00EC6C81 /* NavigationStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationStack.swift; sourceTree = ""; }; 52963387296967ED00EC6C81 /* NavigationStackTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationStackTests.swift; sourceTree = ""; }; 5296338929697C7D00EC6C81 /* NavigationSplitView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationSplitView.swift; sourceTree = ""; }; @@ -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 */, @@ -1001,8 +1004,9 @@ OBJ_1 /* Project object */ = { isa = PBXProject; attributes = { + BuildIndependentTargetsInParallel = YES; LastSwiftMigration = 9999; - LastUpgradeCheck = 1400; + LastUpgradeCheck = 1430; TargetAttributes = { "ViewInspector::ViewInspector" = { LastSwiftMigration = 1320; @@ -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 */, @@ -1433,6 +1438,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_ASSET_PATHS = ""; ENABLE_BITCODE = NO; + ENABLE_MODULE_VERIFIER = YES; ENABLE_TESTABILITY = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -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)"; @@ -1466,6 +1474,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_ASSET_PATHS = ""; ENABLE_BITCODE = NO; + ENABLE_MODULE_VERIFIER = YES; ENABLE_TESTABILITY = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -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)"; diff --git a/ViewInspector.xcodeproj/xcshareddata/xcschemes/ViewInspector-Package.xcscheme b/ViewInspector.xcodeproj/xcshareddata/xcschemes/ViewInspector-Package.xcscheme index 46865d65..de479fca 100644 --- a/ViewInspector.xcodeproj/xcshareddata/xcschemes/ViewInspector-Package.xcscheme +++ b/ViewInspector.xcodeproj/xcshareddata/xcschemes/ViewInspector-Package.xcscheme @@ -1,6 +1,6 @@