|
| 1 | +package dotty.tools |
| 2 | +package dotc |
| 3 | + |
| 4 | +import core.*, Decorators.*, Symbols.* |
| 5 | +import printing.Texts.* |
| 6 | + |
| 7 | +import org.junit.Test |
| 8 | + |
| 9 | +class TupleShowTests extends DottyTest: |
| 10 | + def IntType = defn.IntType |
| 11 | + def LongType = defn.LongType |
| 12 | + def ShortType = defn.ShortType |
| 13 | + def Types_10 = List.fill(5)(IntType) ::: List.fill(5)(LongType) |
| 14 | + def Types_20 = Types_10 ::: Types_10 |
| 15 | + |
| 16 | + val tup0 = defn.tupleType(Nil) |
| 17 | + val tup1 = defn.tupleType(IntType :: Nil) |
| 18 | + val tup2 = defn.tupleType(IntType :: LongType :: Nil) |
| 19 | + val tup3 = defn.tupleType(IntType :: LongType :: ShortType :: Nil) |
| 20 | + val tup21 = defn.tupleType(Types_20 ::: IntType :: Nil) |
| 21 | + val tup22 = defn.tupleType(Types_20 ::: IntType :: LongType :: Nil) |
| 22 | + val tup23 = defn.tupleType(Types_20 ::: IntType :: LongType :: ShortType :: Nil) |
| 23 | + val tup24 = defn.tupleType(Types_20 ::: IntType :: LongType :: ShortType :: ShortType :: Nil) |
| 24 | + |
| 25 | + @Test def tup0_show = chkEq("EmptyTuple.type", i"$tup0") |
| 26 | + @Test def tup1_show = chkEq("Tuple1[Int]", i"$tup1") |
| 27 | + @Test def tup2_show = chkEq("(Int, Long)", i"$tup2") |
| 28 | + @Test def tup3_show = chkEq("(Int, Long, Short)", i"$tup3") |
| 29 | + @Test def tup21_show = chkEq(res21, i"$tup21") |
| 30 | + @Test def tup22_show = chkEq(res22, i"$tup22") |
| 31 | + @Test def tup23_show = chkEq(res23, i"$tup23") |
| 32 | + @Test def tup24_show = chkEq(res24, i"$tup24") |
| 33 | + |
| 34 | + @Test def tup3_text = |
| 35 | + val obt = tup3.toText(ctx.printer) |
| 36 | + val exp = Fluid(List( |
| 37 | + Str(")"), |
| 38 | + Str("Short"), |
| 39 | + Closed(List(Str(", "), Str("Long"))), |
| 40 | + Closed(List(Str(", "), Str("Int"))), |
| 41 | + Str("("), |
| 42 | + )) |
| 43 | + chkEq(exp, obt) |
| 44 | + |
| 45 | + @Test def tup3_layout10 = |
| 46 | + val obt = tup3.toText(ctx.printer).layout(10) |
| 47 | + val exp = Fluid(List( |
| 48 | + Str(" Short)"), |
| 49 | + Str(" Long, "), |
| 50 | + Str("(Int, "), |
| 51 | + )) |
| 52 | + chkEq(exp, obt) |
| 53 | + |
| 54 | + @Test def tup3_show10 = chkEq("(Int,\n Long,\n Short)", tup3.toText(ctx.printer).mkString(10, false)) |
| 55 | + |
| 56 | + val res21 = """|(Int, Int, Int, Int, Int, Long, Long, Long, Long, Long, Int, Int, Int, Int, |
| 57 | + | Int, Long, Long, Long, Long, Long, Int)""".stripMargin |
| 58 | + |
| 59 | + val res22 = """|(Int, Int, Int, Int, Int, Long, Long, Long, Long, Long, Int, Int, Int, Int, |
| 60 | + | Int, Long, Long, Long, Long, Long, Int, Long)""".stripMargin |
| 61 | + |
| 62 | + val res23 = """|(Int, Int, Int, Int, Int, Long, Long, Long, Long, Long, Int, Int, Int, Int, |
| 63 | + | Int, Long, Long, Long, Long, Long, Int, Long, Short)""".stripMargin |
| 64 | + |
| 65 | + val res24 = """|(Int, Int, Int, Int, Int, Long, Long, Long, Long, Long, Int, Int, Int, Int, |
| 66 | + | Int, Long, Long, Long, Long, Long, Int, Long, Short, Short)""".stripMargin |
| 67 | + |
| 68 | + def chkEq[A](expected: A, obtained: A) = assert(expected == obtained, diff(s"$expected", s"$obtained")) |
| 69 | + |
| 70 | + def diff(exp: String, obt: String) = |
| 71 | + val min = math.min(exp.length, obt.length) |
| 72 | + val pre = |
| 73 | + var i = 0 |
| 74 | + while i < min && exp(i) == obt(i) do i += 1 |
| 75 | + exp.take(i) |
| 76 | + val suf = |
| 77 | + val max = min - pre.length - 1 |
| 78 | + var i = 0 |
| 79 | + while i <= max && exp(exp.length - 1 - i) == obt(obt.length - 1 - i) do i += 1 |
| 80 | + exp.drop(exp.length - 1) |
| 81 | + |
| 82 | + import scala.io.AnsiColor.* |
| 83 | + val ellip = BLACK + BOLD + "..." + RESET |
| 84 | + val compactPre = if pre.length <= 20 then pre else ellip + pre.drop(pre.length - 20) |
| 85 | + val compactSuf = if suf.length <= 20 then suf else suf.take(20) + ellip |
| 86 | + def extractDiff(s: String) = s.slice(pre.length, s.length - suf.length) |
| 87 | + s"""|Comparison Failure: |
| 88 | + | expected: $compactPre${CYAN }${extractDiff(exp)}$RESET$compactSuf |
| 89 | + | obtained: $compactPre$MAGENTA${extractDiff(obt)}$RESET$compactSuf |
| 90 | + |""".stripMargin |
0 commit comments