-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathReplCompilerTests.scala
584 lines (515 loc) · 18.4 KB
/
ReplCompilerTests.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package dotty.tools.repl
import scala.language.unsafeNulls
import java.util.regex.Pattern
import org.junit.Assert.{assertEquals, assertFalse, assertTrue}
import org.junit.Assert.{assertTrue => assert}
import org.junit.Test
import dotty.tools.dotc.core.Contexts.Context
class ReplCompilerTests extends ReplTest:
import ReplCompilerTests._
private def lines() =
storedOutput().trim.linesIterator.toList
@Test def compileSingle = initially {
run("def foo: 1 = 1")
assertEquals("def foo: 1", storedOutput().trim)
}
@Test def i18383NoWarnOnUnusedImport: Unit = {
initially {
run("import scala.collection.*")
} andThen {
println(lines().mkString("* ", "\n * ", ""))
}
}
@Test def compileTwo =
initially {
run("def foo: 1 = 1")
} andThen {
val s2 = run("def foo(i: Int): i.type = i")
assertEquals(2, s2.objectIndex)
}
@Test def inspectWrapper =
initially {
run("def foo = 1")
} andThen {
storedOutput() // discard output
run("val x = rs$line$1.foo")
assertEquals("val x: Int = 1", storedOutput().trim)
}
@Test def testVar = initially {
run("var x = 5")
assertEquals("var x: Int = 5", storedOutput().trim)
}
@Test def testRes = initially {
run {
"""|def foo = 1 + 1
|val x = 5 + 5
|1 + 1
|var y = 5
|10 + 10""".stripMargin
}
val expected = List(
"def foo: Int",
"val x: Int = 10",
"val res0: Int = 2",
"var y: Int = 5",
"val res1: Int = 20"
)
assertEquals(expected, lines())
}
@Test def testImportMutable =
initially {
run("import scala.collection.mutable")
} andThen {
assertEquals(1, summon[State].imports.size)
run("""mutable.Map("one" -> 1)""")
assertEquals(
"val res0: scala.collection.mutable.Map[String, Int] = HashMap(one -> 1)",
storedOutput().trim
)
}
@Test def rebindVariable =
initially {
val state = run("var x = 5")
assertEquals("var x: Int = 5", storedOutput().trim)
state
} andThen {
run("x = 10")
assertEquals("x: Int = 10", storedOutput().trim)
}
@Test def defaultParameter = initially {
run("def foo(a: Int = 1): 1 = 1")
assertEquals("def foo(a: Int): 1", storedOutput().trim)
}
@Test def i8677 = initially {
run {
"""|sealed trait T1
|case class X() extends T1
|case class Y() extends T1
|case object O extends T1
""".stripMargin
}
val expected = List(
"// defined trait T1",
"// defined case class X",
"// defined case class Y",
"// defined case object O"
)
assertEquals(expected, lines())
}
@Test def `i3305 SOE meh`: Unit = initially:
run("def foo: Int = 1 + foo; foo")
assert(storedOutput().startsWith("java.lang.StackOverflowError"))
@Test def `i3305 NPE`: Unit = initially:
run("null.toString")
assert(storedOutput().startsWith("java.lang.NullPointerException"))
@Test def `i3305 IAE`: Unit = initially:
run("""throw new IllegalArgumentException("Hello")""")
assertTrue(storedOutput().startsWith("java.lang.IllegalArgumentException: Hello"))
@Test def `i3305 ME`: Unit = initially:
run("val (x, y) = null")
assert(storedOutput().startsWith("scala.MatchError: null"))
@Test def i2789: Unit = initially {
run("(x: Int) => println(x)")
assert(storedOutput().startsWith("val res0: Int => Unit ="))
}
@Test def byNameParam: Unit = initially {
run("def f(g: => Int): Int = g")
assert(storedOutput().startsWith("def f(g: => Int): Int"))
}
@Test def i4051 = initially {
val source =
"""val x: PartialFunction[Int, Int] = { case x => x }
|val y = Map(("A", 1), ("B", 2), ("X", 3)).collect { case (k, v) => v }.toList""".stripMargin
val expected = List(
"val x: PartialFunction[Int, Int] = <function1>",
"val y: List[Int] = List(1, 2, 3)"
)
run(source)
assertEquals(expected, lines())
}
@Test def i5897 =
initially {
run("given Int = 10")
} andThen {
assertEquals(
"lazy val given_Int: Int",
storedOutput().trim
)
run("implicitly[Int]")
assertEquals(
"val res0: Int = 10",
storedOutput().trim
)
}
@Test def i16596 =
initially {
run("""
|import scala.compiletime.{error, ops, requireConst}, ops.int.*
|import scala.quoted.*
|
|sealed trait Nat
|object Nat:
| case object Zero extends Nat
| case class Succ[N <: Nat](prev: N) extends Nat
|
| given zero: Zero.type = Zero
| given buildSucc[N <: Nat](using n: N): Succ[N] = Succ(n)
|
| def value[N <: Nat](using n: N): N = n
|
| def prevImpl[I <: Int: Type](expr: Expr[I])(using Quotes): Expr[I - 1] =
| val prev = expr.valueOrAbort - 1
| // this compiles, but fails at use time
| //Expr(prev).asExprOf[ops.int.-[I, 1]]
| Expr(prev).asInstanceOf[Expr[I - 1]]
|
| inline def prevOf[I <: Int](inline i: I): I - 1 = ${prevImpl('i)}
|
| type FromInt[I <: Int] <: Nat = I match
| case 0 => Zero.type
| case _ => Succ[FromInt[I - 1]]
|
| inline def fromInt[I <: Int & Singleton](i: I): FromInt[i.type] =
| requireConst(i)
| inline i match
| case _: 0 => Zero
| case _ =>
| inline if i < 0
| then error("cannot convert negative to Nat")
| else Succ(fromInt(prevOf[i.type](i)))
""".stripMargin)
}.andThen {
assertMultiLineEquals(
"""// defined trait Nat
|// defined object Nat
|""".stripMargin, storedOutput())
run("Nat.fromInt(2)")
}.andThen {
assertEquals("val res0: Nat.Succ[Nat.Succ[Nat.Zero.type]] = Succ(Succ(Zero))", storedOutput().trim)
run("summon[Nat.FromInt[2]]")
}.andThen {
assertEquals("val res1: Nat.Succ[Nat.Succ[Nat.Zero.type]] = Succ(Succ(Zero))", storedOutput().trim)
run("Nat.fromInt(3)")
}.andThen {
assertEquals("val res2: Nat.Succ[Nat.Succ[Nat.Succ[Nat.Zero.type]]] = Succ(Succ(Succ(Zero)))", storedOutput().trim)
run("summon[Nat.FromInt[3]]")
}.andThen {
assertEquals("val res3: Nat.Succ[Nat.Succ[Nat.Succ[Nat.Zero.type]]] = Succ(Succ(Succ(Zero)))", storedOutput().trim)
}
@Test def i6200 =
initially {
run("""
|trait Ord[T] {
| def compare(x: T, y: T): Int
| extension (x: T) def < (y: T) = compare(x, y) < 0
| extension (x: T) def > (y: T) = compare(x, y) > 0
|}
|
|given IntOrd: Ord[Int] with {
| def compare(x: Int, y: Int) =
| if (x < y) -1 else if (x > y) +1 else 0
|}
""".stripMargin)
} andThen {
assertMultiLineEquals(
"""// defined trait Ord
|// defined object IntOrd""".stripMargin,
storedOutput().trim
)
run("IntOrd")
assert(storedOutput().startsWith("val res0: IntOrd.type ="))
}
@Test def i7934: Unit = contextually {
assertFalse(ParseResult.isIncomplete("_ + 1")) // was: assertThrows[NullPointerException]
}
@Test def `i9374 accept collective extensions`: Unit = contextually {
assert(ParseResult.isIncomplete("extension (x: String)"))
assert(ParseResult.isIncomplete("extension (x: String) {"))
}
@Test def testSingletonPrint = initially {
run("""val a = "hello"; val x: a.type = a""")
assertMultiLineEquals("val a: String = hello\nval x: a.type = hello", storedOutput().trim)
}
@Test def i6574 = initially {
run("val a: 1 | 0 = 1")
assertEquals("val a: 1 | 0 = 1", storedOutput().trim)
}
@Test def `i10214 must show classic MatchError` = initially {
run("val 1 = 2: @unchecked")
assertEquals("scala.MatchError: 2 (of class java.lang.Integer)", storedOutput().linesIterator.next())
}
@Test def `i10214 must show useful regex MatchError` =
initially {
run("""val r = raw"\d+".r""")
} andThen {
run("""val r() = "abc": @unchecked""")
assertEquals("scala.MatchError: abc (of class java.lang.String)", storedOutput().linesIterator.drop(1).next())
}
@Test def `i10214 must show MatchError on literal type` = initially {
run("val (x: 1) = 2: @unchecked")
assertEquals("scala.MatchError: 2 (of class java.lang.Integer)", storedOutput().linesIterator.next())
}
@Test def `i12920 must truncate stack trace to user code` = initially {
run("???")
val all = lines()
assertEquals(3, all.length)
assertEquals("scala.NotImplementedError: an implementation is missing", all.head)
/* avoid asserting much about line number or elided count
scala.NotImplementedError: an implementation is missing
at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
... 28 elided
*/
}
@Test def `i14281 context class loader must be REPL class loader` = initially {
run("class C ; assert(classOf[C].getClassLoader eq Thread.currentThread.getContextClassLoader)")
assertEquals(List("// defined class C"), lines())
}
def assertNotFoundError(id: String): Unit =
val lines = storedOutput().linesIterator
assert(lines.next().startsWith("-- [E006] Not Found Error:"))
assert(lines.drop(2).next().trim().endsWith(s"Not found: $id"))
@Test def i4416 = initially {
val state = run("val x = 1 / 0")
val all = lines()
assertEquals(2, all.length)
assert(all.head.startsWith("java.lang.ArithmeticException:"))
state
} andThen {
val state = run("def foo = x")
assertNotFoundError("x")
state
} andThen {
run("x")
assertNotFoundError("x")
}
@Test def i4416b = initially {
val state = run("val a = 1234")
val _ = storedOutput() // discard output
state
} andThen {
val state = run("val a = 1; val x = ???; val y = x")
val all = lines()
assertEquals(3, all.length)
assertEquals("scala.NotImplementedError: an implementation is missing", all.head)
state
} andThen {
val state = run("x")
assertNotFoundError("x")
state
} andThen {
val state = run("y")
assertNotFoundError("y")
state
} andThen {
run("a") // `a` should retain its original binding
assertEquals("val res0: Int = 1234", storedOutput().trim)
}
@Test def i4416_imports = initially {
run("import scala.collection.mutable")
} andThen {
val state = run("import scala.util.Try; val x = ???")
val _ = storedOutput() // discard output
state
} andThen {
run(":imports") // scala.util.Try should not be imported
assertEquals("import scala.collection.mutable", storedOutput().trim)
}
@Test def i4416_types_defs_aliases = initially {
val state =
run("""|type Foo = String
|trait Bar
|def bar: Bar = ???
|val x = ???
|""".stripMargin)
val all = lines()
assertEquals(3, all.length)
assertEquals("scala.NotImplementedError: an implementation is missing", all.head)
assert("type alias in failed wrapper should not be rendered",
!all.exists(_.startsWith("// defined alias type Foo = String")))
assert("type definitions in failed wrapper should not be rendered",
!all.exists(_.startsWith("// defined trait Bar")))
assert("defs in failed wrapper should not be rendered",
!all.exists(_.startsWith("def bar: Bar")))
state
} andThen {
val state = run("def foo: Foo = ???")
assertNotFoundError("type Foo")
state
} andThen {
val state = run("type B = Bar")
assertNotFoundError("type Bar")
state
} andThen {
run("bar")
assertNotFoundError("bar")
}
@Test def i14473 = initially {
run("""val (x,y) = (if true then "hi" else (42,17)): @unchecked""")
val all = lines()
assertEquals(2, all.length)
assertEquals("scala.MatchError: hi (of class java.lang.String)", all.head)
}
@Test def i14701 = initially {
val state = run("val _ = ???")
val all = lines()
assertEquals(3, all.length)
assertEquals("scala.NotImplementedError: an implementation is missing", all.head)
state
} andThen {
run("val _ = assert(false)")
val all = lines()
assertEquals(3, all.length)
assertEquals("java.lang.AssertionError: assertion failed", all.head)
}
@Test def `i13097 expect lambda after colon` = contextually:
assert(ParseResult.isIncomplete("val x = List(42).foreach:"))
@Test def `i13097 expect template after colon` = contextually:
assert(ParseResult.isIncomplete("class C:"))
@Test def i15562: Unit = initially {
val s1 = run("List(1, 2).filter(_ % 2 == 0).foreach(println)")
assertEquals("2", storedOutput().trim)
s1
} andThen { s1 ?=>
val comp = tabComplete("List(1, 2).filter(_ % 2 == 0).fore")
assertEquals(List("foreach"), comp.distinct)
s1
} andThen {
val s2 = run("List(1, 2).filter(_ % 2 == 0).foreach(println)")
assertEquals("2", storedOutput().trim)
s2
}
@Test def i15562b: Unit = initially {
val s1 = run("List(1, 2).filter(_ % 2 == 0).foreach(println)")
assertEquals("2", storedOutput().trim)
s1
} andThen { s1 ?=>
val comp = tabComplete("val x = false + true; List(1, 2).filter(_ % 2 == 0).fore")
assertEquals(List("foreach"), comp.distinct)
s1
} andThen {
val s2 = run("List(1, 2).filter(_ % 2 == 0).foreach(println)")
assertEquals("2", storedOutput().trim)
s2
}
@Test def `i17333 print null result of toString`: Unit =
initially:
run("val tpolecat = new Object { override def toString(): String = null }")
.andThen:
val last = lines().last
assertTrue(last, last.startsWith("val tpolecat: Object = null"))
assertTrue(last, last.endsWith("""// result of "tpolecat.toString" is null"""))
@Test def `i17333 print toplevel object with null toString`: Unit =
initially:
run("object tpolecat { override def toString(): String = null }")
.andThen:
run("tpolecat")
val last = lines().last
assertTrue(last, last.startsWith("val res0: tpolecat.type = null"))
assertTrue(last, last.endsWith("""// result of "res0.toString" is null"""))
@Test def `i21431 filter out best effort options`: Unit =
initially:
run(":settings -Ybest-effort -Ywith-best-effort-tasty")
.andThen:
run("0") // check for crash
val last = lines()
assertTrue(last(0), last(0) == ("Options incompatible with repl will be ignored: -Ybest-effort, -Ywith-best-effort-tasty"))
assertTrue(last(1), last(1) == ("val res0: Int = 0"))
@Test def `i9879`: Unit = initially:
run {
"""|opaque type A = Int; def getA: A = 0
|object Wrapper { opaque type A = Int; def getA: A = 1 }
|val x = getA
|val y = Wrapper.getA""".stripMargin
}
val expected = List(
"def getA: A",
"// defined object Wrapper",
"val x: A = 0",
"val y: Wrapper.A = 1"
)
assertEquals(expected, lines())
@Test def `i9879b`: Unit = initially:
run {
"""|def test =
| type A = Int
| opaque type B = String
| object Wrapper { opaque type C = Int }
| ()""".stripMargin
}
val all = lines()
assertEquals(6, all.length)
assertTrue(all.head.startsWith("-- [E103] Syntax Error"))
assertTrue(all.exists(_.trim().startsWith("| Illegal start of statement: this modifier is not allowed here")))
@Test def `i16250a`: Unit = initially:
val hints = List(
"this language import is not allowed in the REPL",
"To use this language feature, include the flag `-language:experimental.captureChecking` when starting the REPL"
)
run("import language.experimental.captureChecking")
val all = lines()
assertTrue(hints.forall(hint => all.exists(_.contains(hint))))
@Test def `i16250b`: Unit = initially:
val hints = List(
"this language import is not allowed in the REPL",
"To use this language feature, include the flag `-language:experimental.pureFunctions` when starting the REPL"
)
run("import language.experimental.pureFunctions")
val all = lines()
assertTrue(hints.forall(hint => all.exists(_.contains(hint))))
@Test def `i22844 regression colon eol`: Unit = initially:
run:
"""|println:
| "hello, world"
|""".stripMargin // outdent, but this test does not exercise the bug
assertEquals(List("hello, world"), lines())
@Test def `i22844b regression colon arrow eol`: Unit = contextually:
assertTrue(ParseResult.isIncomplete("List(42).map: x =>"))
object ReplCompilerTests:
private val pattern = Pattern.compile("\\r[\\n]?|\\n");
// Ensure 'expected' and 'actual' contain the same line separator(s).
def assertMultiLineEquals(expected: String, actual: String): Unit =
val expected0 = pattern.matcher(expected).replaceAll(System.lineSeparator)
val actual0 = pattern.matcher(actual).replaceAll(System.lineSeparator)
assertEquals(expected0, actual0)
end ReplCompilerTests
class ReplXPrintTyperTests extends ReplTest(ReplTest.defaultOptions :+ "-Xprint:typer"):
@Test def i9111 = initially {
run("""|enum E {
| case A
|}""".stripMargin)
assert(storedOutput().trim().endsWith("// defined class E"))
}
@Test def i10883 = initially {
run("val a = 42")
assert(storedOutput().trim().endsWith("val a: Int = 42"))
}
end ReplXPrintTyperTests
class ReplVerboseTests extends ReplTest(ReplTest.defaultOptions :+ "-verbose"):
@Test def i9111 = initially {
run("""|enum E {
| case A
|}""".stripMargin)
assert(storedOutput().trim().endsWith("// defined class E"))
}
@Test def i10883 = initially {
run("val a = 42")
assert(storedOutput().trim().endsWith("val a: Int = 42"))
}
@Test def `i4393-incomplete-catch`: Unit = contextually {
assert(ParseResult.isIncomplete("""|try {
| ???
|} catch""".stripMargin))
assert(ParseResult.isIncomplete("""|try {
| ???
|} catch {""".stripMargin))
}
end ReplVerboseTests
class ReplHighlightTests extends ReplTest(ReplTest.defaultOptions.filterNot(_.startsWith("-color")) :+ "-color:always"):
@Test def i18596: Unit = initially:
run("""(1 to 500).foldRight("x") { case (_, n) => s"<x>$n</x>" }""")
@Test def i16904: Unit = initially:
run(""""works not fine"* 10000""")
run("""
case class Tree(left: Tree, right: Tree)
def deepTree(depth: Int): Tree
deepTree(300)""")