Skip to content

Instantly share code, notes, and snippets.

@phenan
Created October 19, 2017 13:12
Show Gist options
  • Save phenan/de3ac81f2a3983ee56be9b0161812b29 to your computer and use it in GitHub Desktop.
Save phenan/de3ac81f2a3983ee56be9b0161812b29 to your computer and use it in GitHub Desktop.
Tries.scala のテスト
import org.scalatest._
import scalaz._
import tries._
/**
* Created by phenan on 2017/10/19.
*/
class TriesTest extends FunSpec with DiagrammedAssertions {
describe ("Tries.apply") {
it ("returns a right disjunction when its argument expression does not throw an exception") {
assert (Tries(10) == \/-(10))
}
it ("returns a left disjunction when its argument expression throws an exception") {
assert (Tries(None.get).isLeft)
}
}
describe ("filters") {
it ("returns the same value as a receiver when the receiver is a right disjunction and it satisfies the given condition") {
assert (Succeeds(10).filters(_ % 2 == 0) == Succeeds(10))
}
it ("returns a left disjunction when the given condition is not satisfied") {
assert (Succeeds(10).filters(_ % 3 == 0).isLeft)
}
it ("returns the same value as a receiver when the receiver is a left disjunction") {
assert ((Fails(new Exception("exception")): Tries[Int]).filters(_ => true).isLeft)
}
}
describe ("catches") {
it ("can catch an exception that are thrown in Tries block") {
val lambda: PartialFunction[Throwable, Int] = { case _: NoSuchElementException => 0 }
assert (Tries[Int](None.get).catches(lambda) == 0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment