summaryrefslogtreecommitdiff
path: root/test/junit/scala/tools/nsc/symtab/FreshNameExtractorTest.scala
blob: 7796345351f28f73c49cbbb5b7ec34cef9a2aa42 (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
package scala.tools.nsc
package symtab

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

import scala.tools.testing.AssertUtil.assertThrows
import scala.reflect.internal.util.FreshNameCreator

@RunWith(classOf[JUnit4])
class FreshNameExtractorTest {
  object symbolTable extends SymbolTableForUnitTesting
  import symbolTable._

  val prefixes = List("foo$", "x$", "bar", "bippy$baz$")

  @Test
  def extractionPreservesPrefix =
    ("" :: prefixes).foreach { creatorPrefix =>
      prefixes.foreach { newPrefix =>
        val Creator = new FreshNameCreator(creatorPrefix)
        val Extractor = new FreshNameExtractor(creatorPrefix)
        val Extractor(extractedPrefix) = TermName(Creator.newName(newPrefix))
        assertEquals(newPrefix, extractedPrefix)
      }
    }

  @Test
  def extractionFailsOnCreatorPrefixMismatch = {
    val Creator = new FreshNameCreator(prefixes.head)
    val Extractor = new FreshNameExtractor(prefixes.tail.head)
    assertThrows[MatchError] {
      TermName(Creator.newName("foo")) match { case Extractor(_) => }
    }
  }

  @Test
  def `no numeric suffix? no problem!` = {
    val Creator   = new FreshNameCreator(prefixes.head)
    val Extractor = new FreshNameExtractor(prefixes.head)
    TermName(Creator.newName("foo") + "bar") match {
      case Extractor(_) =>
    }
  }
}