Skip to content

Commit 908c9bf

Browse files
committed
Add XCTAssert* methods to make sure a tree has a certain subtree
1 parent 855592a commit 908c9bf

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Sources/_SwiftSyntaxTestSupport/Syntax+Assertions.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ public func XCTAssertSameStructure(
5959
XCTAssertNil(diff, diff!.debugDescription, file: file, line: line)
6060
}
6161

62+
/// See `SubtreeMatcher.assertSameStructure`.
63+
public func XCTAssertHasSubstructure(
64+
_ markedText: String,
65+
parse: (String) throws -> Syntax,
66+
afterMarker: String? = nil,
67+
_ expected: SyntaxProtocol,
68+
includeTrivia: Bool = false,
69+
file: StaticString = #filePath, line: UInt = #line
70+
) throws {
71+
let subtreeMatcher = try SubtreeMatcher(markedText, parse: parse)
72+
try subtreeMatcher.assertSameStructure(afterMarker: afterMarker, Syntax(expected), file: file, line: line)
73+
}
74+
75+
/// See `SubtreeMatcher.assertSameStructure`.
76+
public func XCTAssertHasSubstructure(
77+
_ actualTree: SyntaxProtocol,
78+
_ expected: SyntaxProtocol,
79+
includeTrivia: Bool = false,
80+
file: StaticString = #filePath, line: UInt = #line
81+
) throws {
82+
let subtreeMatcher = SubtreeMatcher(Syntax(actualTree))
83+
try subtreeMatcher.assertSameStructure(Syntax(expected), file: file, line: line)
84+
}
85+
6286
/// Allows matching a subtrees of the given `markedText` against
6387
/// `baseline`/`expected` trees, where a combination of markers and the type
6488
/// of the `expected` tree is used to first find the subtree to match. Note
@@ -123,6 +147,11 @@ public struct SubtreeMatcher {
123147
self.actualTree = try parse(text)
124148
}
125149

150+
public init(_ actualTree: Syntax) {
151+
self.markers = ["DEFAULT": 0]
152+
self.actualTree = actualTree
153+
}
154+
126155
/// Same as `Syntax.findFirstDifference(baseline:includeTrivia:)`, but
127156
/// matches against the first subtree from parsing `markedText` that is after
128157
/// `afterMarker` with the root matching the root type of `baseline`.

0 commit comments

Comments
 (0)