From 6d8e6271e2d1f324d5a838d6f1b100c21d60ebd7 Mon Sep 17 00:00:00 2001 From: benkobalog Date: Mon, 12 Feb 2018 16:26:39 +0100 Subject: [PATCH 1/4] Fix #3814 Correct highlighting issues in REPL Correct highlighting after "|", ":", "&" characters and after "case", "val", "val" when using extractors. --- .../dotc/printing/SyntaxHighlighting.scala | 23 +++++++++--- compiler/test-resources/printing/defs | 31 ++++++++++++++++ .../test-resources/printing/patternMatching | 29 +++++++++++++++ .../printing/unionAndIntersectionTypes | 15 ++++++++ .../printing/SyntaxHighlightingTests.scala | 36 +++++++++++++++++++ 5 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 compiler/test-resources/printing/defs create mode 100644 compiler/test-resources/printing/patternMatching create mode 100644 compiler/test-resources/printing/unionAndIntersectionTypes create mode 100644 compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index a4f3d0a522ae..8da388322870 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -42,8 +42,8 @@ object SyntaxHighlighting { 'q' :: 'r' :: 's' :: 't' :: 'u' :: 'v' :: 'w' :: 'x' :: 'y' :: 'z' :: Nil private val typeEnders = - '{' :: '}' :: ')' :: '(' :: '[' :: ']' :: '=' :: ' ' :: ',' :: '.' :: - '\n' :: Nil + '{' :: '}' :: ')' :: '(' :: '[' :: ']' :: '=' :: ' ' :: ',' :: '.' :: '|' :: + '&' :: '\n' :: Nil def apply(chars: Iterable[Char]): Iterable[Char] = { var prev: Char = 0 @@ -53,7 +53,8 @@ object SyntaxHighlighting { @inline def keywordStart = prev == 0 || prev == ' ' || prev == '{' || prev == '(' || - prev == '\n' || prev == '[' || prev == ',' + prev == '\n' || prev == '[' || prev == ',' || prev == ':' || + prev == '|' || prev == '&' @inline def numberStart(c: Char) = c.isDigit && (!prev.isLetter || prev == '.' || prev == ' ' || prev == '(' || prev == '\u0000') @@ -286,9 +287,20 @@ object SyntaxHighlighting { case '@' => true case ',' => true case '.' => true + case ',' => true + case ')' => true case _ => false } + def shouldUpdateLastToken(currentPotentialToken: String): Boolean = + (lastToken, currentPotentialToken) match { + case (_, ("var" | "val" | "def" | "case")) => true + case (("val" | "var"), "=") => true + case ("case", ("=>" | "class" | "object")) => true + case ("def", _) => true + case _ => false + } + while (remaining.nonEmpty && !delim(curr)) { curr = takeChar() if (!delim(curr)) sb += curr @@ -298,12 +310,13 @@ object SyntaxHighlighting { val toAdd = if (shouldHL(str)) highlight(str) - else if (("var" :: "val" :: "def" :: "case" :: Nil).contains(lastToken)) + else if (("var" :: "val" :: "def" :: "case" :: Nil).contains(lastToken) && + !List("=", "=>").contains(str)) valDef(str) else str val suffix = if (delim(curr)) s"$curr" else "" newBuf append (toAdd + suffix) - lastToken = str + if (shouldUpdateLastToken(str)) lastToken = str prev = curr } diff --git a/compiler/test-resources/printing/defs b/compiler/test-resources/printing/defs new file mode 100644 index 000000000000..83d5cba39bc1 --- /dev/null +++ b/compiler/test-resources/printing/defs @@ -0,0 +1,31 @@ +package test + +object A { + val a = 123 + var b = 123 /*Int*/ + var c = "123" // String + var d: Int = 123 + var e:Int = 123;e + e + print(a) + 123;123 + def f = 123 + def f1(x: Int) = 123 + def f2[T](x: T) = { 123 } +} +result: +package test + +object A { + val a = 123 + var b = 123 /*Int*/ + var c = "123" // String + var d: Int = 123 + var e:Int = 123;e + e + print(a) + 123;123 + def f = 123 + def f1(x: Int) = 123 + def f2[T](x: T) = { 123 } +} \ No newline at end of file diff --git a/compiler/test-resources/printing/patternMatching b/compiler/test-resources/printing/patternMatching new file mode 100644 index 000000000000..b7b31abed6aa --- /dev/null +++ b/compiler/test-resources/printing/patternMatching @@ -0,0 +1,29 @@ +val aFruit: Fruit = Apple("red", 123) + +val Apple(color, weight) = aFruit + +sealed trait Fruit +case class Apple(color: String, weight: Double) extends Fruit +case class Orange(weight: Double) extends Fruit +case class Melon(weight: Double) extends Fruit + +aFruit match { + case Apple(_, weight) => println(s"apple: $weight kgs") + case o: Orange => println(s"orange ${o.weight} kgs") + case m @ Melon(weight) => println(s"melon: ${m.weight} kgs") +} +result: +val aFruit: Fruit = Apple("red", 123) + +val Apple(color, weight) = aFruit + +sealed trait Fruit +case class Apple(color: String, weight: Double) extends Fruit +case class Orange(weight: Double) extends Fruit +case class Melon(weight: Double) extends Fruit + +aFruit match { + case Apple(_, weight) => println(s"apple: $weight kgs") + case o: Orange => println(s"orange ${o.weight} kgs") + case m @ Melon(weight) => println(s"melon: ${m.weight} kgs") +} \ No newline at end of file diff --git a/compiler/test-resources/printing/unionAndIntersectionTypes b/compiler/test-resources/printing/unionAndIntersectionTypes new file mode 100644 index 000000000000..544f793f472e --- /dev/null +++ b/compiler/test-resources/printing/unionAndIntersectionTypes @@ -0,0 +1,15 @@ +type A = String|Int| Long +type B = String |Int| Long +type C = String | Int | Long +type D = String&Int& Long +type E = String &Int& Long +type F = String & Int & Long +fn[String|Char](input) +result: +type A = String|Int| Long +type B = String |Int| Long +type C = String | Int | Long +type D = String&Int& Long +type E = String &Int& Long +type F = String & Int & Long +fn[String|Char](input) \ No newline at end of file diff --git a/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala new file mode 100644 index 000000000000..69ccc5039de0 --- /dev/null +++ b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala @@ -0,0 +1,36 @@ +package dotty.tools.dotc.printing + +import dotty.tools.io.JFile +import org.junit.Test +import org.junit.Assert.fail + +import scala.io.Source + +/** Runs all tests contained in `compiler/test-resources/printing/` + * To check test cases, you can use "cat" or "less -r" from bash*/ +class SyntaxHighlightingTests { + + private def scripts(path: String): Array[JFile] = { + val dir = new JFile(getClass.getResource(path).getPath) + assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir") + dir.listFiles + } + + private def testFile(f: JFile): Unit = { + val linesIt = Source.fromFile(f).getLines() + val input = linesIt.takeWhile(_ != "result:").mkString("\n") + val expectedOutput = linesIt.mkString("\n") + val actualOutput = SyntaxHighlighting(input).mkString + + if (expectedOutput != actualOutput) { + println("Expected output:") + println(expectedOutput) + println("Actual output:") + println(actualOutput) + + fail(s"Error in file $f, expected output did not match actual") + } + } + + @Test def syntaxHighlight = scripts("/printing").foreach(testFile) +} From edb401e5479303f7999a47ab39a38627ae099478 Mon Sep 17 00:00:00 2001 From: benkobalog Date: Thu, 15 Feb 2018 13:34:39 +0100 Subject: [PATCH 2/4] Added generator method for test files --- .../printing/SyntaxHighlightingTests.scala | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala index 69ccc5039de0..1db2f20b186d 100644 --- a/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala +++ b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala @@ -1,13 +1,16 @@ package dotty.tools.dotc.printing +import java.io.PrintWriter + import dotty.tools.io.JFile -import org.junit.Test import org.junit.Assert.fail +import org.junit.Test import scala.io.Source /** Runs all tests contained in `compiler/test-resources/printing/` - * To check test cases, you can use "cat" or "less -r" from bash*/ + * To check test cases, you can use "cat" or "less -r" from bash + * To generate test files you can call the generateTestFile method*/ class SyntaxHighlightingTests { private def scripts(path: String): Array[JFile] = { @@ -17,9 +20,9 @@ class SyntaxHighlightingTests { } private def testFile(f: JFile): Unit = { - val linesIt = Source.fromFile(f).getLines() - val input = linesIt.takeWhile(_ != "result:").mkString("\n") - val expectedOutput = linesIt.mkString("\n") + val lines = Source.fromFile(f).getLines() + val input = lines.takeWhile(_ != "result:").mkString("\n") + val expectedOutput = lines.mkString("\n") val actualOutput = SyntaxHighlighting(input).mkString if (expectedOutput != actualOutput) { @@ -28,8 +31,22 @@ class SyntaxHighlightingTests { println("Actual output:") println(actualOutput) + // Call generateTestFile when you want to update a test file + // or generate a test from a scala source file + // if (f.getName == "nameOfFileToConvertToATest") generateTestFile() + fail(s"Error in file $f, expected output did not match actual") } + + /** Writes `input` and `actualOutput` to the current test file*/ + def generateTestFile(): Unit = { + val path = "compiler/test-resources/printing/" + f.getName + new PrintWriter(path) { + write(input + "\nresult:\n" + actualOutput) + close() + } + println(s"Test file for ${f.getName} has been generated") + } } @Test def syntaxHighlight = scripts("/printing").foreach(testFile) From 578af35ba37845948b5055eedc0f99c4d2e5bd1b Mon Sep 17 00:00:00 2001 From: benkobalog Date: Thu, 3 May 2018 16:19:29 +0200 Subject: [PATCH 3/4] Refactor SyntaxHighlight.scala Added comments to updateLastToken function Use syntax highlight tests proposed in pr #4441 instead of the file based solution --- .../dotc/printing/SyntaxHighlighting.scala | 29 +-- compiler/test-resources/printing/defs | 31 --- .../test-resources/printing/patternMatching | 29 --- .../printing/unionAndIntersectionTypes | 15 -- .../printing/SyntaxHighlightingTests.scala | 190 ++++++++++++++---- .../reporting/SyntaxHighlightingTests.scala | 62 ------ 6 files changed, 169 insertions(+), 187 deletions(-) delete mode 100644 compiler/test-resources/printing/defs delete mode 100644 compiler/test-resources/printing/patternMatching delete mode 100644 compiler/test-resources/printing/unionAndIntersectionTypes delete mode 100644 compiler/test/dotty/tools/dotc/reporting/SyntaxHighlightingTests.scala diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index 8da388322870..45952f3bbe1e 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -287,18 +287,24 @@ object SyntaxHighlighting { case '@' => true case ',' => true case '.' => true - case ',' => true - case ')' => true case _ => false } - def shouldUpdateLastToken(currentPotentialToken: String): Boolean = - (lastToken, currentPotentialToken) match { - case (_, ("var" | "val" | "def" | "case")) => true - case (("val" | "var"), "=") => true - case ("case", ("=>" | "class" | "object")) => true - case ("def", _) => true - case _ => false + val valDefStarterTokens = "var" :: "val" :: "def" :: "case" :: Nil + + /** lastToken is used to check whether we want to show something + * in valDef color or not. There are only a few cases when lastToken + * should be updated, that way we can avoid stopping coloring too early. + * eg.: case A(x, y, z) => ??? + * Without this function only x would be colored. + */ + def updateLastToken(currentToken: String): String = + (lastToken, currentToken) match { + case _ if valDefStarterTokens.contains(currentToken) => currentToken + case (("val" | "var"), "=") => currentToken + case ("case", ("=>" | "class" | "object")) => currentToken + case ("def", _) => currentToken + case _ => lastToken } while (remaining.nonEmpty && !delim(curr)) { @@ -310,13 +316,12 @@ object SyntaxHighlighting { val toAdd = if (shouldHL(str)) highlight(str) - else if (("var" :: "val" :: "def" :: "case" :: Nil).contains(lastToken) && - !List("=", "=>").contains(str)) + else if (valDefStarterTokens.contains(lastToken) && !List("=", "=>").contains(str)) valDef(str) else str val suffix = if (delim(curr)) s"$curr" else "" newBuf append (toAdd + suffix) - if (shouldUpdateLastToken(str)) lastToken = str + lastToken = updateLastToken(str) prev = curr } diff --git a/compiler/test-resources/printing/defs b/compiler/test-resources/printing/defs deleted file mode 100644 index 83d5cba39bc1..000000000000 --- a/compiler/test-resources/printing/defs +++ /dev/null @@ -1,31 +0,0 @@ -package test - -object A { - val a = 123 - var b = 123 /*Int*/ - var c = "123" // String - var d: Int = 123 - var e:Int = 123;e - e - print(a) - 123;123 - def f = 123 - def f1(x: Int) = 123 - def f2[T](x: T) = { 123 } -} -result: -package test - -object A { - val a = 123 - var b = 123 /*Int*/ - var c = "123" // String - var d: Int = 123 - var e:Int = 123;e - e - print(a) - 123;123 - def f = 123 - def f1(x: Int) = 123 - def f2[T](x: T) = { 123 } -} \ No newline at end of file diff --git a/compiler/test-resources/printing/patternMatching b/compiler/test-resources/printing/patternMatching deleted file mode 100644 index b7b31abed6aa..000000000000 --- a/compiler/test-resources/printing/patternMatching +++ /dev/null @@ -1,29 +0,0 @@ -val aFruit: Fruit = Apple("red", 123) - -val Apple(color, weight) = aFruit - -sealed trait Fruit -case class Apple(color: String, weight: Double) extends Fruit -case class Orange(weight: Double) extends Fruit -case class Melon(weight: Double) extends Fruit - -aFruit match { - case Apple(_, weight) => println(s"apple: $weight kgs") - case o: Orange => println(s"orange ${o.weight} kgs") - case m @ Melon(weight) => println(s"melon: ${m.weight} kgs") -} -result: -val aFruit: Fruit = Apple("red", 123) - -val Apple(color, weight) = aFruit - -sealed trait Fruit -case class Apple(color: String, weight: Double) extends Fruit -case class Orange(weight: Double) extends Fruit -case class Melon(weight: Double) extends Fruit - -aFruit match { - case Apple(_, weight) => println(s"apple: $weight kgs") - case o: Orange => println(s"orange ${o.weight} kgs") - case m @ Melon(weight) => println(s"melon: ${m.weight} kgs") -} \ No newline at end of file diff --git a/compiler/test-resources/printing/unionAndIntersectionTypes b/compiler/test-resources/printing/unionAndIntersectionTypes deleted file mode 100644 index 544f793f472e..000000000000 --- a/compiler/test-resources/printing/unionAndIntersectionTypes +++ /dev/null @@ -1,15 +0,0 @@ -type A = String|Int| Long -type B = String |Int| Long -type C = String | Int | Long -type D = String&Int& Long -type E = String &Int& Long -type F = String & Int & Long -fn[String|Char](input) -result: -type A = String|Int| Long -type B = String |Int| Long -type C = String | Int | Long -type D = String&Int& Long -type E = String &Int& Long -type F = String & Int & Long -fn[String|Char](input) \ No newline at end of file diff --git a/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala index 1db2f20b186d..76c9194f9d71 100644 --- a/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala +++ b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala @@ -1,53 +1,167 @@ package dotty.tools.dotc.printing -import java.io.PrintWriter - -import dotty.tools.io.JFile -import org.junit.Assert.fail +import org.junit.Assert._ import org.junit.Test -import scala.io.Source - -/** Runs all tests contained in `compiler/test-resources/printing/` - * To check test cases, you can use "cat" or "less -r" from bash - * To generate test files you can call the generateTestFile method*/ +/** 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, "") + test("/** a */", "") + test("/* a */", "") + } - if (expectedOutput != actualOutput) { - println("Expected output:") - println(expectedOutput) - println("Actual output:") - println(actualOutput) + @Test + def types = { + test("type Foo = Int", " = ") + } - // Call generateTestFile when you want to update a test file - // or generate a test from a scala source file - // if (f.getName == "nameOfFileToConvertToATest") generateTestFile() + @Test + def literals = { + test("1", "") + // test("1L", "") + } - fail(s"Error in file $f, expected output did not match actual") - } + @Test + def strings = { + // For some reason we currently use literal color for string + test("\"Hello\"", "") + } - /** Writes `input` and `actualOutput` to the current test file*/ - def generateTestFile(): Unit = { - val path = "compiler/test-resources/printing/" + f.getName - new PrintWriter(path) { - write(input + "\nresult:\n" + actualOutput) - close() - } - println(s"Test file for ${f.getName} has been generated") - } + @Test + def annotations = { + test("@tailrec", "") + } + + @Test + def expressions = { + test("val x = 1 + 2 + 3", " = + + ") + } + + @Test + def valVarDef = { + val source = + """ + |package test + | + |object A { + | val a = 123 + | var b = 123 /*Int*/ + | var c = "123" // String + | var d: Int = 123 + | var e:Int = 123;e + | e + | print(a) + | 123;123 + | def f = 123 + | def f1(x: Int) = 123 + | def f2[T](x: T) = { 123 } + |} + """.stripMargin + val expected = + """ + | test + | + | { + | = + | = + | = : = + | : = ;e + | e + | print(a) + | ; + | = + | (x: ) = + | [](x: ) = { } + |} + """.stripMargin + test(source, expected) + } + + @Test + def patternMatching = { + val source = + """ + |val aFruit: Fruit = Apple("red", 123) + | + |val Apple(color, weight) = aFruit + | + |sealed trait Fruit + |case class Apple(color: String, weight: Double) extends Fruit + |case class Orange(weight: Double) extends Fruit + |case class Melon(weight: Double) extends Fruit + | + |aFruit match { + | case Apple(_, weight) => println(s"apple: $weight kgs") + | case o: Orange => println(s"orange ${o.weight} kgs") + | case m @ Melon(weight) => println(s"melon: ${m.weight} kgs") + |} + """.stripMargin + val expected = + """ + | : = (, ) + | + | (, ) = aFruit + | + | + | (color: , weight: ) + | (weight: ) + | (weight: ) + | + |aFruit { + | (, ) > println(s) + | : > println(s) + | @ () > println(s) + |} + """.stripMargin + test(source, expected) } - @Test def syntaxHighlight = scripts("/printing").foreach(testFile) + @Test + def unionTypes = { + val source = + """ + |type A = String|Int| Long + |type B = String |Int| Long + |type C = String | Int | Long + |type D = String&Int& Long + |type E = String &Int& Long + |type F = String & Int & Long + |fn[String|Char](input) + """.stripMargin + val expected = + """ + | = || + | = || + | = | | + | = && + | = && + | = & & + |fn[|](input) + """.stripMargin + test(source, expected) + } } diff --git a/compiler/test/dotty/tools/dotc/reporting/SyntaxHighlightingTests.scala b/compiler/test/dotty/tools/dotc/reporting/SyntaxHighlightingTests.scala deleted file mode 100644 index 35f81e43263e..000000000000 --- a/compiler/test/dotty/tools/dotc/reporting/SyntaxHighlightingTests.scala +++ /dev/null @@ -1,62 +0,0 @@ -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, "") - test("/** a */", "") - test("/* a */", "") - } - - @Test - def types = { - test("type Foo = Int", " = ") - } - - @Test - def literals = { - test("1", "") - // test("1L", "") - } - - @Test - def strings = { - // For some reason we currently use literal color for string - test("\"Hello\"", "") - } - - @Test - def annotations = { - test("@tailrec", "") - } - - @Test - def expressions = { - test("val x = 1 + 2 + 3", " = + + ") - } -} From 05c0901086222141aef6dc56e358563656660dd1 Mon Sep 17 00:00:00 2001 From: benkbalog Date: Thu, 3 May 2018 19:25:30 +0200 Subject: [PATCH 4/4] Removed useless code from tests, renamed lastToken --- .../dotc/printing/SyntaxHighlighting.scala | 14 +-- .../printing/SyntaxHighlightingTests.scala | 119 ++++-------------- 2 files changed, 32 insertions(+), 101 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index 45952f3bbe1e..08a34a2e9fbc 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -49,7 +49,7 @@ object SyntaxHighlighting { var prev: Char = 0 var remaining = chars.toStream val newBuf = new StringBuilder - var lastToken = "" + var lastValDefToken = "" @inline def keywordStart = prev == 0 || prev == ' ' || prev == '{' || prev == '(' || @@ -292,19 +292,19 @@ object SyntaxHighlighting { val valDefStarterTokens = "var" :: "val" :: "def" :: "case" :: Nil - /** lastToken is used to check whether we want to show something - * in valDef color or not. There are only a few cases when lastToken + /** lastValDefToken is used to check whether we want to show something + * in valDef color or not. There are only a few cases when lastValDefToken * should be updated, that way we can avoid stopping coloring too early. * eg.: case A(x, y, z) => ??? * Without this function only x would be colored. */ def updateLastToken(currentToken: String): String = - (lastToken, currentToken) match { + (lastValDefToken, currentToken) match { case _ if valDefStarterTokens.contains(currentToken) => currentToken case (("val" | "var"), "=") => currentToken case ("case", ("=>" | "class" | "object")) => currentToken case ("def", _) => currentToken - case _ => lastToken + case _ => lastValDefToken } while (remaining.nonEmpty && !delim(curr)) { @@ -316,12 +316,12 @@ object SyntaxHighlighting { val toAdd = if (shouldHL(str)) highlight(str) - else if (valDefStarterTokens.contains(lastToken) && !List("=", "=>").contains(str)) + else if (valDefStarterTokens.contains(lastValDefToken) && !List("=", "=>").contains(str)) valDef(str) else str val suffix = if (delim(curr)) s"$curr" else "" newBuf append (toAdd + suffix) - lastToken = updateLastToken(str) + lastValDefToken = updateLastToken(str) prev = curr } diff --git a/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala index 76c9194f9d71..7166d3cb0db1 100644 --- a/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala +++ b/compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala @@ -61,107 +61,38 @@ class SyntaxHighlightingTests { } @Test - def valVarDef = { - val source = - """ - |package test - | - |object A { - | val a = 123 - | var b = 123 /*Int*/ - | var c = "123" // String - | var d: Int = 123 - | var e:Int = 123;e - | e - | print(a) - | 123;123 - | def f = 123 - | def f1(x: Int) = 123 - | def f2[T](x: T) = { 123 } - |} - """.stripMargin - val expected = - """ - | test - | - | { - | = - | = - | = : = - | : = ;e - | e - | print(a) - | ; - | = - | (x: ) = - | [](x: ) = { } - |} - """.stripMargin - test(source, expected) + def valDef = { + test("val a = 123", " = ") + test("var b = 123 /*Int*/", " = ") + test("""var c = "123" // String""", """ = """) + test("var e:Int = 123;e", " : = ;e") + test("def f = 123", " = ") + test("def f1(x: Int) = 123", " (x: ) = ") + test("def f2[T](x: T) = { 123 }", " [](x: ) = { }") } @Test def patternMatching = { - val source = - """ - |val aFruit: Fruit = Apple("red", 123) - | - |val Apple(color, weight) = aFruit - | - |sealed trait Fruit - |case class Apple(color: String, weight: Double) extends Fruit - |case class Orange(weight: Double) extends Fruit - |case class Melon(weight: Double) extends Fruit - | - |aFruit match { - | case Apple(_, weight) => println(s"apple: $weight kgs") - | case o: Orange => println(s"orange ${o.weight} kgs") - | case m @ Melon(weight) => println(s"melon: ${m.weight} kgs") - |} - """.stripMargin - val expected = - """ - | : = (, ) - | - | (, ) = aFruit - | - | - | (color: , weight: ) - | (weight: ) - | (weight: ) - | - |aFruit { - | (, ) > println(s) - | : > println(s) - | @ () > println(s) - |} - """.stripMargin - test(source, expected) + test("""val aFruit: Fruit = Apple("red", 123)""", + """ : = (, )""") + test("""val Apple(color, weight) = aFruit""", + """ (, ) = aFruit""") + test("""case Apple(_, weight) => println(s"apple: $weight kgs")""", + """ (, ) > println(s)""") + test("""case o: Orange => println(s"orange ${o.weight} kgs")""", + """ : > println(s)""") + test("""case m @ Melon(weight) => println(s"melon: ${m.weight} kgs")""", + """ @ () > println(s)""") } @Test def unionTypes = { - val source = - """ - |type A = String|Int| Long - |type B = String |Int| Long - |type C = String | Int | Long - |type D = String&Int& Long - |type E = String &Int& Long - |type F = String & Int & Long - |fn[String|Char](input) - """.stripMargin - val expected = - """ - | = || - | = || - | = | | - | = && - | = && - | = & & - |fn[|](input) - """.stripMargin - test(source, expected) + test("type A = String|Int| Long", " = || ") + test("type B = String |Int| Long", " = || ") + test("type C = String | Int | Long", " = | | ") + test("type D = String&Int& Long", " = && ") + test("type E = String &Int& Long", " = && ") + test("type F = String & Int & Long", " = & & ") + test("fn[String|Char](input)", "fn[|](input)") } }