-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ReplCompilerTests.scala
179 lines (153 loc) · 4.98 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
package dotty.tools.repl
import org.junit.Assert._
import org.junit.{Ignore, Test}
class ReplCompilerTests extends ReplTest {
private def lines() =
storedOutput().trim.linesIterator.toList
@Test def compileSingle = fromInitialState { implicit state =>
run("def foo: 1 = 1")
assertEquals("def foo: 1", storedOutput().trim)
}
@Test def compileTwo =
fromInitialState { implicit state =>
run("def foo: 1 = 1")
}
.andThen { implicit state =>
val s2 = run("def foo(i: Int): i.type = i")
assertEquals(2, s2.objectIndex)
}
@Test def inspectWrapper =
fromInitialState { implicit state =>
run("def foo = 1")
}.andThen { implicit state =>
storedOutput() // discard output
run("val x = rs$line$1.foo")
assertEquals("val x: Int = 1", storedOutput().trim)
}
@Test def testVar = fromInitialState { implicit state =>
run("var x = 5")
assertEquals("var x: Int = 5", storedOutput().trim)
}
@Test def testRes = fromInitialState { implicit state =>
run {
"""|def foo = 1 + 1
|val x = 5 + 5
|1 + 1
|var y = 5
|10 + 10""".stripMargin
}
val expected = List(
"def foo: Int",
"val res0: Int = 2",
"val res1: Int = 20",
"val x: Int = 10",
"var y: Int = 5"
)
assertEquals(expected, lines())
}
@Test def testImportMutable =
fromInitialState { implicit state =>
run("import scala.collection.mutable")
}
.andThen { implicit state =>
assertEquals(1, 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 =
fromInitialState { implicit s =>
val state = run("var x = 5")
assertEquals("var x: Int = 5", storedOutput().trim)
state
}
.andThen { implicit s =>
run("x = 10")
assertEquals("x: Int = 10", storedOutput().trim)
}
// FIXME: Tests are not run in isolation, the classloader is corrupted after the first exception
@Ignore @Test def i3305: Unit = {
fromInitialState { implicit s =>
run("null.toString")
assertTrue(storedOutput().startsWith("java.lang.NullPointerException"))
}
fromInitialState { implicit s =>
run("def foo: Int = 1 + foo; foo")
assertTrue(storedOutput().startsWith("def foo: Int\njava.lang.StackOverflowError"))
}
fromInitialState { implicit s =>
run("""throw new IllegalArgumentException("Hello")""")
assertTrue(storedOutput().startsWith("java.lang.IllegalArgumentException: Hello"))
}
fromInitialState { implicit s =>
run("val (x, y) = null")
assertTrue(storedOutput().startsWith("scala.MatchError: null"))
}
}
@Test def i2789: Unit = fromInitialState { implicit state =>
run("(x: Int) => println(x)")
assertTrue(storedOutput().startsWith("val res0: Int => Unit ="))
}
@Test def byNameParam: Unit = fromInitialState { implicit state =>
run("def f(g: => Int): Int = g")
assertTrue(storedOutput().startsWith("def f(g: => Int): Int"))
}
@Test def i4051 = fromInitialState { implicit state =>
// FIXME: shouldn't have to pass a type parameter to collect.
val source =
"""val x: PartialFunction[Int, Int] = { case x => x }
|val y = Map(("A", 1), ("B", 2), ("X", 3)).collect[Int] { 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 =
fromInitialState { implicit state => run("given as Int = 10") }
.andThen { implicit state =>
assertEquals(
"def Int_given: Int",
storedOutput().trim
)
run("implicitly[Int]")
assertEquals(
"val res0: Int = 10",
storedOutput().trim
)
}
@Test def i6200 =
fromInitialState { implicit state =>
run("""
|trait Ord[T] {
| def compare(x: T, y: T): Int
| def (x: T) < (y: T) = compare(x, y) < 0
| def (x: T) > (y: T) = compare(x, y) > 0
|}
|
|given IntOrd as Ord[Int] {
| def compare(x: Int, y: Int) =
| if (x < y) -1 else if (x > y) +1 else 0
|}
""".stripMargin) }
.andThen { implicit state =>
assertEquals(
"""// defined trait Ord
|// defined object IntOrd""".stripMargin,
storedOutput().trim
)
run("IntOrd")
assertTrue(storedOutput().startsWith("val res0: IntOrd.type ="))
}
@Test def testSingletonPrint = fromInitialState { implicit state =>
run("""val a = "hello"; val x: a.type = a""")
assertEquals("val a: String = hello\nval x: a.type = hello", storedOutput().trim)
}
@Test def i6574 = fromInitialState { implicit state =>
run("val a: 1 | 0 = 1")
assertEquals("val a: 1 | 0 = 1", storedOutput().trim)
}
}