Skip to content

Commit 3568449

Browse files
authored
Support xfail (#86)
1 parent b7b36cd commit 3568449

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

Sources/_StringProcessing/Algorithms/Consumers/RegexConsumer.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ public struct RegexConsumer<Consumed: BidirectionalCollection>
66
// TODO: consider let, for now lets us toggle tracing
77
var vm: Executor
88

9+
// FIXME: Possibility of fatal error isn't user friendly
910
public init<Capture>(_ regex: Regex<Capture>) {
10-
self.vm = .init(program: try! Compiler(ast: regex.ast).emit())
11+
do {
12+
self.vm = .init(program: try Compiler(ast: regex.ast).emit())
13+
} catch {
14+
fatalError("error: \(error)")
15+
}
16+
}
17+
18+
public init(parsing regex: String) throws {
19+
self.vm = try _compileRegex(regex)
1120
}
1221

1322
func _consuming(

Sources/_StringProcessing/Compiler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class Compiler {
202202
(.upToN, _),
203203
(.range, _),
204204
(_, .possessive):
205-
fatalError("Not yet supported")
205+
throw unsupported("\(quant._dumpBase)")
206206
}
207207
}
208208
}

Tests/RegexTests/MatchTests.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ func matchTest(
1111
dumpAST: Bool = false,
1212
xfail: Bool = false
1313
) {
14-
guard !xfail else {
15-
// XCTExpectFailure {
16-
// XCTFail()
17-
// }
14+
do {
15+
var consumer = try RegexConsumer<String>(parsing: regex)
16+
consumer.vm.engine.enableTracing = enableTracing
17+
guard let range = input.firstRange(of: consumer) else {
18+
throw "expect xfail"
19+
}
20+
21+
if xfail {
22+
XCTAssertNotEqual(String(input[range]), match)
23+
} else {
24+
XCTAssertEqual(String(input[range]), match)
25+
}
26+
} catch {
27+
XCTAssert(xfail)
1828
return
1929
}
20-
21-
let pattern = try! Regex(regex)
22-
var consumer = RegexConsumer<String>(pattern)
23-
consumer.vm.engine.enableTracing = enableTracing
24-
let range = input.firstRange(of: consumer)!
25-
XCTAssertEqual(String(input[range]), match)
2630
}
2731

2832
extension RegexTests {
@@ -120,7 +124,7 @@ extension RegexTests {
120124
matchTest(
121125
#"a{2,}"#, input: "123aaaxyz", match: "aaa", xfail: true)
122126
matchTest(
123-
#"a{1}"#, input: "123aaaxyz", match: "a", xfail: true)
127+
#"a{1}"#, input: "123aaaxyz", match: "a")
124128
matchTest(
125129
#"a{1,2}?"#, input: "123aaaxyz", match: "a", xfail: true)
126130
matchTest(

0 commit comments

Comments
 (0)