From 1080da80769cdc2bef2f06977094caff625b4a15 Mon Sep 17 00:00:00 2001 From: Den Shabalin Date: Thu, 14 Nov 2013 14:51:58 +0100 Subject: refactor out fresh name prefix extraction logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. refactor out FreshNameExtractor out of Quasiquotes cake into SymbolTable (can’t put it outside due to the fact that names are path-dependent) 2. add optional parameter to the fresh name creator to cover additional qq$ prefix needed for quasiquotes 3. add unit tests --- .../quasiquotes/QuasiquoteProperties.scala | 13 +++--- .../tools/nsc/symtab/FreshNameExtractorTest.scala | 47 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 test/junit/scala/tools/nsc/symtab/FreshNameExtractorTest.scala (limited to 'test') diff --git a/test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala b/test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala index b2bce124ee..d398c254fb 100644 --- a/test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala +++ b/test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala @@ -18,13 +18,12 @@ trait Helpers { object simplify extends Transformer { object SimplifiedName { - def unapply[T <: Name](name: T): Option[T] = - name.toString.split("\\$").toSeq match { - case first :+ last if scala.util.Try(last.toInt).isSuccess && first.nonEmpty => - val value = first.mkString("", "$", "$") - Some((if (name.isTermName) TermName(value) else TypeName(value)).asInstanceOf[T]) - case _ => None - } + val st = scala.reflect.runtime.universe.asInstanceOf[scala.reflect.internal.SymbolTable] + val FreshName = new st.FreshNameExtractor + def unapply[T <: Name](name: T): Option[T] = name.asInstanceOf[st.Name] match { + case FreshName(prefix) => + Some((if (name.isTermName) TermName(prefix) else TypeName(prefix)).asInstanceOf[T]) + } } override def transform(tree: Tree): Tree = tree match { diff --git a/test/junit/scala/tools/nsc/symtab/FreshNameExtractorTest.scala b/test/junit/scala/tools/nsc/symtab/FreshNameExtractorTest.scala new file mode 100644 index 0000000000..cf09abdfff --- /dev/null +++ b/test/junit/scala/tools/nsc/symtab/FreshNameExtractorTest.scala @@ -0,0 +1,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") + } + } +} \ No newline at end of file -- cgit v1.2.3