Skip to content

Commit

Permalink
Add some bytecode tests for the pattern matcher.
Browse files Browse the repository at this point in the history
* Switch with alternatives
* Switch with guards
* No `throw` in a match with `case _ =>`
  • Loading branch information
sjrd committed Aug 30, 2018
1 parent 592731d commit 7baec35
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TestBCode extends DottyBytecodeTest {
/** This test verifies that simple matches with `@switch` annotations are
* indeed transformed to a switch
*/
@Test def basicTransfromAnnotated = {
@Test def basicSwitch = {
val source = """
|object Foo {
| import scala.annotation.switch
Expand All @@ -69,6 +69,71 @@ class TestBCode extends DottyBytecodeTest {
}
}

@Test def switchWithAlternatives = {
val source =
"""
|object Foo {
| import scala.annotation.switch
| def foo(i: Int) = (i: @switch) match {
| case 2 => println(2)
| case 1 | 3 | 5 => println(1)
| case 0 => println(0)
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val moduleIn = dir.lookupName("Foo$.class", directory = false)
val moduleNode = loadClassNode(moduleIn.input)
val methodNode = getMethod(moduleNode, "foo")
assert(verifySwitch(methodNode))
}
}

@Test def switchWithGuards = {
val source =
"""
|object Foo {
| import scala.annotation.switch
| def foo(i: Int, b: Boolean) = (i: @switch) match {
| case 2 => println(3)
| case 1 if b => println(2)
| case 1 => println(1)
| case 0 => println(0)
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val moduleIn = dir.lookupName("Foo$.class", directory = false)
val moduleNode = loadClassNode(moduleIn.input)
val methodNode = getMethod(moduleNode, "foo")
assert(verifySwitch(methodNode))
}
}

@Test def matchWithDefaultNoThrowMatchError = {
val source =
"""class Test {
| def test(s: String) = s match {
| case "Hello" => 1
| case _ => 2
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Test.class", directory = false)
val clsNode = loadClassNode(clsIn.input)
val method = getMethod(clsNode, "test")
val throwMatchError = instructionsFromMethod(method).exists {
case Op(Opcodes.ATHROW) => true
case _ => false
}
assertFalse(throwMatchError)
}
}

@Test def failTransform = {
val source = """
|object Foo {
Expand Down

0 comments on commit 7baec35

Please sign in to comment.