Skip to content

Commit 7ff9103

Browse files
committed
Simplify REPL tests
1 parent a0c3203 commit 7ff9103

File tree

4 files changed

+54
-111
lines changed

4 files changed

+54
-111
lines changed

compiler/test/dotty/tools/repl/ReplCompilerTests.scala

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,121 +8,111 @@ import dotc.core.Contexts.Context
88
import dotc.ast.Trees._
99
import dotc.ast.untpd
1010

11-
import results._
12-
import ReplTest._
13-
1411
class ReplCompilerTests extends ReplTest {
1512

16-
@Test def compileSingle: Unit = fromInitialState { implicit state =>
17-
compiler.compile("def foo: 1 = 1").stateOrFail
13+
@Test def compileSingle = fromInitialState { implicit state =>
14+
run("def foo: 1 = 1")
15+
assertEquals("def foo: Int(1)", storedOutput().trim)
1816
}
1917

20-
2118
@Test def compileTwo =
2219
fromInitialState { implicit state =>
23-
compiler.compile("def foo: 1 = 1").stateOrFail
20+
run("def foo: 1 = 1")
2421
}
2522
.andThen { implicit state =>
26-
val s2 = compiler.compile("def foo(i: Int): i.type = i").stateOrFail
27-
assert(s2.objectIndex == 2,
28-
s"Wrong object offset: expected 2 got ${s2.objectIndex}")
23+
val s2 = run("def foo(i: Int): i.type = i")
24+
assertEquals(2, s2.objectIndex)
2925
}
3026

31-
@Test def inspectSingle =
27+
@Test def inspectWrapper =
3228
fromInitialState { implicit state =>
33-
val untpdTree = compiler.compile("def foo: 1 = 1").map(_._1.untpdTree)
34-
35-
untpdTree.fold(
36-
onErrors,
37-
_ match {
38-
case PackageDef(_, List(mod: untpd.ModuleDef)) =>
39-
implicit val ctx = state.run.runContext
40-
assert(mod.name.show == "rs$line$1", mod.name.show)
41-
case tree => fail(s"Unexpected structure: $tree")
42-
}
43-
)
29+
run("def foo = 1")
30+
31+
}.andThen { implicit state =>
32+
storedOutput() // discard output
33+
run("val x = rs$line$1.foo")
34+
assertEquals("val x: Int = 1", storedOutput().trim)
4435
}
4536

4637
@Test def testVar = fromInitialState { implicit state =>
47-
compile("var x = 5")
48-
assertEquals("var x: Int = 5\n", storedOutput())
38+
run("var x = 5")
39+
assertEquals("var x: Int = 5", storedOutput().trim)
4940
}
5041

5142
@Test def testRes = fromInitialState { implicit state =>
52-
compile {
43+
run {
5344
"""|def foo = 1 + 1
5445
|val x = 5 + 5
5546
|1 + 1
5647
|var y = 5
5748
|10 + 10""".stripMargin
5849
}
5950

60-
val expected = Set("def foo: Int",
61-
"val x: Int = 10",
62-
"val res1: Int = 20",
63-
"val res0: Int = 2",
64-
"var y: Int = 5")
51+
val expected = List(
52+
"def foo: Int",
53+
"val x: Int = 10",
54+
"val res0: Int = 2",
55+
"var y: Int = 5",
56+
"val res1: Int = 20"
57+
)
6558

66-
expected === storedOutput().split("\n")
59+
assertEquals(expected, storedOutput().split("\n").toList)
6760
}
6861

6962
@Test def testImportMutable =
7063
fromInitialState { implicit state =>
71-
compile("import scala.collection.mutable")
64+
run("import scala.collection.mutable")
7265
}
7366
.andThen { implicit state =>
74-
assert(state.imports.nonEmpty, "Didn't add import to `State` after compilation")
75-
76-
compile("""mutable.Map("one" -> 1)""")
77-
67+
assertEquals(1, state.imports.size)
68+
run("""mutable.Map("one" -> 1)""")
7869
assertEquals(
79-
"val res0: scala.collection.mutable.Map[String, Int] = Map(one -> 1)\n",
80-
storedOutput()
70+
"val res0: scala.collection.mutable.Map[String, Int] = Map(one -> 1)",
71+
storedOutput().trim
8172
)
8273
}
8374

8475
@Test def rebindVariable =
85-
fromInitialState { implicit s => compile("var x = 5") }
76+
fromInitialState { implicit s =>
77+
val state = run("var x = 5")
78+
assertEquals("var x: Int = 5", storedOutput().trim)
79+
state
80+
}
8681
.andThen { implicit s =>
87-
compile("x = 10")
88-
assertEquals(
89-
"""|var x: Int = 5
90-
|x: Int = 10
91-
|""".stripMargin,
92-
storedOutput()
93-
)
82+
run("x = 10")
83+
assertEquals("x: Int = 10", storedOutput().trim)
9484
}
9585

9686
// FIXME: Tests are not run in isolation, the classloader is corrupted after the first exception
97-
@Ignore def i3305: Unit = {
87+
@Ignore @Test def i3305: Unit = {
9888
fromInitialState { implicit s =>
99-
compile("null.toString")
89+
run("null.toString")
10090
assertTrue(storedOutput().startsWith("java.lang.NullPointerException"))
10191
}
10292

10393
fromInitialState { implicit s =>
104-
compile("def foo: Int = 1 + foo; foo")
94+
run("def foo: Int = 1 + foo; foo")
10595
assertTrue(storedOutput().startsWith("def foo: Int\njava.lang.StackOverflowError"))
10696
}
10797

10898
fromInitialState { implicit s =>
109-
compile("""throw new IllegalArgumentException("Hello")""")
99+
run("""throw new IllegalArgumentException("Hello")""")
110100
assertTrue(storedOutput().startsWith("java.lang.IllegalArgumentException: Hello"))
111101
}
112102

113103
fromInitialState { implicit s =>
114-
compile("val (x, y) = null")
104+
run("val (x, y) = null")
115105
assertTrue(storedOutput().startsWith("scala.MatchError: null"))
116106
}
117107
}
118108

119109
@Test def i2789: Unit = fromInitialState { implicit state =>
120-
compile("(x: Int) => println(x)")
110+
run("(x: Int) => println(x)")
121111
assertTrue(storedOutput().startsWith("val res0: Int => Unit ="))
122112
}
123113

124114
@Test def byNameParam: Unit = fromInitialState { implicit state =>
125-
compile("def f(g: => Int): Int = g")
115+
run("def f(g: => Int): Int = g")
126116
assertTrue(storedOutput().startsWith("def f(g: => Int): Int"))
127117
}
128118
}

compiler/test/dotty/tools/repl/ReplTest.scala

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package dotty.tools
22
package repl
33

44
import dotty.Jars
5-
import java.io.{ OutputStream, PrintStream, ByteArrayOutputStream }
5+
import java.io.{ PrintStream, ByteArrayOutputStream }
66
import org.junit.{ Before, After }
7-
import dotc.core.Contexts.Context
87
import dotc.reporting.MessageRendering
98
import org.junit.Assert.fail
109

11-
import dotc.reporting.diagnostic.MessageContainer
12-
import results.Result
10+
import results._
1311

1412

1513
class ReplTest private (out: ByteArrayOutputStream) extends ReplDriver(
@@ -26,15 +24,6 @@ class ReplTest private (out: ByteArrayOutputStream) extends ReplDriver(
2624
output
2725
}
2826

29-
protected implicit def toParsed(expr: String)(implicit state: State): Parsed = {
30-
implicit val ctx = state.run.runContext
31-
ParseResult(expr) match {
32-
case pr: Parsed => pr
33-
case pr =>
34-
throw new java.lang.AssertionError(s"Expected parsed, got: $pr")
35-
}
36-
}
37-
3827
/** Make sure the context is new before each test */
3928
@Before def init(): Unit =
4029
resetToInitial()
@@ -51,37 +40,3 @@ class ReplTest private (out: ByteArrayOutputStream) extends ReplDriver(
5140
op(state.newRun(compiler, rootCtx))
5241
}
5342
}
54-
55-
56-
object ReplTest {
57-
58-
/** Fail if there are errors */
59-
def onErrors(xs: Seq[MessageContainer]): Nothing = throw new AssertionError(
60-
s"Expected no errors, got: \n${ xs.map(_.message).mkString("\n") }"
61-
)
62-
63-
implicit class TestingStateResult(val res: Result[State]) extends AnyVal {
64-
def stateOrFail: State = res.fold(onErrors, x => x)
65-
}
66-
67-
implicit class TestingStateResultTuple[A](val res: Result[(A, State)]) extends AnyVal {
68-
def stateOrFail: State = res.fold(onErrors, x => x._2)
69-
}
70-
71-
implicit class SetEquals(val xs: Iterable[String]) extends AnyVal {
72-
def ===(other: Iterable[String]): Unit = {
73-
val sortedXs = xs.toVector.sorted
74-
val sortedOther = other.toVector.sorted
75-
76-
val nonMatching = sortedXs
77-
.zip(sortedOther)
78-
.filterNot{ case (a, b) => a == b }
79-
.map { case (a, b) =>
80-
s"""expected element: "$a" got "$b""""
81-
}
82-
83-
if (nonMatching.nonEmpty)
84-
fail(s"\nNot all elements matched:\n${ nonMatching.mkString("\n") }")
85-
}
86-
}
87-
}

compiler/test/dotty/tools/repl/TabcompleteTests.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dotty.tools
22
package repl
33

4-
import dotty.tools.repl.ReplTest._
54
import org.junit.Assert._
65
import org.junit.Test
76

@@ -56,7 +55,7 @@ class TabcompleteTests extends ReplTest {
5655
@Test def completeFromPreviousState: Unit =
5756
fromInitialState { implicit state =>
5857
val src = "class Foo { def comp3 = 3; def comp1 = 1; def comp2 = 2 }"
59-
compiler.compile(src).stateOrFail
58+
run(src)
6059
}
6160
.andThen { implicit state =>
6261
val expected = List("comp1", "comp2", "comp3")

compiler/test/dotty/tools/repl/TypeTests.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ package repl
44
import org.junit.Test
55
import org.junit.Assert._
66

7-
import ReplTest._
8-
97
class TypeTests extends ReplTest {
108
@Test def typeOf1 = fromInitialState { implicit s =>
11-
compiler.typeOf("1")
12-
.fold(onErrors, assertEquals("Int", _))
9+
run(":type 1")
10+
assertEquals("Int", storedOutput().trim)
1311
}
1412

1513
@Test def typeOfBlock = fromInitialState { implicit s =>
16-
compiler.typeOf("{ /** omg omg omg */ 1 + 5; 1 }")
17-
.fold(onErrors, assertEquals("Int", _))
14+
run(":type { /** omg omg omg */ 1 + 5; 1 }")
15+
assertEquals("Int", storedOutput().trim)
1816
}
1917

2018
@Test def typeOfX =
21-
fromInitialState { implicit s => compile("val x = 5") }
19+
fromInitialState { implicit s => run("val x = 5") }
2220
.andThen { implicit s =>
23-
compiler.typeOf("x")
24-
.fold(onErrors, assertEquals("Int", _))
21+
storedOutput() // discard output
22+
run(":type x")
23+
assertEquals("Int", storedOutput().trim)
2524
}
2625
}

0 commit comments

Comments
 (0)