Skip to content

Commit 9843e3f

Browse files
authored
Add Map introspection (#288)
1 parent fb2f27d commit 9843e3f

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changelog
33

44
## master
55

6+
- Added: `Map` introspection (#288)
67
- Added: advanced range-based platform version predicates (#285)
78
- Documentation: generate docs for extensions (#282)
89
- Infrastructure: set up `tea` for CI and local environments (#276)

Sources/ViewTypes/Map.swift

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import SwiftUI
2+
3+
/// An abstract representation of the `Map` type in SwiftUI.
4+
///
5+
/// ### iOS
6+
///
7+
/// ```swift
8+
/// struct ContentView: View {
9+
/// @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
10+
///
11+
/// var body: some View {
12+
/// Map(coordinateRegion: $region)
13+
/// .introspect(.map, on: .iOS(.v14, .v15, .v16, .v17)) {
14+
/// print(type(of: $0)) // MKMapView
15+
/// }
16+
/// }
17+
/// }
18+
/// ```
19+
///
20+
/// ### tvOS
21+
///
22+
/// ```swift
23+
/// struct ContentView: View {
24+
/// @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
25+
///
26+
/// var body: some View {
27+
/// Map(coordinateRegion: $region)
28+
/// .introspect(.map, on: .tvOS(.v14, .v15, .v16, .v17)) {
29+
/// print(type(of: $0)) // MKMapView
30+
/// }
31+
/// }
32+
/// }
33+
/// ```
34+
///
35+
/// ### macOS
36+
///
37+
/// ```swift
38+
/// struct ContentView: View {
39+
/// @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
40+
///
41+
/// var body: some View {
42+
/// Map(coordinateRegion: $region)
43+
/// .introspect(.map, on: .macOS(.v11, .v12, .v13, .v14)) {
44+
/// print(type(of: $0)) // MKMapView
45+
/// }
46+
/// }
47+
/// }
48+
/// ```
49+
public struct MapType: IntrospectableViewType {}
50+
51+
#if canImport(MapKit)
52+
import MapKit
53+
54+
extension IntrospectableViewType where Self == MapType {
55+
public static var map: Self { .init() }
56+
}
57+
58+
extension iOSViewVersion<MapType, MKMapView> {
59+
@available(*, unavailable, message: "Map isn't available on iOS 13")
60+
public static let v13 = Self.unavailable()
61+
public static let v14 = Self(for: .v14)
62+
public static let v15 = Self(for: .v15)
63+
public static let v16 = Self(for: .v16)
64+
public static let v17 = Self(for: .v17)
65+
}
66+
67+
extension tvOSViewVersion<MapType, MKMapView> {
68+
@available(*, unavailable, message: "Map isn't available on tvOS 13")
69+
public static let v13 = Self.unavailable()
70+
public static let v14 = Self(for: .v14)
71+
public static let v15 = Self(for: .v15)
72+
public static let v16 = Self(for: .v16)
73+
public static let v17 = Self(for: .v17)
74+
}
75+
76+
extension macOSViewVersion<MapType, MKMapView> {
77+
@available(*, unavailable, message: "Map isn't available on macOS 10.15")
78+
public static let v10_15 = Self.unavailable()
79+
public static let v11 = Self(for: .v11)
80+
public static let v12 = Self(for: .v12)
81+
public static let v13 = Self(for: .v13)
82+
public static let v14 = Self(for: .v14)
83+
}
84+
#endif

Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
D58547FA2A1D12270068ADF4 /* NavigationSplitViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58547F92A1D12270068ADF4 /* NavigationSplitViewTests.swift */; };
9898
D58CE15629C621B30081BFB0 /* SwiftUIIntrospect in Frameworks */ = {isa = PBXBuildFile; productRef = D58CE15529C621B30081BFB0 /* SwiftUIIntrospect */; };
9999
D58CE15829C621DD0081BFB0 /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58CE15729C621DD0081BFB0 /* TestUtils.swift */; };
100+
D5AAF56F2A502EF000CAFFB6 /* MapTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5AAF56E2A502EF000CAFFB6 /* MapTests.swift */; };
100101
D5AD0D912A114B98003D8DEC /* TextFieldWithVerticalAxisTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5AD0D902A114B98003D8DEC /* TextFieldWithVerticalAxisTests.swift */; };
101102
D5ADFACC2A4A22AE009494FD /* SheetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ADFACB2A4A22AE009494FD /* SheetTests.swift */; };
102103
D5ADFACE2A4A3482009494FD /* FullScreenCoverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ADFACD2A4A3482009494FD /* FullScreenCoverTests.swift */; };
@@ -184,6 +185,7 @@
184185
D58547F72A1CDD740068ADF4 /* NavigationStackTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationStackTests.swift; sourceTree = "<group>"; };
185186
D58547F92A1D12270068ADF4 /* NavigationSplitViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationSplitViewTests.swift; sourceTree = "<group>"; };
186187
D58CE15729C621DD0081BFB0 /* TestUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestUtils.swift; sourceTree = "<group>"; };
188+
D5AAF56E2A502EF000CAFFB6 /* MapTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapTests.swift; sourceTree = "<group>"; };
187189
D5AD0D902A114B98003D8DEC /* TextFieldWithVerticalAxisTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldWithVerticalAxisTests.swift; sourceTree = "<group>"; };
188190
D5ADFACB2A4A22AE009494FD /* SheetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SheetTests.swift; sourceTree = "<group>"; };
189191
D5ADFACD2A4A3482009494FD /* FullScreenCoverTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FullScreenCoverTests.swift; sourceTree = "<group>"; };
@@ -262,6 +264,7 @@
262264
D575067F2A27C55600A628E4 /* ListWithInsetStyleTests.swift */,
263265
D575067B2A27C24600A628E4 /* ListWithPlainStyleTests.swift */,
264266
D57506832A27C8D400A628E4 /* ListWithSidebarStyleTests.swift */,
267+
D5AAF56E2A502EF000CAFFB6 /* MapTests.swift */,
265268
D58547F92A1D12270068ADF4 /* NavigationSplitViewTests.swift */,
266269
D58547F72A1CDD740068ADF4 /* NavigationStackTests.swift */,
267270
D5F8D5EE2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift */,
@@ -602,6 +605,7 @@
602605
D5ADFAD02A4A3E54009494FD /* PopoverTests.swift in Sources */,
603606
D58119D82A23B3B00081F853 /* ButtonTests.swift in Sources */,
604607
D55F448D2A1FF209003381E4 /* ListTests.swift in Sources */,
608+
D5AAF56F2A502EF000CAFFB6 /* MapTests.swift in Sources */,
605609
D58547FA2A1D12270068ADF4 /* NavigationSplitViewTests.swift in Sources */,
606610
D5F8D5EF2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift in Sources */,
607611
D57506882A27CB9800A628E4 /* FormTests.swift in Sources */,

Tests/Tests/ViewTypes/MapTests.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#if canImport(MapKit)
2+
import MapKit
3+
import SwiftUI
4+
import SwiftUIIntrospect
5+
import XCTest
6+
7+
@available(iOS 14, tvOS 14, macOS 11, *)
8+
final class MapTests: XCTestCase {
9+
typealias PlatformMap = MKMapView
10+
11+
func testMap() throws {
12+
guard #available(iOS 14, tvOS 14, macOS 11, *) else {
13+
throw XCTSkip()
14+
}
15+
16+
XCTAssertViewIntrospection(of: PlatformMap.self) { spies in
17+
let spy0 = spies[0]
18+
let spy1 = spies[1]
19+
let spy2 = spies[2]
20+
21+
let region = Binding.constant(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)))
22+
23+
VStack {
24+
Map(coordinateRegion: region)
25+
.introspect(
26+
.map,
27+
on: .iOS(.v14, .v15, .v16, .v17), .tvOS(.v14, .v15, .v16, .v17), .macOS(.v11, .v12, .v13, .v14),
28+
customize: spy0
29+
)
30+
31+
Map(coordinateRegion: region)
32+
.introspect(
33+
.map,
34+
on: .iOS(.v14, .v15, .v16, .v17), .tvOS(.v14, .v15, .v16, .v17), .macOS(.v11, .v12, .v13, .v14),
35+
customize: spy1
36+
)
37+
38+
Map(coordinateRegion: region)
39+
.introspect(
40+
.map,
41+
on: .iOS(.v14, .v15, .v16, .v17), .tvOS(.v14, .v15, .v16, .v17), .macOS(.v11, .v12, .v13, .v14),
42+
customize: spy2
43+
)
44+
}
45+
} extraAssertions: {
46+
XCTAssertNotIdentical($0[safe: 0], $0[safe: 1])
47+
XCTAssertNotIdentical($0[safe: 0], $0[safe: 2])
48+
XCTAssertNotIdentical($0[safe: 1], $0[safe: 2])
49+
}
50+
}
51+
}
52+
#endif

0 commit comments

Comments
 (0)