Skip to content

Commit 4ca71e0

Browse files
authored
Merge pull request #4441 from dotty-staging/syntax-hl-tests
Create simple syntax highlighting tests
2 parents a3c4ec7 + afe6667 commit 4ca71e0

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import util.Chars
1010
/** This object provides functions for syntax highlighting in the REPL */
1111
object SyntaxHighlighting {
1212

13+
// Keep in sync with SyntaxHighlightingTests
1314
val NoColor = Console.RESET
1415
val CommentColor = Console.BLUE
1516
val KeywordColor = Console.YELLOW
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package dotty.tools.dotc.printing
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
6+
/** Adapted from Ammonite HighlightTests
7+
*/
8+
class SyntaxHighlightingTests {
9+
import SyntaxHighlighting._
10+
11+
private def test(source: String, expected: String): Unit = {
12+
val highlighted = SyntaxHighlighting.apply(source)
13+
.mkString
14+
.replace(NoColor, ">")
15+
.replace(CommentColor, "<C|")
16+
.replace(KeywordColor, "<K|")
17+
.replace(ValDefColor, "<V|")
18+
.replace(LiteralColor, "<L|")
19+
.replace(StringColor, "<S|")
20+
.replace(TypeColor, "<T|")
21+
// .replace(AnnotationColor, "<A|") // is the same color as type color
22+
23+
if (expected != highlighted) {
24+
// assertEquals produces weird expected/found message
25+
fail(s"expected: $expected but was: $highlighted")
26+
}
27+
}
28+
29+
@Test
30+
def comments = {
31+
test("//a", "<C|//a>")
32+
test("/** a */", "<C|/** a */>")
33+
test("/* a */", "<C|/* a */>")
34+
}
35+
36+
@Test
37+
def types = {
38+
test("type Foo = Int", "<K|type> <T|Foo> = <T|Int>")
39+
}
40+
41+
@Test
42+
def literals = {
43+
test("1", "<L|1>")
44+
// test("1L", "<L|1L>")
45+
}
46+
47+
@Test
48+
def strings = {
49+
// For some reason we currently use literal color for string
50+
test("\"Hello\"", "<L|\"Hello\">")
51+
}
52+
53+
@Test
54+
def annotations = {
55+
test("@tailrec", "<T|@tailrec>")
56+
}
57+
58+
@Test
59+
def expressions = {
60+
test("val x = 1 + 2 + 3", "<K|val> <V|x> = <L|1> + <L|2> + <L|3>")
61+
}
62+
}

0 commit comments

Comments
 (0)