Skip to content

Commit 830d1b8

Browse files
committed
Add test for stricter indentation
Ref lampepfl/dotty 10671
1 parent 7b26811 commit 830d1b8

File tree

5 files changed

+124
-47
lines changed

5 files changed

+124
-47
lines changed

compiler/test/dotty/tools/DottyTest.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ trait DottyTest extends ContextEscapeDetection {
3838
override def clearCtx() = {
3939
ctx = null
4040
}
41+
def resetCtx() = {
42+
clearCtx()
43+
ctx = initialCtx
44+
}
4145

4246
protected def initializeCtx(fc: FreshContext): Unit = {
4347
fc.setSetting(fc.settings.encoding, "UTF8")
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dotty.tools
2+
package dotc
3+
package parsing
4+
5+
import dotty.tools.io._
6+
import scala.io.Codec
7+
import util._
8+
import Tokens._, Scanners._
9+
import org.junit.Test
10+
11+
class DottyScannerTest extends ScannerTest {
12+
13+
val blackList = List(
14+
"/scaladoc/scala/tools/nsc/doc/html/page/Index.scala",
15+
"/scaladoc/scala/tools/nsc/doc/html/page/Template.scala"
16+
)
17+
18+
def scanDir(path: String): Unit = scanDir(Directory(path))
19+
20+
def scanDir(dir: Directory): Unit = {
21+
if (blackList exists (dir.jpath.toString endsWith _))
22+
println(s"blacklisted package: ${dir.toAbsolute.jpath}")
23+
else
24+
for (f <- dir.files)
25+
if (f.name.endsWith(".scala"))
26+
if (blackList exists (f.jpath.toString endsWith _))
27+
println(s"blacklisted file: ${f.toAbsolute.jpath}")
28+
else
29+
scan(new PlainFile(f))
30+
for (d <- dir.dirs)
31+
scanDir(d.path)
32+
}
33+
34+
@Test
35+
def scanList() = {
36+
println(System.getProperty("user.dir"))
37+
scan("compiler/src/dotty/tools/dotc/core/Symbols.scala")
38+
scan("compiler/src/dotty/tools/dotc/core/Symbols.scala")
39+
}
40+
41+
@Test
42+
def scanDotty() = {
43+
scanDir("compiler/src")
44+
}
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dotty.tools
2+
package dotc
3+
package parsing
4+
5+
import ast.untpd._
6+
import org.junit.Test
7+
8+
class IndentTest extends ScannerTest:
9+
10+
@Test
11+
def parseBraces: Unit =
12+
val code = s"""
13+
|class A {
14+
| val x = 1
15+
| val y = 2
16+
|}""".stripMargin
17+
assert(scanTextEither(code).isRight)
18+
19+
@Test
20+
def parseIndents: Unit =
21+
val code = s"""
22+
|class A:
23+
| val x = 1
24+
|""".stripMargin
25+
assert(scanTextEither(code).isRight)
26+
27+
@Test
28+
def superfluousIndents: Unit =
29+
val code = s"""
30+
|class A:
31+
| val x = 1
32+
| val y = 2
33+
|""".stripMargin
34+
assert(scanTextEither(code).isLeft)

compiler/test/dotty/tools/dotc/parsing/ParserTest.scala

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import core._
88
import scala.io.Codec
99
import Tokens._, Parsers._
1010
import ast.untpd._
11+
import reporting.Diagnostic
1112
import org.junit.Test
1213
import scala.collection.mutable.ListBuffer
1314

1415
class ParserTest extends DottyTest {
16+
import ParserTest._
1517

1618
def parse(name: String): Tree = parse(new PlainFile(File(name)))
1719

@@ -21,17 +23,25 @@ class ParserTest extends DottyTest {
2123
def reset() = {
2224
parsed = 0
2325
parsedTrees.clear()
26+
resetCtx()
2427
}
2528

26-
def parse(file: PlainFile): Tree = parseSource(new SourceFile(file, Codec.UTF8))
29+
def parse(file: PlainFile): Tree = parseSourceEither(new SourceFile(file, Codec.UTF8)).toTry.get
2730

28-
private def parseSource(source: SourceFile): Tree = {
31+
private def parseSourceEither(source: SourceFile): Either[ParserError, Tree] = {
2932
//println("***** parsing " + source.file)
3033
val parser = new Parser(source)
3134
val tree = parser.parse()
32-
parsed += 1
33-
parsedTrees += tree
34-
tree
35+
if (getCtx.reporter.hasErrors) {
36+
val result = Left(ParserError(getCtx.reporter.allErrors))
37+
reset()
38+
result
39+
}
40+
else {
41+
parsed += 1
42+
parsedTrees += tree
43+
Right(tree)
44+
}
3545
}
3646

3747
def parseDir(path: String): Unit = parseDir(Directory(path))
@@ -43,5 +53,12 @@ class ParserTest extends DottyTest {
4353
parseDir(d.path)
4454
}
4555

46-
def parseText(code: String): Tree = parseSource(SourceFile.virtual("<code>", code))
56+
def parseText(code: String): Tree = parseTextEither(code).toTry.get
57+
58+
def parseTextEither(code: String): Either[ParserError, Tree] =
59+
parseSourceEither(SourceFile.virtual("<code>", code))
60+
}
61+
62+
object ParserTest {
63+
case class ParserError(errors: List[Diagnostic.Error]) extends RuntimeException
4764
}

compiler/test/dotty/tools/dotc/parsing/ScannerTest.scala

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,32 @@ import dotty.tools.io._
66
import scala.io.Codec
77
import util._
88
import Tokens._, Scanners._
9-
import org.junit.Test
9+
import reporting.Diagnostic
1010

11-
class ScannerTest extends DottyTest {
12-
13-
val blackList = List(
14-
"/scaladoc/scala/tools/nsc/doc/html/page/Index.scala",
15-
"/scaladoc/scala/tools/nsc/doc/html/page/Template.scala"
16-
)
11+
trait ScannerTest extends DottyTest:
12+
import ParserTest._
1713

1814
def scan(name: String): Unit = scan(new PlainFile(File(name)))
1915

20-
def scan(file: PlainFile): Unit = {
16+
def scan(file: PlainFile): Unit = scanSourceEither(new SourceFile(file, Codec.UTF8)).toTry.get
17+
18+
def reset() = resetCtx()
19+
20+
private def scanSourceEither(source: SourceFile): Either[ParserError, Unit] =
2121
//println("***** scanning " + file)
22-
val source = new SourceFile(file, Codec.UTF8)
2322
val scanner = new Scanner(source)
2423
var i = 0
25-
while (scanner.token != EOF) {
24+
while scanner.token != EOF do
2625
// print("[" + scanner.token.show +"]")
2726
scanner.nextToken()
2827
// i += 1
2928
// if (i % 10 == 0) println()
30-
}
31-
}
32-
33-
def scanDir(path: String): Unit = scanDir(Directory(path))
34-
35-
def scanDir(dir: Directory): Unit = {
36-
if (blackList exists (dir.jpath.toString endsWith _))
37-
println(s"blacklisted package: ${dir.toAbsolute.jpath}")
38-
else
39-
for (f <- dir.files)
40-
if (f.name.endsWith(".scala"))
41-
if (blackList exists (f.jpath.toString endsWith _))
42-
println(s"blacklisted file: ${f.toAbsolute.jpath}")
43-
else
44-
scan(new PlainFile(f))
45-
for (d <- dir.dirs)
46-
scanDir(d.path)
47-
}
48-
49-
@Test
50-
def scanList() = {
51-
println(System.getProperty("user.dir"))
52-
scan("compiler/src/dotty/tools/dotc/core/Symbols.scala")
53-
scan("compiler/src/dotty/tools/dotc/core/Symbols.scala")
54-
}
55-
56-
@Test
57-
def scanDotty() = {
58-
scanDir("src")
59-
}
60-
}
29+
30+
if getCtx.reporter.hasErrors || getCtx.reporter.hasWarnings then
31+
val result = Left(ParserError(getCtx.reporter.allErrors))
32+
reset()
33+
result
34+
else Right(())
35+
36+
def scanTextEither(code: String): Either[ParserError, Unit] = scanSourceEither(SourceFile.virtual("<code>", code))
37+
def scanText(code: String): Unit = scanTextEither(code).toTry.get

0 commit comments

Comments
 (0)