forked from mapbox/mapbox-directions-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnnotationTests.swift
142 lines (120 loc) · 7.11 KB
/
AnnotationTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import XCTest
#if !os(Linux)
import CoreLocation
#if SWIFT_PACKAGE
import OHHTTPStubsSwift
#endif
import OHHTTPStubs
@testable import MapboxDirections
class AnnotationTests: XCTestCase {
override func tearDown() {
HTTPStubs.removeAllStubs()
super.tearDown()
}
func testAnnotation() {
let expectation = self.expectation(description: "calculating directions should return results")
let queryParams: [String: String?] = [
"alternatives": "false",
"geometries": "polyline",
"overview": "full",
"steps": "false",
"continue_straight": "true",
"access_token": BogusToken,
"annotations": "distance,duration,speed,congestion,maxspeed,congestion_numeric"
]
stub(condition: isHost("api.mapbox.com")
&& containsQueryParams(queryParams)) { _ in
let path = Bundle.module.path(forResource: "annotation", ofType: "json")
return HTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: ["Content-Type": "application/json"])
}
let options = RouteOptions(coordinates: [
CLLocationCoordinate2D(latitude: 37.780602, longitude: -122.431373),
CLLocationCoordinate2D(latitude: 37.758859, longitude: -122.404058),
], profileIdentifier: .automobileAvoidingTraffic)
options.shapeFormat = .polyline
options.includesSteps = false
options.includesAlternativeRoutes = false
options.routeShapeResolution = .full
options.attributeOptions = [.distance, .expectedTravelTime, .speed, .congestionLevel, .numericCongestionLevel, .maximumSpeedLimit]
var route: Route?
let task = Directions(credentials: BogusCredentials).calculate(options) { (session, disposition) in
switch disposition {
case let .failure(error):
XCTFail("Error! \(error)")
case let .success(response):
XCTAssertNotNil(response.routes)
XCTAssertEqual(response.routes!.count, 1)
route = response.routes!.first!
expectation.fulfill()
}
}
XCTAssertNotNil(task)
waitForExpectations(timeout: 2) { (error) in
XCTAssertNil(error, "Error: \(error!.localizedDescription)")
XCTAssertEqual(task.state, .completed)
}
XCTAssertNotNil(route)
if let route = route {
XCTAssertNotNil(route.shape)
XCTAssertEqual(route.shape?.coordinates.count, 154)
XCTAssertEqual(route.legs.count, 1)
}
if let leg = route?.legs.first {
XCTAssertEqual(leg.segmentDistances?.count, 153)
XCTAssertEqual(leg.segmentSpeeds?.count, 153)
XCTAssertEqual(leg.expectedSegmentTravelTimes?.count, 153)
XCTAssertEqual(leg.segmentCongestionLevels?.count, 153)
XCTAssertEqual(leg.segmentCongestionLevels?.firstIndex(of: .unknown), 2)
XCTAssertEqual(leg.segmentCongestionLevels?.firstIndex(of: .low), 0)
XCTAssertEqual(leg.segmentCongestionLevels?.firstIndex(of: .moderate), 14)
XCTAssertFalse(leg.segmentCongestionLevels?.contains(.heavy) ?? true)
XCTAssertFalse(leg.segmentCongestionLevels?.contains(.severe) ?? true)
XCTAssertEqual(leg.segmentNumericCongestionLevels?.count, 153)
XCTAssertEqual(leg.segmentNumericCongestionLevels?.firstIndex(of: nil), 2)
XCTAssertEqual(leg.segmentNumericCongestionLevels?.firstIndex(of: 12), 91)
XCTAssertEqual(leg.segmentNumericCongestionLevels?.firstIndex(of: 32), 60)
XCTAssertFalse(leg.segmentNumericCongestionLevels?.contains(26) ?? true)
XCTAssertEqual(leg.segmentMaximumSpeedLimits?.count, 153)
XCTAssertEqual(leg.segmentMaximumSpeedLimits?.first, Measurement(value: 48, unit: .kilometersPerHour))
XCTAssertEqual(leg.segmentMaximumSpeedLimits?.firstIndex(of: nil), 2)
XCTAssertFalse(leg.segmentMaximumSpeedLimits?.contains(Measurement(value: .infinity, unit: .kilometersPerHour)) ?? true)
}
}
func testSpeedLimits() {
func assert(_ speedLimitDescriptorJSON: [String: Any], roundTripsWith expectedSpeedLimitDescriptor: SpeedLimitDescriptor) {
let speedLimitDescriptorData = try! JSONSerialization.data(withJSONObject: speedLimitDescriptorJSON, options: [])
var speedLimitDescriptor: SpeedLimitDescriptor?
XCTAssertNoThrow(speedLimitDescriptor = try JSONDecoder().decode(SpeedLimitDescriptor.self, from: speedLimitDescriptorData))
XCTAssertEqual(speedLimitDescriptor, expectedSpeedLimitDescriptor)
speedLimitDescriptor = expectedSpeedLimitDescriptor
let encoder = JSONEncoder()
var encodedData: Data?
XCTAssertNoThrow(encodedData = try encoder.encode(speedLimitDescriptor))
XCTAssertNotNil(encodedData)
if let encodedData = encodedData {
var encodedSpeedLimitDescriptorJSON: [String: Any?]?
XCTAssertNoThrow(encodedSpeedLimitDescriptorJSON = try JSONSerialization.jsonObject(with: encodedData, options: []) as? [String: Any?])
XCTAssertNotNil(encodedSpeedLimitDescriptorJSON)
XCTAssert(JSONSerialization.objectsAreEqual(speedLimitDescriptorJSON, encodedSpeedLimitDescriptorJSON, approximate: true))
}
}
XCTAssertEqual(SpeedLimitDescriptor(speed: Measurement(value: 55, unit: .milesPerHour)),
.some(speed: Measurement(value: 55, unit: .milesPerHour)))
XCTAssertEqual(Measurement<UnitSpeed>(speedLimitDescriptor: .some(speed: Measurement(value: 55, unit: .milesPerHour))),
Measurement(value: 55, unit: .milesPerHour))
assert(["speed": 55.0, "unit": "mph"], roundTripsWith: .some(speed: Measurement(value: 55, unit: .milesPerHour)))
XCTAssertEqual(SpeedLimitDescriptor(speed: Measurement(value: 80, unit: .kilometersPerHour)),
.some(speed: Measurement(value: 80, unit: .kilometersPerHour)))
XCTAssertEqual(Measurement<UnitSpeed>(speedLimitDescriptor: .some(speed: Measurement(value: 80, unit: .kilometersPerHour))),
Measurement(value: 80, unit: .kilometersPerHour))
assert(["speed": 80.0, "unit": "km/h"], roundTripsWith: .some(speed: Measurement(value: 80, unit: .kilometersPerHour)))
XCTAssertEqual(SpeedLimitDescriptor(speed: nil), .unknown)
XCTAssertNil(Measurement<UnitSpeed>(speedLimitDescriptor: .unknown))
assert(["unknown": true], roundTripsWith: .unknown)
XCTAssertEqual(SpeedLimitDescriptor(speed: Measurement(value: .infinity, unit: .kilometersPerHour)), .none)
XCTAssertEqual(Measurement<UnitSpeed>(speedLimitDescriptor: .none),
Measurement(value: .infinity, unit: .kilometersPerHour))
assert(["none": true], roundTripsWith: .none)
}
}
#endif