Files
swift-mirror/test/StringProcessing/Runtime/regex_basic.swift
Hamish Knight 128f5d4bc6 Update regex literal lexing and emission
Update the lexing implementation to defer to the
regex library, which will pass back the pointer
from to resume lexing, and update the emission to
call the new `Regex(_regexString:version:)`
overload, that will accept the regex string with
delimiters.

Because this uses the library's lexing
implementation, the delimiters are now `'/.../'`
and `'|...|'` instead of plain `'...'`.
2021-12-17 18:05:31 +00:00

61 lines
1.5 KiB
Swift

// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-string-processing)
// REQUIRES: libswift,string_processing,executable_test
import StdlibUnittest
var RegexBasicTests = TestSuite("RegexBasic")
extension String {
func expectMatch<T>(
_ regex: Regex<T>,
file: String = #file,
line: UInt = #line
) -> RegexMatch<T> {
guard let result = match(regex) else {
expectUnreachable("Failed match", file: file, line: line)
fatalError()
}
return result
}
}
RegexBasicTests.test("Basic") {
let input = "aabccd"
let match1 = input.expectMatch('/aabcc./')
expectEqual("aabccd", input[match1.range])
expectEqual(.empty, match1.captures)
let match2 = input.expectMatch('/a*b.+./')
expectEqual("aabccd", input[match2.range])
expectEqual(.empty, match2.captures)
}
RegexBasicTests.test("Modern") {
let input = "aabccd"
let match1 = input.expectMatch('|a a bc c /*hello*/ .|')
expectEqual("aabccd", input[match1.range])
expectEqual(.empty, match1.captures)
}
RegexBasicTests.test("Captures") {
let input = """
A6F0..A6F1 ; Extend # Mn [2] BAMUM COMBINING MARK KOQNDON..BAMUM \
COMBINING MARK TUKWENTIS
"""
let regex = '/([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*/'
let match1 = input.expectMatch(regex)
expectEqual(input[...], input[match1.range])
expectEqual(
.tuple([
.substring("A6F0"),
.optional(.substring("A6F1")),
.substring("Extend")
]),
match1.captures)
}
runAllTests()