Skip to content

Commit a9b7f7b

Browse files
committed
Add testing of correct semantic DB occurence positions
1 parent 50815da commit a9b7f7b

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

modules/build/src/test/scala/scala/build/tests/BuildTests.scala

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,37 @@ abstract class BuildTests(server: Boolean) extends munit.FunSuite {
146146
}
147147

148148
test("semantic DB") {
149-
val testInputs = TestInputs(
150-
os.rel / "simple.sc" ->
151-
"""val n = 2
152-
|println(s"n=$n")
153-
|""".stripMargin
149+
import scala.meta.internal.semanticdb.*
150+
151+
val scriptContents =
152+
"""val n = 2
153+
|println(s"n=$n")
154+
|""".stripMargin
155+
156+
val expectedSymbolOccurences = Seq(
157+
SymbolOccurrence(
158+
Some(Range(0, 4, 0, 5)),
159+
"_empty_/simple.n.",
160+
SymbolOccurrence.Role.DEFINITION
161+
),
162+
SymbolOccurrence(
163+
Some(Range(1, 8, 1, 9)),
164+
"scala/StringContext#s().",
165+
SymbolOccurrence.Role.REFERENCE
166+
),
167+
SymbolOccurrence(
168+
Some(Range(1, 0, 1, 7)),
169+
"scala/Predef.println(+1).",
170+
SymbolOccurrence.Role.REFERENCE
171+
),
172+
SymbolOccurrence(
173+
Some(Range(1, 13, 1, 14)),
174+
"_empty_/simple.n.",
175+
SymbolOccurrence.Role.REFERENCE
176+
)
154177
)
178+
179+
val testInputs = TestInputs(os.rel / "simple.sc" -> scriptContents)
155180
val buildOptions = defaultOptions.copy(
156181
scalaOptions = defaultOptions.scalaOptions.copy(
157182
generateSemanticDbs = Some(true)
@@ -173,6 +198,23 @@ abstract class BuildTests(server: Boolean) extends munit.FunSuite {
173198
val doc = TextDocuments.parseFrom(semDb)
174199
val uris = doc.documents.map(_.uri)
175200
expect(uris == Seq("simple.sc"))
201+
202+
val occurences = doc.documents.flatMap(_.occurrences)
203+
expect(occurences.forall(_.range.isDefined))
204+
205+
val sortedOccurences = doc.documents.flatMap(_.occurrences)
206+
.sortBy(s =>
207+
s.range.map(r => (r.startLine, r.startCharacter)).getOrElse((Int.MaxValue, Int.MaxValue))
208+
)
209+
val sortedExpectedOccurences = expectedSymbolOccurences
210+
.sortBy(s =>
211+
s.range.map(r => (r.startLine, r.startCharacter)).getOrElse((Int.MaxValue, Int.MaxValue))
212+
)
213+
214+
munit.Assertions.assert(
215+
sortedOccurences == sortedExpectedOccurences,
216+
clue = doc.documents.flatMap(_.occurrences)
217+
)
176218
}
177219
}
178220

0 commit comments

Comments
 (0)