Skip to content

Commit 63180e6

Browse files
committed
IDE: add test infrastructure for diagnostics
1 parent f239b67 commit 63180e6

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dotty.tools.languageserver
2+
3+
import org.junit.Test
4+
5+
import dotty.tools.dotc.reporting.diagnostic.ErrorMessageID._
6+
import dotty.tools.languageserver.util.Code._
7+
import org.eclipse.lsp4j.DiagnosticSeverity._
8+
9+
class DiagnosticsTest {
10+
@Test def diagnosticWrongType: Unit =
11+
code"""object Test {
12+
| val x: Int = $m1"foo"$m2
13+
|}""".withSource
14+
.diagnostics(m1,
15+
(m1 to m2, """found: String("foo")
16+
|required: Int""".stripMargin, Error, Some(TypeMismatchID))
17+
)
18+
}

language-server/test/dotty/tools/languageserver/util/CodeRange.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ case class CodeRange(start: CodeMarker, end: CodeMarker) {
1919
if (!checked) {
2020
assert(start.file == end.file, s"$start and $end where not in the same file")
2121
assert(start.line <= end.line, s"Expected $end to be after $start")
22-
assert(start.line != end.line || start.character < end.character, s"Expected $end to be after $start")
22+
assert(start.line != end.line || start.character <= end.character, s"Expected $end to be at or after $start")
2323
checked = true
2424
}
2525
}

language-server/test/dotty/tools/languageserver/util/CodeTester.scala

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import dotty.tools.languageserver.util.actions._
55
import dotty.tools.languageserver.util.embedded.CodeMarker
66
import dotty.tools.languageserver.util.server.{TestFile, TestServer}
77

8+
import dotty.tools.dotc.reporting.diagnostic.ErrorMessageID
89
import dotty.tools.dotc.util.Signatures.Signature
910

10-
import org.eclipse.lsp4j.{CompletionItemKind, DocumentHighlightKind}
11+
import org.eclipse.lsp4j.{ CompletionItemKind, DocumentHighlightKind, Diagnostic, DiagnosticSeverity }
12+
13+
import org.junit.Assert.assertEquals
1114

1215
/**
1316
* Simulates an LSP client for test in a project defined by `sources`.
@@ -30,6 +33,32 @@ class CodeTester(projects: List[Project]) {
3033

3134
private val positions: PositionContext = getPositions(files)
3235

36+
/** Check that the last diagnostics that have been published so far by the server
37+
* for a given file match `expected`.
38+
*
39+
* @param marker The marker defining the source file from which to query.
40+
* @param expected The expected diagnostics to be found
41+
*/
42+
def diagnostics(marker: CodeMarker,
43+
expected: (CodeRange, String, DiagnosticSeverity, Option[ErrorMessageID])*): this.type = {
44+
implicit val posCtx = positions
45+
46+
def toDiagnostic(range: CodeRange, message: String, severity: DiagnosticSeverity,
47+
errorCode: Option[ErrorMessageID]): Diagnostic = {
48+
val location = range.toLocation
49+
new Diagnostic(
50+
location.getRange, message, severity, /*source=*/"", errorCode.map(_.errorNumber.toString).getOrElse(null)
51+
)
52+
}
53+
54+
val expectedParams = marker.toPublishDiagnosticsParams(expected.toList.map(toDiagnostic))
55+
val actualParams = testServer.client.diagnostics.get.reverse.find(_.getUri == marker.uri)
56+
.getOrElse(throw new Exception(s"No published diagnostics for ${marker.uri}"))
57+
assertEquals(expectedParams, actualParams)
58+
59+
this
60+
}
61+
3362
/**
3463
* Perform a hover over `range`, verifies that result matches `expected`.
3564
*

language-server/test/dotty/tools/languageserver/util/embedded/CodeMarker.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package dotty.tools.languageserver.util.embedded
33
import dotty.tools.languageserver.util.server.TestFile
44
import dotty.tools.languageserver.util.{CodeRange, PositionContext}
55

6+
import scala.collection.JavaConverters._
7+
68
import org.eclipse.lsp4j._
79

810
import PositionContext.PosCtx
@@ -16,6 +18,9 @@ class CodeMarker(val name: String) extends Embedded {
1618
/** The file containing this marker. */
1719
def file: PosCtx[TestFile] = posCtx.positionOf(this)._1
1820

21+
/** The uri of the file containing this marker. */
22+
def uri: PosCtx[String] = file.uri
23+
1924
/** The line containing this marker. */
2025
def line: PosCtx[Int] = posCtx.positionOf(this)._2
2126

@@ -34,6 +39,9 @@ class CodeMarker(val name: String) extends Embedded {
3439
def toCompletionParams: PosCtx[CompletionParams] =
3540
new CompletionParams(toTextDocumentIdentifier, toPosition)
3641

42+
def toPublishDiagnosticsParams(diagnostics: List[Diagnostic]): PosCtx[PublishDiagnosticsParams] =
43+
new PublishDiagnosticsParams(file.uri, diagnostics.asJava)
44+
3745
def toRenameParams(newName: String): PosCtx[RenameParams] =
3846
new RenameParams(toTextDocumentIdentifier, toPosition, newName)
3947

0 commit comments

Comments
 (0)