summaryrefslogtreecommitdiff
path: root/test/junit/scala/tools/nsc/symtab/FreshNameExtractorTest.scala
blob: cf09abdfffdbb11336608e8ac7d4e8f6b7ce7058 (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] {
      val Extractor(_) = TermName(Creator.newName("foo"))
    }
  }

  @Test
  def extractionsFailsIfNameDoesntEndWithNumber = {
    val Creator = new FreshNameCreator(prefixes.head)
    val Extractor = new FreshNameExtractor(prefixes.head)
    assertThrows[MatchError] {
      val Extractor(_) = TermName(Creator.newName("foo") + "bar")
    }
  }
}