forked from pointfreeco/swift-url-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutingErrorTests.swift
135 lines (125 loc) · 3.15 KB
/
RoutingErrorTests.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
import Parsing
import URLRouting
import XCTest
class RoutingErrorTests: XCTestCase {
func testError() {
enum BookRoute {
case fetch
}
struct BookRouter: ParserPrinter {
var body: some Router<BookRoute> {
Route(.case(BookRoute.fetch))
}
}
struct Options {
var sort: Sort = .name
var direction: Direction = .asc
var count: Int = 10
enum Direction: String, CaseIterable, Decodable {
case asc, desc
}
enum Sort: String, CaseIterable, Decodable {
case name
case category = "category"
}
}
enum BooksRoute {
case book(id: UUID, route: BookRoute)
case search(Options)
}
struct BooksRouter: ParserPrinter {
var body: some Router<BooksRoute> {
OneOf {
Route(.case(BooksRoute.book(id:route:))) {
Path { UUID.parser() }
BookRouter()
}
Route(.case(BooksRoute.search)) {
Path { "search" }
Parse(.memberwise(Options.init(sort:direction:count:))) {
Query {
Field("sort", default: .name) { Options.Sort.parser() }
Field("direction", default: .asc) { Options.Direction.parser() }
Field("count", default: 10) { Int.parser() }
}
}
}
}
}
}
enum UserRoute {
case books(BooksRoute)
case fetch
}
struct UserRouter: ParserPrinter {
var body: some Router<UserRoute> {
OneOf {
Route(.case(UserRoute.books)) {
Path { "books" }
BooksRouter()
}
Route(.case(UserRoute.fetch))
}
}
}
struct CreateUser: Codable {
let bio: String
let name: String
}
enum UsersRoute {
case create(CreateUser)
case user(id: Int, route: UserRoute)
}
struct UsersRouter: ParserPrinter {
var body: some Router<UsersRoute> {
OneOf {
Route(.case(UsersRoute.create)) {
Method.post
Body(.json(CreateUser.self))
}
Route(.case(UsersRoute.user(id:route:))) {
Path { Int.parser() }
UserRouter()
}
}
}
}
enum SiteRoute {
case aboutUs
case contactUs
case home
case users(UsersRoute)
}
struct SiteRouter: ParserPrinter {
var body: some Router<SiteRoute> {
OneOf {
Route(.case(SiteRoute.aboutUs)) {
Path { "about-us" }
}
Route(.case(SiteRoute.contactUs)) {
Path { "contact-us" }
}
Route(.case(SiteRoute.home))
Route(.case(SiteRoute.users)) {
Path { "users" }
UsersRouter()
}
}
}
}
XCTAssertThrowsError(try SiteRouter().parse(URLRequestData(path: "/123"))) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:2
1 | /123
| ^ expected "about-us"
| ^ expected "contact-us"
| ^ expected end of input
| ^ expected "users"
""",
"\(error)"
)
}
}
}