-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathPrefixTests.swift
122 lines (107 loc) · 3.48 KB
/
PrefixTests.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
import Parsing
import XCTest
final class PrefixTests: XCTestCase {
func testPrefixSuccess() {
var input = "42 Hi!"[...]
XCTAssertEqual("42", try Prefix(2).parse(&input))
XCTAssertEqual(" Hi!", input)
}
func testPrefixUnder() {
var input = "42 Hi!"[...]
XCTAssertThrowsError(try Prefix(8).parse(&input))
XCTAssertEqual("", input)
}
func testPrefixOver() {
var input = "42 Hi!"[...]
XCTAssertThrowsError(try Prefix(10).parse(&input)) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:7
1 | 42 Hi!
| ^ expected 4 more elements
""",
"\(error)"
)
}
XCTAssertEqual("", input)
}
func testPrefixWhile() {
var input = "42 Hello, world!"[...]
XCTAssertEqual("42", try Prefix(while: { $0.isNumber }).parse(&input))
XCTAssertEqual(" Hello, world!", input)
}
func testPrefixWhileAlwaysSucceeds() {
var input = "Hello, world!"[...]
XCTAssertEqual("", try Prefix(while: { $0.isNumber }).parse(&input))
XCTAssertEqual("Hello, world!", input)
}
func testPrefixRangeFromSuccess() {
var input = "42 Hello, world!"[...]
XCTAssertEqual("42 Hello, world!", try Prefix(1...).parse(&input))
XCTAssertEqual("", input)
}
func testPrefixRangeFromFailure() {
var input = "42 Hello, world!"[...]
XCTAssertThrowsError(try Prefix(100...) { _ in true }.parse(&input)) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:17
1 | 42 Hello, world!
| ^ expected 84 more elements satisfying predicate
""",
"\(error)"
)
}
XCTAssertEqual("", input)
}
func testPrefixRangeFromWhileSuccess() {
var input = "42 Hello, world!"[...]
XCTAssertEqual("42", try Prefix(1..., while: { $0.isNumber }).parse(&input))
XCTAssertEqual(" Hello, world!", input)
}
func testPrefixRangeThroughSuccess() {
var input = "42 Hello, world!"[...]
XCTAssertEqual("42 Hello, ", try Prefix(...10).parse(&input))
XCTAssertEqual("world!", input)
}
func testPrefixRangeThroughWhileSuccess() {
var input = "42 Hello, world!"[...]
XCTAssertEqual("42", try Prefix(...10, while: { $0.isNumber }).parse(&input))
XCTAssertEqual(" Hello, world!", input)
}
func testPrefixLengthFromWhileSuccess() {
var input = "42 Hello, world!"[...]
XCTAssertEqual("4", try Prefix(1, while: { $0.isNumber }).parse(&input))
XCTAssertEqual("2 Hello, world!", input)
}
func testPrintUpstreamInputFailure() {
struct MyParserPrinter: ParserPrinter {
var body: some ParserPrinter<Substring, (Substring, Character)> {
Prefix { $0 != "\n" }
First()
}
}
XCTAssertThrowsError(try MyParserPrinter().print(("Hello", " "))) { error in
XCTAssertEqual(
"""
error: round-trip expectation failed
A "Prefix" parser's predicate satisfied the first element printed by the next printer.
During a round-trip, the "Prefix" parser would have parsed this element, which means the \
data handed to the next printer is in an invalid state.
""",
"\(error)"
)
}
}
func testPrintWithMaxCountAllowMatchingNextElement() {
struct MyParserPrinter: ParserPrinter {
var body: some ParserPrinter<Substring, (Substring, Character)> {
Prefix(3) { $0.isNumber }
First()
}
}
XCTAssertEqual("1230", try MyParserPrinter().print(("123", "0")))
}
}