Skip to content

Commit

Permalink
test: Fix worksheet tests after changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Nov 20, 2024
1 parent 91aff44 commit 2180ce1
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,9 @@ class WorkspaceLspService(

override def inlayHints(
params: lsp4j.InlayHintParams
): CompletableFuture[java.util.List[lsp4j.InlayHint]] =
): CompletableFuture[java.util.List[lsp4j.InlayHint]] = {
getServiceFor(params.getTextDocument.getUri()).inlayHints(params)

}
override def inlayHintResolve(
inlayHint: lsp4j.InlayHint
): CompletableFuture[lsp4j.InlayHint] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class WorksheetProvider(
}
val hint = new InlayHint(
statEnd,
messages.Either.forLeft(" // " + stat.summary()),
messages.Either.forLeft(" // " + truncatify(stat)),
)
hint.setTooltip(stat.details())
hint
Expand Down
33 changes: 27 additions & 6 deletions tests/mtest/src/main/scala/tests/TestInlayHints.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,37 @@ object TestInlayHints {
// |
// v
// val y<<: T/*(0:5,0:5)*/>> = x
def decorationString(inlayHint: InlayHint): String = {
def decorationString(inlayHint: InlayHint, withTooltip: Boolean): String = {
val buffer = ListBuffer.empty[String]

val labels = inlayHint.getLabel().asScala match {
case Left(label) => List(label)
case Right(labelParts) => labelParts.asScala.map(_.getValue()).toList
}

val tooltip = Option(inlayHint.getTooltip()).map(t =>
t.asScala match {
case Left(tooltip) => tooltip
case Right(markdown) => markdown.getValue()
}
)
val data =
inlayHint.getData().asInstanceOf[Array[Any]]
buffer += "/*"
labels.zip(data).foreach { case (label, data) =>
buffer += label
buffer ++= readData(data)
if (data != null) {
labels.zip(data).foreach { case (label, data) =>
buffer += label
buffer ++= readData(data)
}
} else {
buffer += labels.mkString
}
if (withTooltip)
tooltip.foreach { tooltip =>
buffer += "| "
buffer += tooltip.replace("\n", "\\n")
buffer += " |"
}
buffer += "*/"
buffer.toList.mkString
}
Expand All @@ -52,9 +69,13 @@ object TestInlayHints {
}
}

def applyInlayHints(text: String, inlayHints: List[InlayHint]): String = {
def applyInlayHints(
text: String,
inlayHints: List[InlayHint],
withTooltip: Boolean
): String = {
val textEdits = inlayHints.map { hint =>
val newText = decorationString(hint)
val newText = decorationString(hint, withTooltip)
val range = new l.Range(hint.getPosition(), hint.getPosition())
new TextEdit(
range,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ abstract class BaseInlayHintsExpectSuite(
)
val inlayHints =
compiler.inlayHints(pcParams).get().asScala.toList
TestInlayHints.applyInlayHints(file.code, inlayHints)
TestInlayHints.applyInlayHints(
file.code,
inlayHints,
withTooltip = false,
)
},
)
}
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/src/main/scala/tests/BaseInlayHintsLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ abstract class BaseInlayHintsLspSuite(name: String, scalaVersion: String)
code,
expected,
workspace,
withTooltip = false,
postprocessObtained = identity,
)
} yield ()
}
Expand Down
177 changes: 63 additions & 114 deletions tests/unit/src/main/scala/tests/BaseWorksheetLspSuite.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tests

import scala.concurrent.duration._
import scala.concurrent.Promise

import scala.meta.internal.metals.InitializationOptions
Expand Down Expand Up @@ -77,16 +78,16 @@ abstract class BaseWorksheetLspSuite(
),
)
_ = assertNoDiagnostics()
_ = assertNoDiff(
client.syntheticDecorations,
_ <- server.assertInlayHints(
"a/src/main/scala/foo/Main.worksheet.sc",
getExpected(
"""|identity(42) // : Int = 42
|val name = sourcecode.Name.generate.value // : String = "name"
"""|identity(42)/* // : Int = 42*/
|val name = sourcecode.Name.generate.value/* // : String = "name"*/
|""".stripMargin,
Map(
"3" ->
"""|identity(42) // : Int = 42
|val name = sourcecode.Name.generate.value // : String = name
"""|identity(42)/* // : Int = 42*/
|val name = sourcecode.Name.generate.value/* // : String = name*/
|""".stripMargin
),
scalaVersion,
Expand All @@ -106,11 +107,9 @@ abstract class BaseWorksheetLspSuite(
|""".stripMargin
)
_ <- server.didOpen("a/Main.worksheet.sc")
// check that ANSI colors were stripped
_ = assertNotContains(client.syntheticDecorations, "\u001b")
_ = assertNoDiff(
client.syntheticDecorations,
"""|pprint.pprintln("Hello, world!") // "Hello, world!"
_ <- server.assertInlayHints(
"a/Main.worksheet.sc",
"""|pprint.pprintln("Hello, world!")/* // "Hello, world!"*/
|""".stripMargin,
)
} yield ()
Expand All @@ -134,25 +133,24 @@ abstract class BaseWorksheetLspSuite(
)
_ <- server.didOpen("a/Main.worksheet.sc")
_ = assertNoDiagnostics()
_ = assertNoDiff(
client.syntheticDecorations,
_ <- server.assertInlayHints(
"a/Main.worksheet.sc",
getExpected(
"""|import java.nio.file.Files
|val name = "Susan" // : String = "Susan"
|val greeting = s"Hello $name" // : String = "Hello Susan"
|println(greeting + "\nHow are you?") // Hello Susan
|1.to(10).toVector // : Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|val List(a, b) = List(42, 10) // a: Int = 42, b: Int = 10
|val name = "Susan"/* // : String = "Susan"*/
|val greeting = s"Hello $name"/* // : String = "Hello Susan"*/
|println(greeting + "\nHow are you?")/* // Hello Susan...*/
|1.to(10).toVector/* // : Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)*/
|val List(a, b) = List(42, 10)/* // a: Int = 42, b: Int = 10*/
|""".stripMargin,
Map(
"3" ->
"""|import java.nio.file.Files
|val name = "Susan" // : String = Susan
|val greeting = s"Hello $name" // : String = Hello Susan
|println(greeting + "\nHow are you?") // Hello Susan…
|1.to(10).toVector // : Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|val List(a, b) = List(42, 10) // a: Int = 42, b: Int = 10
|given str: String = ""
|val name = "Susan"/* // : String = Susan*/
|val greeting = s"Hello $name"/* // : String = Hello Susan*/
|println(greeting + "\nHow are you?")/* // Hello Susan...*/
|1.to(10).toVector/* // : Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)*/
|val List(a, b) = List(42, 10)/* // a: Int = 42, b: Int = 10*/
|""".stripMargin
),
scalaVersion,
Expand All @@ -179,85 +177,29 @@ abstract class BaseWorksheetLspSuite(
)
_ <- server.didOpen("a/src/main/scala/Main.worksheet.sc")
_ = assertNoDiagnostics()
_ = assertNoDiff(
client.syntheticDecorations,
getExpected(
"""|import java.nio.file.Files
|val name = "Susan" // : String = "Susan"
|val greeting = s"Hello $name" // : String = "Hello Susan"
|println(greeting + "\nHow are you?") // Hello Susan…
|1.to(10).toVector // : Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|val List(a, b) = List(42, 10) // a: Int = 42, b: Int = 10
|""".stripMargin,
Map(
"3" ->
"""|import java.nio.file.Files
|val name = "Susan" // : String = Susan
|val greeting = s"Hello $name" // : String = Hello Susan
|println(greeting + "\nHow are you?") // Hello Susan…
|1.to(10).toVector // : Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|val List(a, b) = List(42, 10) // a: Int = 42, b: Int = 10
|""".stripMargin
),
scalaVersion,
),
)
_ = assertNoDiff(
client.syntheticDecorationHoverMessage,
_ <- server.assertInlayHints(
"a/src/main/scala/Main.worksheet.sc",
getExpected(
"""|import java.nio.file.Files
|val name = "Susan"
|```scala
|name: String = "Susan"
|```
|val greeting = s"Hello $name"
|```scala
|greeting: String = "Hello Susan"
|```
|println(greeting + "\nHow are you?")
|```scala
|// Hello Susan
|// How are you?
|```
|1.to(10).toVector
|```scala
|res1: Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|```
|val List(a, b) = List(42, 10)
|```scala
|a: Int = 42
|b: Int = 10
|```
|val name = "Susan"/* // : String = "Susan"| name: String = "Susan" |*/
|val greeting = s"Hello $name"/* // : String = "Hello Susan"| greeting: String = "Hello Susan" |*/
|println(greeting + "\nHow are you?")/* // Hello Susan...| // Hello Susan\n// How are you? |*/
|1.to(10).toVector/* // : Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)| res1: Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |*/
|val List(a, b) = List(42, 10)/* // a: Int = 42, b: Int = 10| a: Int = 42\nb: Int = 10 |*/
|""".stripMargin,
Map(
"3" ->
"""|import java.nio.file.Files
|val name = "Susan"
|```scala
|name: String = Susan
|```
|val greeting = s"Hello $name"
|```scala
|greeting: String = Hello Susan
|```
|println(greeting + "\nHow are you?")
|```scala
|// Hello Susan
|// How are you?
|```
|1.to(10).toVector
|```scala
|res1: Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|```
|val List(a, b) = List(42, 10)
|```scala
|a: Int = 42
|b: Int = 10
|```
|val name = "Susan"/* // : String = Susan| name: String = Susan |*/
|val greeting = s"Hello $name"/* // : String = Hello Susan| greeting: String = Hello Susan |*/
|println(greeting + "\nHow are you?")/* // Hello Susan...| // Hello Susan\n// How are you? |*/
|1.to(10).toVector/* // : Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)| res1: Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |*/
|val List(a, b) = List(42, 10)/* // a: Int = 42, b: Int = 10| a: Int = 42\nb: Int = 10 |*/
|""".stripMargin
),
scalaVersion,
),
withTooltip = true,
)
} yield ()
}
Expand All @@ -266,6 +208,7 @@ abstract class BaseWorksheetLspSuite(
cleanWorkspace()
val cancelled = Promise[Unit]()
client.onWorkDoneProgressStart = { (message, cancelParams) =>
scribe.info(message)
if (message.startsWith("Evaluating worksheet")) {
cancelled.trySuccess(())
server.fullServer.didCancelWorkDoneProgress(cancelParams)
Expand All @@ -283,18 +226,20 @@ abstract class BaseWorksheetLspSuite(
|""".stripMargin
)
_ <- server.didOpen("a/src/main/scala/Main.worksheet.sc")
_ <- cancelled.future
_ = client.onWorkDoneProgressStart = (_, _) => {}
_ <- cancelled.future.withTimeout(10.seconds)
_ = client.onWorkDoneProgressStart = (message, _) => {
scribe.info(message)
}
_ <- server.didSave("a/src/main/scala/Main.worksheet.sc")(
_.replace("Stream", "// Stream")
)
_ <- server.didSave("a/src/main/scala/Main.worksheet.sc")(
_.replace("42", "43")
)
_ = assertNoDiff(
client.syntheticDecorations,
_ <- server.assertInlayHints(
"a/src/main/scala/Main.worksheet.sc",
"""|
|println(43) // 43
|println(43)/* // 43*/
|// Stream.from(10).last
|""".stripMargin,
)
Expand All @@ -314,10 +259,9 @@ abstract class BaseWorksheetLspSuite(
|""".stripMargin
)
_ <- server.didOpen("a/src/main/scala/Main.worksheet.sc")
_ = assertNoDiff(
client.syntheticDecorations,
"""|
|val x = 42 // : Int = 42
_ <- server.assertInlayHints(
"a/src/main/scala/Main.worksheet.sc",
"""|val x = 42/* // : Int = 42*/
|throw new RuntimeException("boom")
|""".stripMargin,
)
Expand Down Expand Up @@ -372,10 +316,10 @@ abstract class BaseWorksheetLspSuite(
_ <- server.didOpen("b/src/main/scala/core/Lib2.scala")
_ <- server.didOpen("b/src/main/scala/foo/Main.worksheet.sc")
_ = assertNoDiagnostics()
_ = assertNoDiff(
client.workspaceDecorations("b/src/main/scala/foo/Main.worksheet.sc"),
"""|println(core.Lib) // Lib
|println(core.Lib2) // Lib2
_ <- server.assertInlayHints(
"b/src/main/scala/foo/Main.worksheet.sc",
"""|println(core.Lib)/* // Lib*/
|println(core.Lib2)/* // Lib2*/
|""".stripMargin,
)
} yield ()
Expand All @@ -400,8 +344,13 @@ abstract class BaseWorksheetLspSuite(
)
// completions work despite error
_ = assertNoDiff(identity, "identity[A](x: A): A")
// decorations do not appear for non ".worksheet.sc" files.
_ = assertNoDiff(client.syntheticDecorations, "")
// worksheet evaluations do not appear for non ".worksheet.sc" files.
_ <- server.assertInlayHints(
"b/src/main/scala/foo/Main.worksheet.sc",
"""|identity(42)
|val x: Int = ""
|""".stripMargin,
)
} yield ()
}

Expand Down Expand Up @@ -463,20 +412,20 @@ abstract class BaseWorksheetLspSuite(
_ <- server.didOpen("a/src/main/scala/a/Util.scala")
_ <- server.didOpen("a/src/main/scala/a/Main.worksheet.sc")
_ = assertNoDiagnostics()
_ = assertNoDiff(
client.workspaceDecorations("a/src/main/scala/a/Main.worksheet.sc"),
_ <- server.assertInlayHints(
"a/src/main/scala/a/Main.worksheet.sc",
"""
|a.Util.increase(1) // : Int = 2
|a.Util.increase(1)/* // : Int = 2*/
|""".stripMargin,
)
_ <- server.didSave("a/src/main/scala/a/Util.scala")(
_.replace("n + 1", "n + 2")
)
_ <- server.didSave("a/src/main/scala/a/Main.worksheet.sc")(identity)
_ = assertNoDiff(
client.workspaceDecorations("a/src/main/scala/a/Main.worksheet.sc"),
_ <- server.assertInlayHints(
"a/src/main/scala/a/Main.worksheet.sc",
"""
|a.Util.increase(1) // : Int = 3
|a.Util.increase(1)/* // : Int = 3*/
|""".stripMargin,
)
} yield ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ object QuickBuildInitializer extends BuildServerInitializer {
for {
initializeResult <- server.initialize(workspaceFolders)
_ <- server.initialized()
_ <- server.didChangeConfiguration("{}")
} yield {
if (!expectError) {
server.assertBuildServerConnection()
Expand Down
Loading

0 comments on commit 2180ce1

Please sign in to comment.