-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathFlatMapTests.swift
48 lines (44 loc) · 1.29 KB
/
FlatMapTests.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
import Parsing
import XCTest
final class FlatMapTests: XCTestCase {
func testSuccess() {
var input = "42 Hello, world!"[...].utf8
XCTAssertEqual(43, try Int.parser().flatMap { Always($0 + 1) }.parse(&input))
XCTAssertEqual(" Hello, world!", Substring(input))
input = "42 Hello, world!"[...].utf8
XCTAssertEqual(43, try Int.parser().flatMap { return Always($0 + 1) }.parse(&input))
XCTAssertEqual(" Hello, world!", Substring(input))
}
func testUpstreamFailure() {
var input = "Hello, world!"[...].utf8
XCTAssertThrowsError(try Int.parser().flatMap { Always($0 + 1) }.parse(&input)) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:1
1 | Hello, world!
| ^ expected integer
""",
"\(error)"
)
}
XCTAssertEqual("Hello, world!", Substring(input))
}
func testDownstreamFailure() {
var input = "Hello, world!"[...].utf8
XCTAssertThrowsError(
try Prefix(2).flatMap { _ in Int.parser() }.parse(&input)
) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:1-2
1 | Hello, world!
| ^^ expected integer
""",
"\(error)"
)
}
XCTAssertEqual("llo, world!", Substring(input))
}
}