Skip to content

Commit b061ee5

Browse files
authored
Merge pull request #2027 from ahoppen/ahoppen/some-substructure
Change `substructure` parameter of `assertParse` to `some SyntaxProtocol`
2 parents c02bf11 + 9e3f9cd commit b061ee5

29 files changed

+1038
-1287
lines changed

Sources/_SwiftSyntaxTestSupport/IncrementalParseTestUtils.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ public func assertIncrementalParse(
7272
)
7373

7474
// Substructure
75-
let subtreeMatcher = SubtreeMatcher(Syntax(incrementallyParsedNewTree), markers: [:])
75+
let subtreeMatcher = SubtreeMatcher(incrementallyParsedNewTree, markers: [:])
7676
do {
77-
try subtreeMatcher.assertSameStructure(Syntax(newTree), includeTrivia: true, file: file, line: line)
77+
try subtreeMatcher.assertSameStructure(newTree, includeTrivia: true, file: file, line: line)
7878
} catch {
7979
XCTFail("Matching for a subtree failed with error: \(error)", file: file, line: line)
8080
}

Sources/_SwiftSyntaxTestSupport/Syntax+Assertions.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ public struct SubtreeMatcher {
8888
self.actualTree = try parse(text)
8989
}
9090

91-
public init(_ actualTree: Syntax, markers: [String: Int]) {
91+
public init(_ actualTree: some SyntaxProtocol, markers: [String: Int]) {
9292
self.markers = markers.isEmpty ? ["DEFAULT": 0] : markers
93-
self.actualTree = actualTree
93+
self.actualTree = Syntax(actualTree)
9494
}
9595

9696
/// Same as `Syntax.findFirstDifference(baseline:includeTrivia:)`, but
9797
/// matches against the first subtree from parsing `markedText` that is after
9898
/// `afterMarker` with the root matching the root type of `baseline`.
99-
public func findFirstDifference(afterMarker: String? = nil, baseline: Syntax, includeTrivia: Bool = false) throws -> TreeDifference? {
99+
public func findFirstDifference(afterMarker: String? = nil, baseline: some SyntaxProtocol, includeTrivia: Bool = false) throws -> TreeDifference? {
100100
let afterMarker = afterMarker ?? markers.first!.key
101101
guard let subtreeStart = markers[afterMarker] else {
102102
throw SubtreeError.invalidMarker(name: afterMarker)
@@ -113,7 +113,7 @@ public struct SubtreeMatcher {
113113
/// `init(markedText:)` has the same structure as `expected`.
114114
public func assertSameStructure(
115115
afterMarker: String? = nil,
116-
_ expected: Syntax,
116+
_ expected: some SyntaxProtocol,
117117
includeTrivia: Bool = false,
118118
additionalInfo: String? = nil,
119119
file: StaticString = #filePath,

Tests/SwiftOperatorsTest/OperatorTableTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ extension OperatorTable {
9090
XCTAssertFalse(parenthesizedSyntax.containsExprSequence)
9191

9292
// Make sure the two have the same structure.
93-
let subtreeMatcher = SubtreeMatcher(Syntax(foldedSyntax), markers: [:])
93+
let subtreeMatcher = SubtreeMatcher(foldedSyntax, markers: [:])
9494
do {
95-
try subtreeMatcher.assertSameStructure(Syntax(parenthesizedSyntax))
95+
try subtreeMatcher.assertSameStructure(parenthesizedSyntax)
9696
} catch {
9797
XCTFail("Matching for a subtree failed with error: \(error)")
9898
}

Tests/SwiftParserTest/Assertions.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public struct AssertParseOptions: OptionSet {
531531
/// parsing the resulting `String` as a ``SourceFileSyntax``.
532532
func assertParse(
533533
_ markedSource: String,
534-
substructure expectedSubstructure: Syntax? = nil,
534+
substructure expectedSubstructure: (some SyntaxProtocol)? = Optional<Syntax>.none,
535535
substructureAfterMarker: String = "START",
536536
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
537537
applyFixIts: [String]? = nil,
@@ -609,7 +609,7 @@ fileprivate func assertRoundTrip<S: SyntaxProtocol>(
609609
func assertParse<S: SyntaxProtocol>(
610610
_ markedSource: String,
611611
_ parse: (inout Parser) -> S,
612-
substructure expectedSubstructure: Syntax? = nil,
612+
substructure expectedSubstructure: (some SyntaxProtocol)? = Optional<Syntax>.none,
613613
substructureAfterMarker: String = "START",
614614
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
615615
applyFixIts: [String]? = nil,
@@ -648,11 +648,11 @@ func assertParse<S: SyntaxProtocol>(
648648

649649
// Substructure
650650
if let expectedSubstructure {
651-
let subtreeMatcher = SubtreeMatcher(Syntax(tree), markers: markerLocations)
651+
let subtreeMatcher = SubtreeMatcher(tree, markers: markerLocations)
652652
do {
653653
try subtreeMatcher.assertSameStructure(
654654
afterMarker: substructureAfterMarker,
655-
Syntax(expectedSubstructure),
655+
expectedSubstructure,
656656
includeTrivia: options.contains(.substructureCheckTrivia),
657657
file: file,
658658
line: line
@@ -760,9 +760,9 @@ func assertBasicFormat<S: SyntaxProtocol>(
760760
let formattedReparsed = Syntax(parse(&formattedParser))
761761

762762
do {
763-
let subtreeMatcher = SubtreeMatcher(Syntax(formattedReparsed), markers: [:])
763+
let subtreeMatcher = SubtreeMatcher(formattedReparsed, markers: [:])
764764
try subtreeMatcher.assertSameStructure(
765-
Syntax(sourceTree),
765+
sourceTree,
766766
includeTrivia: false,
767767
additionalInfo: """
768768
Removing trivia, formatting using BasicFormat and re-parsing did not produce the same syntax tree.

Tests/SwiftParserTest/AttributeTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ final class AttributeTests: XCTestCase {
149149
@1️⃣rethrows
150150
protocol P { }
151151
""",
152-
substructure: Syntax(TokenSyntax.identifier("rethrows")),
152+
substructure: TokenSyntax.identifier("rethrows"),
153153
substructureAfterMarker: "1️⃣"
154154
)
155155
}
@@ -327,7 +327,7 @@ final class AttributeTests: XCTestCase {
327327
assertParse(
328328
"@_implements(1️⃣\(baseType), f())",
329329
AttributeSyntax.parse,
330-
substructure: Syntax(TypeSyntax.parse(from: &parser)),
330+
substructure: TypeSyntax.parse(from: &parser),
331331
substructureAfterMarker: "1️⃣",
332332
line: line
333333
)

Tests/SwiftParserTest/AvailabilityTests.swift

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,8 @@ final class AvailabilityTests: XCTestCase {
106106
@available(OSX 10)
107107
func test() {}
108108
""",
109-
substructure: Syntax(
110-
VersionTupleSyntax(
111-
major: .integerLiteral("10")
112-
)
109+
substructure: VersionTupleSyntax(
110+
major: .integerLiteral("10")
113111
)
114112
)
115113

@@ -118,11 +116,9 @@ final class AvailabilityTests: XCTestCase {
118116
@available(OSX 10.0)
119117
func test() {}
120118
""",
121-
substructure: Syntax(
122-
VersionTupleSyntax(
123-
major: .integerLiteral("10"),
124-
components: VersionComponentListSyntax([VersionComponentSyntax(number: .integerLiteral("0"))])
125-
)
119+
substructure: VersionTupleSyntax(
120+
major: .integerLiteral("10"),
121+
components: VersionComponentListSyntax([VersionComponentSyntax(number: .integerLiteral("0"))])
126122
)
127123
)
128124

@@ -131,14 +127,12 @@ final class AvailabilityTests: XCTestCase {
131127
@available(OSX 10.0.1)
132128
func test() {}
133129
""",
134-
substructure: Syntax(
135-
VersionTupleSyntax(
136-
major: .integerLiteral("10"),
137-
components: VersionComponentListSyntax([
138-
VersionComponentSyntax(number: .integerLiteral("0")),
139-
VersionComponentSyntax(number: .integerLiteral("1")),
140-
])
141-
)
130+
substructure: VersionTupleSyntax(
131+
major: .integerLiteral("10"),
132+
components: VersionComponentListSyntax([
133+
VersionComponentSyntax(number: .integerLiteral("0")),
134+
VersionComponentSyntax(number: .integerLiteral("1")),
135+
])
142136
)
143137
)
144138

0 commit comments

Comments
 (0)