Skip to content

Fix dumping logic for tests #82

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
Dec 16, 2021
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
17 changes: 14 additions & 3 deletions Sources/_MatchingEngine/Regex/AST/ASTProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ extension _ASTPrintable {
public var debugDescription: String { _dump() }

var _children: [AST]? {
(self as? _ASTParent)?.children
if let children = (self as? _ASTParent)?.children {
return children
}
if let children = (self as? AST)?.children {
return children
}
return nil
}

func _print() -> String {
Expand All @@ -62,8 +68,13 @@ extension _ASTPrintable {
guard let children = _children else {
return _dumpBase
}
let sub = children.lazy.map {
$0._dump()
let sub = children.lazy.compactMap {
// Exclude trivia for now, as we don't want it to appear when performing
// comparisons of dumped output in tests.
// TODO: We should eventually have some way of filtering out trivia for
// tests, so that it can appear in regular dumps.
if $0.isTrivia { return nil }
return $0._dump()
}.joined(separator: ",")
return "\(_dumpBase)(\(sub))"
}
Expand Down
15 changes: 13 additions & 2 deletions Sources/_MatchingEngine/Regex/AST/CustomCharClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ extension CustomCC {

extension CustomCC: _ASTNode {
public var _dumpBase: String {
// FIXME: print out members...
"customCharacterClass"
"customCharacterClass(\(members))"
}
}

extension CustomCC.Member: _ASTPrintable {
public var _dumpBase: String {
switch self {
case .custom(let cc): return "\(cc)"
case .atom(let a): return "\(a)"
case .range(let lhs, let rhs):
return "range \(lhs) to \(rhs)"
case .setOperation(let lhs, let op, let rhs):
return "op \(lhs) \(op.value) \(rhs)"
}
}
}
2 changes: 1 addition & 1 deletion Sources/_MatchingEngine/Regex/AST/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extension AST.Group.Kind: _ASTPrintable {
public var _dumpBase: String {
switch self {
case .capture: return "capture"
case .namedCapture(let s): return "capture<\(s)>"
case .namedCapture(let s): return "capture<\(s.value)>"
case .nonCapture: return "nonCapture"
case .nonCaptureReset: return "nonCaptureReset"
case .atomicNonCapturing: return "atomicNonCapturing"
Expand Down
41 changes: 41 additions & 0 deletions Tests/RegexTests/ParseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ func parseWithDelimitersTest(_ input: String, _ expecting: AST) {
}
}

/// Make sure the AST for two regex strings get compared differently.
func parseNotEqualTest(_ lhs: String, _ rhs: String,
syntax: SyntaxOptions = .traditional) {
let lhsAST = try! parse(lhs, syntax)
let rhsAST = try! parse(rhs, syntax)
if lhsAST == rhsAST || lhsAST._dump() == rhsAST._dump() {
XCTFail("""
AST: \(lhsAST._dump())
Should not be equal to: \(rhsAST._dump())
""")
}
}

extension RegexTests {
func testParse() {
parseTest(
Expand Down Expand Up @@ -194,6 +207,16 @@ extension RegexTests {
parseTest("[[:word:]]", charClass(posixProp_m(.posix(.word))))
parseTest("[[:xdigit:]]", charClass(posixProp_m(.posix(.xdigit))))

parseTest("[[:ascii:]]", charClass(posixProp_m(.ascii)))
parseTest("[[:cntrl:]]", charClass(posixProp_m(.generalCategory(.control))))
parseTest("[[:digit:]]", charClass(posixProp_m(.generalCategory(.decimalNumber))))
parseTest("[[:lower:]]", charClass(posixProp_m(.binary(.lowercase))))
parseTest("[[:punct:]]", charClass(posixProp_m(.generalCategory(.punctuation))))
parseTest("[[:space:]]", charClass(posixProp_m(.binary(.whitespace))))
parseTest("[[:upper:]]", charClass(posixProp_m(.binary(.uppercase))))

parseTest("[[:UPPER:]]", charClass(posixProp_m(.binary(.uppercase))))

parseTest("[[:isALNUM:]]", charClass(posixProp_m(.posix(.alnum))))
parseTest("[[:AL_NUM:]]", charClass(posixProp_m(.posix(.alnum))))
parseTest("[[:script=Greek:]]", charClass(posixProp_m(.script(.greek))))
Expand Down Expand Up @@ -436,6 +459,24 @@ extension RegexTests {
parseWithDelimitersTest("'/a b/'", concat("a", " ", "b"))
parseWithDelimitersTest("'|a b|'", concat("a", "b"))

// Make sure dumping output correctly reflects differences in AST.
parseNotEqualTest(#"abc"#, #"abd"#)

parseNotEqualTest(#"[abc[:space:]\d]+"#,
#"[abc[:upper:]\d]+"#)

parseNotEqualTest(#"[abc[:space:]\d]+"#,
#"[ac[:space:]\d]+"#)

parseNotEqualTest(#"[abc[:space:]\d]+"#,
#"[acc[:space:]\s]+"#)

parseNotEqualTest(#"[abc[:space:]\d]+"#,
#"[acc[:space:]\d]*"#)

parseNotEqualTest(#"([a-c&&e]*)+"#,
#"([a-d&&e]*)+"#)

// TODO: failure tests
}

Expand Down
4 changes: 3 additions & 1 deletion Tests/RegexTests/SyntaxOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ private let dplus = oneOrMore(
.greedy, atom(.escaped(.decimalDigit)))
private let dotAST = concat(
dplus, ".", dplus, ".", dplus, ".", dplus)
private let dotASTQuoted = concat(
dplus, quote("."), dplus, quote("."), dplus, quote("."), dplus)

extension RegexTests {

Expand Down Expand Up @@ -42,7 +44,7 @@ extension RegexTests {
dotAST, syntax: .modern)
parseTest(
#" \d+ "." \d+ "." \d+ "." \d+ "#,
dotAST, syntax: .modern)
dotASTQuoted, syntax: .modern)
}

func testModernRanges() {
Expand Down