15
15
//===----------------------------------------------------------------------===//
16
16
17
17
import SwiftSyntax
18
- import SwiftSyntaxParser
19
18
import XCTest
20
19
21
20
/// Verifies that there is a next item returned by the iterator and that it
@@ -35,22 +34,55 @@ public func XCTAssertNextIsNil<Iterator: IteratorProtocol>(_ iterator: inout Ite
35
34
}
36
35
37
36
/// Verifies that the tree parsed from `actual` has the same structure as
38
- /// `expected`, ie. it has the same structure and optionally the same trivia
39
- /// (if `includeTrivia` is set).
40
- public func XCTAssertSameStructure( _ actual: String , _ expected: Syntax , includeTrivia: Bool = false ,
41
- file: StaticString = #filePath, line: UInt = #line) throws {
42
- let actualTree = try Syntax ( SyntaxParser . parse ( source: actual) )
37
+ /// `expected` when parsed with `parse`, ie. it has the same structure and
38
+ /// optionally the same trivia (if `includeTrivia` is set).
39
+ public func XCTAssertSameStructure(
40
+ _ actual: String ,
41
+ parse: ( String ) throws -> Syntax ,
42
+ _ expected: Syntax ,
43
+ includeTrivia: Bool = false ,
44
+ file: StaticString = #filePath, line: UInt = #line
45
+ ) throws {
46
+ let actualTree = try parse ( actual)
43
47
XCTAssertSameStructure ( actualTree, expected, includeTrivia: includeTrivia, file: file, line: line)
44
48
}
45
49
46
50
/// Verifies that two trees are equivalent, ie. they have the same structure
47
51
/// and optionally the same trivia if `includeTrivia` is set.
48
- public func XCTAssertSameStructure( _ actual: Syntax , _ expected: Syntax , includeTrivia: Bool = false ,
49
- file: StaticString = #filePath, line: UInt = #line) {
52
+ public func XCTAssertSameStructure(
53
+ _ actual: SyntaxProtocol ,
54
+ _ expected: SyntaxProtocol ,
55
+ includeTrivia: Bool = false ,
56
+ file: StaticString = #filePath, line: UInt = #line
57
+ ) {
50
58
let diff = actual. findFirstDifference ( baseline: expected, includeTrivia: includeTrivia)
51
59
XCTAssertNil ( diff, diff!. debugDescription, file: file, line: line)
52
60
}
53
61
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
+
54
86
/// Allows matching a subtrees of the given `markedText` against
55
87
/// `baseline`/`expected` trees, where a combination of markers and the type
56
88
/// of the `expected` tree is used to first find the subtree to match. Note
@@ -98,7 +130,7 @@ public struct SubtreeMatcher {
98
130
/// The syntax tree from parsing source *with markers removed*.
99
131
private var actualTree : Syntax
100
132
101
- public init ( _ markedText: String ) throws {
133
+ public init ( _ markedText: String , parse : ( String ) throws -> Syntax ) throws {
102
134
var text = " "
103
135
var markers = [ String: Int] ( )
104
136
var lastIndex = markedText. startIndex
@@ -112,7 +144,12 @@ public struct SubtreeMatcher {
112
144
text += markedText [ lastIndex ..< markedText. endIndex]
113
145
114
146
self . markers = markers. isEmpty ? [ " DEFAULT " : 0 ] : markers
115
- self . actualTree = try Syntax ( SyntaxParser . parse ( source: text) )
147
+ self . actualTree = try parse ( text)
148
+ }
149
+
150
+ public init ( _ actualTree: Syntax ) {
151
+ self . markers = [ " DEFAULT " : 0 ]
152
+ self . actualTree = actualTree
116
153
}
117
154
118
155
/// Same as `Syntax.findFirstDifference(baseline:includeTrivia:)`, but
@@ -149,7 +186,7 @@ public enum SubtreeError: Error, CustomStringConvertible {
149
186
case let . invalidMarker( name) :
150
187
return " Could not find marker with name ' \( name) ' "
151
188
case let . invalidSubtree( tree, afterUTF8Offset, type) :
152
- return " Could not find subtree after UTF8 offset \( afterUTF8Offset) with type \( type) in: \n \( tree. debugDescription) "
189
+ return " Could not find subtree after UTF8 offset \( afterUTF8Offset) with type \( type) in: \n \( tree. debugDescription ( includeChildren : true ) ) "
153
190
}
154
191
}
155
192
}
0 commit comments