|
| 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 | + |
| 63 | + @Test |
| 64 | + def valDef = { |
| 65 | + test("val a = 123", "<K|val> <V|a> = <L|123>") |
| 66 | + test("var b = 123 /*Int*/", "<K|var> <V|b> = <L|123> <C|/*Int*/>") |
| 67 | + test("""var c = "123" // String""", """<K|var> <V|c> = <L|"123"> <C|// String>""") |
| 68 | + test("var e:Int = 123;e", "<K|var> <V|e>:<T|Int> = <L|123>;e") |
| 69 | + test("def f = 123", "<K|def> <V|f> = <L|123>") |
| 70 | + test("def f1(x: Int) = 123", "<K|def> <V|f1>(x: <T|Int>) = <L|123>") |
| 71 | + test("def f2[T](x: T) = { 123 }", "<K|def> <V|f2>[<T|T>](x: <T|T>) = { <L|123> }") |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + def patternMatching = { |
| 76 | + test("""val aFruit: Fruit = Apple("red", 123)""", |
| 77 | + """<K|val> <V|aFruit>: <T|Fruit> = <T|Apple>(<L|"red">, <L|123>)""") |
| 78 | + test("""val Apple(color, weight) = aFruit""", |
| 79 | + """<K|val> <T|Apple>(<V|color>, <V|weight>) = aFruit""") |
| 80 | + test("""case Apple(_, weight) => println(s"apple: $weight kgs")""", |
| 81 | + """<K|case> <T|Apple>(<V|_>, <V|weight>) <T|=>> println(s<L|"apple: <V|$weight <L|kgs">)""") |
| 82 | + test("""case o: Orange => println(s"orange ${o.weight} kgs")""", |
| 83 | + """<K|case> <V|o>: <T|Orange> <T|=>> println(s<L|"orange <V|${o.weight}<L| kgs">)""") |
| 84 | + test("""case m @ Melon(weight) => println(s"melon: ${m.weight} kgs")""", |
| 85 | + """<K|case> <V|m> @ <T|Melon>(<V|weight>) <T|=>> println(s<L|"melon: <V|${m.weight}<L| kgs">)""") |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + def unionTypes = { |
| 90 | + test("type A = String|Int| Long", "<K|type> <T|A> = <T|String>|<T|Int>| <T|Long>") |
| 91 | + test("type B = String |Int| Long", "<K|type> <T|B> = <T|String> |<T|Int>| <T|Long>") |
| 92 | + test("type C = String | Int | Long", "<K|type> <T|C> = <T|String> | <T|Int> | <T|Long>") |
| 93 | + test("type D = String&Int& Long", "<K|type> <T|D> = <T|String>&<T|Int>& <T|Long>") |
| 94 | + test("type E = String &Int& Long", "<K|type> <T|E> = <T|String> &<T|Int>& <T|Long>") |
| 95 | + test("type F = String & Int & Long", "<K|type> <T|F> = <T|String> & <T|Int> & <T|Long>") |
| 96 | + test("fn[String|Char](input)", "fn[<T|String>|<T|Char>](input)") |
| 97 | + } |
| 98 | +} |
0 commit comments