1
1
import scala .tools .nsc .Settings
2
- import scala .tools .nsc .interpreter .ILoop
2
+ import scala .tools .nsc .interpreter .{ ILoop , replProps }
3
3
import scala .tools .nsc .settings .ClassPathRepresentationType
4
4
import scala .tools .partest ._
5
5
6
6
object Test extends StoreReporterDirectTest {
7
7
def code = ???
8
8
9
+ lazy val headerLength = replProps.welcome.lines.size
10
+ lazy val promptLength = replProps.prompt.lines.size - 1 // extra newlines
11
+
9
12
def compileCode (code : String , jarFileName : String ) = {
10
13
val classpath = List (sys.props(" partest.lib" ), testOutput.path) mkString sys.props(" path.separator" )
11
14
compileString(newCompiler(" -cp" , classpath, " -d" , s " ${testOutput.path}/ $jarFileName" ))(code)
@@ -56,14 +59,21 @@ object Test extends StoreReporterDirectTest {
56
59
val jar = " test1.jar"
57
60
compileCode(app1, jar)
58
61
59
- val codeToRun = toCodeInSeparateLines(s " :require ${testOutput.path}/ $jar" , " test.Test.test()" )
62
+ val codeToRun = s """
63
+ |:require ${testOutput.path}/ $jar
64
+ |test.Test.test()
65
+ | """ .stripMargin.trim
60
66
val output = ILoop .run(codeToRun, settings)
61
- val lines = output.split(" \n " )
62
- assert {
63
- lines(4 ).contains(" Added" ) && lines(4 ).contains(" test1.jar" )
64
- }
67
+ var lines = output.lines.drop(headerLength)
68
+ lines = lines drop promptLength
69
+ val added = lines.next
70
+ assert (
71
+ added.contains(" Added" ) && added.contains(" test1.jar" ),
72
+ s " [ ${added}] in [ ${output.lines.mkString(" /" )}] "
73
+ )
74
+ lines = lines drop promptLength
65
75
assert {
66
- lines(lines.length - 3 ) .contains(" testing..." )
76
+ lines.next .contains(" testing..." )
67
77
}
68
78
}
69
79
@@ -73,14 +83,21 @@ object Test extends StoreReporterDirectTest {
73
83
val jar2 = " test2.jar"
74
84
compileCode(app2, jar2)
75
85
76
- val codeToRun = toCodeInSeparateLines(s " :require ${testOutput.path}/ $jar1" , s " :require ${testOutput.path}/ $jar2" )
86
+ val codeToRun = s """
87
+ |:require ${testOutput.path}/ $jar1
88
+ |:require ${testOutput.path}/ $jar2
89
+ | """ .stripMargin.trim
77
90
val output = ILoop .run(codeToRun, settings)
78
- val lines = output.split(" \n " )
91
+ var lines = output.lines.drop(headerLength)
92
+ lines = lines drop promptLength
93
+ val added = lines.next
79
94
assert {
80
- lines( 4 ) .contains(" Added" ) && lines( 4 ) .contains(" test1.jar" )
95
+ added .contains(" Added" ) && added .contains(" test1.jar" )
81
96
}
97
+ lines = lines drop promptLength
98
+ val msg = lines.next
82
99
assert {
83
- lines(lines.length - 3 ). contains(" test2.jar" ) && lines(lines.length - 3 ) .contains(" existing classpath entries conflict" )
100
+ msg. contains(" test2.jar" ) && msg .contains(" existing classpath entries conflict" )
84
101
}
85
102
}
86
103
@@ -90,28 +107,42 @@ object Test extends StoreReporterDirectTest {
90
107
val jar3 = " test3.jar"
91
108
compileCode(app3, jar3)
92
109
93
- val codeToRun = toCodeInSeparateLines(s " :require ${testOutput.path}/ $jar1" , s " :require ${testOutput.path}/ $jar3" , " test.Test3.test()" )
110
+ val codeToRun = s """
111
+ |:require ${testOutput.path}/ $jar1
112
+ |:require ${testOutput.path}/ $jar3
113
+ |test.Test3.test()
114
+ | """ .stripMargin.trim
94
115
val output = ILoop .run(codeToRun, settings)
95
- val lines = output.split(" \n " )
116
+ var lines = output.lines.drop(headerLength)
117
+ lines = lines drop promptLength
118
+ val added = lines.next
96
119
assert {
97
- lines( 4 ) .contains(" Added" ) && lines( 4 ) .contains(" test1.jar" )
120
+ added .contains(" Added" ) && added .contains(" test1.jar" )
98
121
}
122
+ lines = lines drop (2 * promptLength + 1 )
99
123
assert {
100
- lines(lines.length - 3 ) .contains(" new object in existing package" )
124
+ lines.next .contains(" new object in existing package" )
101
125
}
102
126
}
103
127
104
128
def test4 (): Unit = {
105
129
// twice the same jar should be rejected
106
130
val jar1 = " test1.jar"
107
- val codeToRun = toCodeInSeparateLines(s " :require ${testOutput.path}/ $jar1" , s " :require ${testOutput.path}/ $jar1" )
131
+ val codeToRun = s """
132
+ |:require ${testOutput.path}/ $jar1
133
+ |:require ${testOutput.path}/ $jar1
134
+ | """ .stripMargin.trim
108
135
val output = ILoop .run(codeToRun, settings)
109
- val lines = output.split(" \n " )
136
+ var lines = output.lines.drop(headerLength)
137
+ lines = lines drop promptLength
138
+ val added = lines.next
110
139
assert {
111
- lines( 4 ) .contains(" Added" ) && lines( 4 ) .contains(" test1.jar" )
140
+ added .contains(" Added" ) && added .contains(" test1.jar" )
112
141
}
142
+ lines = lines drop promptLength
143
+ val msg = lines.next
113
144
assert {
114
- lines(lines.length - 3 ). contains(" test1.jar" ) && lines(lines.length - 3 ) .contains(" existing classpath entries conflict" )
145
+ msg. contains(" test1.jar" ) && msg .contains(" existing classpath entries conflict" )
115
146
}
116
147
}
117
148
@@ -127,7 +158,10 @@ object Test extends StoreReporterDirectTest {
127
158
// classloader to parse .class files in order to read their names.
128
159
val jar = " test6.jar"
129
160
compileCode(app6, jar)
130
- val codeToRun = toCodeInSeparateLines(s " :require ${testOutput.path}/ $jar" , " import test6._; new A; new Z" )
161
+ val codeToRun = s """
162
+ |:require ${testOutput.path}/ $jar
163
+ |import test6._; new A; new Z
164
+ | """ .stripMargin.trim
131
165
val output = ILoop .run(codeToRun, settings)
132
166
assert(output.contains(" created test6.A" ), output)
133
167
assert(output.contains(" created test6.Z" ), output)
@@ -141,6 +175,4 @@ object Test extends StoreReporterDirectTest {
141
175
test5()
142
176
test6()
143
177
}
144
-
145
- def toCodeInSeparateLines (lines : String * ): String = lines mkString " \n "
146
178
}
0 commit comments