Skip to content

Commit 83e1ca3

Browse files
committed
Add SwiftLexicalLookup cache unit testing.
1 parent e7c02d8 commit 83e1ca3

File tree

2 files changed

+117
-26
lines changed

2 files changed

+117
-26
lines changed

Sources/SwiftLexicalLookup/Scopes/ScopeSyntax.swift

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,57 @@ extension SyntaxProtocol {
5050
with config: LookupConfig = LookupConfig(),
5151
cache: LookupCache? = nil
5252
) -> [LookupResult] {
53-
scope?.lookup(identifier, at: self.position, with: config, cache: cache) ?? []
53+
if let cache, let identifier {
54+
let filteredResult: [LookupResult] = (scope?.lookup(nil, at: self.position, with: config, cache: cache) ?? [])
55+
.compactMap { result in
56+
switch result {
57+
case .fromScope(let syntax, let withNames):
58+
let filteredNames =
59+
withNames
60+
.filter { name in
61+
name.identifier == identifier
62+
}
63+
64+
guard filteredNames.count != 0 else { return nil }
65+
return .fromScope(syntax, withNames: filteredNames)
66+
default:
67+
return result
68+
}
69+
}
70+
71+
var resultWithMergedSequentialResults: [LookupResult] = []
72+
var i = 0
73+
74+
while i < filteredResult.count {
75+
let thisResult = filteredResult[i]
76+
77+
if i < filteredResult.count - 1,
78+
case .fromScope(let thisScope, let thisNames) = thisResult,
79+
thisScope.asProtocol(SyntaxProtocol.self) is SequentialScopeSyntax
80+
{
81+
var accumulator = thisNames
82+
83+
while i < filteredResult.count - 1,
84+
case .fromScope(let otherScope, let otherNames) = filteredResult[i + 1],
85+
otherScope.asProtocol(SyntaxProtocol.self) is SequentialScopeSyntax,
86+
thisScope.id == otherScope.id
87+
{
88+
accumulator += otherNames
89+
i += 1
90+
}
91+
92+
resultWithMergedSequentialResults.append(.fromScope(thisScope, withNames: accumulator))
93+
} else {
94+
resultWithMergedSequentialResults.append(thisResult)
95+
}
96+
97+
i += 1
98+
}
99+
100+
return resultWithMergedSequentialResults
101+
} else {
102+
return scope?.lookup(identifier, at: self.position, with: config, cache: cache) ?? []
103+
}
54104
}
55105
}
56106

Tests/SwiftLexicalLookupTest/Assertions.swift

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,37 +90,78 @@ func assertLexicalNameLookup(
9090
useNilAsTheParameter: Bool = false,
9191
config: LookupConfig = LookupConfig()
9292
) {
93+
let expectedResults = references.mapValues { expectations in
94+
expectations.flatMap { expectation in
95+
expectation.expectedNames.flatMap { expectedName in
96+
expectedName.marker
97+
}
98+
}
99+
}
100+
101+
// Perform test without cache
102+
assertLexicalScopeQuery(
103+
source: source,
104+
methodUnderTest: { marker, tokenAtMarker in
105+
testFunction(
106+
marker: marker,
107+
tokenAtMarker: tokenAtMarker,
108+
references: references,
109+
useNilAsTheParameter: useNilAsTheParameter,
110+
config: config,
111+
cache: nil
112+
)
113+
},
114+
expected: expectedResults,
115+
expectedResultTypes: expectedResultTypes
116+
)
117+
118+
// Perform test with cache
119+
let cache = LookupCache()
93120
assertLexicalScopeQuery(
94121
source: source,
95122
methodUnderTest: { marker, tokenAtMarker in
96-
let lookupIdentifier = Identifier(tokenAtMarker)
123+
testFunction(
124+
marker: marker,
125+
tokenAtMarker: tokenAtMarker,
126+
references: references,
127+
useNilAsTheParameter: useNilAsTheParameter,
128+
config: config,
129+
cache: cache
130+
)
131+
},
132+
expected: expectedResults,
133+
expectedResultTypes: expectedResultTypes
134+
)
135+
}
97136

98-
let result = tokenAtMarker.lookup(useNilAsTheParameter ? nil : lookupIdentifier, with: config)
137+
/// Asserts result of unqualified lookup for the given `marker` and `tokenAtMarker`.
138+
/// Returns flattened array of syntax nodes returned by the query.
139+
private func testFunction(
140+
marker: String,
141+
tokenAtMarker: TokenSyntax,
142+
references: [String: [ResultExpectation]],
143+
useNilAsTheParameter: Bool,
144+
config: LookupConfig,
145+
cache: LookupCache?
146+
) -> [SyntaxProtocol] {
147+
let lookupIdentifier = Identifier(tokenAtMarker)
99148

100-
guard let expectedValues = references[marker] else {
101-
XCTFail("For marker \(marker), couldn't find result expectation")
102-
return []
103-
}
149+
let result = tokenAtMarker.lookup(useNilAsTheParameter ? nil : lookupIdentifier, with: config, cache: cache)
104150

105-
ResultExpectation.assertResult(marker: marker, result: result, expectedValues: expectedValues)
151+
guard let expectedValues = references[marker] else {
152+
XCTFail("For marker \(marker), couldn't find result expectation")
153+
return []
154+
}
106155

107-
return result.flatMap { lookUpResult in
108-
lookUpResult.names.flatMap { lookupName in
109-
if case .equivalentNames(let names) = lookupName {
110-
return names.map(\.syntax)
111-
} else {
112-
return [lookupName.syntax]
113-
}
114-
}
115-
}
116-
},
117-
expected: references.mapValues { expectations in
118-
expectations.flatMap { expectation in
119-
expectation.expectedNames.flatMap { expectedName in
120-
expectedName.marker
121-
}
156+
ResultExpectation.assertResult(marker: marker, result: result, expectedValues: expectedValues)
157+
158+
return result.flatMap { lookUpResult in
159+
lookUpResult.names.flatMap { lookupName in
160+
if case .equivalentNames(let names) = lookupName {
161+
return names.map(\.syntax)
162+
} else {
163+
return [lookupName.syntax]
122164
}
123-
},
124-
expectedResultTypes: expectedResultTypes
125-
)
165+
}
166+
}
126167
}

0 commit comments

Comments
 (0)