From e5ed594a89f4e468f3a9e754eb75687885908ba3 Mon Sep 17 00:00:00 2001 From: Den Shabalin Date: Sat, 8 Dec 2012 13:47:14 +0100 Subject: Adds extractors for TypeName, TermName and Modifiers This change allows to pattern match over type names, term names and modifiers. Otherwise it can be quite painful to match over complex trees as each name or modifiers requires a guard. This pull request also changes the name of default constructor for term and type names i.e. TypeName(s) instead of newTermName(s). This is shorter to type, more consistent with the rest of reflection api and consistent with the way it will be pattern matched later on. --- test/files/scalacheck/ReflectionExtractors.scala | 52 ++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 test/files/scalacheck/ReflectionExtractors.scala (limited to 'test/files/scalacheck') diff --git a/test/files/scalacheck/ReflectionExtractors.scala b/test/files/scalacheck/ReflectionExtractors.scala new file mode 100644 index 0000000000..a2615feb3e --- /dev/null +++ b/test/files/scalacheck/ReflectionExtractors.scala @@ -0,0 +1,52 @@ +import org.scalacheck._ +import Prop._ +import Gen._ +import Arbitrary._ + +import scala.reflect.runtime.universe._ +import Flag._ + +object Test extends Properties("reflection extractors") { + + val genFlag = oneOf( + TRAIT, INTERFACE, MUTABLE, MACRO, DEFERRED, ABSTRACT, FINAL, SEALED, + IMPLICIT, LAZY, OVERRIDE, PRIVATE, PROTECTED, LOCAL, CASE, ABSOVERRIDE, + BYNAMEPARAM, PARAM, COVARIANT, CONTRAVARIANT, DEFAULTPARAM, PRESUPER, + DEFAULTINIT + ) + val genModifiers = + for(flag <- genFlag; privateWithin <- genName) + yield Modifiers(flag, privateWithin, Nil) + val genTermName = for(name <- arbitrary[String]) yield TermName(name) + val genTypeName = for(name <- arbitrary[String]) yield TypeName(name) + val genName = oneOf(genTermName, genTypeName) + + implicit val arbTermName: Arbitrary[TermName] = Arbitrary(genTermName) + implicit val arbTypeName: Arbitrary[TypeName] = Arbitrary(genTypeName) + implicit val arbName: Arbitrary[Name] = Arbitrary(genName) + implicit val arbMods: Arbitrary[Modifiers] = Arbitrary(genModifiers) + + property("extract term name") = forAll { (name: TermName) => + val TermName(s) = name + s == name.toString + } + + property("extract type name") = forAll { (name: TypeName) => + val TypeName(s) = name + s == name.toString + } + + property("extract term or type name") = forAll { (name: Name) => + name match { + case TermName(s) => s == name.toString + case TypeName(s) => s == name.toString + } + } + + property("extract modifiers") = forAll { (mods: Modifiers) => + val Modifiers(flags, priv, annots) = mods + flags == mods.flags && + priv == mods.privateWithin && + annots == mods.annotations + } +} \ No newline at end of file -- cgit v1.2.3