-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathUTF8Tests.swift
74 lines (69 loc) · 2.06 KB
/
UTF8Tests.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
import Parsing
import XCTest
final class UTF8Tests: XCTestCase {
func testSubstringNormalization() {
var input = "\u{00E9}e\u{0301}e\u{0341} Hello, world"[...].utf8
let parser = From<Conversions.UTF8ViewToSubstring, Substring, String>(.substring) { "é" }
XCTAssertNoThrow(try parser.parse(&input))
XCTAssertEqual("e\u{0301}e\u{0341} Hello, world", Substring(input))
XCTAssertNoThrow(try parser.parse(&input))
XCTAssertEqual("e\u{0341} Hello, world", Substring(input))
XCTAssertNoThrow(try parser.parse(&input))
XCTAssertEqual(" Hello, world", Substring(input))
XCTAssertThrowsError(try parser.parse(&input)) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:4
1 | ééé Hello, world
| ^ expected "é"
""",
"\(error)"
)
}
XCTAssertEqual(" Hello, world", Substring(input))
}
func testUnicodeScalars() {
var input = "🇺🇸 Hello, world"[...].unicodeScalars
let parser = "🇺".unicodeScalars
XCTAssertNoThrow(try parser.parse(&input))
XCTAssertEqual("🇸 Hello, world", Substring(input))
XCTAssertThrowsError(try parser.parse(&input)) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:1
1 | 🇺🇸 Hello, world
| ^ expected "🇺"
""",
"\(error)"
)
}
XCTAssertEqual("🇸 Hello, world", Substring(input))
}
func testUTF8Normalization() {
enum City {
case losAngeles
case newYork
case sanJose
}
let city = OneOf {
"Los Angeles".utf8.map { City.losAngeles }
"New York".utf8.map { City.newYork }
"San Jos\u{00E9}".utf8.map { City.sanJose }
}
XCTAssertThrowsError(try city.parse("San Jose\u{0301}")) { error in
XCTAssertEqual(
"""
error: unexpected input
--> input:1:1
1 | San José
| ^ expected "Los Angeles"
| ^ expected "New York"
| ^ expected "San José"
""",
"\(error)"
)
}
}
}