Skip to content

Add tests for AnyRegexOutput #371

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 3 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 16 additions & 6 deletions Sources/_StringProcessing/Regex/AnyRegexOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public struct AnyRegexOutput {
/// The depth of `Optioals`s wrapping the underlying value. For example,
/// `Substring` has optional depth `0`, and `Int??` has optional depth `2`.
let optionalDepth: Int

/// The bounds of the output element.
let bounds: Range<String.Index>?
}
Expand Down Expand Up @@ -90,7 +91,7 @@ extension AnyRegexOutput {
/// - Parameter type: The expected output type.
/// - Returns: The output, if the underlying value can be converted to the
/// output type; otherwise `nil`.
public func `as`<Output>(_ type: Output.Type) -> Output? {
public func `as`<Output>(_ type: Output.Type = Output.self) -> Output? {
let elements = _elements.map {
StructuredCapture(
optionalCount: $0.optionalDepth,
Expand Down Expand Up @@ -206,23 +207,30 @@ extension Regex.Match where Output == AnyRegexOutput {
/// - Parameter type: The expected output type.
/// - Returns: A match generic over the output type, if the underlying values
/// can be converted to the output type; otherwise, `nil`.
public func `as`<Output>(_ type: Output.Type) -> Regex<Output>.Match? {
public func `as`<Output>(
_ type: Output.Type = Output.self
) -> Regex<Output>.Match? {
fatalError("FIXME: Not implemented")
}
}

@available(SwiftStdlib 5.7, *)
extension Regex where Output == AnyRegexOutput {
extension Regex {
/// Returns whether a named-capture with `name` exists
public func contains(captureNamed name: String) -> Bool {
fatalError("FIXME: not implemented")
program.tree.root._captureList.captures.contains(where: {
$0.name == name
})
}
}

@available(SwiftStdlib 5.7, *)
extension Regex where Output == AnyRegexOutput {
/// Creates a type-erased regex from an existing regex.
///
/// Use this initializer to fit a regex with strongly typed captures into the
/// use site of a dynamic regex, i.e. one that was created from a string.
public init<Output>(_ match: Regex<Output>) {
public init<Output>(_ regex: Regex<Output>) {
fatalError("FIXME: Not implemented")
}

Expand All @@ -231,7 +239,9 @@ extension Regex where Output == AnyRegexOutput {
/// - Parameter type: The expected output type.
/// - Returns: A regex generic over the output type if the underlying types can be converted.
/// Returns `nil` otherwise.
public func `as`<Output>(_ type: Output.Type) -> Regex<Output>? {
public func `as`<Output>(
_ type: Output.Type = Output.self
) -> Regex<Output>? {
fatalError("FIXME: Not implemented")
}
}
120 changes: 120 additions & 0 deletions Tests/RegexTests/AnyRegexOutputTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

import _StringProcessing
import XCTest

// Test that our existential capture and concrete captures are
// the same
private func checkSame(
_ aro: AnyRegexOutput,
_ concrete: (Substring, fieldA: Substring, fieldB: Substring)
) {
XCTAssertEqual(aro[0].substring, concrete.0)

XCTAssertEqual(aro["fieldA"]!.substring, concrete.1)
XCTAssertEqual(aro["fieldA"]!.substring, concrete.fieldA)

XCTAssertEqual(aro[1].substring, concrete.1)

XCTAssertEqual(aro["fieldB"]!.substring, concrete.2)
XCTAssertEqual(aro["fieldB"]!.substring, concrete.fieldB)

XCTAssertEqual(aro[2].substring, concrete.2)

}
private func checkSame(
_ aro: Regex<AnyRegexOutput>.Match,
_ concrete: Regex<(Substring, fieldA: Substring, fieldB: Substring)>.Match
) {
checkSame(aro.output, concrete.output)

XCTAssertEqual(aro.0, concrete.0)
XCTAssertEqual(aro[0].substring, concrete.0)

XCTAssertEqual(aro["fieldA"]!.substring, concrete.1)
XCTAssertEqual(aro["fieldA"]!.substring, concrete.fieldA)
XCTAssertEqual(aro[1].substring, concrete.1)

XCTAssertEqual(aro["fieldB"]!.substring, concrete.2)
XCTAssertEqual(aro["fieldB"]!.substring, concrete.fieldB)
XCTAssertEqual(aro[2].substring, concrete.2)
}
private func checkSame(
_ aro: Regex<AnyRegexOutput>,
_ concrete: Regex<(Substring, fieldA: Substring, fieldB: Substring)>
) {
XCTAssertEqual(
aro.contains(captureNamed: "fieldA"),
concrete.contains(captureNamed: "fieldA"))
XCTAssertEqual(
aro.contains(captureNamed: "fieldB"),
concrete.contains(captureNamed: "fieldB"))
XCTAssertEqual(
aro.contains(captureNamed: "notAField"),
concrete.contains(captureNamed: "notAField"))
}

extension RegexTests {
func testFoo1() {
let regex = try! Regex(#"""
(?x)
(?<fieldA> [^,]*)
,
(?<fieldB> [^,]*)
"""#)

let match = "abc,def".wholeMatch(of: regex)!
XCTAssertEqual(match.0, "abc,def")
XCTAssertEqual(match[0].substring, "abc,def")

XCTAssertEqual(match["fieldA"]!.substring, "abc")
XCTAssertEqual(match.output["fieldA"]!.substring, "abc")
XCTAssertEqual(match[1].substring, "abc")

XCTAssertEqual(match["fieldB"]!.substring, "def")
XCTAssertEqual(match.output["fieldB"]!.substring, "def")
XCTAssertEqual(match[2].substring, "def")

XCTAssertNil(match["notACapture"])
XCTAssertNil(match.output["notACapture"])
XCTAssertEqual(match.count, 3)

XCTAssert(regex.contains(captureNamed: "fieldA"))
XCTAssert(regex.contains(captureNamed: "fieldB"))
XCTAssertFalse(regex.contains(captureNamed: "notAField"))

// MARK: Check equivalence with concrete

let regexConcrete:
Regex<(Substring, fieldA: Substring, fieldB: Substring)>
= try! Regex(#"""
(?x)
(?<fieldA> [^,]*)
,
(?<fieldB> [^,]*)
"""#)
checkSame(regex, regexConcrete)

let matchConcrete = "abc,def".wholeMatch(of: regexConcrete)!
checkSame(match, matchConcrete)

let output = match.output
let concreteOutput = matchConcrete.output
checkSame(output, concreteOutput)

// TODO: ARO init from concrete match tuple

let concreteOutputCasted = output.as(
(Substring, fieldA: Substring, fieldB: Substring).self
)!
checkSame(output, concreteOutputCasted)

var concreteOutputCopy = concreteOutput
concreteOutputCopy = output.as()!
checkSame(output, concreteOutputCopy)

// TODO: Regex<ARO>.Match: init from tuple match and as to tuple match

// TODO: Regex<ARO>: init from tuple regex and as cast to tuple regex

}
}