From f10dcc76dff4aa9146cd1ec4052fe136f597a260 Mon Sep 17 00:00:00 2001 From: cchantep Date: Wed, 29 Oct 2014 17:25:01 +0100 Subject: SI-8336: add `fold` & `toEither` to `util.Try` `res.fold(fa, fb)` evaluates to `fa(e)` if `res` is a `Failure(e)`, or `fb(value)` if `res` is a `Success(value)`. (If `fb` throws an exception `e` when applied, `fa(e)`.) --- test/files/jvm/try-type-tests.scala | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'test/files/jvm') diff --git a/test/files/jvm/try-type-tests.scala b/test/files/jvm/try-type-tests.scala index 962afbd30f..b3926020f0 100644 --- a/test/files/jvm/try-type-tests.scala +++ b/test/files/jvm/try-type-tests.scala @@ -118,6 +118,44 @@ trait TryStandard { assert(f.transform(succ, fail).get == 0) } + def testSuccessEither(): Unit = { + val t = Success(1) + assert(t.toEither.isRight) + } + + def testFailureEither(): Unit = { + val t = Failure(new Exception("foo")) + assert(t.toEither.isLeft) + } + + def testFoldSuccess(): Unit = { + val t = Success(1) + val res = t.fold("Throws " + _, "Returns " + _) + assert(res == "Returns 1") + } + + def testFoldFailure(): Unit = { + val t = Failure(new Exception("foo")) + val res = t.fold("Throws " + _, "Returns " + _) + assert(res == "Throws java.lang.Exception: foo") + } + + def testFoldSuccessFailure(): Unit = { + val t = Success(1) + val res = t.fold("Throws " + _, _ => throw new Exception("foo")) + assert(res == "Throws java.lang.Exception: foo") + } + + def testFoldFailureFailure(): Unit = { + val t = Failure(new Exception("foo")) + val res = try { + t.fold(_ => throw new Exception("bar"), "Returns " + _) + } catch { + case e: Throwable => "Throws " + e + } + assert(res == "Throws java.lang.Exception: bar") + } + testForeachSuccess() testForeachFailure() testFlatMapSuccess() @@ -136,6 +174,11 @@ trait TryStandard { testFailedFailure() testSuccessTransform() testFailureTransform() + testSuccessEither() + testFailureEither() + testFoldSuccess() + testFoldFailure() + testFoldSuccessFailure() } object Test -- cgit v1.2.3