Skip to content

Commit

Permalink
improvement: Run fallback tests for both Scala 2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Aug 2, 2024
1 parent 8597999 commit 2ca70c3
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 285 deletions.
319 changes: 319 additions & 0 deletions tests/slow/src/test/scala/tests/feature/DefinitionCrossLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import scala.concurrent.Future

import scala.meta.internal.metals.BuildInfo
import scala.meta.internal.metals.InitializationOptions
import scala.meta.internal.metals.MetalsServerConfig

import munit.Location
import org.eclipse.{lsp4j => l}
Expand All @@ -21,6 +22,9 @@ class DefinitionCrossLspSuite
)
)

override def serverConfig: MetalsServerConfig = super.serverConfig.copy(
)

if (super.isValidScalaVersionForEnv(BuildInfo.scala211)) {
test("2.11") {
basicDefinitionTest(BuildInfo.scala211)
Expand Down Expand Up @@ -320,4 +324,319 @@ class DefinitionCrossLspSuite
_ = assert(locations.head.getUri().endsWith("a/Main.scala"))
} yield ()
}

for (version <- Seq(BuildInfo.scala213, BuildInfo.scala3)) {
test(s"fallback-to-workspace-search-$version") {
cleanWorkspace()
for {
_ <- initialize(
s"""
|/metals.json
|{
| "a": { "scalaVersion" : "${version}"},
| "b": { "scalaVersion" : "${version}", "dependsOn": [ "a" ] }
|
|}
|/a/src/main/scala/a/Main.scala
|package a
|
|class Main
|object Main {
| // Error that makes the whole target not compile
| val name: Int = "John"
| case class Bar()
| val ver: Version = null
|}
|
|class Version
|
|/b/src/main/scala/b/Foo.scala
|package b
|import a.Main
|import a.Main.Bar
|
|object Foo{
| val ver: Version = null
| val nm = Main.name
| val foo = Bar()
| val m: Main = new Main()
|}
|""".stripMargin
)
_ <- server.didOpen("a/src/main/scala/a/Main.scala")
_ <- server.didOpen("b/src/main/scala/b/Foo.scala")
_ = assertNoDiff(
client.workspaceDiagnostics,
if (version == BuildInfo.scala3)
"""|a/src/main/scala/a/Main.scala:6:19: error: Found: ("John" : String)
|Required: Int
| val name: Int = "John"
| ^^^^^^
|""".stripMargin
else
"""|a/src/main/scala/a/Main.scala:6:19: error: type mismatch;
| found : String("John")
| required: Int
| val name: Int = "John"
| ^^^^^^
|""".stripMargin,
)
_ = assertNoDiff(
server.workspaceDefinitions,
"""|/a/src/main/scala/a/Main.scala
|package a
|
|class Main/*L2*/
|object Main/*L3*/ {
| // Error that makes the whole target not compile
| val name/*L5*/: Int/*Int.scala*/ = "John"
| case class Bar/*L6*/()
| val ver/*L7*/: Version/*L10*/ = null
|}
|
|class Version/*L10*/
|
|/b/src/main/scala/b/Foo.scala
|package b
|import a/*<no symbol>*/.Main/*;Main.scala:2;Main.scala:3*/
|import a/*<no symbol>*/.Main/*Main.scala:3*/.Bar/*Main.scala:6*/
|
|object Foo/*L4*/{
| val ver/*L5*/: Version/*<no symbol>*/ = null
| val nm/*L6*/ = Main/*Main.scala:3*/.name/*Main.scala:5*/
| val foo/*L7*/ = Bar/*Main.scala:6*/()
| val m/*L8*/: Main/*Main.scala:2*/ = new Main/*Main.scala:2*/()
|}
|""".stripMargin,
)
} yield ()
}

test(s"fallback-to-workspace-search-shadowing-$version") {
cleanWorkspace()
for {
_ <- initialize(
s"""
|/metals.json
|{
| "a": { "scalaVersion" : "${version}" },
| "b": { "scalaVersion" : "${version}", "dependsOn": [ "a" ] }
|
|}
|/a/src/main/scala/a/Main.scala
|package a
|
|object Main {
| val `na-me`: Int = "John"
| object Other
|}
|
|object Main2 {
| object Other
|}
|
|
|/b/src/main/scala/b/Foo.scala
|package b
|
|object Foo {
| import a.Main.Other
| val other = Other
| a.Main.`na-me`
|}
|object Foo2 {
| import a.Main2.Other
| val other = Other
|}
|object Foo3 {
| val Foo = ""
| Foo
|}
|""".stripMargin
)
_ <- server.didOpen("a/src/main/scala/a/Main.scala")
_ <- server.didOpen("b/src/main/scala/b/Foo.scala")
_ = assertNoDiff(
client.workspaceDiagnostics,
if (version == BuildInfo.scala3)
"""|a/src/main/scala/a/Main.scala:4:22: error: Found: ("John" : String)
|Required: Int
| val `na-me`: Int = "John"
| ^^^^^^
|""".stripMargin
else
"""|a/src/main/scala/a/Main.scala:4:22: error: type mismatch;
| found : String("John")
| required: Int
| val `na-me`: Int = "John"
| ^^^^^^
|""".stripMargin,
)
_ = assertNoDiff(
server.workspaceDefinitions,
"""|/a/src/main/scala/a/Main.scala
|package a
|
|object Main/*L2*/ {
| val `na-me`/*L3*/: Int/*Int.scala*/ = "John"
| object Other/*L4*/
|}
|
|object Main2/*L7*/ {
| object Other/*L8*/
|}
|
|
|/b/src/main/scala/b/Foo.scala
|package b
|
|object Foo/*L2*/ {
| import a/*<no symbol>*/.Main/*Main.scala:2*/.Other/*Main.scala:4*/
| val other/*L4*/ = Other/*Main.scala:4*/
| a/*<no symbol>*/.Main/*Main.scala:2*/.`na-me`/*Main.scala:3*/
|}
|object Foo2/*L7*/ {
| import a/*<no symbol>*/.Main2/*Main.scala:7*/.Other/*Main.scala:8*/
| val other/*L9*/ = Other/*Main.scala:8*/
|}
|object Foo3/*L11*/ {
| val Foo/*L12*/ = ""
| Foo/*L12*/
|}
|""".stripMargin,
)
} yield ()
}

test(s"fallback-to-workspace-search-packages-$version") {
cleanWorkspace()
for {
_ <- initialize(
s"""
|/metals.json
|{
| "a": { "scalaVersion" : "${version}" }
|}
|/a/src/main/scala/a/Main.scala
|package a
|
|object Main {
| val name: Int = "John"
|}
|
|/a/src/main/scala/a/b/Foo.scala
|package a
|package b
|
|object Foo {
| Main.name
|}
|
|/a/src/main/scala/a/b/Bar.scala
|package a.b
|
|object Bar {
| Main.name
|}
|
|/a/src/main/scala/a/b/c/OtherMain.scala
|package a.b.c
|
|object Main {
| val name: Int = "Lemon"
|}
|
|/a/src/main/scala/a/b/c/d/Baz.scala
|package a
|package b
|package c
|package d
|
|object Baz {
| Main.name
|}
|
|""".stripMargin
)
_ <- server.didOpen("a/src/main/scala/a/Main.scala")
_ <- server.didOpen("a/src/main/scala/a/b/Foo.scala")
_ <- server.didOpen("a/src/main/scala/a/b/Bar.scala")
_ <- server.didOpen("a/src/main/scala/a/b/c/OtherMain.scala")
_ <- server.didOpen("a/src/main/scala/a/b/c/d/Baz.scala")
_ = assertNoDiff(
client.workspaceDiagnostics,
if (version == BuildInfo.scala3)
"""|a/src/main/scala/a/Main.scala:4:19: error: Found: ("John" : String)
|Required: Int
| val name: Int = "John"
| ^^^^^^
|a/src/main/scala/a/b/Bar.scala:4:3: error: Not found: Main
| Main.name
| ^^^^
|a/src/main/scala/a/b/c/OtherMain.scala:4:19: error: Found: ("Lemon" : String)
|Required: Int
| val name: Int = "Lemon"
| ^^^^^^^
|""".stripMargin
else
"""|a/src/main/scala/a/Main.scala:4:19: error: type mismatch;
| found : String("John")
| required: Int
| val name: Int = "John"
| ^^^^^^
|a/src/main/scala/a/b/Bar.scala:4:3: error: not found: value Main
| Main.name
| ^^^^
|a/src/main/scala/a/b/c/OtherMain.scala:4:19: error: type mismatch;
| found : String("Lemon")
| required: Int
| val name: Int = "Lemon"
| ^^^^^^^
|""".stripMargin,
)
_ = assertNoDiff(
server.workspaceDefinitions,
"""|/a/src/main/scala/a/Main.scala
|package a
|
|object Main/*L2*/ {
| val name/*L3*/: Int/*Int.scala*/ = "John"
|}
|
|/a/src/main/scala/a/b/Bar.scala
|package a.b
|
|object Bar/*L2*/ {
| Main/*<no symbol>*/.name/*<no symbol>*/
|}
|
|/a/src/main/scala/a/b/Foo.scala
|package a
|package b
|
|object Foo/*L3*/ {
| Main/*Main.scala:2*/.name/*Main.scala:3*/
|}
|
|/a/src/main/scala/a/b/c/OtherMain.scala
|package a.b.c
|
|object Main/*L2*/ {
| val name/*L3*/: Int/*Int.scala*/ = "Lemon"
|}
|
|/a/src/main/scala/a/b/c/d/Baz.scala
|package a
|package b
|package c
|package d
|
|object Baz/*L5*/ {
| Main/*OtherMain.scala:2*/.name/*OtherMain.scala:3*/
|}
|""".stripMargin,
)
} yield ()
}
}
}
5 changes: 5 additions & 0 deletions tests/unit/src/main/scala/tests/TestingServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,11 @@ final case class TestingServer(
var last = List[String]()
trees.tokenized(input).get.foreach { token =>
val params = token.toPositionParams(identifier)
// Scala 3 doesn't count ` as part of the word which is the same as most editors
if (token.text.startsWith("`")) {
val position = params.getPosition()
position.setCharacter(position.getCharacter() + 1)
}
val definition = server
.definitionOrReferences(params, definitionOnly = true)
.asJava
Expand Down
Loading

0 comments on commit 2ca70c3

Please sign in to comment.