-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathParsingErrorTests.swift
146 lines (138 loc) · 3.18 KB
/
ParsingErrorTests.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
import Parsing
import XCTest
class ParsingErrorTests: XCTestCase {
func testUnknownInput() {
struct MyInput {}
struct MyOutput {}
struct MyParser: Parser {
func parse(_ input: inout MyInput) throws -> MyOutput {
struct MyError: Error {}
throw MyError()
}
}
XCTAssertThrowsError(
try Parse {
MyParser()
MyParser()
}
.parse(MyInput())
) { error in
XCTAssertEqual(
"""
error: MyError()
""",
"\(error)"
)
}
}
func testAlignsLineNumber() {
let parser = Many(101...) {
"Hello"
} separator: {
"\n"
}
XCTAssertThrowsError(
try parser.parse(String(repeating: "Hello\n", count: 100) + "World")
) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:100:6
100 | Hello
| ^ expected 1 more value of "()"
""",
"\(error)"
)
}
}
func testTruncatesLongLines() {
XCTAssertThrowsError(
try Many(101...) { "hello" }.parse(
String(repeating: "hello", count: 100) + String(repeating: "world", count: 100)
)
) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:501
1 | …hellohellohellohelloworldworldworldworldworldworldworldworldworldworldworl…
| ^ expected 1 more value of "()"
""",
"\(error)"
)
}
XCTAssertThrowsError(
try Many(101...) { "hello" }.parse(
String(repeating: "hello", count: 100) + "world"
)
) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:501
1 | …hellohellohellohelloworld
| ^ expected 1 more value of "()"
""",
"\(error)"
)
}
XCTAssertThrowsError(
try "hello".parse(
String(repeating: "world", count: 100)
)
) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:1
1 | worldworldworldworldworldworldworldworldworldworldworldworldworldworldworld…
| ^ expected "hello"
""",
"\(error)"
)
}
}
func testComplexStringLiteralParserError() {
let asciiByte = Prefix<Substring>(1...) { $0 != "\"" && $0 >= " " }.map(String.init)
let stringLiteral = Parse(input: Substring.self) {
"\""
Many(into: "") {
$0.append(contentsOf: $1)
} element: {
OneOf {
asciiByte
Parse {
"\\"
OneOf {
"\n".map { "\n" }
"\r".map { "\r" }
"\t".map { "\t" }
}
}
}
} terminator: {
"\""
}
}
XCTAssertThrowsError(
try stringLiteral.parse(
"""
"Hello
world"
"""
)
) { error in
XCTAssertEqual(
#"""
error: unexpected input
--> input:1:7
1 | "Hello
| ^ expected 1 element satisfying predicate
| ^ expected "\\"
| ^ expected "\""
"""#,
"\(error)"
)
}
}
}