summaryrefslogtreecommitdiff
path: root/test/files
diff options
context:
space:
mode:
authorDen Shabalin <den.shabalin@gmail.com>2013-10-31 13:41:53 +0100
committerDen Shabalin <den.shabalin@gmail.com>2013-11-12 14:04:42 +0100
commita4a3ab0d722412b9ecf267b178bb866087867cf9 (patch)
tree2f3a55eeb5c8a8e05fbf302f86ed26201b7b9c7b /test/files
parentd73680557d4037bed69bc0ce982566f3915361c3 (diff)
downloadscala-a4a3ab0d722412b9ecf267b178bb866087867cf9.tar.gz
scala-a4a3ab0d722412b9ecf267b178bb866087867cf9.tar.bz2
scala-a4a3ab0d722412b9ecf267b178bb866087867cf9.zip
implement inverse transformation to mkFor
This effectively reconstructs a sequence of enumerators and body from the tree produced by mkFor. This lets to define bi-directional SyntacticFor and SyntacticForYield constructors/extractors to work with for loops. Correctness of the transformation is tested by a scalacheck test that generates a sequence of random enumerators, sugars them into maps/flatMaps/foreach/withFilter calls and reconstructs them back.
Diffstat (limited to 'test/files')
-rw-r--r--test/files/scalacheck/quasiquotes/ForProps.scala38
-rw-r--r--test/files/scalacheck/quasiquotes/Test.scala1
2 files changed, 39 insertions, 0 deletions
diff --git a/test/files/scalacheck/quasiquotes/ForProps.scala b/test/files/scalacheck/quasiquotes/ForProps.scala
new file mode 100644
index 0000000000..234f4d10eb
--- /dev/null
+++ b/test/files/scalacheck/quasiquotes/ForProps.scala
@@ -0,0 +1,38 @@
+import org.scalacheck._, Prop._, Gen._, Arbitrary._
+import scala.reflect.runtime.universe._, Flag._, build.{Ident => _, _}
+
+object ForProps extends QuasiquoteProperties("for") {
+ case class ForEnums(val value: List[Tree])
+
+ def genSimpleBind: Gen[Bind] =
+ for(name <- genTermName)
+ yield pq"$name @ _"
+
+ def genForFilter: Gen[Tree] =
+ for(cond <- genIdent(genTermName))
+ yield SyntacticFilter(cond)
+
+ def genForFrom: Gen[Tree] =
+ for(lhs <- genSimpleBind; rhs <- genIdent(genTermName))
+ yield SyntacticValFrom(lhs, rhs)
+
+ def genForEq: Gen[Tree] =
+ for(lhs <- genSimpleBind; rhs <- genIdent(genTermName))
+ yield SyntacticValEq(lhs, rhs)
+
+ def genForEnums(size: Int): Gen[ForEnums] =
+ for(first <- genForFrom; rest <- listOfN(size, oneOf(genForFrom, genForFilter, genForEq)))
+ yield new ForEnums(first :: rest)
+
+ implicit val arbForEnums: Arbitrary[ForEnums] = arbitrarySized(genForEnums)
+
+ property("construct-reconstruct for") = forAll { (enums: ForEnums, body: Tree) =>
+ val SyntacticFor(recoveredEnums, recoveredBody) = SyntacticFor(enums.value, body)
+ recoveredEnums ≈ enums.value && recoveredBody ≈ body
+ }
+
+ property("construct-reconstruct for-yield") = forAll { (enums: ForEnums, body: Tree) =>
+ val SyntacticForYield(recoveredEnums, recoveredBody) = SyntacticForYield(enums.value, body)
+ recoveredEnums ≈ enums.value && recoveredBody ≈ body
+ }
+} \ No newline at end of file
diff --git a/test/files/scalacheck/quasiquotes/Test.scala b/test/files/scalacheck/quasiquotes/Test.scala
index 73cac0368c..8b1e779ab2 100644
--- a/test/files/scalacheck/quasiquotes/Test.scala
+++ b/test/files/scalacheck/quasiquotes/Test.scala
@@ -12,5 +12,6 @@ object Test extends Properties("quasiquotes") {
include(DefinitionConstructionProps)
include(DefinitionDeconstructionProps)
include(DeprecationProps)
+ include(ForProps)
include(TypecheckedProps)
}