From 6c4ea364f4a7dfe6f6804c588beb483979ea789a Mon Sep 17 00:00:00 2001 From: phaller Date: Mon, 26 Nov 2012 02:05:15 +0100 Subject: Fix #19 - Add tests where the result of an await is assigned to a variable external to the async block. - Clean up HygieneSpec. --- .../scala/scala/async/run/hygiene/Hygiene.scala | 92 ++++++++++++++++------ 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/src/test/scala/scala/async/run/hygiene/Hygiene.scala b/src/test/scala/scala/async/run/hygiene/Hygiene.scala index d0be2e0..cbe8df6 100644 --- a/src/test/scala/scala/async/run/hygiene/Hygiene.scala +++ b/src/test/scala/scala/async/run/hygiene/Hygiene.scala @@ -6,47 +6,87 @@ package scala.async package run package hygiene -import language.{reflectiveCalls, postfixOps} -import concurrent._ -import scala.concurrent.{Future, ExecutionContext, future, Await} -import scala.concurrent.duration._ -import scala.async.Async.{async, await} import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +@RunWith(classOf[JUnit4]) +class HygieneSpec { -class HygieneClass { - - import ExecutionContext.Implicits.global - - def m1(x: Int): Future[Int] = future { - x + 2 - } + import scala.async.AsyncId.{async, await} - def m2(y: Int) = { + @Test + def `is hygenic`() { val state = 23 val result: Any = "result" def resume(): Any = "resume" - async { - val f1 = m1(state) - val x = await(f1) - val y = await(future(result)) - val z = await(future(resume())) + val res = async { + val f1 = state + 2 + val x = await(f1) + val y = await(result) + val z = await(resume()) (x, y, z) } + res._1 mustBe (25) + res._2 mustBe ("result") + res._3 mustBe ("resume") } -} -@RunWith(classOf[JUnit4]) -class HygieneSpec { - - @Test def `is hygenic`() { - val o = new HygieneClass - val fut = o.m2(10) - val res = Await.result(fut, 2 seconds) +/* TODO: +[error] /Users/phaller/git/async/src/test/scala/scala/async/run/hygiene/Hygiene.scala:52: not found: value tr$1 +[error] val f1 = async { state + 2 } +[error] ^ + @Test + def `is hygenic`() { + val state = 23 + val result: Any = "result" + def resume(): Any = "resume" + val res = async { + val f1 = async { state + 2 } + val x = await(f1) + val y = await(async { result }) + val z = await(async { resume() }) + (x, y, z) + } res._1 mustBe (25) res._2 mustBe ("result") res._3 mustBe ("resume") } +*/ + + @Test + def `external var as result of await`() { + var ext = 0 + async { + ext = await(12) + } + ext mustBe (12) + } + + @Test + def `external var as result of await 2`() { + var ext = 0 + val inp = 10 + async { + if (inp > 0) + ext = await(12) + else + ext = await(10) + } + ext mustBe (12) + } + + @Test + def `external var as result of await 3`() { + var ext = 0 + val inp = 10 + async { + val x = if (inp > 0) + await(12) + else + await(10) + ext = x + await(2) + } + ext mustBe (14) + } } -- cgit v1.2.3