Skip to content

Commit e65a1d2

Browse files
committed
Fix scala-js/scala-js#3414: Better test kit
1 parent c563156 commit e65a1d2

File tree

13 files changed

+1043
-390
lines changed

13 files changed

+1043
-390
lines changed
Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package org.scalajs.jsenv.test
22

3-
import org.scalajs.jsenv._
4-
53
import org.junit.{Before, Test}
6-
import org.junit.Assert._
74
import org.junit.Assume._
85

9-
import scala.concurrent.Await
10-
import scala.concurrent.ExecutionContext.Implicits.global
6+
import org.scalajs.jsenv._
7+
import org.scalajs.jsenv.test.kit.TestKit
118

129
private[test] class ComTests(config: JSEnvSuiteConfig) {
13-
private val kit = new TestComKit(config)
10+
private val kit = new TestKit(config.jsEnv, config.awaitTimeout)
1411

1512
@Before
1613
def before: Unit = {
@@ -19,74 +16,76 @@ private[test] class ComTests(config: JSEnvSuiteConfig) {
1916

2017
@Test
2118
def basicTest: Unit = {
22-
val run = kit.start("""
19+
kit.withComRun("""
2320
scalajsCom.init(function(msg) { scalajsCom.send("received: " + msg); });
2421
scalajsCom.send("Hello World");
25-
""", RunConfig())
22+
""") { run =>
2623

27-
try {
28-
assertEquals("Hello World", run.waitNextMessage())
24+
run.expectMsg("Hello World")
2925

3026
for (i <- 0 to 10) {
31-
run.run.send(i.toString)
32-
assertEquals(s"received: $i", run.waitNextMessage())
27+
run
28+
.send(i.toString)
29+
.expectMsg(s"received: $i")
3330
}
34-
} finally {
35-
run.closeAndWait()
31+
32+
run.expectNoMsgs()
33+
.closeRun()
3634
}
3735
}
3836

3937
@Test
4038
def jsExitsOnMessageTest: Unit = {
4139
assumeTrue(config.supportsExit)
4240

43-
val run = kit.start("""
41+
kit.withComRun("""
4442
scalajsCom.init(function(msg) { __ScalaJSEnv.exitFunction(0); });
4543
for (var i = 0; i < 10; ++i)
4644
scalajsCom.send("msg: " + i);
47-
""", RunConfig())
45+
""") { run =>
4846

49-
try {
5047
for (i <- 0 until 10)
51-
assertEquals(s"msg: $i", run.waitNextMessage())
52-
53-
run.run.send("quit")
48+
run.expectMsg(s"msg: $i")
5449

55-
Await.result(run.run.future, config.awaitTimeout)
56-
} finally {
57-
run.run.close()
50+
run
51+
.send("quit")
52+
.expectNoMsgs()
53+
.succeeds()
5854
}
5955
}
6056

6157
@Test
6258
def multiEnvTest: Unit = {
6359
val n = 10
6460
val runs = List.fill(5) {
65-
kit.start("""
61+
kit.startWithCom("""
6662
scalajsCom.init(function(msg) {
6763
scalajsCom.send("pong");
6864
});
69-
""", RunConfig())
65+
""")
7066
}
7167

7268
try {
7369
for (_ <- 0 until n) {
74-
runs.foreach(_.run.send("ping"))
75-
runs.foreach(r => assertEquals("pong", r.waitNextMessage()))
70+
runs.foreach(_.send("ping"))
71+
runs.foreach(_.expectMsg("pong"))
72+
}
73+
74+
runs.foreach {
75+
_.expectNoMsgs()
76+
.closeRun()
7677
}
7778
} finally {
78-
runs.foreach(_.closeAndWait())
79+
runs.foreach(_.close())
7980
}
8081
}
8182

8283
private def replyTest(msg: String) = {
83-
val run = kit.start("scalajsCom.init(scalajsCom.send);", RunConfig())
84-
85-
try {
86-
run.run.send(msg)
87-
assertEquals(msg, run.waitNextMessage())
88-
} finally {
89-
run.closeAndWait()
84+
kit.withComRun("scalajsCom.init(scalajsCom.send);") {
85+
_.send(msg)
86+
.expectMsg(msg)
87+
.expectNoMsgs()
88+
.closeRun()
9089
}
9190
}
9291

@@ -103,11 +102,10 @@ private[test] class ComTests(config: JSEnvSuiteConfig) {
103102

104103
@Test
105104
def noInitTest: Unit = {
106-
val run = kit.start("", RunConfig())
107-
try {
108-
run.run.send("Dummy")
109-
} finally {
110-
run.closeAndWait()
105+
kit.withComRun("") {
106+
_.send("Dummy")
107+
.expectNoMsgs()
108+
.closeRun()
111109
}
112110
}
113111
}
Lines changed: 77 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,85 @@
11
package org.scalajs.jsenv.test
22

3-
import scala.concurrent.Await
3+
import org.junit.Assume._
4+
import org.junit.{Test, Before}
45

56
import org.scalajs.io.VirtualBinaryFile
67
import org.scalajs.jsenv._
7-
8-
import org.junit.Assert._
9-
import org.junit.Assume._
10-
import org.junit.{Test, Before}
8+
import org.scalajs.jsenv.test.kit.{TestKit, Run}
119

1210
private[test] class RunTests(config: JSEnvSuiteConfig, withCom: Boolean) {
13-
private val kit = new TestKit(config, withCom)
14-
import kit._
11+
private val kit = new TestKit(config.jsEnv, config.awaitTimeout)
12+
13+
private def withRun(input: Input)(body: Run => Unit) = {
14+
if (withCom) kit.withComRun(input)(body)
15+
else kit.withRun(input)(body)
16+
}
17+
18+
private def withRun(code: String, config: RunConfig = RunConfig())(body: Run => Unit) = {
19+
if (withCom) kit.withComRun(code, config)(body)
20+
else kit.withRun(code, config)(body)
21+
}
1522

1623
@Test
1724
def failureTest: Unit = {
18-
"""
19-
var a = {};
20-
a.foo();
21-
""".fails()
25+
withRun("""
26+
var a = {};
27+
a.foo();
28+
""") {
29+
_.fails()
30+
}
2231
}
2332

2433
@Test
2534
def syntaxErrorTest: Unit = {
26-
"""
27-
{
28-
""".fails()
35+
withRun("{") {
36+
_.fails()
37+
}
2938
}
3039

3140
@Test
3241
def throwExceptionTest: Unit = {
33-
"""
34-
throw 1;
35-
""".fails()
42+
withRun("throw 1;") {
43+
_.fails()
44+
}
3645
}
3746

3847
@Test
3948
def catchExceptionTest: Unit = {
40-
"""
41-
try {
42-
throw "hello world";
43-
} catch (e) {
44-
console.log(e);
49+
withRun("""
50+
try {
51+
throw "hello world";
52+
} catch (e) {
53+
console.log(e);
54+
}
55+
""") {
56+
_.expectOut("hello world\n")
57+
.closeRun()
4558
}
46-
""" hasOutput "hello world\n"
4759
}
4860

4961
@Test // Failed in Phantom - #2053
5062
def utf8Test: Unit = {
51-
"""
52-
console.log("\u1234");
53-
""" hasOutput "\u1234\n";
63+
withRun("""console.log("\u1234")""") {
64+
_.expectOut("\u1234\n")
65+
.closeRun()
66+
}
5467
}
5568

5669
@Test
5770
def allowScriptTags: Unit = {
58-
"""
59-
console.log("<script></script>");
60-
""" hasOutput "<script></script>\n";
71+
withRun("""console.log("<script></script>");""") {
72+
_.expectOut("<script></script>\n")
73+
.closeRun()
74+
}
6175
}
6276

6377
@Test
6478
def jsExitsTest: Unit = {
6579
assumeTrue(config.supportsExit)
6680

67-
val run = kit.start("__ScalaJSEnv.exitFunction(0);", RunConfig())
68-
try {
69-
Await.result(run.future, config.awaitTimeout)
70-
} finally {
71-
run.close()
81+
withRun("__ScalaJSEnv.exitFunction(0);") {
82+
_.succeeds()
7283
}
7384
}
7485

@@ -92,46 +103,50 @@ private[test] class RunTests(config: JSEnvSuiteConfig, withCom: Boolean) {
92103

93104
val result = strlists.map(_.mkString(" ") + "\n").mkString("")
94105

95-
codes.mkString("").hasOutput(result)
106+
withRun(codes.mkString("")) {
107+
_.expectOut(result)
108+
.closeRun()
109+
}
96110
}
97111

98112
@Test // Node.js console.log hack didn't allow to log non-Strings - #561
99113
def nonStringTest: Unit = {
100-
"""
101-
console.log(1);
102-
console.log(undefined);
103-
console.log(null);
104-
console.log({});
105-
console.log([1,2]);
106-
""" hasOutput
107-
"""|1
108-
|undefined
109-
|null
110-
|[object Object]
111-
|1,2
112-
|""".stripMargin
114+
withRun("""
115+
console.log(1);
116+
console.log(undefined);
117+
console.log(null);
118+
console.log({});
119+
console.log([1,2]);
120+
""") {
121+
_.expectOut("1\n")
122+
.expectOut("undefined\n")
123+
.expectOut("null\n")
124+
.expectOut("[object Object]\n")
125+
.expectOut("1,2\n")
126+
.closeRun()
127+
}
113128
}
114129

115130
@Test
116131
def fastCloseTest: Unit = {
117132
/* This test also tests a failure mode where the ExternalJSRun is still
118133
* piping output while the client calls close.
119134
*/
120-
val run = kit.start("", RunConfig())
121-
run.close()
122-
awaitAfterClose(run)
135+
withRun("") {
136+
_.closeRun()
137+
}
123138
}
124139

125140
@Test
126141
def multiCloseAfterTerminatedTest: Unit = {
127-
val run = kit.start("", RunConfig())
128-
run.close()
129-
awaitAfterClose(run)
130-
131-
// Should be noops (and not fail).
132-
run.close()
133-
run.close()
134-
run.close()
142+
withRun("") { run =>
143+
run.closeRun()
144+
145+
// Should be noops (and not fail).
146+
run.closeRun()
147+
run.closeRun()
148+
run.closeRun()
149+
}
135150
}
136151

137152
@Test
@@ -145,13 +160,8 @@ private[test] class RunTests(config: JSEnvSuiteConfig, withCom: Boolean) {
145160
}
146161

147162
// `start` may not throw but must fail asynchronously
148-
val run = kit.start(badFile, RunConfig())
149-
try {
150-
Await.ready(run.future, config.awaitTimeout)
151-
assertTrue("Bad file should have made run fail",
152-
run.future.value.get.isFailure)
153-
} finally {
154-
run.close()
163+
withRun(Input.ScriptsToLoad(badFile :: Nil)) {
164+
_.fails()
155165
}
156166
}
157167

@@ -169,6 +179,6 @@ private[test] class RunTests(config: JSEnvSuiteConfig, withCom: Boolean) {
169179
@Test(expected = classOf[IllegalArgumentException])
170180
def ensureValidate: Unit = {
171181
val cfg = RunConfig().withEternallyUnsupportedOption(true)
172-
kit.start("", cfg).close()
182+
withRun("", cfg)(identity)
173183
}
174184
}

0 commit comments

Comments
 (0)