-
Notifications
You must be signed in to change notification settings - Fork 34
/
DeepLinkingTests.swift
298 lines (260 loc) · 10.3 KB
/
DeepLinkingTests.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//
// DeepLinkingTests.swift
// DeepLinkingTests
//
// Created by Joshua Smith on 5/18/17.
// Copyright © 2017 iJoshSmith. All rights reserved.
//
import XCTest
@testable import DeepLinking
class DeepLinkingTests: XCTestCase {
// MARK: - Path tests
func test_path_termA_matches() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [ADeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://a")!) {
XCTAssertTrue(result is ADeepLink)
}
else {
XCTFail()
}
}
func test_path_termA_doesNotMatch() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [ADeepLink.self])
let result = recognizer.deepLink(matching: URL(string: "test://b")!)
XCTAssertNil(result)
}
func test_path_termsAB_matches() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [ABDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://a/b/")!) {
XCTAssertTrue(result is ABDeepLink)
}
else {
XCTFail()
}
}
func test_path_termsAB_doesNotMatch() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [ABDeepLink.self])
let result = recognizer.deepLink(matching: URL(string: "test://b/a/")!)
XCTAssertNil(result)
}
func test_path_int_matches() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [IntDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://42")!) as? IntDeepLink {
XCTAssertEqual(result.number, 42)
}
else {
XCTFail()
}
}
func test_path_int_doesNotMatch() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [IntDeepLink.self])
let result = recognizer.deepLink(matching: URL(string: "test://thisIsNotAnInt/")!)
XCTAssertNil(result)
}
func test_path_bool_matches() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [BoolDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://true")!) as? BoolDeepLink {
XCTAssertEqual(result.boolean, true)
}
else {
XCTFail()
}
}
func test_path_bool_doesNotMatch() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [BoolDeepLink.self])
let result = recognizer.deepLink(matching: URL(string: "test://thisIsNotABool/")!)
XCTAssertNil(result)
}
func test_path_double_matches() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [DoubleDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://3.14")!) as? DoubleDeepLink {
XCTAssertEqual(result.number, 3.14)
}
else {
XCTFail()
}
}
func test_path_double_doesNotMatch() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [DoubleDeepLink.self])
let result = recognizer.deepLink(matching: URL(string: "test://thisIsNotADouble/")!)
XCTAssertNil(result)
}
func test_path_string_matches_text() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [StringDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://hello")!) as? StringDeepLink {
XCTAssertEqual(result.text, "hello")
}
else {
XCTFail()
}
}
func test_path_string_matches_number() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [StringDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://123")!) as? StringDeepLink {
XCTAssertEqual(result.text, "123")
}
else {
XCTFail()
}
}
func test_path_string_doesNotMatch_missing_value() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [StringDeepLink.self])
let result = recognizer.deepLink(matching: URL(string: "test://?param=foo")!)
XCTAssertNil(result)
}
func test_path_stringAndInt_matches() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [StringAndIntDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://foo/42")!) as? StringAndIntDeepLink {
XCTAssertEqual(result.text, "foo")
XCTAssertEqual(result.number, 42)
}
else {
XCTFail()
}
}
func test_path_stringAndInt_doesNotMatch() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [StringAndIntDeepLink.self])
let result = recognizer.deepLink(matching: URL(string: "test://42/foo")!)
XCTAssertNil(result)
}
// MARK: - Query tests
func test_query_requiredInt_matches() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [QueryRequiredIntDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://?number=42")!) as? QueryRequiredIntDeepLink {
XCTAssertEqual(result.number, 42)
}
else {
XCTFail()
}
}
func test_query_requiredInt_doesNotMatchDouble() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [QueryRequiredIntDeepLink.self])
let result = recognizer.deepLink(matching: URL(string: "test://?notAnInt=3.14")!)
XCTAssertNil(result)
}
func test_query_optionalInt_matches() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [QueryOptionalIntDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://?number=42")!) as? QueryOptionalIntDeepLink {
XCTAssertEqual(result.number, 42)
}
else {
XCTFail()
}
}
func test_query_optionalInt_parameterNotIncluded() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [QueryOptionalIntDeepLink.self])
if let result = recognizer.deepLink(matching: URL(string: "test://?someOtherParameter=hello")!) as? QueryOptionalIntDeepLink {
XCTAssertNil(result.number)
}
else {
XCTFail()
}
}
// MARK: - Path and Query test
struct DisplayMessageDeepLink: DeepLink {
static let template = DeepLinkTemplate()
.term("display")
.string(named: "messageType")
.queryStringParameters([
.optionalString(named: "username"),
.requiredBool(named: "mustAccept")
])
init(values: DeepLinkValues) {
self.messageType = values.path["messageType"] as! String
self.mustAccept = values.query["mustAccept"] as! Bool
self.username = values.query["username"] as? String
}
let messageType: String
let mustAccept: Bool
let username: String?
}
func test_display_message_deep_link() {
// A deep link recognizer that knows about the custom deep link type.
let recognizer = DeepLinkRecognizer(deepLinkTypes: [DisplayMessageDeepLink.self])
// A URL which conforms to the "display message" deep link schema.
let url = URL(string: "test://display/upgrade?mustAccept=true&username=Billy%20Bob")!
// Verify that the recognizer creates a properly configured deep link.
if let deepLink = recognizer.deepLink(matching: url) as? DisplayMessageDeepLink {
XCTAssertEqual(deepLink.messageType, "upgrade")
XCTAssertEqual(deepLink.mustAccept, true)
XCTAssertEqual(deepLink.username, "Billy Bob")
}
else {
XCTFail()
}
}
// MARK: - Searches through deep link types
func test_searches_through_deep_link_types() {
let recognizer = DeepLinkRecognizer(deepLinkTypes: [IntDeepLink.self,
BoolDeepLink.self,
DoubleDeepLink.self,
StringDeepLink.self])
let url = URL(string: "test://3.14")!
if let deepLink = recognizer.deepLink(matching: url) as? DoubleDeepLink {
XCTAssertEqual(deepLink.number, 3.14)
}
else {
XCTFail()
}
}
}
// MARK: - Deep Links
struct ADeepLink: DeepLink {
public static let template = DeepLinkTemplate().term("a")
public init(values: DeepLinkValues) {}
}
struct ABDeepLink: DeepLink {
public static let template = DeepLinkTemplate().term("a").term("b")
public init(values: DeepLinkValues) {}
}
struct IntDeepLink: DeepLink {
public static let template = DeepLinkTemplate().int(named: "number")
public init(values: DeepLinkValues) {
self.number = values.path["number"] as! Int
}
let number: Int
}
struct BoolDeepLink: DeepLink {
public static let template = DeepLinkTemplate().bool(named: "boolean")
public init(values: DeepLinkValues) {
self.boolean = values.path["boolean"] as! Bool
}
let boolean: Bool
}
struct DoubleDeepLink: DeepLink {
public static let template = DeepLinkTemplate().double(named: "number")
public init(values: DeepLinkValues) {
self.number = values.path["number"] as! Double
}
let number: Double
}
struct StringDeepLink: DeepLink {
public static let template = DeepLinkTemplate().string(named: "text")
public init(values: DeepLinkValues) {
self.text = values.path["text"] as! String
}
let text: String
}
struct StringAndIntDeepLink: DeepLink {
public static let template = DeepLinkTemplate().string(named: "text").int(named: "number")
public init(values: DeepLinkValues) {
self.text = values.path["text"] as! String
self.number = values.path["number"] as! Int
}
let text: String
let number: Int
}
struct QueryRequiredIntDeepLink: DeepLink {
public static let template = DeepLinkTemplate().queryStringParameters([.requiredInt(named: "number")])
public init(values: DeepLinkValues) {
self.number = values.query["number"] as! Int
}
let number: Int
}
struct QueryOptionalIntDeepLink: DeepLink {
public static let template = DeepLinkTemplate().queryStringParameters([.optionalInt(named: "number")])
public init(values: DeepLinkValues) {
self.number = values.query["number"] as? Int
}
let number: Int?
}