1010//
1111//===----------------------------------------------------------------------===//
1212
13- import Foundation
1413@_spi ( Experimental) import SwiftLexicalLookup
1514import SwiftParser
1615import SwiftSyntax
1716import XCTest
1817import _SwiftSyntaxTestSupport
1918
20- /// Used to define result type expectectations for given markers.
21- enum MarkerExpectation {
22- /// Specifies a separate type for each result marker.
23- case distinct( [ String : SyntaxProtocol . Type ] )
24- /// Specifies a common type for all results
25- /// apart from the ones defined explicitly in `except`.
26- case all( SyntaxProtocol . Type , except: [ String : SyntaxProtocol . Type ] = [ : ] )
27- /// Does not assert result types.
28- case none
29-
30- /// Assert `actual` result labeled with `marker`
31- /// according to the rules represented by this expectation.
32- fileprivate func assertMarkerType( marker: String , actual: SyntaxProtocol ) {
33- switch self {
34- case . all( let expectedType, except: let dictionary) :
35- assertMarkerType ( marker: marker, actual: actual, expectedType: dictionary [ marker] ?? expectedType)
36- case . distinct( let dictionary) :
37- if let expectedType = dictionary [ marker] {
38- assertMarkerType ( marker: marker, actual: actual, expectedType: expectedType)
39- } else {
40- XCTFail ( " For result \( marker) , could not find type expectation " )
41- }
42- case . none:
43- break
44- }
45- }
46-
47- /// Assert whether `actual` type matches `expectedType`.
48- private func assertMarkerType( marker: String , actual: SyntaxProtocol , expectedType: SyntaxProtocol . Type ) {
49- XCTAssert (
50- actual. is ( expectedType) ,
51- " For result \( marker) , expected type \( expectedType) doesn't match the actual type \( actual. syntaxNodeType) "
52- )
53- }
54- }
55-
56- /// Used to define
57- enum ResultExpectation {
58- case fromScope( ScopeSyntax . Type , expectedNames: [ String ] )
59- case fromFileScope( expectedNames: [ String ] )
60-
61- var expectedNames : [ String ] {
62- switch self {
63- case . fromScope( _, let expectedNames) :
64- expectedNames
65- case . fromFileScope( expectedNames: let expectedNames) :
66- expectedNames
67- }
68- }
69-
70- var debugDescription : String {
71- switch self {
72- case . fromScope:
73- " fromScope "
74- case . fromFileScope:
75- " fromFileScope "
76- }
77- }
78- }
79-
80- extension LookupResult {
81- var debugDescription : String {
82- switch self {
83- case . fromScope:
84- " fromScope "
85- case . fromFileScope:
86- " fromFileScope "
87- }
88- }
89- }
90-
9119/// `methodUnderTest` is called with the token at every position marker in the keys of `expected`.
9220/// It then asserts that the positions of the syntax nodes returned by `methodUnderTest` are the values in `expected`.
9321/// It also checks whether result types match rules specified in `expectedResultTypes`.
@@ -185,21 +113,7 @@ func assertLexicalNameLookup(
185113 return [ ]
186114 }
187115
188- for (actual, expected) in zip ( result, expectedValues) {
189- switch ( actual, expected) {
190- case ( . fromScope( let scope, withNames: _) , . fromScope( let expectedType, expectedNames: _) ) :
191- XCTAssert (
192- scope. syntaxNodeType == expectedType,
193- " For marker \( marker) , scope result type of \( scope. syntaxNodeType) doesn't match expected \( expectedType) "
194- )
195- case ( . fromFileScope, . fromFileScope) :
196- break
197- default :
198- XCTFail (
199- " For marker \( marker) , actual result kind \( actual. debugDescription) doesn't match expected \( expected. debugDescription) "
200- )
201- }
202- }
116+ ResultExpectation . assertResult ( marker: marker, result: result, expectedValues: expectedValues)
203117
204118 return result. flatMap { lookUpResult in
205119 lookUpResult. names. map { lookupName in
@@ -209,7 +123,9 @@ func assertLexicalNameLookup(
209123 } ,
210124 expected: references. mapValues { expectations in
211125 expectations. flatMap { expectation in
212- expectation. expectedNames
126+ expectation. expectedNames. map { expectedName in
127+ expectedName. marker
128+ }
213129 }
214130 } ,
215131 expectedResultTypes: expectedResultTypes
0 commit comments