Skip to content

Commit 54c691a

Browse files
committed
Add basic completion tests
1 parent fb3dede commit 54c691a

File tree

6 files changed

+88
-3
lines changed

6 files changed

+88
-3
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dotty.tools.languageserver
2+
3+
import org.junit.Test
4+
5+
import dotty.tools.languageserver.util.BaseTest
6+
import dotty.tools.languageserver.util.Code._
7+
8+
class CompletionTest extends BaseTest {
9+
10+
@Test def competion0: Unit = {
11+
12+
checkActions(code"class Foo { val xyz: Int = 0; def y: Int = xy$m1 }".completion(m1,
13+
List(
14+
("clone", "Method", "(): Object"),
15+
("!=", "Method", "(x$0: Any): Boolean"),
16+
("wait", "Method", "(x$0: Long, x$1: Int): Unit"),
17+
("wait", "Method", "(x$0: Long): Unit"),
18+
("wait", "Method", "(): Unit"),
19+
("notifyAll", "Method", "(): Unit"),
20+
("finalize", "Method", "(): Unit"),
21+
("asInstanceOf", "Method", "[X0] => X0"),
22+
("==", "Method", "(x$0: Any): Boolean"),
23+
("equals", "Method", "(x$0: Any): Boolean"),
24+
("isInstanceOf", "Method", "[X0] => Boolean"),
25+
("getClass", "Method", "(): Class[_]"),
26+
("##", "Method", "=> Int"),
27+
("eq", "Method", "(x$0: Object): Boolean"),
28+
("synchronized", "Method", "[X0](x$0: X0): X0"),
29+
("xyz", "Field", "Int"),
30+
("ne", "Method", "(x$0: Object): Boolean"),
31+
("toString", "Method", "(): String"),
32+
("hashCode", "Method", "(): Int"),
33+
("y", "Method", "=> Int"),
34+
("notify", "Method", "(): Unit")
35+
)
36+
))
37+
}
38+
}

language-server/test/dotty/tools/languageserver/Main.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ object Main {
1717
if (method.getAnnotation(classOf[Test]) ne null) {
1818
print(s"Testing $clazz.${method.getName} ")
1919
try {
20-
method.invoke(clazz.getConstructor().newInstance())
20+
try {
21+
method.invoke(clazz.getConstructor().newInstance())
22+
} catch {
23+
case ex: InvocationTargetException => throw ex.getCause
24+
}
2125
println(Console.GREEN + "passed" + Console.RESET)
2226
passed += 1
2327
} catch {
@@ -51,6 +55,7 @@ object Main {
5155
}
5256

5357
private def testsClasses = List(
58+
classOf[CompletionTest],
5459
classOf[HoverTest],
5560
classOf[DefinitionTest],
5661
classOf[ReferencesTest],

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import java.nio.file.Paths
55

66
import dotty.tools.dotc.util.DiffUtil
77
import dotty.tools.languageserver.util.Code._
8-
import dotty.tools.languageserver.util.actions.{CodeDefinition, CodeHover, CodeMark, CodeReferences}
8+
import dotty.tools.languageserver.util.actions._
99
import dotty.tools.languageserver.util.server.TestServer
1010

1111
class BaseTest {
@@ -53,6 +53,9 @@ class BaseTest {
5353
case _: CodeHover => testServer.hover(file, line, character).toString
5454
case _: CodeDefinition => testServer.definition(file, line, character).toString
5555
case codeRef: CodeReferences => testServer.references(file, line, character, codeRef.withDecl).toString
56+
case code: CodeCompletion =>
57+
val (_, pos) = marks(code.mark)
58+
testServer.completion(file, pos.line, pos.char).toString
5659
}
5760

5861
val expected = action.code.expected(marks)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ object Code {
5858

5959
def noRef: EmptyReference.type = EmptyReference
6060

61-
case class CodeWithActions(text: String, actions: List[ActionOnRange], marks: List[(CodeMark, Position)])
61+
case class CodeWithActions(text: String, actions: List[ActionOnRange], marks: List[(CodeMark, Position)]) {
62+
def completion(mark: CodeMark, completions: List[(String, String, String)]) =
63+
CodeWithActions(text, ActionOnRange(CodeCompletion(mark, completions), Range(Position(0, 0), Position(0, 1))) :: actions, marks)
64+
}
6265

6366
case class ActionOnRange(code: CodeWithAction, range: actions.Range)
6467

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dotty.tools.languageserver.util.actions
2+
3+
import dotty.tools.languageserver.util.actions.Responses._
4+
5+
case class CodeCompletion(mark: CodeMark, completions: List[(String, String, String)]) extends CodeWithAction {
6+
override def text: String = ""
7+
def expected(marks: Map[CodeMark, (String, Position)]): String =
8+
s"""Either [
9+
| left = null
10+
| right = CompletionList [
11+
| isIncomplete = false
12+
| items = SeqWrapper (
13+
|${completions.map(comp => completionItem(comp._1, comp._2, comp._3)).mkString(",\n")}
14+
| )
15+
|]
16+
|]""".stripMargin
17+
18+
def completionItem(label: String, kind: String, detail: String): String =
19+
s""" CompletionItem [
20+
| label = "$label"
21+
| kind = $kind
22+
| detail = "$detail"
23+
| documentation = null
24+
| sortText = null
25+
| filterText = null
26+
| insertText = null
27+
| insertTextFormat = null
28+
| textEdit = null
29+
| additionalTextEdits = null
30+
| command = null
31+
| data = null
32+
| ]""".stripMargin
33+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class TestServer(testFolder: Path) {
7070
server.references(rp).get().asScala.toList
7171
}
7272

73+
def completion(file: TestFile, line: Int, character: Int) =
74+
server.completion(pos(file, line, character)).get()
75+
7376
private def pos(file: TestFile, line: Int, character: Int) = {
7477
val tdpp = new TextDocumentPositionParams
7578
tdpp.setTextDocument(file.textDocumentIdentifier)

0 commit comments

Comments
 (0)