|
| 1 | +package dotty.tools.backend.jvm |
| 2 | + |
| 3 | +import org.junit.Assert._ |
| 4 | +import org.junit.Test |
| 5 | + |
| 6 | +class StringInterpolatorOptTest extends DottyBytecodeTest { |
| 7 | + import ASMConverters._ |
| 8 | + |
| 9 | + @Test def testRawInterpolator = { |
| 10 | + val source = |
| 11 | + """ |
| 12 | + |class Foo { |
| 13 | + | val one = 1 |
| 14 | + | val two = "two" |
| 15 | + | val three = 3.0 |
| 16 | + | |
| 17 | + | def meth1: String = raw"$one plus $two$three\n" |
| 18 | + | def meth2: String = "" + one + " plus " + two + three + "\\n" |
| 19 | + |} |
| 20 | + """.stripMargin |
| 21 | + |
| 22 | + checkBCode(source) { dir => |
| 23 | + val clsIn = dir.lookupName("Foo.class", directory = false).input |
| 24 | + val clsNode = loadClassNode(clsIn) |
| 25 | + val meth1 = getMethod(clsNode, "meth1") |
| 26 | + val meth2 = getMethod(clsNode, "meth2") |
| 27 | + |
| 28 | + val instructions1 = instructionsFromMethod(meth1) |
| 29 | + val instructions2 = instructionsFromMethod(meth2) |
| 30 | + |
| 31 | + assert(instructions1 == instructions2, |
| 32 | + "the `` string interpolator incorrectly converts to string concatenation\n" + |
| 33 | + diffInstructions(instructions1, instructions2)) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test def testSInterpolator = { |
| 38 | + val source = |
| 39 | + """ |
| 40 | + |class Foo { |
| 41 | + | val one = 1 |
| 42 | + | val two = "two" |
| 43 | + | val three = 3.0 |
| 44 | + | |
| 45 | + | def meth1: String = s"$one plus $two$three\n" |
| 46 | + | def meth2: String = "" + one + " plus " + two + three + "\n" |
| 47 | + |} |
| 48 | + """.stripMargin |
| 49 | + |
| 50 | + checkBCode(source) { dir => |
| 51 | + val clsIn = dir.lookupName("Foo.class", directory = false).input |
| 52 | + val clsNode = loadClassNode(clsIn) |
| 53 | + val meth1 = getMethod(clsNode, "meth1") |
| 54 | + val meth2 = getMethod(clsNode, "meth2") |
| 55 | + |
| 56 | + val instructions1 = instructionsFromMethod(meth1) |
| 57 | + val instructions2 = instructionsFromMethod(meth2) |
| 58 | + |
| 59 | + assert(instructions1 == instructions2, |
| 60 | + "the `s` string interpolator incorrectly converts to string concatenation\n" + |
| 61 | + diffInstructions(instructions1, instructions2)) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments