From e8a338b45976a1178d359264e88b6a54310d22d1 Mon Sep 17 00:00:00 2001 From: Yang Bo Date: Fri, 5 Apr 2019 15:56:02 +0800 Subject: [PATCH] Remove Each --- build.sbt | 6 - each/.js/build.sbt | 1 - each/.jvm/build.sbt | 1 - each/build.sbt.shared | 25 - .../scala/com/thoughtworks/each/Monadic.scala | 379 ------------- .../scala/com/thoughtworks/each/package.scala | 13 - .../com/thoughtworks/each/ReportingTest.scala | 160 ------ .../each/TraverseComprehensionTest211.scala | 43 -- .../thoughtworks/each/AnnotationTest.scala | 81 --- .../each/ComprehensionImplicitsTest.scala | 80 --- .../scala/com/thoughtworks/each/Issue38.scala | 17 - .../thoughtworks/each/MonadicErrorTest.scala | 334 ------------ .../com/thoughtworks/each/MonadicTest.scala | 500 ------------------ .../each/TraverseComprehensionTest.scala | 74 --- 14 files changed, 1714 deletions(-) delete mode 120000 each/.js/build.sbt delete mode 120000 each/.jvm/build.sbt delete mode 100644 each/build.sbt.shared delete mode 100644 each/src/main/scala/com/thoughtworks/each/Monadic.scala delete mode 100644 each/src/main/scala/com/thoughtworks/each/package.scala delete mode 100644 each/src/test/scala-2.11/com/thoughtworks/each/ReportingTest.scala delete mode 100644 each/src/test/scala-2.11/com/thoughtworks/each/TraverseComprehensionTest211.scala delete mode 100644 each/src/test/scala/com/thoughtworks/each/AnnotationTest.scala delete mode 100644 each/src/test/scala/com/thoughtworks/each/ComprehensionImplicitsTest.scala delete mode 100644 each/src/test/scala/com/thoughtworks/each/Issue38.scala delete mode 100644 each/src/test/scala/com/thoughtworks/each/MonadicErrorTest.scala delete mode 100755 each/src/test/scala/com/thoughtworks/each/MonadicTest.scala delete mode 100644 each/src/test/scala/com/thoughtworks/each/TraverseComprehensionTest.scala diff --git a/build.sbt b/build.sbt index 642f05a..a362738 100644 --- a/build.sbt +++ b/build.sbt @@ -15,12 +15,6 @@ lazy val coreJVM = core.jvm lazy val coreJS = core.js -lazy val each = crossProject.crossType(CrossType.Pure).dependsOn(core, `comprehension-monad`) - -lazy val eachJVM = each.jvm - -lazy val eachJS = each.js - lazy val `comprehension-monad` = crossProject.crossType(CrossType.Pure) lazy val `comprehension-monadJVM` = `comprehension-monad`.jvm diff --git a/each/.js/build.sbt b/each/.js/build.sbt deleted file mode 120000 index 570a7b6..0000000 --- a/each/.js/build.sbt +++ /dev/null @@ -1 +0,0 @@ -../build.sbt.shared \ No newline at end of file diff --git a/each/.jvm/build.sbt b/each/.jvm/build.sbt deleted file mode 120000 index 570a7b6..0000000 --- a/each/.jvm/build.sbt +++ /dev/null @@ -1 +0,0 @@ -../build.sbt.shared \ No newline at end of file diff --git a/each/build.sbt.shared b/each/build.sbt.shared deleted file mode 100644 index 349d092..0000000 --- a/each/build.sbt.shared +++ /dev/null @@ -1,25 +0,0 @@ -organization := "com.thoughtworks.each" - -name := "each" - -scalacOptions += "-deprecation" - -scalacOptions += "-feature" - -scalacOptions += "-unchecked" - -description := "A macro library that converts native imperative syntax to scalaz's monadic expressions." - -libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test - -libraryDependencies ++= { - if (scalaVersion.value.startsWith("2.10.")) { - Seq() - } else { - Seq("org.scala-lang.modules" %% "scala-xml" % "1.0.5" % Test) - } -} - -addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full) - -libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value % Provided diff --git a/each/src/main/scala/com/thoughtworks/each/Monadic.scala b/each/src/main/scala/com/thoughtworks/each/Monadic.scala deleted file mode 100644 index 2117185..0000000 --- a/each/src/main/scala/com/thoughtworks/each/Monadic.scala +++ /dev/null @@ -1,379 +0,0 @@ -/* -Copyright 2015 ThoughtWorks, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package com.thoughtworks.each - -import com.thoughtworks.sde.core.{MonadicFactory, Preprocessor} -import macrocompat.bundle - -import scala.annotation.{StaticAnnotation, compileTimeOnly} -import scala.language.experimental.macros -import scala.language.{higherKinds, implicitConversions} -import scala.reflect.macros._ -import scalaz._ -import scalaz.effect.MonadCatchIO -import scalaz.syntax.{FoldableOps, MonadPlusOps, TraverseOps} - -/** - * @author 杨博 (Yang Bo) <pop.atry@gmail.com> - */ -object Monadic { - - - @inline - implicit final class ToMonadicLoopOps[F[_], A](underlying: F[A]) { - - def monadicLoop = new MonadicLoop(underlying) - - } - - @inline - implicit def getUnderlying[F[_], A](monadicLoop: MonadicLoop[F, A]): F[A] = monadicLoop.underlying - - object MonadicLoop { - - @bundle - private[MonadicLoop] final class MacroBundle(val c: blackbox.Context) { - - import c.universe._ - - def foreach(f: Tree)(foldable: Tree): Tree = { - val q"$monadicLoop.foreach[$u]($f)($foldable)" = c.macroApplication - val monadicLoopName = TermName(c.freshName("monadicLoop")) - q""" - val $monadicLoopName = $monadicLoop - _root_.com.thoughtworks.sde.core.MonadicFactory.Instructions.foreach[ - $monadicLoopName.F, - $monadicLoopName.Element, - $u - ]($monadicLoopName.underlying, $foldable, $f) - """ - } - - def map(f: Tree)(traverse: Tree): Tree = { - val q"$monadicLoop.map[$b]($f)($traverse)" = c.macroApplication - val monadicLoopName = TermName(c.freshName("monadicLoop")) - q""" - val $monadicLoopName = $monadicLoop - new _root_.com.thoughtworks.each.Monadic.MonadicLoop[$monadicLoopName.F, $b]( - _root_.com.thoughtworks.sde.core.MonadicFactory.Instructions.map[ - $monadicLoopName.F, - $monadicLoopName.Element, - $b - ]($monadicLoopName.underlying, $traverse, $f) - ) - """ - } - - def flatMap(f: Tree)(traverse: Tree, bind: Tree): Tree = { - val q"$monadicLoop.flatMap[$b]($f)($traverse, $bind)" = c.macroApplication - val monadicLoopName = TermName(c.freshName("monadicLoop")) - q""" - val $monadicLoopName = $monadicLoop - new _root_.com.thoughtworks.each.Monadic.MonadicLoop[$monadicLoopName.F, $b]( - _root_.com.thoughtworks.sde.core.MonadicFactory.Instructions.flatMap[ - $monadicLoopName.F, - $monadicLoopName.Element, - $b - ]($monadicLoopName.underlying, $traverse, $bind, $f) - ) - """ - } - - def filter(f: Tree)(traverse: Tree, monadPlus: Tree): Tree = { - val q"$monadicLoop.${TermName("filter" | "withFilter")}($f)($traverse, $monadPlus)" = c.macroApplication - val monadicLoopName = TermName(c.freshName("monadicLoop")) - q""" - val $monadicLoopName = $monadicLoop - new _root_.com.thoughtworks.each.Monadic.MonadicLoop[$monadicLoopName.F, $monadicLoopName.Element]( - _root_.com.thoughtworks.sde.core.MonadicFactory.Instructions.filter[ - $monadicLoopName.F, - $monadicLoopName.Element - ]($monadicLoopName.underlying, $traverse, $monadPlus, $f) - ) - """ - } - - } - - @inline - implicit def toFoldableOps[F[_] : Foldable, A](monadicLoop: MonadicLoop[F, A]): FoldableOps[F, A] = { - scalaz.syntax.foldable.ToFoldableOps(monadicLoop.underlying) - } - - @inline - implicit def toTraverseOps[F[_] : Traverse, A](monadicLoop: MonadicLoop[F, A]): TraverseOps[F, A] = { - scalaz.syntax.traverse.ToTraverseOps(monadicLoop.underlying) - } - - @inline - implicit def toMonadPlusOps[F[_] : MonadPlus, A](monadicLoop: MonadicLoop[F, A]): MonadPlusOps[F, A] = { - scalaz.syntax.monadPlus.ToMonadPlusOps(monadicLoop.underlying) - } - - } - - @deprecated( - message = """ - Use `@monadic[X] def f = { ... }` instead of `monadic[X] { ... }`. - Note that you can remove `.monadicLoop` in `@monadic` methods. - """, - since = "1.0.1") - final class MonadicLoop[F0[_], A](val underlying: F0[A]) { - - type F[X] = F0[X] - - type Element = A - - @inline - def toFoldableOps(implicit foldable: Foldable[F]) = scalaz.syntax.foldable.ToFoldableOps(underlying) - - @inline - def toTraverseOps(implicit traverse: Traverse[F]) = scalaz.syntax.traverse.ToTraverseOps(underlying) - - def foreach[U](f: A => U)(implicit foldable: Foldable[F]): Unit = macro MonadicLoop.MacroBundle.foreach - - def map[B](f: A => B)(implicit traverse: Traverse[F]): MonadicLoop[F, B] = macro MonadicLoop.MacroBundle.map - - def flatMap[B](f: A => F[B])(implicit traverse: Traverse[F], bind: Bind[F]): MonadicLoop[F, B] = macro MonadicLoop.MacroBundle.flatMap - - def filter(f: A => Boolean)(implicit traverse: Traverse[F], monadPlus: MonadPlus[F]): MonadicLoop[F, A] = macro MonadicLoop.MacroBundle.filter - - def withFilter(f: A => Boolean)(implicit traverse: Traverse[F], monadPlus: MonadPlus[F]): MonadicLoop[F, A] = macro MonadicLoop.MacroBundle.filter - - } - - /** - * An implicit view to enable `for` `yield` comprehension for a monadic value. - * - * @param v the monadic value. - * @param F0 a helper to infer types. - * @tparam FA type of the monadic value. - * @return the temporary wrapper that contains the `each` method. - */ - @inline - implicit def toMonadicLoopOpsUnapply[FA](v: FA)(implicit F0: Unapply[Foldable, FA]) = { - new ToMonadicLoopOps[F0.M, F0.A](F0(v)) - } - - object EachOps { - - @bundle - private[EachOps] final class MacroBundle(val c: whitebox.Context) { - - import c.universe._ - - def each: Tree = { - val q"$ops.each" = c.macroApplication - val opsName = TermName(c.freshName("ops")) - q""" - val $opsName = $ops - _root_.com.thoughtworks.sde.core.MonadicFactory.Instructions.each[ - $opsName.M, - $opsName.A - ]($opsName.underlying) - """ - } - } - - } - - /** - * The temporary wrapper that contains the `each` method. - * - * @param underlying the underlying monadic value. - * @tparam M0 the higher kinded type of the monadic value. - * @tparam A0 the element type of of the monadic value. - */ - final case class EachOps[M0[_], A0](underlying: M0[A0]) { - - type M[A] = M0[A] - - type A = A0 - - /** - * Semantically, returns the result in the monadic value. - * - * This macro must be inside a `monadic` - * or a `catchIoMonadic` block. - * - * This is not a real method, thus it will never actually execute. - * Instead, the call to this method will be transformed to a monadic expression. - * The actually result is passing as a parameter to some [[scalaz.Monad#bind]] and [[scalaz.Monad#point]] calls - * instead of as a return value. - * - * @return the result in the monadic value. - */ - def each: A = macro EachOps.MacroBundle.each - - } - - /** - * An implicit view to enable `.each` for a monadic value. - * - * @param v the monadic value. - * @param F0 a helper to infer types. - * @tparam FA type of the monadic value. - * @return the temporary wrapper that contains the `each` method. - */ - @inline - implicit def toEachOpsUnapply[FA](v: FA)(implicit F0: Unapply[Bind, FA]): EachOps[F0.M, F0.A] = new EachOps[F0.M, F0.A](F0(v)) - - /** - * An implicit view to enable `.each` for a monadic value. - * - * @param v the monadic value. - * @return the temporary wrapper that contains the `each` method. - */ - @inline - implicit def toEachOps[F[_], A](v: F[A]): EachOps[F, A] = new EachOps(v) - - @bundle - final class AnnotationBundle(context: whitebox.Context) extends Preprocessor(context) { - - import c.universe._ - - - private def macroTransform(m: Tree, annottees: Seq[Tree]): Tree = { - - val (f, tc) = c.macroApplication match { - case q"new $annotationClass[$f]()($tc).macroTransform(..$annottees)" => - (f, tc) - case q"new $annotationClass[$f]($tc).macroTransform(..$annottees)" => - (f, tc) - case q"new $annotationClass[$f]().macroTransform(..$annottees)" => - (f, q"_root_.scala.Predef.implicitly[$m[$f]]") - } - - val eachOpsName = TermName(c.freshName("eachOps")) - val toEachOpsName = TermName(c.freshName("ToEachOps")) - - replaceDefBody(annottees, { body => - q""" - _root_.com.thoughtworks.sde.core.MonadicFactory[ - $m, - $f - ].apply { - object $toEachOpsName { - import scala.language.implicitConversions - implicit def $eachOpsName[A](fa: $f[A]): _root_.com.thoughtworks.each.Monadic.EachOps[$f, A] = { - new _root_.com.thoughtworks.each.Monadic.EachOps[$f, A](fa) - } - } - import $toEachOpsName.$eachOpsName - ${(new ComprehensionTransformer).transform(body)} - }($tc) - """ - }) - } - - def throwableMonadic(annottees: Tree*): Tree = { - macroTransform(tq"_root_.com.thoughtworks.each.Monadic.MonadThrowable", annottees) - } - - def monadic(annottees: Tree*): Tree = { - macroTransform(tq"_root_.scalaz.Monad", annottees) - } - - def catchIoMonadic(annottees: Tree*): Tree = { - macroTransform(tq"_root_.scalaz.MonadCatchIO", annottees) - } - - } - - /** - * @usecase def monadic[F[_]](body: AnyRef)(implicit monad: Monad[F]): F[body.type] = ??? - * - * Captures all the result in the `body` and converts them into a `F`. - * - * Note that `body` must not contain any `try` / `catch` / `throw` expressions. - * @tparam F the higher kinded type of the monadic expression. - * @param body the imperative style expressions that will be transform to monadic style. - * @param monad the monad that executes expressions in `body`. - * @return - */ - @inline - def monadic[F[_]] = MonadicFactory[Monad, F] - - @compileTimeOnly("enable macro paradise to expand macro annotations") - final class monadic[F[_]] extends StaticAnnotation { - def macroTransform(annottees: Any*): Any = macro AnnotationBundle.monadic - } - - /** - * @usecase def catchIoMonadic[F[_]](body: AnyRef)(implicit monad: MonadCatchIO[F]): F[body.type] = ??? - * - * Captures all the result in the `body` and converts them into a `F`. - * - * Note that `body` may contain any `try` / `catch` / `throw` expressions. - * @tparam F the higher kinded type of the monadic expression. - * @param body the imperative style expressions that will be transform to monadic style. - * @param monad the monad that executes expressions in `body`. - * @return - */ - @inline - def catchIoMonadic[F[_]] = MonadicFactory[MonadCatchIO, F] - - @compileTimeOnly("enable macro paradise to expand macro annotations") - final class catchIoMonadic[F[_]] extends StaticAnnotation { - def macroTransform(annottees: Any*): Any = macro AnnotationBundle.catchIoMonadic - } - - // TODO: create Unapply instead - @inline - implicit def eitherTMonadThrowable[F[_], G[_[_], _]](implicit F0: Monad[({type g[y] = G[F, y]})#g]): MonadThrowable[ - ({type f[x] = EitherT[({type g[y] = G[F, y]})#g, Throwable, x]})#f - ] = { - EitherT.eitherTMonadError[({type g[y] = G[F, y]})#g, Throwable] - } - - @inline - implicit def lazyEitherTMonadThrowable[F[_], G[_[_], _]](implicit F0: Monad[({type g[y] = G[F, y]})#g]): MonadThrowable[ - ({type f[x] = LazyEitherT[({type g[y] = G[F, y]})#g, Throwable, x]})#f - ] = { - LazyEitherT.lazyEitherTMonadError[({type g[y] = G[F, y]})#g, Throwable] - } - - - /** - * A [[scalaz.Monad]] that supports exception handling. - * - * Note this is a simplified version of [[scalaz.MonadError]]. - * - * @tparam F the higher kinded type of the monad. - */ - type MonadThrowable[F[_]] = MonadError[F, Throwable] - - /** - * @usecase def throwableMonadic[F[_]](body: AnyRef)(implicit monad: MonadThrowable[F]): F[body.type] = ??? - * - * Captures all the result in the `body` and converts them into a `F`. - * - * Note that `body` may contain any `try` / `catch` / `throw` expressions. - * @tparam F the higher kinded type of the monadic expression. - * @param body the imperative style expressions that will be transform to monadic style. - * @param monad the monad that executes expressions in `body`. - * @return - */ - @inline - def throwableMonadic[F[_]] = MonadicFactory[MonadThrowable, F] - - @compileTimeOnly("enable macro paradise to expand macro annotations") - final class throwableMonadic[F[_]] extends StaticAnnotation { - def macroTransform(annottees: Any*): Any = macro AnnotationBundle.throwableMonadic - } - -} diff --git a/each/src/main/scala/com/thoughtworks/each/package.scala b/each/src/main/scala/com/thoughtworks/each/package.scala deleted file mode 100644 index 24edb9b..0000000 --- a/each/src/main/scala/com/thoughtworks/each/package.scala +++ /dev/null @@ -1,13 +0,0 @@ -package com.thoughtworks - -/** - * @author 杨博 (Yang Bo) <pop.atry@gmail.com> - */ -package object each { - - /** - * Contains implicit methods to work with types that support `for`/`yield` comprehension. - */ - val ComprehensionImplicits = sde.comprehensionMonad.ComprehensionMonad - -} diff --git a/each/src/test/scala-2.11/com/thoughtworks/each/ReportingTest.scala b/each/src/test/scala-2.11/com/thoughtworks/each/ReportingTest.scala deleted file mode 100644 index 40777dc..0000000 --- a/each/src/test/scala-2.11/com/thoughtworks/each/ReportingTest.scala +++ /dev/null @@ -1,160 +0,0 @@ -package com.thoughtworks.each - -import com.thoughtworks.each.Monadic._ -import org.junit.{Assert, Test} - -import scala.concurrent.duration.Duration -import scala.concurrent.{Await, Future} -import scalaz._ -import scalaz.std.list._ -import scalaz.syntax.traverse._ - - -class ReportingTest { - - private trait AppAction[A] - - private case object GetEmailList extends AppAction[Throwable \/ List[String]] - - private case class GetContactNameByEmail(email: String) extends AppAction[Throwable \/ String] - - private type FreeCommand[A] = Free[AppAction, A] - - private type Script[A] = EitherT[FreeCommand, Throwable, A] - - private def toScript[A](action: AppAction[Throwable \/ A]): Script[A] = { - new Script[A](Free.liftF(action)) - } - - import scala.language.{higherKinds, implicitConversions} - - private implicit def cast[From, To](from: Script[From])(implicit view: From => To): Script[To] = { - Monad[Script].map[From, To](from)(view) - } - - private def eachScript: Script[xml.Elem] = throwableMonadic[Script] { - val emailList = toScript(GetEmailList).each - - - { - (for { - email: String <- emailList.monadicLoop - if email.matches( """[a-z.\-_]+@[a-z.\-_]+""") - } yield { - - - - - }).toList - }
- {toScript(GetContactNameByEmail(email)).each} - - {email} -
- - - } - - private def rawScript: Script[xml.Elem] = { - toScript(GetEmailList).flatMap { emailList => - emailList.traverseM[Script, xml.Elem] { email => - toScript(GetContactNameByEmail(email)).map { name => - if (email.matches( """[^@]+@[^@]+""")) { - List( - - {name} - - - {email} - - ) - } else { - Nil - } - } - }.map { trs => - - - - {trs} -
- - - } - } - } - - @Test - def testReporting(): Unit = { - val Data = Map( - "atryyang@thoughtworks.com" -> "Yang Bo", - "invalid-mail-address" -> "N/A", - "john.smith@gmail.com" -> "John Smith" - ) - val interpreter = new (AppAction ~> scalaz.Id.Id) { - override def apply[A](fa: AppAction[A]): A = { - fa match { - case GetEmailList => { - \/-(Data.keys.toList) - } - case GetContactNameByEmail(email) =>Data.get(email) match { - case None => { - -\/(new NoSuchElementException) - } - case Some(name) => { - \/-(name) - } - } - } - } - } - - val rawHtml = - xml.Xhtml.toXhtml(xml.Utility.trim(rawScript.run.foldMap(interpreter).fold(throw _, identity))) - - val eachHtml = - xml.Xhtml.toXhtml(xml.Utility.trim(eachScript.run.foldMap(interpreter).fold(throw _, identity))) - - Assert.assertEquals(rawHtml, eachHtml) - - } - - @Test - def testAsyncReporting(): Unit = { - val Data = Map( - "atryyang@thoughtworks.com" -> "Yang Bo", - "invalid-mail-address" -> "N/A", - "john.smith@gmail.com" -> "John Smith" - ) - val interpreter = new (AppAction ~> Future) { - override def apply[A](fa: AppAction[A]): Future[A] = { - fa match { - case GetEmailList => { - Future.successful(\/-(Data.keys.toList)) - } - case GetContactNameByEmail(email) => Future.successful(Data.get(email) match { - case None => { - -\/(new NoSuchElementException) - } - case Some(name) => { - \/-(name) - } - }) - } - } - } - - import scala.concurrent.ExecutionContext.Implicits.global - import scalaz.std.scalaFuture._ - - val rawHtml = - xml.Xhtml.toXhtml(xml.Utility.trim(Await.result(rawScript.run.foldMap(interpreter), Duration.Inf).fold(throw _, identity))) - - val eachHtml = - xml.Xhtml.toXhtml(xml.Utility.trim(Await.result(eachScript.run.foldMap(interpreter), Duration.Inf).fold(throw _, identity))) - - Assert.assertEquals(rawHtml, eachHtml) - - } - -} diff --git a/each/src/test/scala-2.11/com/thoughtworks/each/TraverseComprehensionTest211.scala b/each/src/test/scala-2.11/com/thoughtworks/each/TraverseComprehensionTest211.scala deleted file mode 100644 index ae43060..0000000 --- a/each/src/test/scala-2.11/com/thoughtworks/each/TraverseComprehensionTest211.scala +++ /dev/null @@ -1,43 +0,0 @@ -package com.thoughtworks.each - -import com.thoughtworks.each.Monadic._ -import org.junit.{Assert, Test} - -import scalaz.std.list._ -import scalaz.std.option._ - -class TraverseComprehensionTest211 { - - @Test - def testFilter(): Unit = { - val n = Some(4000) - - val result = monadic[Option] { - (for { - i <- List(300, 20).monadicLoop - if i > 100 - } yield { - i + n.each - }).underlying - } - Assert.assertEquals(Some(List(4300)), result) - } - - @Test - def testComplex(): Unit = { - val n = Some(4000) - val result = monadic[Option] { - (for { - i <- List(300, 20).monadicLoop - (j, k) <- List(50000 -> "1111", 600000 -> "yyy").monadicLoop - if i > n.each - 3900 - a = i + j - } yield { - a + n.each * k.length - }).underlying - } - - Assert.assertEquals(Some(List(66300, 612300)), result) - } - -} diff --git a/each/src/test/scala/com/thoughtworks/each/AnnotationTest.scala b/each/src/test/scala/com/thoughtworks/each/AnnotationTest.scala deleted file mode 100644 index 0a9d31f..0000000 --- a/each/src/test/scala/com/thoughtworks/each/AnnotationTest.scala +++ /dev/null @@ -1,81 +0,0 @@ -/* -Copyright 2016 ThoughtWorks, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package com.thoughtworks.each - -import com.thoughtworks.each.Monadic.monadic -import org.junit.{Assert, Test} -import scalaz.std.option._ -import scalaz.std.list._ - -/** - * @author 杨博 (Yang Bo) <pop.atry@gmail.com> - */ -class AnnotationTest { - - @Test - def testForeach(): Unit = { - val n = Some(10) - @monadic[Option] val result = { - var count = 1 - for (i <- List(300, 20)) { - count += i * n.each - } - count - } - Assert.assertEquals(Some(3201), result) - } - - @Test - def testMap(): Unit = { - val n = Some(4000) - @monadic[Option] val result = { - for (i <- List(300, 20)) yield { - i + n.each - } - } - Assert.assertEquals(Some(List(4300, 4020)), result) - } - - @Test - def testFlatMap(): Unit = { - val n = Some(4000) - @monadic[Option] val result = { - for { - i <- List(300, 20) - j <- List(50000, 600000) - } yield { - i + j + n.each - } - } - Assert.assertEquals(Some(List(54300, 604300, 54020, 604020)), result) - } - - @Test - def testFilter(): Unit = { - val n = Some(4000) - - @monadic[Option] val result = { - for { - i <- List(300, 20) - if i > 100 - } yield { - i + n.each - } - } - Assert.assertEquals(Some(List(4300)), result) - } -} diff --git a/each/src/test/scala/com/thoughtworks/each/ComprehensionImplicitsTest.scala b/each/src/test/scala/com/thoughtworks/each/ComprehensionImplicitsTest.scala deleted file mode 100644 index 816cfcd..0000000 --- a/each/src/test/scala/com/thoughtworks/each/ComprehensionImplicitsTest.scala +++ /dev/null @@ -1,80 +0,0 @@ -/* -Copyright 2015 ThoughtWorks, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package com.thoughtworks.each - -import org.junit.{Assert, Test} - -class ComprehensionImplicitsTest { - - @Test - def testListPoint(): Unit = { - val seqApplicative = ComprehensionImplicits.comprehensionMonad[List] - Assert.assertEquals(List("hello, applicative"), seqApplicative.point("hello, applicative")) - } - - @Test - def testSeqPoint(): Unit = { - val seqApplicative = ComprehensionImplicits.comprehensionMonad[Seq] - Assert.assertEquals(Seq("hello, applicative"), seqApplicative.point("hello, applicative")) - } - - @Test - def testSeqAp(): Unit = { - val seqApplicative = ComprehensionImplicits.comprehensionMonad[Seq] - Assert.assertEquals(Seq("Hello1!", "Hello2!", "Hello1?", "Hello2?"), seqApplicative.ap(Seq("Hello1", "Hello2"))(Seq((_: String) + "!", (_: String) + "?"))) - } - - @Test - def testOptionMap(): Unit = { - val optionBind = ComprehensionImplicits.comprehensionMonad[Option] - Assert.assertEquals( - Option("hello, applicative"), optionBind.map(Option("hello, ")) { a => - a + "applicative" - }) - } - - @Test - def testOptionBind(): Unit = { - val optionBind = ComprehensionImplicits.comprehensionMonad[Option] - Assert.assertEquals( - Option("hello, applicative"), - optionBind.bind(Option("hello, ")) { a => - Option(a + "applicative") - }) - } - - @Test - def testSeqMap(): Unit = { - val seqBind = ComprehensionImplicits.comprehensionMonad[Seq] - Assert.assertEquals( - Seq("hello, applicative"), seqBind.map(Seq("hello, ")) { a => - a + "applicative" - }) - } - - - @Test - def testSeqBind(): Unit = { - val seqBind = ComprehensionImplicits.comprehensionMonad[Seq] - Assert.assertEquals( - Seq("hello, applicative"), - seqBind.bind(Seq("hello, ")) { a => - Seq(a + "applicative") - }) - } - -} diff --git a/each/src/test/scala/com/thoughtworks/each/Issue38.scala b/each/src/test/scala/com/thoughtworks/each/Issue38.scala deleted file mode 100644 index 5cdf3a9..0000000 --- a/each/src/test/scala/com/thoughtworks/each/Issue38.scala +++ /dev/null @@ -1,17 +0,0 @@ -package com.thoughtworks.each - -import scalaz.std.option._ -import Monadic._ - -final class Issue38 { - def shouldNotWarning = { - monadic[Option] { - Option( - { - val mm = 1 - mm - }).each; - 1 - } - } -} \ No newline at end of file diff --git a/each/src/test/scala/com/thoughtworks/each/MonadicErrorTest.scala b/each/src/test/scala/com/thoughtworks/each/MonadicErrorTest.scala deleted file mode 100644 index 0091ac3..0000000 --- a/each/src/test/scala/com/thoughtworks/each/MonadicErrorTest.scala +++ /dev/null @@ -1,334 +0,0 @@ -/* -Copyright 2016 ThoughtWorks, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - */ - -package com.thoughtworks.each - -import java.io.IOException - -import org.junit.{Assert, Test} -import Monadic.{throwableMonadic, _} - -import scalaz._ -import scala.language.higherKinds -import scala.language.existentials -import scala.language.implicitConversions -import scalaz.std.option._ - -class MonadicErrorTest { - - @Test - def testAnnotation(): Unit = { - - case object MyException extends Exception - - type OptionScript[A] = EitherT[Option, Throwable, A] - - val either = { - var count = 0 - - import scala.language.implicitConversions - implicit def cast[From, To](from: OptionScript[From])(implicit view: From => To): OptionScript[To] = { - Monad[OptionScript].map[From, To](from)(view) - } - - @throwableMonadic[OptionScript] - val either = { - try { - count += 1 - throw MyException - count += 10 - throw new Exception("Unreachable code") - } catch { - case MyException => { - count += 100 - count - } - } finally { - count += 1000 - } - } - Assert.assertEquals(1101, count) - either - } - - Assert.assertEquals(Some(\/-(101)), either.run) - } - - @Test - def testTryCatchOption(): Unit = { - - case object MyException extends Exception - - type OptionScript[A] = EitherT[Option, Throwable, A] - - val either = { - var count = 0 - - import scala.language.implicitConversions - implicit def cast[From, To](from: OptionScript[From])(implicit view: From => To): OptionScript[To] = { - Monad[OptionScript].map[From, To](from)(view) - } - - val either = throwableMonadic[OptionScript] { - try { - count += 1 - throw MyException - count += 10 - throw new Exception("Unreachable code") - } catch { - case MyException => { - count += 100 - count - } - } finally { - count += 1000 - } - } - Assert.assertEquals(1101, count) - either - } - - Assert.assertEquals(Some(\/-(101)), either.run) - } - - private trait Command[A] - - private case object RandomInt extends Command[Throwable \/ Int] - - private case class Count(delta: Int) extends Command[Throwable \/ Int] - - private type FreeCommand[A] = Free[Command, A] - - private type Script[A] = EitherT[FreeCommand, Throwable, A] - - private val randomInt: Script[Int] = EitherT[FreeCommand, Throwable, Int](Free.liftF(RandomInt)) - - private def count(delta: Int): Script[Int] = EitherT[FreeCommand, Throwable, Int](Free.liftF(Count(delta))) - - private case object MyException extends Exception - - def noScript(randomInt: () => Int) = { - var count = 0 - count += 50000 - try { - if (randomInt() > 100) { - count += 600000 - throw MyException - count += 7000000 - 789 - } else if (randomInt() > 10) { - count += 1 - 123 - } else { - count += 20 - (throw new IOException): Int - } - } catch { - case e: IOException => { - count += 300 - 456 - } - } finally { - count += 4000 - } - } - - private def newScriptWithoutEach: Script[Int] = { - import scalaz.syntax.monadError._ - - count(50000).flatMap { _ => - implicitly[MonadThrowable[Script]].handleError { - Monad[Script].ifM( - randomInt map { - _ > 100 - }, { - count(600000).flatMap { _ => - implicitly[MonadThrowable[Script]].raiseError(MyException) flatMap { _: Nothing => - count(7000000).map { _ => - 789 - } - } - } - }, { - Monad[Script].ifM(randomInt map { - _ > 100 - }, { - count(1).map { _ => - 123 - } - }, { - count(20).flatMap { _ => - implicitly[MonadThrowable[Script]].raiseError(new IOException) map { x: Nothing => - x: Int - } - } - }) - } - ) - } { - case e: IOException => { - count(300).map { _ => - 456 - } - } - case e => { - count(4000).flatMap { _ => - implicitly[MonadThrowable[Script]].raiseError(e) - } - } - } - } - - } - - private def newScript: Script[Int] = { - throwableMonadic[Script] { - count(50000).each - try { - if (randomInt.each > 100) { - count(600000).each - throw MyException - count(7000000).each - 789 - } else if (randomInt.each > 10) { - count(1).each - 123 - } else { - count(20).each - (throw new IOException): Int - } - } catch { - case e: IOException => { - count(300).each - 456 - } - } finally { - count(4000).each - } - } - } - - @Test - def testFreeMyException(): Unit = { - val script = newScript - var count = 0 - val result: Throwable \/ Int = script.run.foldMap(new (Command ~> Id.Id) { - override def apply[A](command: Command[A]): A = { - command match { - case Count(delta) => { - count += delta - \/-(count) - } - case RandomInt => { - -\/(MyException) - } - } - } - }) - Assert.assertEquals(\/.fromTryCatchNonFatal(noScript(() => throw MyException)), result) - Assert.assertEquals(-\/(MyException), result) - Assert.assertEquals(54000, count) - } - - @Test - def testFreeIOException(): Unit = { - val script = newScript - var count = 0 - val result: Throwable \/ Int = script.run.foldMap(new (Command ~> Id.Id) { - override def apply[A](command: Command[A]): A = { - command match { - case Count(delta) => { - count += delta - \/-(count) - } - case RandomInt => { - -\/(new IOException) - } - } - } - }) - Assert.assertEquals(\/.fromTryCatchNonFatal(noScript(() => throw new IOException)), result) - Assert.assertEquals(\/-(456), result) - Assert.assertEquals(54300, count) - } - - @Test - def testFree150(): Unit = { - val script = newScript - var count = 0 - val result: Throwable \/ Int = script.run.foldMap(new (Command ~> Id.Id) { - override def apply[A](command: Command[A]): A = { - command match { - case Count(delta) => { - count += delta - \/-(count) - } - case RandomInt => { - \/-(150) - } - } - } - }) - Assert.assertEquals(\/.fromTryCatchNonFatal(noScript(() => 150)), result) - Assert.assertEquals(-\/(MyException), result) - Assert.assertEquals(654000, count) - } - - @Test - def testFree15(): Unit = { - val script = newScript - var count = 0 - val result: Throwable \/ Int = script.run.foldMap(new (Command ~> Id.Id) { - override def apply[A](command: Command[A]): A = { - command match { - case Count(delta) => { - count += delta - \/-(count) - } - case RandomInt => { - \/-(15) - } - } - } - }) - Assert.assertEquals(\/.fromTryCatchNonFatal(noScript(() => 15)), result) - Assert.assertEquals(\/-(123), result) - Assert.assertEquals(54001, count) - } - - @Test - def testFree5(): Unit = { - val script = newScript - var count = 0 - val result: Throwable \/ Int = script.run.foldMap(new (Command ~> Id.Id) { - override def apply[A](command: Command[A]): A = { - command match { - case Count(delta) => { - count += delta - \/-(count) - } - case RandomInt => { - \/-(5) - } - } - } - }) - Assert.assertEquals(\/.fromTryCatchNonFatal(noScript(() => 5)), result) - Assert.assertEquals(\/-(456), result) - Assert.assertEquals(54320, count) - } - -} diff --git a/each/src/test/scala/com/thoughtworks/each/MonadicTest.scala b/each/src/test/scala/com/thoughtworks/each/MonadicTest.scala deleted file mode 100755 index 0f001d7..0000000 --- a/each/src/test/scala/com/thoughtworks/each/MonadicTest.scala +++ /dev/null @@ -1,500 +0,0 @@ -/* -Copyright 2015 ThoughtWorks, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package com.thoughtworks.each - -import com.thoughtworks.each.ComprehensionImplicits._ -import com.thoughtworks.each.Monadic._ -import org.junit.{Assert, Test} - -import scala.concurrent.duration.Duration -import scala.concurrent.{Await, Future} -import scalaz.{IndexedStateT, Monad} - -import scalaz.effect.IO - -class MonadicTest { - - @Test - def testOption(): Unit = { - def plusOne(intOption: Option[Int]) = monadic[Option] { - intOption.each + 1 - } - Assert.assertEquals(None, plusOne(None)) - Assert.assertEquals(Some(16), plusOne(Some(15))) - } - - @Test - def testSeq(): Unit = { - def plusOne(intSeq: Seq[Int]) = monadic[Seq] { - intSeq.each + 1 - } - Assert.assertEquals(Seq.empty, plusOne(Seq.empty)) - Assert.assertEquals(Seq(16), plusOne(Seq(15))) - Assert.assertEquals(Seq(16, -1, 10), plusOne(Seq(15, -2, 9))) - } - - @Test - def testFuture(): Unit = { - import scala.concurrent.ExecutionContext.Implicits.global - val f101 = monadic[Future] { - Future(1).each + Future(100).each - } - Assert.assertEquals(101, Await.result(f101, Duration.Inf)) - } - - @Test - def testPow(): Unit = { - val pow = monadic[Seq](math.pow(2.0, (0 to 10).each)) - Assert.assertEquals(Seq(1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0, 1024.0), pow) - } - - @Test - def testMultiply(): Unit = { - val result = monadic[Seq]((0 to 3).each * (10 to 13).each) - Assert.assertEquals(Seq(0, 0, 0, 0, 10, 11, 12, 13, 20, 22, 24, 26, 30, 33, 36, 39), result) - } - - @Test - def testIoWhile(): Unit = { - - def s = IO("123") - var count = 0 - val io = monadic[IO] { - var i = 0 - while (i < 100) { - count += s.each.length - i += 1 - } - i - } - - Assert.assertEquals(100, io.unsafePerformIO()) - - Assert.assertEquals(300, count) - } - - @Test - def testWhile(): Unit = { - def s = Option("123") - var count = 0 - monadic[Option] { - val i = 100 - while (i != 100) { - count += s.each.length - } - } - Assert.assertEquals(0, count) - } - - - @Test - def testIf(): Unit = { - val ifOption = monadic[Option] { - val i = Option(1) - val j: Int = if (i.each > 1) 2 else 10 - i.each + j - } - - Assert.assertEquals(Some(11), ifOption) - } - - - @Test - def testReturn(): Unit = { - - def returnExprssions(input: Option[Int]): Option[Int] = monadic[Option] { - if (input.each < 0) { - return Some(-1) - } - if (input.each < 10) { - return Some(0) - } - input.each - } - - Assert.assertEquals(Some(-1), returnExprssions(Some(-1234))) - Assert.assertEquals(Some(0), returnExprssions(Some(5))) - Assert.assertEquals(Some(13), returnExprssions(Some(13))) - Assert.assertEquals(None, returnExprssions(None)) - } - - @Test - def testImport(): Unit = { - object A { - def currentImport = "A" - } - - object B { - def currentImport = "B" - } - - object C { - def currentImport = "C" - } - - val result = monadic[Option] { - import A._ - Assert.assertEquals("A", currentImport) - - { - import B._ - Assert.assertEquals("B", currentImport) - - { - import C._ - Assert.assertEquals("C", currentImport) - } - } - - currentImport - } - - Assert.assertEquals(Some("A"), result) - } - - @Test - def testAssignExpressions(): Unit = { - val assignExp = monadic[Option] { - var pi = 3.1415 - pi = 1.0 - pi - } - - Assert.assertEquals(Some(1.0), assignExp) - - } - - @Test - def testDefDef(): Unit = { - - val lengthOption = monadic[Option] { - def s = Option(Nil) - s.each.length - } - - Assert.assertEquals(Monad[Option].map { - def s = Option(Nil) - s - }(_.length), lengthOption) - } - - @Test - def testSomeNilLength(): Unit = { - val s = Option(Nil) - - val lengthOption = monadic[Option] { - s.each.length - } - - Assert.assertEquals(Monad[Option].map(s)(_.length), lengthOption) - - } - - @Test - def testNoneLength(): Unit = { - val s: Option[Seq[Nothing]] = None - - val lengthOption = monadic[Option] { - s.each.length - } - - Assert.assertEquals(Monad[Option].map(s)(_.length), lengthOption) - - } - - @Test - def testNewByOption(): Unit = { - val newS = monadic[Option] { - new String("a string") - } - - Assert.assertEquals(Monad[Option].pure(new String("a string")), newS) - Assert.assertEquals(Some(new String("a string")), newS) - } - - @Test - def testNewBySeq(): Unit = { - val newS = monadic[Seq] { - new String("a string") - } - - Assert.assertEquals(Monad[Seq].pure(new String("a string")), newS) - Assert.assertEquals(Seq(new String("a string")), newS) - } - - @Test - def testConcatSeq = { - - val list1 = Seq("foo", "bar", "baz") - val list2 = Seq("Hello", "World!") - - val concatSeq = monadic[Seq](list1.each.substring(0, 2) + " " + list2.each.substring(1, 4)) - - Assert.assertEquals( - for { - string1 <- list1 - string2 <- list2 - } yield (string1.substring(0, 2) + " " + string2.substring(1, 4)), - concatSeq) - Assert.assertEquals(Seq("fo ell", "fo orl", "ba ell", "ba orl", "ba ell", "ba orl"), concatSeq) - } - - @Test - def testConcatSet = { - - val list1 = Set("foo", "bar", "baz") - val list2 = Set("Hello", "World!") - - val concatSet = monadic[Set](list1.each.substring(0, 2) + " " + list2.each.substring(1, 4)) - - Assert.assertEquals( - for { - string1 <- list1 - string2 <- list2 - } yield (string1.substring(0, 2) + " " + string2.substring(1, 4)), - concatSet) - Assert.assertEquals(Set("fo ell", "fo orl", "ba ell", "ba orl", "ba ell", "ba orl"), concatSet) - } - - @Test - def testBlock(): Unit = { - var count = 0 - val io = monadic[IO] { - val _ = IO(()).each - count += 1 - count += 1 - count - } - Assert.assertEquals(0, count) - Assert.assertEquals(2, io.unsafePerformIO()) - Assert.assertEquals(2, count) - - } - - @Test - def testCatch(): Unit = { - var count = 0 - val io = catchIoMonadic[IO] { - val _ = IO(()).each - try { - count += 1 - (null: Array[Int])(0) - } catch { - case e: NullPointerException => { - count += 1 - 100 - } - } finally { - count += 1 - } - } - Assert.assertEquals(0, count) - Assert.assertEquals(100, io.unsafePerformIO()) - Assert.assertEquals(3, count) - } - - @Test - def testThrowCatch(): Unit = { - var count = 0 - val io = catchIoMonadic[IO] { - val _ = IO(()).each - try { - count += 1 - throw new Exception - } catch { - case e: Exception => { - count += 1 - 100 - } - } finally { - count += 1 - } - } - Assert.assertEquals(0, count) - Assert.assertEquals(100, io.unsafePerformIO()) - Assert.assertEquals(3, count) - } - - @Test - def testNestedClass(): Unit = { - trait Base { - def bar: Int - } - val nestedClass = monadic[Option][Base] { - class Foo() extends Base { - def bar = 100 - } - new Foo - } - - Assert.assertEquals(100, nestedClass.get.bar) - } - - @Test - def testVarIf(): Unit = { - var count = 0 - def io(initialValue: Int) = monadic[IO] { - var i = initialValue - if (i == 0) { - i = 1 - } else { - i = 2 - } - i += 10 - i - } - - Assert.assertEquals(11, io(0).unsafePerformIO()) - Assert.assertEquals(12, io(-1).unsafePerformIO()) - - val state = { - IndexedStateT.stateTMonadState[Int, IO].ifM( - IndexedStateT.stateTMonadState[Int, IO].get.map(_ == 0), - IndexedStateT.stateTMonadState[Int, IO].put(1), - IndexedStateT.stateTMonadState[Int, IO].put(2) - ).flatMap { _ => - IndexedStateT.stateTMonadState[Int, IO].get - }.flatMap { v => - IndexedStateT.stateTMonadState[Int, IO].put(v + 10) - }.flatMap { _ => - IndexedStateT.stateTMonadState[Int, IO].get - } - } - - Assert.assertEquals(state.eval(0).unsafePerformIO(), io(0).unsafePerformIO()) - Assert.assertEquals(state.eval(-1).unsafePerformIO(), io(-1).unsafePerformIO()) - } - - @Test - def testMatch(): Unit = { - - val optionHead = monadic[Option] { - (Option(Seq("foo", "bar", "baz")).each match { - case head :: tail => { - Some(head) - } - case _ => { - None - } - }).each - } - - Assert.assertEquals(Some("foo"), optionHead) - } - - @Test - def testIoDoWhile(): Unit = { - def s = IO("123") - var count = 0 - val io = monadic[IO] { - var i = 0 - do { - count += s.each.length - i += 1 - } while (i < 100) - i - } - - Assert.assertEquals(100, io.unsafePerformIO()) - - Assert.assertEquals(300, count) - } - - - @Test - def testDoWhile(): Unit = { - def s = Option("123") - var count = 0 - val option = monadic[Option] { - var i = 0 - do { - count += s.each.length - i += 1 - } while (i < 0) - i - } - - Assert.assertEquals(Some(1), option) - - Assert.assertEquals(3, count) - } - - @Test - def testThis(): Unit = { - import scala.language.existentials - val thisClass = monadic[Option] { - this.getClass.asInstanceOf[Class[MonadicTest]] - } - - Assert.assertEquals(Some(classOf[MonadicTest]), thisClass) - } - - @Test - def testTuple(): Unit = { - val result = monadic[Option] { - val (a, b, c) = Some((1, 2, 3)).each - a + b + c - } - - Assert.assertEquals(Some(6), result) - } - - @Test - def testSuper(): Unit = { - class Super { - def foo = "super" - } - - object Child extends Super { - override def foo = "child" - - val superFoo = monadic[Option] { - super.foo - } - } - - Assert.assertEquals(Some("super"), Child.superFoo) - } - - @Test - def testAnnotation(): Unit = { - val selector = Seq(1, 2, 3) - Assert.assertEquals(Some(Seq(1, 2, 3)), monadic[Option] { - (selector: @unchecked) match { - case s: Seq[String@unchecked] => { - s - } - } - }) - } - - @Test - def testXml(): Unit = { - val someFoo = Option() - val result = monadic[Option] { - - {someFoo.each} - - } - Assert.assertEquals(Some( - - - ), result) - } -} - - \ No newline at end of file diff --git a/each/src/test/scala/com/thoughtworks/each/TraverseComprehensionTest.scala b/each/src/test/scala/com/thoughtworks/each/TraverseComprehensionTest.scala deleted file mode 100644 index 5aacaa2..0000000 --- a/each/src/test/scala/com/thoughtworks/each/TraverseComprehensionTest.scala +++ /dev/null @@ -1,74 +0,0 @@ -package com.thoughtworks.each - -import org.junit.{Assert, Test} -import Monadic._ -import scalaz.std.option._ -import scalaz.std.list._ - -class TraverseComprehensionTest { - - @Test - def testAnnotationForeach(): Unit = { - import scalaz.std.iterable._ - val n = Some(10) - @monadic[Option] - val result = { - var count = 1 - for (i <- 1 to 10) { - count += i * n.each - } - count - } - Assert.assertEquals(Some(551), result) - } - - @Test - def testForeach(): Unit = { - val n = Some(10) - val result = monadic[Option] { - var count = 1 - for (i <- List(300, 20).monadicLoop) { - count += i * n.each - } - count - } - Assert.assertEquals(Some(3201), result) - } - - @Test - def testMap(): Unit = { - val n = Some(4000) - val result = monadic[Option] { - (for (i <- List(300, 20).monadicLoop) yield { - i + n.each - }).underlying - } - Assert.assertEquals(Some(List(4300, 4020)), result) - } - - - @Test - def testMapWithAnotherType(): Unit = { - val result = monadic[Option] { - (for (i <- List("foo", "bar-baz").monadicLoop) yield { - i.length - }).underlying - } - Assert.assertEquals(Some(List(3, 7)), result) - } - - @Test - def testFlatMap(): Unit = { - val n = Some(4000) - val result = monadic[Option] { - (for { - i <- List(300, 20).monadicLoop - j <- List(50000, 600000).monadicLoop - } yield { - i + j + n.each - }).underlying - } - Assert.assertEquals(Some(List(54300, 604300, 54020, 604020)), result) - } - -}