-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
CompletionTest.scala
290 lines (240 loc) · 9.9 KB
/
CompletionTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package scala.tools.nsc.interpreter
import java.io.{PrintWriter, StringWriter}
import org.junit.Assert.{assertEquals, assertTrue}
import org.junit.Test
import scala.reflect.internal.util.{BatchSourceFile, SourceFile}
import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.shell._
class CompletionTest {
val EmptyString = "" // def string results include the empty string so that JLine won't insert "def ..." at the cursor
def newIMain(classBased: Boolean = false): IMain = {
val settings = new Settings()
settings.Xjline.value = "off"
settings.usejavacp.value = true
settings.Yreplclassbased.value = classBased
new IMain(settings, new ReplReporterImpl(settings, new PrintWriter(new StringWriter)))
}
private def setup(sources: SourceFile*): Completion = {
val intp = newIMain()
intp.compileSources(sources: _*)
val completer = new ReplCompletion(intp)
completer
}
private def interpretLines(lines: String*): (Completion, Repl, Accumulator) = {
val intp = newIMain()
lines.foreach(intp.interpret)
val acc = new Accumulator
val completer = new ReplCompletion(intp, acc)
(completer, intp, acc)
}
implicit class BeforeAfterCompletion(completion: Completion) {
def complete(before: String, after: String = ""): CompletionResult =
completion.complete(before + after, before.length)
}
@Test
def t4438_arrayCompletion(): Unit = {
val completer = setup()
checkExact(completer, "Array(1, 2, 3) rev")("reverse", "reverseIterator", "reverseMap")
}
@Test
def classBased(): Unit = {
val intp = newIMain()
val completer = new ReplCompletion(intp)
checkExact(completer, "object O { def x_y_z = 1 }; import O._; x_y")("x_y_z")
}
@Test
def completions(): Unit = {
testCompletions(classBased = false)
}
@Test
def completionsReplClassBased(): Unit = {
testCompletions(classBased = true)
}
private def testCompletions(classBased: Boolean): Unit = {
val intp = newIMain(classBased)
val completer = new ReplCompletion(intp)
checkExact(completer, "object O { def x_y_z = 1 }; import O._; x_y")("x_y_z")
checkExact(completer, "object O { private def x_y_z = 1 }; import O._; x_y")()
checkExact(completer, "object O { private def x_y_z = 1; x_y", "}")("x_y_z")
checkExact(completer, "object x_y_z; import x_y")("x_y_z")
checkExact(completer, "object x_y_z { def a_b_c }; import x_y_z.a_b")("a_b_c")
checkExact(completer, "object X { private[this] def definition = 0; def")("definition")
// stable terms are offered in type completion as they might be used as a prefix
checkExact(completer, """object O { def x_y_z = 0; val x_z_y = ""; type T = x_""")("x_z_y")
checkExact(completer, """def method { def x_y_z = 0; val x_z_y = ""; type T = x_""")("x_z_y")
checkExact(completer, "asInstanceO", includeUniversal = false)()
checkExact(completer, "asInstanceO", "", includeUniversal = true)("asInstanceOf")
// Output is sorted
assertEquals(List("prefix_aaa", "prefix_nnn", "prefix_zzz"), completer.complete( """class C { def prefix_nnn = 0; def prefix_zzz = 0; def prefix_aaa = 0; prefix_""").candidates.filter(!_.isUniversal).map(_.defString))
// Enable implicits to check completion enrichment
checkExact(completer, """'c'.toU""")("toUpper")
checkExact(completer, """val c = 'c'; c.toU""")("toUpper")
intp.interpret("object O { def x_y_x = 1; def x_y_z = 2; def getFooBarZot = 3}; ")
checkExact(new ReplCompletion(intp), """object O2 { val x = O.""")("x_y_x", "x_y_z", "getFooBarZot")
}
@Test
def annotations(): Unit = {
val completer = setup()
checkExact(completer, "def foo[@specialize", " A]")("specialized")
checkExact(completer, "def foo[@specialize")("specialized")
checkExact(completer, """@deprecatedN""", """ class Foo""")("deprecatedName")
checkExact(completer, """{@deprecatedN""")("deprecatedName")
}
@Test
def incompleteStringInterpolation(): Unit = {
val completer = setup()
checkExact(completer, """val x_y_z = 1; s"${x_""", "}\"")("x_y_z")
checkExact(completer, """val x_y_z = 1; s"${x_""", "\"")("x_y_z")
}
@Test
def symbolically(): Unit = {
val completer = setup()
checkExact(completer, """class C { def +++(a: Any) = 0; def ---(a: Any) = 0; this.++""")("+++")
}
@Test
def previousLineCompletions(): Unit = {
val (completer, intp, _) = interpretLines(
"class C { val x_y_z = 42 }",
"object O { type T = Int }")
checkExact(completer, "new C().x_y")("x_y_z")
checkExact(completer, "(1 : O.T).toCha")("toChar")
intp.interpret("case class X_y_z()")
val completer1 = new ReplCompletion(intp)
checkExact(completer1, "new X_y_")("X_y_z")
checkExact(completer1, "X_y_")("X_y_z")
checkExact(completer1, "X_y_z.app")("apply")
}
@Test
def previousResultInvocation(): Unit = {
val (completer, _, _) = interpretLines("1 + 1")
checkExact(completer, ".toCha")("toChar")
}
@Test
def multiLineInvocation(): Unit = {
val (completer, _, accumulator) = interpretLines()
accumulator += "class C {"
checkExact(completer, "1 + 1.toCha")("toChar")
}
@Test
def defStringConstructor(): Unit = {
val intp = newIMain()
val completer = new ReplCompletion(intp)
checkExact(completer, "class Shazam(i: Int); new Shaza")("Shazam")
checkExact(completer, "class Shazam(i: Int); new Shazam")(EmptyString, "def <init>(i: Int): Shazam")
checkExact(completer, "class Shazam(i: Int) { def this(x: String) = this(0) }; new Shaza")("Shazam")
checkExact(completer, "class Shazam(i: Int) { def this(x: String) = this(0) }; new Shazam")(EmptyString, "def <init>(i: Int): Shazam", "def <init>(x: String): Shazam")
}
@Test
def treePrint(): Unit = {
val completer = setup()
checkExact(completer, " 1.toHexString //print")(EmptyString, "scala.Predef.intWrapper(1).toHexString // : String")
}
@Test
def replGeneratedCodeDeepPackages(): Unit = {
val completer = setup(new BatchSourceFile("<paste>", "package p1.p2.p3; object Ping { object Pong }"))
checkExact(completer, "p1.p2.p")("p3")
checkExact(completer, "p1.p2.p3.P")("Ping")
checkExact(completer, "p1.p2.p3.Ping.Po")("Pong")
}
@Test
def constructor(): Unit = {
val intp = newIMain()
val completer = new ReplCompletion(intp)
checkExact(completer, "class Shazam{}; new Shaz")("Shazam")
intp.interpret("class Shazam {}")
checkExact(completer, "new Shaz")("Shazam")
}
@Test
def completionWithComment(): Unit = {
val completer = setup()
val withMultilineCommit =
"""|Array(1, 2, 3)
| .map(_ + 1) /* then we do reverse */
| .rev""".stripMargin
assertTrue(
completer.complete(withMultilineCommit).candidates.map(_.defString).contains("reverseMap")
)
val withInlineCommit =
"""|Array(1, 2, 3)
| .map(_ + 1) // then we do reverse
| .rev""".stripMargin
assertTrue(
completer.complete(withInlineCommit).candidates.map(_.defString).contains("reverseMap")
)
}
@Test
def isDeprecated(): Unit = {
val (completer, _, _) = interpretLines(
"""object Stale { @deprecated("","") def oldie = ??? }""",
"""object Stuff { @deprecated("","") def `this` = ??? ; @deprecated("","") def `that` = ??? }"""
)
val candidates1 = completer.complete("Stale.ol").candidates
assertEquals(1, candidates1.size)
assertTrue(candidates1.forall(_.isDeprecated))
val candidates2 = completer.complete("Stuff.th").candidates
assertEquals(2, candidates2.size)
assertTrue(candidates2.forall(_.isDeprecated))
}
@Test
def isNotDeprecated(): Unit = {
val (completer, _, _) = interpretLines(
"""object Stuff { def `this` = ??? ; def `that` = ??? }"""
)
val candidates = completer.complete("Stuff.th").candidates
assertEquals(2, candidates.size)
assert(candidates.forall(!_.isDeprecated), "No deprecations")
}
@Test
def importTypesAndTermsBoth(): Unit = {
val (completer, _, _) = interpretLines(
"""object A { class Type; object Term }"""
)
val candidates1 = completer.complete("A.T").candidates
assertEquals("Term", candidates1.map(_.defString).mkString(" "))
val candidates2 = completer.complete("import A.T").candidates
assertEquals("Term Type", candidates2.map(_.defString).sorted.mkString(" "))
}
@Test
def dependentTypeImplicits_t10353(): Unit = {
val code =
"""
package test
// tests for autocomplete on repl
object Test {
trait Conv[In] {
type Out
def apply(in: In): Out
}
object Conv {
type Aux[In, Out0] = Conv[In] { type Out = Out0 }
implicit val int2String = new Conv[Int] {
type Out = String
override def apply(i: Int) = i.toString
}
}
// autocomplete works on repl: `test.Test.withParens().<TAB>` shows completions for String
def withParens[Out]()(implicit conv: Conv.Aux[Int, Out]): Out = "5".asInstanceOf[Out]
// autocomplete doesn't work on repl: `test.Test.withoutParens.` doesn't suggest anything
// when saving intermediate result it works though: `val a = test.Test.withoutParens; a.<TAB>`
def withoutParens[Out](implicit conv: Conv.Aux[Int, Out]): Out = "5".asInstanceOf[Out]
}
// this works fine
object Test2 {
trait A
implicit val a: A = ???
def withParens()(implicit a: A): String = "something"
def withoutParens(implicit a: A): String = "something"
}
"""
val completer = setup(new BatchSourceFile("<paste>", code))
checkExact(completer, "val x = test.Test.withoutParens; x.charA")("charAt")
checkExact(completer, "test.Test.withoutParens.charA")("charAt")
}
def checkExact(completer: Completion, before: String, after: String = "", includeUniversal: Boolean = false)(expected: String*): Unit = {
val actual =
completer.complete(before, after).candidates
.filter(c => includeUniversal || !c.isUniversal)
.map(_.defString)
assertEquals(expected.sorted.mkString(" "), actual.toSeq.distinct.sorted.mkString(" "))
}
}