Skip to content

Commit 982deae

Browse files
committed
Add test for #620
The issue in #6642 shows that errors may be latent in seemingly simple fixes if not tested, which can backlash us at critical moment.
1 parent ed8c6ee commit 982deae

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package dotty
2+
package tools
3+
package dotc
4+
5+
import vulpix.TestConfiguration
6+
import vulpix.TestConfiguration
7+
import reporting.TestReporter
8+
9+
import java.io._
10+
import java.nio.file.{Path => JPath}
11+
import java.lang.System.{lineSeparator => EOL}
12+
13+
import interfaces.Diagnostic.INFO
14+
import dotty.tools.io.Directory
15+
16+
import scala.io.Source
17+
import org.junit.Test
18+
19+
class PrintingTest {
20+
val testsDir = "tests/printing"
21+
val options = List("-Xprint:frontend", "-color:never", "-classpath", TestConfiguration.basicClasspath)
22+
23+
private def fileContent(filePath: String): List[String] =
24+
if (new File(filePath).exists)
25+
Source.fromFile(filePath, "UTF-8").getLines().toList
26+
else Nil
27+
28+
29+
private def compileFile(path: JPath): Boolean = {
30+
val baseFilePath = path.toAbsolutePath.toString.stripSuffix(".scala")
31+
val outFilePath = baseFilePath + ".out"
32+
val checkFilePath = baseFilePath + ".check"
33+
val ps = new PrintStream(new File(outFilePath))
34+
val reporter = TestReporter.reporter(ps, INFO)
35+
36+
try {
37+
Main.process((path.toString::options).toArray, reporter, null)
38+
} catch {
39+
case e: Throwable =>
40+
println(s"Compile $path exception:")
41+
e.printStackTrace()
42+
}
43+
finally ps.close()
44+
45+
val actual = fileContent(outFilePath)
46+
val expected = fileContent(checkFilePath)
47+
48+
val success =
49+
actual.length == expected.length &&
50+
(actual, expected).zipped.forall(_ == _)
51+
52+
success || {
53+
println(
54+
s"""|Output from '$outFilePath' did not match check file. Actual output:
55+
|${actual.mkString(EOL)}
56+
|""".stripMargin + "\n")
57+
58+
println(
59+
s"""Test output dumped in: ${outFilePath}
60+
| See diff of the checkfile
61+
| > diff $checkFilePath $outFilePath
62+
| Replace checkfile with current output output
63+
| > mv $outFilePath $checkFilePath
64+
""".stripMargin)
65+
66+
false
67+
}
68+
}
69+
70+
@Test
71+
def printing: Unit = {
72+
val res = Directory(testsDir).list.toList
73+
.filter(f => f.extension == "scala")
74+
.map { f => compileFile(f.jpath) }
75+
76+
val failed = res.filter(!_)
77+
78+
val msg = s"Pass: ${res.length - failed.length}, Failed: ${failed.length}"
79+
80+
assert(failed.length == 0, msg)
81+
82+
println(msg)
83+
}
84+
}

tests/printing/i620.check

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package O {
2+
package O.A {
3+
class D() extends Object() {
4+
class C() extends Object() {
5+
protected[D] def a: Int = 0
6+
private[D] def b: Int = 0
7+
private def c: Int = 0
8+
protected def d: Int = 0
9+
private[A] def e: Int = 0
10+
protected[A] def f: Int = 0
11+
def g: Int = 0
12+
}
13+
private[D] class E() extends Object() {}
14+
private class F() extends Object() {}
15+
private[A] class G() extends Object() {}
16+
protected[D] class H() extends Object() {}
17+
protected class I() extends Object() {}
18+
protected[A] class J() extends Object() {}
19+
class K() extends Object() {}
20+
protected[D] val a: Int = 0
21+
private[D] val b: Int = 0
22+
private val c: Int = 0
23+
protected val d: Int = 0
24+
private[A] val e: Int = 0
25+
protected[A] val f: Int = 0
26+
val g: Int = 0
27+
}
28+
}
29+
}

tests/printing/i620.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// test printing of private[D] and protected[D]
2+
3+
package O
4+
package A
5+
6+
class D {
7+
class C {
8+
protected[D] def a: Int = 0
9+
private[D] def b: Int = 0
10+
private def c: Int = 0
11+
protected def d: Int = 0
12+
private[A] def e: Int = 0
13+
protected[A] def f: Int = 0
14+
def g: Int = 0
15+
}
16+
17+
private[D] class E
18+
private class F
19+
private[A] class G
20+
protected[D] class H
21+
protected class I
22+
protected[A] class J
23+
class K
24+
25+
protected[D] val a: Int = 0
26+
private[D] val b: Int = 0
27+
private val c: Int = 0
28+
protected val d: Int = 0
29+
private[A] val e: Int = 0
30+
protected[A] val f: Int = 0
31+
val g: Int = 0
32+
}

0 commit comments

Comments
 (0)