Skip to content

Commit d5702d9

Browse files
Add custom dump to CLLocation (#150)
* Add custom dump to CLLocation * Reorder based off initializer --------- Co-authored-by: Stephen Celis <stephen@stephencelis.com>
1 parent 2a2a938 commit d5702d9

2 files changed

Lines changed: 132 additions & 0 deletions

File tree

Sources/CustomDump/Conformances/CoreLocation.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
11
#if canImport(CoreLocation)
22
import CoreLocation
33

4+
@available(iOS 13.4, macOS 10.15.4, tvOS 13.4, watchOS 6.2, *)
5+
extension CLLocation: CustomDumpReflectable {
6+
public var customDumpMirror: Mirror {
7+
var ellipsoidalAltitude: Any? = nil
8+
var sourceInformation: Any? = nil
9+
10+
if #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) {
11+
ellipsoidalAltitude = self.ellipsoidalAltitude
12+
sourceInformation = self.sourceInformation
13+
}
14+
15+
let children: KeyValuePairs<String, Any?> = [
16+
"coordinate": coordinate,
17+
"altitude": altitude,
18+
"horizontalAccuracy": horizontalAccuracy,
19+
"verticalAccuracy": verticalAccuracy,
20+
"course": course,
21+
"courseAccuracy": courseAccuracy,
22+
"speed": speed,
23+
"speedAccuracy": speedAccuracy,
24+
"timestamp": timestamp,
25+
"sourceInformation": sourceInformation,
26+
"ellipsoidalAltitude": ellipsoidalAltitude,
27+
"floor": floor as Any,
28+
]
29+
30+
return Mirror(
31+
self,
32+
children: children.compactMap {
33+
guard let value = $1 else { return nil }
34+
return ($0, value)
35+
},
36+
displayStyle: .class
37+
)
38+
}
39+
}
40+
41+
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
42+
extension CLLocationSourceInformation: CustomDumpReflectable {
43+
public var customDumpMirror: Mirror {
44+
Mirror(
45+
self,
46+
children: [
47+
"isProducedByAccessory": isProducedByAccessory,
48+
"isSimulatedBySoftware": isSimulatedBySoftware,
49+
],
50+
displayStyle: .class
51+
)
52+
}
53+
}
54+
55+
extension CLFloor: CustomDumpReflectable {
56+
public var customDumpMirror: Mirror {
57+
Mirror(self, children: ["level": level], displayStyle: .class)
58+
}
59+
}
60+
461
#if compiler(>=5.4)
562
extension CLAccuracyAuthorization: CustomDumpStringConvertible {
663
public var customDumpDescription: String {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#if canImport(CoreLocation)
2+
import CoreLocation
3+
import CustomDump
4+
import XCTest
5+
6+
final class CoreLocationTests: XCTestCase {
7+
func testCLLocation() {
8+
guard #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) else { return }
9+
10+
let date = Date(timeIntervalSinceReferenceDate: 0)
11+
let sourceInfo = CLLocationSourceInformation(
12+
softwareSimulationState: true,
13+
andExternalAccessoryState: false
14+
)
15+
16+
let item = CLLocation(
17+
coordinate: .init(latitude: 10, longitude: 20), altitude: 300,
18+
horizontalAccuracy: 4, verticalAccuracy: 5,
19+
course: 6, courseAccuracy: 7,
20+
speed: 8, speedAccuracy: 9,
21+
timestamp: date, sourceInfo: sourceInfo
22+
)
23+
24+
var dump = ""
25+
customDump(item, to: &dump)
26+
27+
expectNoDifference(
28+
dump,
29+
"""
30+
CLLocation(
31+
coordinate: CLLocationCoordinate2D(
32+
latitude: 10.0,
33+
longitude: 20.0
34+
),
35+
altitude: 300.0,
36+
horizontalAccuracy: 4.0,
37+
verticalAccuracy: 5.0,
38+
course: 6.0,
39+
courseAccuracy: 7.0,
40+
speed: 8.0,
41+
speedAccuracy: 9.0,
42+
timestamp: Date(2001-01-01T00:00:00.000Z),
43+
sourceInformation: CLLocationSourceInformation(
44+
isProducedByAccessory: false,
45+
isSimulatedBySoftware: true
46+
),
47+
ellipsoidalAltitude: 0.0,
48+
floor: nil
49+
)
50+
"""
51+
)
52+
}
53+
54+
class FakeFloor: CLFloor {
55+
init(level: Int) {
56+
_level = level
57+
super.init()
58+
}
59+
60+
required init?(coder: NSCoder) { return nil }
61+
62+
private var _level: Int
63+
override var level: Int { _level }
64+
}
65+
66+
func testCLFloor() {
67+
let floor = FakeFloor(level: 10)
68+
69+
var dump = ""
70+
customDump(floor, to: &dump)
71+
72+
XCTAssertEqual(dump, "CLFloor(level: 10)")
73+
}
74+
}
75+
#endif

0 commit comments

Comments
 (0)