-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9427 from da-tubi/test/constantFolder
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
test/junit/scala/tools/nsc/typechecker/ConstantFolderTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package scala.tools.nsc.typechecker | ||
|
||
import org.junit.Test | ||
import org.junit.Assert.assertTrue | ||
|
||
import scala.tools.testkit.BytecodeTesting | ||
|
||
|
||
class ConstantFolderTest extends BytecodeTesting { | ||
import compiler._ | ||
import global._ | ||
|
||
override def compilerArgs = "-Ystop-after:typer" | ||
|
||
def literalValDefAssert(tree: Tree, name: String, constant: Constant): Unit = { | ||
val valDef: ValDef = tree.collect { | ||
case node @ ValDef(_, _, _, _) if node.name.decodedName.toString.trim == name => | ||
node | ||
}.head | ||
|
||
assertTrue { | ||
valDef.collect { case node @ Literal(constant) => node }.nonEmpty | ||
} | ||
} | ||
|
||
@Test | ||
def testStringFolder(): Unit = { | ||
val code = | ||
"""object O { | ||
| final val x = "123" + "Scala" | ||
|} | ||
""".stripMargin | ||
val run = compiler.newRun() | ||
run.compileSources(List(BytecodeTesting.makeSourceFile(code, "UnitTestSource.scala"))) | ||
val tree = run.units.next().body | ||
literalValDefAssert(tree, "x", Constant("123Scala")) | ||
} | ||
|
||
@Test | ||
def testBooleanFolder(): Unit = { | ||
val code = | ||
"""object O { | ||
| final val x0 = true && true | ||
| final val x1 = true && false | ||
| final val x2 = false && true | ||
| final val x3 = false && false | ||
| final val x4 = true || true | ||
| final val x5 = true || false | ||
| final val x6 = false || true | ||
| final val x7 = false || false | ||
|} | ||
""".stripMargin | ||
val run = compiler.newRun() | ||
run.compileSources(List(BytecodeTesting.makeSourceFile(code, "UnitTestSource.scala"))) | ||
val tree = run.units.next().body | ||
literalValDefAssert(tree, "x0", Constant(true)) | ||
literalValDefAssert(tree, "x1", Constant(false)) | ||
literalValDefAssert(tree, "x2", Constant(false)) | ||
literalValDefAssert(tree, "x3", Constant(false)) | ||
literalValDefAssert(tree, "x4", Constant(true)) | ||
literalValDefAssert(tree, "x5", Constant(true)) | ||
literalValDefAssert(tree, "x6", Constant(true)) | ||
literalValDefAssert(tree, "x7", Constant(false)) | ||
} | ||
} |