@@ -8,121 +8,111 @@ import dotc.core.Contexts.Context
8
8
import dotc .ast .Trees ._
9
9
import dotc .ast .untpd
10
10
11
- import results ._
12
- import ReplTest ._
13
-
14
11
class ReplCompilerTests extends ReplTest {
15
12
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)
18
16
}
19
17
20
-
21
18
@ Test def compileTwo =
22
19
fromInitialState { implicit state =>
23
- compiler.compile (" def foo: 1 = 1" ).stateOrFail
20
+ run (" def foo: 1 = 1" )
24
21
}
25
22
.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)
29
25
}
30
26
31
- @ Test def inspectSingle =
27
+ @ Test def inspectWrapper =
32
28
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)
44
35
}
45
36
46
37
@ 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 )
49
40
}
50
41
51
42
@ Test def testRes = fromInitialState { implicit state =>
52
- compile {
43
+ run {
53
44
""" |def foo = 1 + 1
54
45
|val x = 5 + 5
55
46
|1 + 1
56
47
|var y = 5
57
48
|10 + 10""" .stripMargin
58
49
}
59
50
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
+ )
65
58
66
- expected === storedOutput().split(" \n " )
59
+ assertEquals( expected, storedOutput().split(" \n " ).toList )
67
60
}
68
61
69
62
@ Test def testImportMutable =
70
63
fromInitialState { implicit state =>
71
- compile (" import scala.collection.mutable" )
64
+ run (" import scala.collection.mutable" )
72
65
}
73
66
.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)""" )
78
69
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
81
72
)
82
73
}
83
74
84
75
@ 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
+ }
86
81
.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)
94
84
}
95
85
96
86
// 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 = {
98
88
fromInitialState { implicit s =>
99
- compile (" null.toString" )
89
+ run (" null.toString" )
100
90
assertTrue(storedOutput().startsWith(" java.lang.NullPointerException" ))
101
91
}
102
92
103
93
fromInitialState { implicit s =>
104
- compile (" def foo: Int = 1 + foo; foo" )
94
+ run (" def foo: Int = 1 + foo; foo" )
105
95
assertTrue(storedOutput().startsWith(" def foo: Int\n java.lang.StackOverflowError" ))
106
96
}
107
97
108
98
fromInitialState { implicit s =>
109
- compile (""" throw new IllegalArgumentException("Hello")""" )
99
+ run (""" throw new IllegalArgumentException("Hello")""" )
110
100
assertTrue(storedOutput().startsWith(" java.lang.IllegalArgumentException: Hello" ))
111
101
}
112
102
113
103
fromInitialState { implicit s =>
114
- compile (" val (x, y) = null" )
104
+ run (" val (x, y) = null" )
115
105
assertTrue(storedOutput().startsWith(" scala.MatchError: null" ))
116
106
}
117
107
}
118
108
119
109
@ Test def i2789 : Unit = fromInitialState { implicit state =>
120
- compile (" (x: Int) => println(x)" )
110
+ run (" (x: Int) => println(x)" )
121
111
assertTrue(storedOutput().startsWith(" val res0: Int => Unit =" ))
122
112
}
123
113
124
114
@ Test def byNameParam : Unit = fromInitialState { implicit state =>
125
- compile (" def f(g: => Int): Int = g" )
115
+ run (" def f(g: => Int): Int = g" )
126
116
assertTrue(storedOutput().startsWith(" def f(g: => Int): Int" ))
127
117
}
128
118
}
0 commit comments