summaryrefslogtreecommitdiff
path: root/test/junit/scala/tools/nsc/transform/delambdafy/DelambdafyTest.scala
blob: 609f481721ddc364a68302b08d79f13f1abbfdb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package scala.tools.nsc.transform.delambdafy

import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

import scala.reflect.io.Path.jfile2path
import scala.tools.nsc.io.AbstractFile
import scala.tools.testing.BytecodeTesting._
import scala.tools.testing.TempDir

@RunWith(classOf[JUnit4])
class DelambdafyTest {
  def compileToMultipleOutputWithDelamdbafyMethod(): List[(String, Array[Byte])] = {
    val codeForMultiOutput = """
object Delambdafy {
  type -->[D, I] = PartialFunction[D, I]

  def main(args: Array[String]): Unit = {
    val result = List(1, 2, 4).map { a =>
      val list = List("1", "2", "3").map { _ + "test" }
      list.find { _ == a.toString + "test" }
    }
    println(result)
    lazy val _foo = foo(result) {
      case x :: xs if x isDefined => x.get.length
      case _ => 0
    }
    println(_foo)
    lazy val bar: Int => Int = {
      case 2 => 23
      case _ =>
        val v = List(1).map { _ + 42 }.head
        v + 31
    }
    bar(3)
    lazy val _baz = baz {
      case 1 =>
        val local = List(1).map(_ + 1)
        local.head
    }
  }

  def baz[T](f: Any --> Any): Any => Any = f

  def foo(b: List[Option[String]])(a: List[Option[String]] => Int): Int = a(b)
}
"""
    val srcFile = makeSourceFile(codeForMultiOutput, "delambdafyTest.scala")
    val outDir = AbstractFile.getDirectory(TempDir.createTempDir())
    val outDirPath = outDir.canonicalPath
    val extraArgs = "-Ydelambdafy:method"
    val argsWithOutDir = extraArgs + s" -d $outDirPath -cp $outDirPath"
    val compiler = newCompilerWithoutVirtualOutdir(extraArgs = argsWithOutDir)
    compiler.global.settings.outputDirs.add(srcFile.file, outDir)

    new compiler.global.Run().compileSources(List(srcFile))

    val classfiles = getGeneratedClassfiles(outDir)
    outDir.delete()
    classfiles
  }

  @Test
  def shouldFindOutputFoldersForAllPromotedLambdasAsMethod(): Unit = {
    val actual = compileToMultipleOutputWithDelamdbafyMethod()

    assertTrue(actual.length > 0)
  }
}