Skip to content

Commit 74f3b99

Browse files
authored
Add test fixtures for renderAsBuilderDSL (#423)
1 parent 7969272 commit 74f3b99

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

Tests/RegexTests/RenderDSLTests.swift

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
import XCTest
13+
import _RegexParser
14+
@_spi(PatternConverter) @testable
15+
import _StringProcessing
16+
17+
class RenderDSLTests: XCTestCase {}
18+
19+
func testConversion(
20+
_ regex: String,
21+
_ expectedDSL: String,
22+
file: StaticString = #file, line: UInt = #line
23+
) throws {
24+
let ast = try _RegexParser.parse(regex, .semantic, .traditional)
25+
let actualDSL = renderAsBuilderDSL(ast: ast)._trimmingSuffix(while: \.isWhitespace)
26+
XCTAssertEqual(actualDSL, expectedDSL[...], file: file, line: line)
27+
}
28+
29+
extension RenderDSLTests {
30+
func testSimpleConversions() throws {
31+
try testConversion(#"ab+c"#, """
32+
Regex {
33+
"a"
34+
OneOrMore {
35+
"b"
36+
}
37+
"c"
38+
}
39+
""")
40+
41+
try testConversion(#"(?:a*)b?(c+)"#, """
42+
Regex {
43+
ZeroOrMore {
44+
"a"
45+
}
46+
Optionally {
47+
"b"
48+
}
49+
Capture {
50+
OneOrMore {
51+
"c"
52+
}
53+
}
54+
}
55+
""")
56+
57+
try testConversion(#"\d+"#, """
58+
Regex {
59+
OneOrMore {
60+
.digit
61+
}
62+
}
63+
""")
64+
try XCTExpectFailure("Invalid leading dot syntax in non-initial position") {
65+
try testConversion(#":\d:"#, """
66+
Regex {
67+
":"
68+
CharacterClass.digit
69+
":"
70+
}
71+
""")
72+
}
73+
}
74+
75+
func testOptions() throws {
76+
try XCTExpectFailure("Options like '(?i)' aren't converted") {
77+
try testConversion(#"(?i)abc"#, """
78+
Regex {
79+
"abc"
80+
}.ignoresCase()
81+
""")
82+
}
83+
84+
try XCTExpectFailure("Options like '(?i:...)' aren't converted") {
85+
try testConversion(#"(?i:abc)"#, """
86+
Regex {
87+
"abc"
88+
}.ignoresCase()
89+
""")
90+
}
91+
}
92+
93+
func testAlternations() throws {
94+
try testConversion(#"a|b"#, """
95+
Regex {
96+
ChoiceOf {
97+
"a"
98+
"b"
99+
}
100+
}
101+
""")
102+
103+
try XCTExpectFailure("Concatenations in alternations aren't grouped") {
104+
try testConversion(#"\da|b"#, """
105+
Regex {
106+
ChoiceOf {
107+
Regex {
108+
.digit
109+
"a"
110+
}
111+
"bc"
112+
}
113+
}
114+
""")
115+
}
116+
}
117+
}

Tests/RegexTests/UTS18Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fileprivate func expectFirstMatch<Output: Equatable>(
6262
}
6363

6464
#if os(Linux)
65-
func XCTExpectFailure(_ message: String? = nil, body: () -> Void) {}
65+
func XCTExpectFailure(_ message: String? = nil, body: () throws -> Void) rethrows {}
6666
#endif
6767

6868
// MARK: - Basic Unicode Support: Level 1

0 commit comments

Comments
 (0)