diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-06 04:45:39 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-06 04:45:39 -0700 |
commit | c39c7276c38f9ef66fd7054609ef33627efe5177 (patch) | |
tree | b76ae78c4dbefe5b144701fb53d9b15b806b5060 /test | |
parent | 12e1a9a5826e6b76687d65520929f41c9498777b (diff) | |
parent | ab87bbe8a5728d198057824d3a8c32cfbe20978a (diff) | |
download | scala-c39c7276c38f9ef66fd7054609ef33627efe5177.tar.gz scala-c39c7276c38f9ef66fd7054609ef33627efe5177.tar.bz2 scala-c39c7276c38f9ef66fd7054609ef33627efe5177.zip |
Merge pull request #830 from heathermiller/topic/tryeither-fixes
SI-5981, SI-5979, SI-5973, SI-5890 Closed. Maintenance to Try.
Diffstat (limited to 'test')
-rw-r--r-- | test/disabled/run/syncchannel.check (renamed from test/files/run/syncchannel.check) | 0 | ||||
-rw-r--r-- | test/disabled/run/syncchannel.scala (renamed from test/files/run/syncchannel.scala) | 0 | ||||
-rw-r--r-- | test/files/jvm/scala-concurrent-tck.scala | 156 | ||||
-rw-r--r-- | test/files/jvm/try-type-tests.scala | 250 | ||||
-rw-r--r-- | test/files/neg/t5589neg.check | 4 |
5 files changed, 330 insertions, 80 deletions
diff --git a/test/files/run/syncchannel.check b/test/disabled/run/syncchannel.check index d81cc0710e..d81cc0710e 100644 --- a/test/files/run/syncchannel.check +++ b/test/disabled/run/syncchannel.check diff --git a/test/files/run/syncchannel.scala b/test/disabled/run/syncchannel.scala index 66ae47fd0a..66ae47fd0a 100644 --- a/test/files/run/syncchannel.scala +++ b/test/disabled/run/syncchannel.scala diff --git a/test/files/jvm/scala-concurrent-tck.scala b/test/files/jvm/scala-concurrent-tck.scala index 012460147a..407027f904 100644 --- a/test/files/jvm/scala-concurrent-tck.scala +++ b/test/files/jvm/scala-concurrent-tck.scala @@ -138,7 +138,7 @@ trait FutureCallbacks extends TestBase { testOnSuccessWhenFailed() testOnFailure() testOnFailureWhenSpecialThrowable(5, new Error) - testOnFailureWhenSpecialThrowable(6, new scala.util.control.ControlThrowable { }) + // testOnFailureWhenSpecialThrowable(6, new scala.util.control.ControlThrowable { }) //TODO: this test is currently problematic, because NonFatal does not match InterruptedException //testOnFailureWhenSpecialThrowable(7, new InterruptedException) testOnFailureWhenTimeoutException() @@ -599,10 +599,10 @@ trait FutureProjections extends TestBase { throw cause } f.failed onComplete { - case Success(t) => + case Right(t) => assert(t == cause) done() - case Failure(t) => + case Left(t) => assert(false) } } @@ -624,9 +624,9 @@ trait FutureProjections extends TestBase { done => val f = future { 0 } f.failed onComplete { - case Success(t) => + case Right(t) => assert(false) - case Failure(t) => + case Left(t) => assert(t.isInstanceOf[NoSuchElementException]) done() } @@ -733,80 +733,80 @@ trait Exceptions extends TestBase { } -trait TryEitherExtractor extends TestBase { - - import scala.util.{Try, Success, Failure} - - def testSuccessMatch(): Unit = once { - done => - val thisIsASuccess = Success(42) - thisIsASuccess match { - case Success(v) => - done() - assert(v == 42) - case Failure(e) => - done() - assert(false) - case other => - done() - assert(false) - } - } - - def testRightMatch(): Unit = once { - done => - val thisIsNotASuccess: Right[Throwable, Int] = Right(43) - thisIsNotASuccess match { - case Success(v) => - done() - assert(v == 43) - case Failure(e) => - done() - assert(false) - case other => - done() - assert(false) - } - } - - def testFailureMatch(): Unit = once { - done => - val thisIsAFailure = Failure(new Exception("I'm an exception")) - thisIsAFailure match { - case Success(v) => - done() - assert(false) - case Failure(e) => - done() - assert(e.getMessage == "I'm an exception") - case other => - done() - assert(false) - } - } - - def testLeftMatch(): Unit = once { - done => - val thisIsNotAFailure: Left[Throwable, Int] = Left(new Exception("I'm an exception")) - thisIsNotAFailure match { - case Success(v) => - done() - assert(false) - case Failure(e) => - done() - assert(e.getMessage == "I'm an exception") - case other => - done() - assert(false) - } +// trait TryEitherExtractor extends TestBase { + +// import scala.util.{Try, Success, Failure} + +// def testSuccessMatch(): Unit = once { +// done => +// val thisIsASuccess = Success(42) +// thisIsASuccess match { +// case Success(v) => +// done() +// assert(v == 42) +// case Failure(e) => +// done() +// assert(false) +// case other => +// done() +// assert(false) +// } +// } + +// def testRightMatch(): Unit = once { +// done => +// val thisIsNotASuccess: Right[Throwable, Int] = Right(43) +// thisIsNotASuccess match { +// case Success(v) => +// done() +// assert(v == 43) +// case Failure(e) => +// done() +// assert(false) +// case other => +// done() +// assert(false) +// } +// } + +// def testFailureMatch(): Unit = once { +// done => +// val thisIsAFailure = Failure(new Exception("I'm an exception")) +// thisIsAFailure match { +// case Success(v) => +// done() +// assert(false) +// case Failure(e) => +// done() +// assert(e.getMessage == "I'm an exception") +// case other => +// done() +// assert(false) +// } +// } + +// def testLeftMatch(): Unit = once { +// done => +// val thisIsNotAFailure: Left[Throwable, Int] = Left(new Exception("I'm an exception")) +// thisIsNotAFailure match { +// case Success(v) => +// done() +// assert(false) +// case Failure(e) => +// done() +// assert(e.getMessage == "I'm an exception") +// case other => +// done() +// assert(false) +// } - } +// } - testSuccessMatch() - testRightMatch() - testFailureMatch() - testLeftMatch() -} +// testSuccessMatch() +// testRightMatch() +// testFailureMatch() +// testLeftMatch() +// } trait CustomExecutionContext extends TestBase { import scala.concurrent.{ ExecutionContext, Awaitable } @@ -935,7 +935,7 @@ with FutureCombinators with FutureProjections with Promises with Exceptions -with TryEitherExtractor +// with TryEitherExtractor with CustomExecutionContext { System.exit(0) diff --git a/test/files/jvm/try-type-tests.scala b/test/files/jvm/try-type-tests.scala new file mode 100644 index 0000000000..eecbb0ae57 --- /dev/null +++ b/test/files/jvm/try-type-tests.scala @@ -0,0 +1,250 @@ +import scala.util.{Try, Success, Failure} + +// tests the basic combinators on Try +trait TryStandard { + + def testForeachSuccess(): Unit = { + val t = Success(1) + var res = 0 + t.foreach(x => res = x * 10) + assert(res == 10) + } + + def testForeachFailure(): Unit = { + val t = Failure(new Exception("foo")) + t.foreach(x => assert(false)) + } + + def testFlatMapSuccess(): Unit = { + val t = Success(1) + val n = t.flatMap(x => Try(x * 10)) + assert(n.get == 10) + } + + def testFlatMapFailure(): Unit = { + val t = Failure(new Exception("foo")) + val n = t.flatMap{ x => assert(false); Try() } + } + + def testMapSuccess(): Unit = { + val t = Success(1) + val n = t.map(x => x * 10) + assert(n.get == 10) + } + + def testMapFailure(): Unit = { + val t = Failure(new Exception("foo")) + val n = t.map(x => assert(false)) + } + + def testFilterSuccessTrue(): Unit = { + val t = Success(1) + val n = t.filter(x => x > 0) + assert(n.get == 1) + } + + def testFilterSuccessFalse(): Unit = { + val t = Success(1) + val n = t.filter(x => x < 0) + n match { + case Success(v) => assert(false) + case Failure(e: NoSuchElementException) => assert(true) + } + } + + def testFilterFailure(): Unit = { + val t = Failure(new Exception("foo")) + val n = t.filter{ x => assert(false); true } + } + + def testRescueSuccess(): Unit = { + val t = Success(1) + t.rescue{ case x => assert(false); Try() } + } + + def testRescueFailure(): Unit = { + val t = Failure(new Exception("foo")) + val n = t.rescue{ case x => Try(1) } + assert(n.get == 1) + } + + def testRecoverSuccess(): Unit = { + val t = Success(1) + t.recover{ case x => assert(false); 99 } + } + + def testRecoverFailure(): Unit = { + val t = Failure(new Exception("foo")) + val n = t.recover{ case x => 1 } + assert(n.get == 1) + } + + def testFlattenSuccess(): Unit = { + val f = Failure(new Exception("foo")) + val t = Success(f) + assert(t.flatten == f) + } + + def testFailedSuccess(): Unit = { + val t = Success(1) + val n = t.failed + n match { + case Failure(e: UnsupportedOperationException) => assert(true) + case _ => assert(false) + } + } + + def testFailedFailure(): Unit = { + val t = Failure(new Exception("foo")) + val n = t.failed + n match { + case Success(e: Exception) => assert(true) + case _ => assert(false) + } + } + + testForeachSuccess() + testForeachFailure() + testFlatMapSuccess() + testFlatMapFailure() + testMapSuccess() + testMapFailure() + testFilterSuccessTrue() + testFilterSuccessFalse() + testFilterFailure() + testRescueSuccess() + testRescueFailure() + testRecoverSuccess() + testRecoverFailure() + testFlattenSuccess() + testFailedSuccess() + testFailedFailure() +} + +// tests that implicit conversions from Try to Either behave as expected +trait TryImplicitConversionTry2Either { + + def testTry2RightMap(): Unit = { + val t = Success(1) + val n = t.right.map(x => x * 100) + assert(n == Right(100)) + } + + def testTry2LeftMap(): Unit = { + val e = new Exception("foo") + val t = Failure(e) + val n = t.left.map(x => x) + assert(n == Left(e)) + } + + def testTry2FoldSuccess(): Unit = { + val t = Success(1) + val n = t.fold(x => assert(false), y => y * 200) + assert(n == 200) + } + + def testTry2FoldFailure(): Unit = { + val e = new Exception("foo") + val t = Failure(e) + val n = t.fold(x => x, y => assert(false)) + assert(n == e) + } + + def testTry2SwapSuccess(): Unit = { + val t = Success(1) + val n = t.swap + assert(n == Left(1)) + } + + def testTry2SwapFailure(): Unit = { + val e = new Exception("foo") + val t = Failure(e) + val n = t.swap + assert(n == Right(e)) + } + + // def testTry2MergeSucccess(): Unit = { + // val t: Try[Int] = Success(1) + // val n = (t: Either[Any, Any]).t.merge // connecting two implicit conversions + // assert(n == 1) + // } + + // def testTry2MergeFailure(): Unit = { + // val e = new Exception("foo") + // val t = Failure(e) + // val n = (t: Either[Any, Any]).merge // connecting two implicit conversions + // assert(n == e) + // } + + testTry2RightMap() + testTry2LeftMap() + testTry2FoldSuccess() + testTry2FoldFailure() + testTry2SwapSuccess() + testTry2SwapFailure() + // testTry2MergeSucccess() + // testTry2MergeFailure() +} + +// tests that implicit conversions from Either to Try behave as expected +trait TryImplicitConversionEither2Try { + + def testRight2FilterSuccessTrue(): Unit = { + def expectsTry[U <% Try[Int]](rght: U): Try[Int] = { + val n = rght.filter(x => x > 0) // this should be converted to a Try + n + } + val r = Right(1) + val n = expectsTry(r) + assert(n == Success(1)) + } + + def testRight2FilterSuccessFalse(): Unit = { + def expectsTry[U <% Try[Int]](rght: U): Try[Int] = { + val n = rght.filter(x => x < 0) // this should be converted to a Try + n + } + val r = Right(1) + val n = expectsTry(r) + n match { + case Failure(e: NoSuchElementException) => assert(true) + case _ => assert(false) + } + } + + def testLeft2FilterFailure(): Unit = { + def expectsTry[U <% Try[Int]](rght: U): Try[Int] = { + val n = rght.filter(x => x > 0) // this should be converted to a Try + n + } + val r = Left(new Exception("foo")) + val n = expectsTry(r) + n match { + case Failure(e: Exception) => assert(true) + case _ => assert(false) + } + } + + def testRight2GetSuccess(): Unit = { + def expectsTry[U <% Try[Int]](rght: U): Int = { + val n = rght.get // this should be converted to a Try + n + } + val r = Right(1) + val n = expectsTry(r) + assert(n == 1) + } + + testRight2FilterSuccessTrue() + testRight2FilterSuccessFalse() + testLeft2FilterFailure() + testRight2GetSuccess() +} + +object Test +extends App +with TryStandard +with TryImplicitConversionTry2Either +with TryImplicitConversionEither2Try { + System.exit(0) +}
\ No newline at end of file diff --git a/test/files/neg/t5589neg.check b/test/files/neg/t5589neg.check index b3ff16d7e4..f1dad94df3 100644 --- a/test/files/neg/t5589neg.check +++ b/test/files/neg/t5589neg.check @@ -1,4 +1,4 @@ -t5589neg.scala:2: warning: `withFilter' method does not yet exist on Either.RightProjection[Int,String], using `filter' method instead +t5589neg.scala:2: warning: `withFilter' method does not yet exist on scala.util.Either.RightProjection[Int,String], using `filter' method instead def f5(x: Either[Int, String]) = for ((y1, y2: String) <- x.right) yield ((y1, y2)) ^ t5589neg.scala:2: error: constructor cannot be instantiated to expected type; @@ -6,7 +6,7 @@ t5589neg.scala:2: error: constructor cannot be instantiated to expected type; required: String def f5(x: Either[Int, String]) = for ((y1, y2: String) <- x.right) yield ((y1, y2)) ^ -t5589neg.scala:3: warning: `withFilter' method does not yet exist on Either.RightProjection[Int,String], using `filter' method instead +t5589neg.scala:3: warning: `withFilter' method does not yet exist on scala.util.Either.RightProjection[Int,String], using `filter' method instead def f6(x: Either[Int, String]) = for ((y1, y2: Any) <- x.right) yield ((y1, y2)) ^ t5589neg.scala:3: error: constructor cannot be instantiated to expected type; |