forked from mapbox/mapbox-directions-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualInstructionComponentTests.swift
214 lines (196 loc) · 10.3 KB
/
VisualInstructionComponentTests.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import XCTest
@testable import MapboxDirections
class VisualInstructionComponentTests: XCTestCase {
func testTextComponent() {
let componentJSON = [
"type": "text",
"text": "Take a hike",
"imageBaseURL": "",
]
let componentData = try! JSONSerialization.data(withJSONObject: componentJSON, options: [])
var component: VisualInstruction.Component?
XCTAssertNoThrow(component = try JSONDecoder().decode(VisualInstruction.Component.self, from: componentData))
XCTAssertNotNil(component)
if let component = component {
switch component {
case .text(let text):
XCTAssertEqual(text.text, "Take a hike")
default:
XCTFail("Text component should not be decoded as any other kind of component.")
}
}
}
func testImageComponent() {
let componentJSON: [String : Any] = [
"text": "I 95",
"type": "icon",
"imageBaseURL": "https://s3.amazonaws.com/mapbox/shields/v3/i-95",
"mapbox_shield": [
"base_url": "https://api.mapbox.com/styles/v1/",
"name": "us-interstate",
"text_color": "white",
"display_ref": "95"
]
]
let componentData = try! JSONSerialization.data(withJSONObject: componentJSON, options: [])
var component: VisualInstruction.Component?
XCTAssertNoThrow(component = try JSONDecoder().decode(VisualInstruction.Component.self, from: componentData))
XCTAssertNotNil(component)
if let component = component {
switch component {
case .image(let image, let alternativeText):
XCTAssertEqual(image.imageBaseURL?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/i-95")
XCTAssertEqual(image.imageURL(scale: 1, format: .svg)?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/[email protected]")
XCTAssertEqual(image.imageURL(scale: 3, format: .svg)?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/[email protected]")
XCTAssertEqual(image.imageURL(scale: 3, format: .png)?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/[email protected]")
XCTAssertEqual(alternativeText.text, "I 95")
XCTAssertNil(alternativeText.abbreviation)
XCTAssertNil(alternativeText.abbreviationPriority)
XCTAssertEqual(image.shield?.baseURL, URL(string: "https://api.mapbox.com/styles/v1/")!)
XCTAssertEqual(image.shield?.name, "us-interstate")
XCTAssertEqual(image.shield?.textColor, "white")
XCTAssertEqual(image.shield?.text, "95")
default:
XCTFail("Image component should not be decoded as any other kind of component.")
}
}
let shield = VisualInstruction.Component.ShieldRepresentation(baseURL: URL(string: "https://api.mapbox.com/styles/v1/")!, name: "us-interstate", textColor: "white", text: "95")
component = .image(image: .init(imageBaseURL: URL(string: "https://s3.amazonaws.com/mapbox/shields/v3/i-95")!, shield: shield),
alternativeText: .init(text: "I 95", abbreviation: nil, abbreviationPriority: nil))
let encoder = JSONEncoder()
var encodedData: Data?
XCTAssertNoThrow(encodedData = try encoder.encode(component))
XCTAssertNotNil(encodedData)
if let encodedData = encodedData {
var encodedComponentJSON: [String: Any?]?
XCTAssertNoThrow(encodedComponentJSON = try JSONSerialization.jsonObject(with: encodedData, options: []) as? [String: Any?])
XCTAssertNotNil(encodedComponentJSON)
XCTAssert(JSONSerialization.objectsAreEqual(componentJSON, encodedComponentJSON, approximate: false))
}
}
func testShield() {
let shieldJSON = [
"base_url": "https://api.mapbox.com/styles/v1/",
"name": "us-interstate",
"text_color": "white",
"display_ref": "95",
]
let shieldData = try! JSONSerialization.data(withJSONObject: shieldJSON, options: [])
var shield: VisualInstruction.Component.ShieldRepresentation?
XCTAssertNoThrow(shield = try JSONDecoder().decode(VisualInstruction.Component.ShieldRepresentation.self, from: shieldData))
XCTAssertNotNil(shield)
let url = URL(string: "https://api.mapbox.com/styles/v1/")
if let shield = shield {
XCTAssertEqual(shield.baseURL, url)
XCTAssertEqual(shield.name, "us-interstate")
XCTAssertEqual(shield.textColor, "white")
XCTAssertEqual(shield.text, "95")
}
shield = .init(baseURL: url!, name: "us-interstate", textColor: "white", text: "95")
let encoder = JSONEncoder()
var encodedData: Data?
XCTAssertNoThrow(encodedData = try encoder.encode(shield))
XCTAssertNotNil(encodedData)
if let encodedData = encodedData {
var encodedShieldJSON: [String: Any]?
XCTAssertNoThrow(encodedShieldJSON = try JSONSerialization.jsonObject(with: encodedData, options: []) as? [String: Any])
XCTAssertNotNil(encodedShieldJSON)
XCTAssert(JSONSerialization.objectsAreEqual(shieldJSON, encodedShieldJSON, approximate: false))
}
}
func testShieldImageComponent() {
let componentJSON: [String : Any] = [
"text": "I 95",
"type": "icon",
"mapbox_shield": [
"base_url": "https://api.mapbox.com/styles/v1/",
"name": "us-interstate",
"text_color": "white",
"display_ref": "95"
]
]
let componentData = try! JSONSerialization.data(withJSONObject: componentJSON, options: [])
var component: VisualInstruction.Component?
XCTAssertNoThrow(component = try JSONDecoder().decode(VisualInstruction.Component.self, from: componentData))
XCTAssertNotNil(component)
if let component = component {
switch component {
case .image(let image, let alternativeText):
XCTAssertNil(image.imageBaseURL?.absoluteString)
XCTAssertEqual(alternativeText.text, "I 95")
XCTAssertNil(alternativeText.abbreviation)
XCTAssertNil(alternativeText.abbreviationPriority)
XCTAssertEqual(image.shield?.baseURL, URL(string: "https://api.mapbox.com/styles/v1/")!)
XCTAssertEqual(image.shield?.name, "us-interstate")
XCTAssertEqual(image.shield?.textColor, "white")
XCTAssertEqual(image.shield?.text, "95")
default:
XCTFail("Image component should not be decoded as any other kind of component.")
}
}
let shield = VisualInstruction.Component.ShieldRepresentation(baseURL: URL(string: "https://api.mapbox.com/styles/v1/")!, name: "us-interstate", textColor: "white", text: "95")
component = .image(image: .init(imageBaseURL: nil, shield: shield),
alternativeText: .init(text: "I 95", abbreviation: nil, abbreviationPriority: nil))
let encoder = JSONEncoder()
var encodedData: Data?
XCTAssertNoThrow(encodedData = try encoder.encode(component))
XCTAssertNotNil(encodedData)
if let encodedData = encodedData {
var encodedComponentJSON: [String: Any?]?
XCTAssertNoThrow(encodedComponentJSON = try JSONSerialization.jsonObject(with: encodedData, options: []) as? [String: Any?])
XCTAssertNotNil(encodedComponentJSON)
XCTAssert(JSONSerialization.objectsAreEqual(componentJSON, encodedComponentJSON, approximate: false))
}
}
func testLaneComponent() {
let componentJSON: [String: Any?] = [
"text": "",
"type": "lane",
"active": true,
"directions": ["right", "straight"],
"active_direction": "right",
]
let componentData = try! JSONSerialization.data(withJSONObject: componentJSON, options: [])
var component: VisualInstruction.Component?
XCTAssertNoThrow(component = try JSONDecoder().decode(VisualInstruction.Component.self, from: componentData))
XCTAssertNotNil(component)
if let component = component {
if case let .lane(indications, isUsable, preferredDirection) = component {
XCTAssertEqual(indications, [.straightAhead, .right])
XCTAssertTrue(isUsable)
XCTAssertEqual(preferredDirection, .right)
} else {
XCTFail("Lane component should not be decoded as any other kind of component.")
}
}
component = .lane(indications: [.straightAhead, .right], isUsable: true, preferredDirection: .right)
let encoder = JSONEncoder()
var encodedData: Data?
XCTAssertNoThrow(encodedData = try encoder.encode(component))
XCTAssertNotNil(encodedData)
if let encodedData = encodedData {
var encodedComponentJSON: [String: Any?]?
XCTAssertNoThrow(encodedComponentJSON = try JSONSerialization.jsonObject(with: encodedData, options: []) as? [String: Any?])
XCTAssertNotNil(encodedComponentJSON)
XCTAssert(JSONSerialization.objectsAreEqual(componentJSON, encodedComponentJSON, approximate: false))
}
}
func testUnrecognizedComponent() {
let componentJSON = [
"type": "emoji",
"text": "👈",
]
let componentData = try! JSONSerialization.data(withJSONObject: componentJSON, options: [])
var component: VisualInstruction.Component?
XCTAssertNoThrow(component = try JSONDecoder().decode(VisualInstruction.Component.self, from: componentData))
XCTAssertNotNil(component)
if let component = component {
switch component {
case .text(let text):
XCTAssertEqual(text.text, "👈")
default:
XCTFail("Component of unrecognized type should be decoded as text component.")
}
}
}
}