summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@typesafe.com>2016-02-03 12:58:29 +0100
committerLukas Rytz <lukas.rytz@typesafe.com>2016-02-03 12:58:29 +0100
commit2c78fb31c9f34ccfe262d7b8660c9e486424823a (patch)
treeb5e941014fe4e1b675443d51e76d78895a2401ff /test
parentaa84aaa924cfba614a9803298a3e431d21417ad2 (diff)
parentcd0211cf0fb755281c7607bf6a172df306b5686f (diff)
downloadscala-2c78fb31c9f34ccfe262d7b8660c9e486424823a.tar.gz
scala-2c78fb31c9f34ccfe262d7b8660c9e486424823a.tar.bz2
scala-2c78fb31c9f34ccfe262d7b8660c9e486424823a.zip
Merge pull request #4737 from soc/SI-9315
SI-9315 Desugar string concat to java.lang.StringBuilder ...
Diffstat (limited to 'test')
-rw-r--r--test/junit/scala/tools/nsc/backend/jvm/StringConcatTest.scala70
1 files changed, 70 insertions, 0 deletions
diff --git a/test/junit/scala/tools/nsc/backend/jvm/StringConcatTest.scala b/test/junit/scala/tools/nsc/backend/jvm/StringConcatTest.scala
new file mode 100644
index 0000000000..80cde6c9a9
--- /dev/null
+++ b/test/junit/scala/tools/nsc/backend/jvm/StringConcatTest.scala
@@ -0,0 +1,70 @@
+package scala.tools.nsc
+package backend.jvm
+
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+import scala.tools.asm.Opcodes._
+import org.junit.Assert._
+
+import scala.tools.testing.AssertUtil._
+
+import CodeGenTools._
+import scala.tools.partest.ASMConverters
+import ASMConverters._
+import scala.tools.testing.ClearAfterClass
+
+object StringConcatTest extends ClearAfterClass.Clearable {
+ var compiler = newCompiler()
+ def clear(): Unit = { compiler = null }
+}
+
+@RunWith(classOf[JUnit4])
+class StringConcatTest extends ClearAfterClass {
+ ClearAfterClass.stateToClear = StringConcatTest
+ val compiler = StringConcatTest.compiler
+
+ val commonPreInstructions = List(Label(0), LineNumber(1, Label(0)), TypeOp(NEW, "java/lang/StringBuilder"), Op(DUP), Invoke(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false), Ldc(LDC, "abc"), Invoke(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false), VarOp(ALOAD, 0))
+
+ val commonPostInstructions = List(Invoke(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false), Op(ARETURN), Label(12))
+
+ def instructionsWithCommonParts(instructions: List[Instruction]) = commonPreInstructions ++ instructions ++ commonPostInstructions
+
+ def instructionsForResultMethod(code: String): List[Instruction] = {
+ val methods = compileMethods(compiler)(code)
+ val resultMethod = methods.find(_.name == "result").get
+ instructionsFromMethod(resultMethod)
+ }
+
+ @Test
+ def concatStringToStringBuilder: Unit = {
+ val code = """ def string = "def"; def result = "abc" + string """
+ val actualInstructions = instructionsForResultMethod(code)
+ val expectedInstructions = instructionsWithCommonParts(List(Invoke(INVOKEVIRTUAL, "C", "string", "()Ljava/lang/String;", false), Invoke(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false)))
+ assertSameCode(actualInstructions, expectedInstructions)
+ }
+
+ @Test
+ def concatStringBufferToStringBuilder: Unit = {
+ val code = """ def stringBuffer = new java.lang.StringBuffer("def"); def result = "abc" + stringBuffer """
+ val actualInstructions = instructionsForResultMethod(code)
+ val expectedInstructions = instructionsWithCommonParts(List(Invoke(INVOKEVIRTUAL, "C", "stringBuffer", "()Ljava/lang/StringBuffer;", false), Invoke(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/StringBuffer;)Ljava/lang/StringBuilder;", false)))
+ assertSameCode(actualInstructions, expectedInstructions)
+ }
+
+ @Test
+ def concatCharSequenceToStringBuilder: Unit = {
+ val code = """ def charSequence: CharSequence = "def"; def result = "abc" + charSequence """
+ val actualInstructions = instructionsForResultMethod(code)
+ val expectedInstructions = instructionsWithCommonParts(List(Invoke(INVOKEVIRTUAL, "C", "charSequence", "()Ljava/lang/CharSequence;", false), Invoke(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/CharSequence;)Ljava/lang/StringBuilder;", false)))
+ assertSameCode(actualInstructions, expectedInstructions)
+ }
+
+ @Test
+ def concatIntToStringBuilder: Unit = {
+ val code = """ def int = 123; def result = "abc" + int """
+ val actualInstructions = instructionsForResultMethod(code)
+ val expectedInstructions = instructionsWithCommonParts(List(Invoke(INVOKEVIRTUAL, "C", "int", "()I", false), Invoke(INVOKESTATIC, "scala/runtime/BoxesRunTime", "boxToInteger", "(I)Ljava/lang/Integer;", false), Invoke(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false)))
+ assertSameCode(actualInstructions, expectedInstructions)
+ }
+}