Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Ritchie committed Feb 14, 2013
1 parent 7f3d1e6 commit 8e38487
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 145 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2013 Twitter 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.twitter.algebird.util

import com.twitter.algebird._
import com.twitter.util.{ Future, Return, Try }

object UtilAlgebras {
implicit val futureMonad: Monad[Future] = new Monad[Future] {
def apply[T](v: T) = Future.value(v);
def flatMap[T, U](m: Future[T])(fn: T => Future[U]) = m.flatMap(fn)
}
implicit val tryMonad: Monad[Try] = new Monad[Try] {
def apply[T](v: T) = Return(v);
def flatMap[T,U](m: Try[T])(fn: T => Try[U]) = m.flatMap(fn)
}

implicit def futureSemigroup[T:Semigroup]: Semigroup[Future[T]] = new MonadSemigroup[T, Future]
implicit def futureMonoid[T:Monoid]: Monoid[Future[T]] = new MonadMonoid[T, Future]
implicit def futureGroup[T:Group]: Group[Future[T]] = new MonadGroup[T, Future]
implicit def futureRing[T:Ring]: Ring[Future[T]] = new MonadRing[T, Future]
implicit def futureField[T:Field]: Field[Future[T]] = new MonadField[T, Future]

implicit def trySemigroup[T:Semigroup]: Semigroup[Try[T]] = new MonadSemigroup[T, Try]
implicit def tryMonoid[T:Monoid]: Monoid[Try[T]] = new MonadMonoid[T, Try]
implicit def tryGroup[T:Group]: Group[Try[T]] = new MonadGroup[T, Try]
implicit def tryRing[T:Ring]: Ring[Try[T]] = new MonadRing[T, Try]
implicit def tryField[T:Field]: Field[Try[T]] = new MonadField[T, Try]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2013 Twitter 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.twitter.algebird.util

import com.twitter.algebird.MonadLaws.monadLaws
import com.twitter.util.{ Future, Throw, Return, Try }
import org.scalacheck.{ Arbitrary, Properties }

import Arbitrary.arbitrary

object UtilAlgebraProperties extends Properties("UtilAlgebras") {
import UtilAlgebras._

def toOption[T](f: Future[T]): Option[T] =
if (f.isReturn) Some(f.get) else None

implicit def futureA[T: Arbitrary]: Arbitrary[Future[T]] =
Arbitrary {
arbitrary[T].map { l => Future.value(l) } |
Future.exception(new RuntimeException("fail!"))
}

implicit def returnA[T: Arbitrary]: Arbitrary[Try[T]] =
Arbitrary {
arbitrary[T].map { l => Return(l) } |
Throw(new RuntimeException("fail!"))
}

property("futureMonad") = monadLaws[Future, Int, String, Long] { (f1, f2) =>
toOption(f1) == toOption(f2)
}
property("tryMonad") = monadLaws[Try, Int, String, Long]()
}
13 changes: 12 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ object AlgebirdBuild extends Build {
).settings(
test := { }
).aggregate(algebirdTest,
algebirdCore)
algebirdCore,
algebirdUtil)

lazy val algebirdCore = Project(
id = "algebird-core",
Expand All @@ -89,4 +90,14 @@ object AlgebirdBuild extends Build {
"org.scala-tools.testing" %% "specs" % "1.6.9"
)
).dependsOn(algebirdCore)

lazy val algebirdUtil = Project(
id = "algebird-util",
base = file("algebird-util"),
settings = sharedSettings
).settings(
name := "algebird-util",
resolvers += "Twitter Maven" at "http://maven.twttr.com",
libraryDependencies += "com.twitter" % "util-core" % "5.3.15"
).dependsOn(algebirdCore, algebirdTest % "compile->test")
}

0 comments on commit 8e38487

Please sign in to comment.