forked from mapbox/mapbox-directions-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForeignMemberContainerTests.swift
102 lines (84 loc) · 4.7 KB
/
ForeignMemberContainerTests.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
import XCTest
import Foundation
import Turf
@testable import MapboxDirections
class ForeignMemberContainerTests: XCTestCase {
func testRouteRefreshForeignMembersCoding() {
guard let fixtureURL = Bundle.module.url(forResource: "RouteRefreshResponseWithForeignMembers",
withExtension:"json") else {
XCTFail()
return
}
guard let fixtureData = try? Data(contentsOf: fixtureURL, options:.mappedIfSafe) else {
XCTFail()
return
}
var fixtureJSON: [String: Any?]?
XCTAssertNoThrow(fixtureJSON = try JSONSerialization.jsonObject(with: fixtureData, options: []) as? [String: Any?])
let decoder = JSONDecoder()
decoder.userInfo[.credentials] = BogusCredentials
decoder.userInfo[.responseIdentifier] = "bogusId"
decoder.userInfo[.routeIndex] = 0
decoder.userInfo[.startLegIndex] = 0
var response: RouteRefreshResponse?
XCTAssertNoThrow(response = try decoder.decode(RouteRefreshResponse.self, from: fixtureData))
let encoder = JSONEncoder()
var encodedResponse: Data?
var encodedRouteRefreshJSON: [String: Any?]?
XCTAssertNoThrow(encodedResponse = try encoder.encode(response))
XCTAssertNoThrow(encodedRouteRefreshJSON = try JSONSerialization.jsonObject(with: encodedResponse!, options: []) as? [String: Any?])
XCTAssertNotNil(encodedRouteRefreshJSON)
// Remove default keys not found in the original API response.
encodedRouteRefreshJSON?.removeValue(forKey: "uuid")
XCTAssertTrue(JSONSerialization.objectsAreEqual(fixtureJSON, encodedRouteRefreshJSON, approximate: true))
}
func testRouteResponseForeignMembersCoding() {
guard let fixtureURL = Bundle.module.url(forResource: "RouteResponseWithForeignMembers",
withExtension:"json") else {
XCTFail()
return
}
guard let fixtureData = try? Data(contentsOf: fixtureURL, options:.mappedIfSafe) else {
XCTFail()
return
}
var fixtureJSON: [String: Any?]?
XCTAssertNoThrow(fixtureJSON = try JSONSerialization.jsonObject(with: fixtureData, options: []) as? [String: Any?])
let options = RouteOptions(coordinates: [.init(latitude: 0,
longitude: 0),
.init(latitude: 1,
longitude: 1)])
options.shapeFormat = .geoJSON
let decoder = JSONDecoder()
decoder.userInfo[.options] = options
decoder.userInfo[.credentials] = BogusCredentials
var response: RouteResponse?
XCTAssertNoThrow(response = try decoder.decode(RouteResponse.self, from: fixtureData))
let encoder = JSONEncoder()
encoder.userInfo[.options] = options
encoder.userInfo[.credentials] = BogusCredentials
var encodedResponse: Data?
var encodedRouteResponseJSON: [String: Any?]?
XCTAssertNoThrow(encodedResponse = try encoder.encode(response))
XCTAssertNoThrow(encodedRouteResponseJSON = try JSONSerialization.jsonObject(with: encodedResponse!, options: []) as? [String: Any?])
XCTAssertNotNil(encodedRouteResponseJSON)
// Remove default keys not found in the original API response.
if var encodedRoutesJSON = encodedRouteResponseJSON?["routes"] as? [[String: Any?]] {
if var encodedLegJSON = encodedRoutesJSON[0]["legs"] as? [[String: Any?]] {
encodedLegJSON[0].removeValue(forKey: "source")
encodedLegJSON[0].removeValue(forKey: "destination")
encodedLegJSON[0].removeValue(forKey: "profileIdentifier")
encodedRoutesJSON[0]["legs"] = encodedLegJSON
encodedRouteResponseJSON?["routes"] = encodedRoutesJSON
}
}
if var encodedWaypointsJSON = encodedRouteResponseJSON?["waypoints"] as? [[String: Any?]] {
encodedWaypointsJSON[0].removeValue(forKey: "separatesLegs")
encodedWaypointsJSON[0].removeValue(forKey: "allowsArrivingOnOppositeSide")
encodedWaypointsJSON[1].removeValue(forKey: "separatesLegs")
encodedWaypointsJSON[1].removeValue(forKey: "allowsArrivingOnOppositeSide")
encodedRouteResponseJSON?["waypoints"] = encodedWaypointsJSON
}
XCTAssertTrue(JSONSerialization.objectsAreEqual(fixtureJSON, encodedRouteResponseJSON, approximate: true))
}
}