Skip to content

Add test fixtures for renderAsBuilderDSL #423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions Tests/RegexTests/RenderDSLTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import XCTest
import _RegexParser
@_spi(PatternConverter) @testable
import _StringProcessing

class RenderDSLTests: XCTestCase {}

func testConversion(
_ regex: String,
_ expectedDSL: String,
file: StaticString = #file, line: UInt = #line
) throws {
let ast = try _RegexParser.parse(regex, .semantic, .traditional)
let actualDSL = renderAsBuilderDSL(ast: ast)._trimmingSuffix(while: \.isWhitespace)
XCTAssertEqual(actualDSL, expectedDSL[...], file: file, line: line)
}

extension RenderDSLTests {
func testSimpleConversions() throws {
try testConversion(#"ab+c"#, """
Regex {
"a"
OneOrMore {
"b"
}
"c"
}
""")

try testConversion(#"(?:a*)b?(c+)"#, """
Regex {
ZeroOrMore {
"a"
}
Optionally {
"b"
}
Capture {
OneOrMore {
"c"
}
}
}
""")

try testConversion(#"\d+"#, """
Regex {
OneOrMore {
.digit
}
}
""")
try XCTExpectFailure("Invalid leading dot syntax in non-initial position") {
try testConversion(#":\d:"#, """
Regex {
":"
CharacterClass.digit
":"
}
""")
}
}

func testOptions() throws {
try XCTExpectFailure("Options like '(?i)' aren't converted") {
try testConversion(#"(?i)abc"#, """
Regex {
"abc"
}.ignoresCase()
""")
}

try XCTExpectFailure("Options like '(?i:...)' aren't converted") {
try testConversion(#"(?i:abc)"#, """
Regex {
"abc"
}.ignoresCase()
""")
}
}

func testAlternations() throws {
try testConversion(#"a|b"#, """
Regex {
ChoiceOf {
"a"
"b"
}
}
""")

try XCTExpectFailure("Concatenations in alternations aren't grouped") {
try testConversion(#"\da|b"#, """
Regex {
ChoiceOf {
Regex {
.digit
"a"
}
"bc"
}
}
""")
}
}
}
2 changes: 1 addition & 1 deletion Tests/RegexTests/UTS18Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fileprivate func expectFirstMatch<Output: Equatable>(
}

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

// MARK: - Basic Unicode Support: Level 1
Expand Down