Skip to content

Create simple syntax highlighting tests #4441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import util.Chars
/** This object provides functions for syntax highlighting in the REPL */
object SyntaxHighlighting {

// Keep in sync with SyntaxHighlightingTests
val NoColor = Console.RESET
val CommentColor = Console.BLUE
val KeywordColor = Console.YELLOW
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dotty.tools.dotc.printing

import org.junit.Assert._
import org.junit.Test

/** Adapted from Ammonite HighlightTests
*/
class SyntaxHighlightingTests {
import SyntaxHighlighting._

private def test(source: String, expected: String): Unit = {
val highlighted = SyntaxHighlighting.apply(source)
.mkString
.replace(NoColor, ">")
.replace(CommentColor, "<C|")
.replace(KeywordColor, "<K|")
.replace(ValDefColor, "<V|")
.replace(LiteralColor, "<L|")
.replace(StringColor, "<S|")
.replace(TypeColor, "<T|")
// .replace(AnnotationColor, "<A|") // is the same color as type color

if (expected != highlighted) {
// assertEquals produces weird expected/found message
fail(s"expected: $expected but was: $highlighted")
}
}

@Test
def comments = {
test("//a", "<C|//a>")
test("/** a */", "<C|/** a */>")
test("/* a */", "<C|/* a */>")
}

@Test
def types = {
test("type Foo = Int", "<K|type> <T|Foo> = <T|Int>")
}

@Test
def literals = {
test("1", "<L|1>")
// test("1L", "<L|1L>")
}

@Test
def strings = {
// For some reason we currently use literal color for string
test("\"Hello\"", "<L|\"Hello\">")
}

@Test
def annotations = {
test("@tailrec", "<T|@tailrec>")
}

@Test
def expressions = {
test("val x = 1 + 2 + 3", "<K|val> <V|x> = <L|1> + <L|2> + <L|3>")
}
}