summaryrefslogtreecommitdiff
path: root/src/partest/scala/tools/partest/IcodeTest.scala
blob: ebb194b14fe1d617df80aa15d87f21be4cbd1bac (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
/* NSC -- new Scala compiler
 * Copyright 2005-2012 LAMP/EPFL
 * @author Paul Phillips
 */

package scala.tools.partest

import scala.tools.nsc._
import nest.FileUtil._
import io.Directory

/** A trait for testing icode.  All you need is this in a
 *  partest source file:
 *  {{{
 *    object Test extends IcodeTest
 *  }}}
 *  And then the generated output will be the icode for everything
 *  in that file.  See source for possible customizations.
 */
abstract class IcodeTest extends DirectTest {
  // override to check icode at a different point.
  def printIcodeAfterPhase = "icode"
  // override to use source code other than the file being tested.
  def code = testPath.slurp()

  override def extraSettings: String = "-usejavacp -Xprint-icode:" + printIcodeAfterPhase

  // Compile, read in all the *.icode files, delete them, and return their contents
  def collectIcode(args: String*): List[String] = {
    compile("-d" :: testOutput.path :: args.toList : _*)
    val icodeFiles = testOutput.files.toList filter (_ hasExtension "icode")

    try     icodeFiles sortBy (_.name) flatMap (f => f.lines.toList)
    finally icodeFiles foreach (f => f.delete())
  }

  // Default show() compiles the code with and without optimization and
  // outputs the diff.
  def show() {
    val lines1 = collectIcode("")
    val lines2 = collectIcode("-optimise")

    println(compareContents(lines1, lines2))
  }
}