|
| 1 | +package dotty.tools |
| 2 | +package repl |
| 3 | + |
| 4 | +import java.io.File |
| 5 | +import java.nio.file.Files |
| 6 | + |
| 7 | +import org.junit.{ After, AfterClass, BeforeClass, Ignore, Test } |
| 8 | +import org.junit.Assert._ |
| 9 | +import io.{ Directory, PlainDirectory } |
| 10 | +import dotc.core.Contexts._ |
| 11 | +import dotc.reporting.{ ErrorMessagesTest, StoreReporter } |
| 12 | +import vulpix.TestConfiguration |
| 13 | + |
| 14 | +object ShadowingBatchTests: |
| 15 | + val dir = Directory(Files.createTempDirectory("batch-shadow")) |
| 16 | + |
| 17 | + @BeforeClass def suiteStarting: Unit = dir.createDirectory() |
| 18 | + @AfterClass def suiteFinished: Unit = dir.deleteRecursively() |
| 19 | + |
| 20 | +class ShadowingBatchTests extends ErrorMessagesTest: |
| 21 | + import ShadowingBatchTests._ |
| 22 | + |
| 23 | + @After def testFinished: Unit = dir.list.foreach(_.deleteRecursively()) |
| 24 | + |
| 25 | + val compiler = new dotc.Compiler() |
| 26 | + |
| 27 | + override def initializeCtx(ictx: FreshContext) = inContext(ictx) { |
| 28 | + super.initializeCtx(ictx) |
| 29 | + val settings = ictx.settings; import settings._ |
| 30 | + ictx.setSetting(outputDir, new PlainDirectory(dir)) |
| 31 | + ictx.setSetting(classpath, classpath.value + File.pathSeparator + dir.jpath.toAbsolutePath) |
| 32 | + } |
| 33 | + |
| 34 | + @Test def file = |
| 35 | + checkMessages("class C(val c: Int)").expectNoErrors |
| 36 | + checkMessages("object rsline1 {\n def line1 = new C().c\n}").expect { (_, msgs) => |
| 37 | + assertMessageCount(1, msgs) |
| 38 | + assertEquals("missing argument for parameter c of constructor C in class C: (c: Int): C", msgs.head.message) |
| 39 | + } |
| 40 | + checkMessages("object rsline2 {\n def line2 = new C(13).c\n}").expectNoErrors |
| 41 | + checkMessages("object rsline3 {\n class C { val c = 42 }\n}").expectNoErrors |
| 42 | + checkMessages("import rsline3._\nobject rsline4 {\n def line4 = new C().c\n}").expectNoErrors |
| 43 | + |
| 44 | + @Test def directory = |
| 45 | + checkMessages("package foo\nclass C").expectNoErrors |
| 46 | + checkMessages("object rsline1 {\n def line1 = foo\n}").expect { (_, msgs) => |
| 47 | + assertMessageCount(1, msgs) |
| 48 | + assertEquals("package foo is not a value", msgs.head.message) |
| 49 | + } |
| 50 | + checkMessages("object rsline2 {\n val foo = 2\n}").expectNoErrors |
| 51 | + checkMessages("import rsline2._\nobject rsline3 {\n def line3 = foo\n}").expectNoErrors |
| 52 | + |
| 53 | + @Test def directoryJava = |
| 54 | + checkMessages("object rsline1 {\n def line1 = java\n}").expect { (_, msgs) => |
| 55 | + assertMessageCount(1, msgs) |
| 56 | + assertEquals("package java is not a value", msgs.head.message) |
| 57 | + } |
| 58 | + checkMessages("object rsline2 {\n val java = 2\n}").expectNoErrors |
| 59 | + checkMessages("import rsline2._\nobject rsline3 {\n def line3 = java\n}").expectNoErrors |
| 60 | + |
| 61 | + def checkMessages(source: String): Report = |
| 62 | + ctx = newContext |
| 63 | + val run = compiler.newRun(using ctx.fresh) |
| 64 | + run.compileFromStrings(List(source)) |
| 65 | + val runCtx = run.runContext |
| 66 | + if runCtx.reporter.hasErrors then |
| 67 | + val rep = runCtx.reporter.asInstanceOf[StoreReporter] |
| 68 | + val msgs = rep.removeBufferedMessages(using runCtx).map(_.msg).reverse |
| 69 | + new Report(msgs, runCtx) |
| 70 | + else new EmptyReport |
| 71 | +end ShadowingBatchTests |
0 commit comments