diff options
Diffstat (limited to 'src/test/scala')
-rw-r--r-- | src/test/scala/scala/async/TestUtils.scala | 4 | ||||
-rw-r--r-- | src/test/scala/scala/async/TreeInterrogation.scala | 3 | ||||
-rw-r--r-- | src/test/scala/scala/async/neg/NakedAwait.scala | 72 | ||||
-rw-r--r-- | src/test/scala/scala/async/run/anf/AnfTransformSpec.scala | 146 |
4 files changed, 221 insertions, 4 deletions
diff --git a/src/test/scala/scala/async/TestUtils.scala b/src/test/scala/scala/async/TestUtils.scala index bac22a3..0ae78b8 100644 --- a/src/test/scala/scala/async/TestUtils.scala +++ b/src/test/scala/scala/async/TestUtils.scala @@ -50,9 +50,9 @@ trait TestUtils { m.mkToolBox(options = compileOptions) } - def expectError(errorSnippet: String, compileOptions: String = "")(code: String) { + def expectError(errorSnippet: String, compileOptions: String = "", baseCompileOptions: String = "-cp target/scala-2.10/classes")(code: String) { intercept[ToolBoxError] { - eval(code, compileOptions) + eval(code, compileOptions + " " + baseCompileOptions) }.getMessage mustContain errorSnippet } } diff --git a/src/test/scala/scala/async/TreeInterrogation.scala b/src/test/scala/scala/async/TreeInterrogation.scala index 1293bdf..1ed9be2 100644 --- a/src/test/scala/scala/async/TreeInterrogation.scala +++ b/src/test/scala/scala/async/TreeInterrogation.scala @@ -32,7 +32,6 @@ class TreeInterrogation { val varDefs = tree1.collect { case ValDef(mods, name, _, _) if mods.hasFlag(Flag.MUTABLE) => name } - // TODO no need to lift `y` as it is only accessed from a single state. - varDefs.map(_.decoded).toSet mustBe(Set("state$async", "onCompleteHandler$async", "x$1", "z$1", "y$1")) + varDefs.map(_.decoded).toSet mustBe(Set("state$async", "onCompleteHandler$async", "x$1", "z$1")) } } diff --git a/src/test/scala/scala/async/neg/NakedAwait.scala b/src/test/scala/scala/async/neg/NakedAwait.scala index db67f18..66bc947 100644 --- a/src/test/scala/scala/async/neg/NakedAwait.scala +++ b/src/test/scala/scala/async/neg/NakedAwait.scala @@ -16,4 +16,76 @@ class NakedAwait { """.stripMargin } } + + + @Test + def `await not allowed in by-name argument`() { + expectError("await must not be used under a by-name argument.") { + """ + | import _root_.scala.async.AsyncId._ + | def foo(a: Int)(b: => Int) = 0 + | async { foo(0)(await(0)) } + """.stripMargin + } + } + + @Test + def `await not allowed in boolean short circuit argument 1`() { + expectError("await must not be used under a by-name argument.") { + """ + | import _root_.scala.async.AsyncId._ + | async { true && await(false) } + """.stripMargin + } + } + + @Test + def `await not allowed in boolean short circuit argument 2`() { + expectError("await must not be used under a by-name argument.") { + """ + | import _root_.scala.async.AsyncId._ + | async { true || await(false) } + """.stripMargin + } + } + + @Test + def nestedObject() { + expectError("await must not be used under a nested object.") { + """ + | import _root_.scala.async.AsyncId._ + | async { object Nested { await(false) } } + """.stripMargin + } + } + + @Test + def nestedTrait() { + expectError("await must not be used under a nested trait.") { + """ + | import _root_.scala.async.AsyncId._ + | async { trait Nested { await(false) } } + """.stripMargin + } + } + + @Test + def nestedClass() { + expectError("await must not be used under a nested class.") { + """ + | import _root_.scala.async.AsyncId._ + | async { class Nested { await(false) } } + """.stripMargin + } + } + + @Test + def nestedFunction() { + expectError("await must not be used under a nested anonymous function.") { + """ + | import _root_.scala.async.AsyncId._ + | async { () => { await(false) } } + """.stripMargin + } + } } diff --git a/src/test/scala/scala/async/run/anf/AnfTransformSpec.scala b/src/test/scala/scala/async/run/anf/AnfTransformSpec.scala new file mode 100644 index 0000000..0abb937 --- /dev/null +++ b/src/test/scala/scala/async/run/anf/AnfTransformSpec.scala @@ -0,0 +1,146 @@ +/** + * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com> + */ + +package scala.async +package run +package anf + +import language.{reflectiveCalls, postfixOps} +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 + + +class AnfTestClass { + + import ExecutionContext.Implicits.global + + def base(x: Int): Future[Int] = future { + x + 2 + } + + def m(y: Int): Future[Int] = async { + val f = base(y) + await(f) + } + + def m2(y: Int): Future[Int] = async { + val f = base(y) + val f2 = base(y + 1) + await(f) + await(f2) + } + + def m3(y: Int): Future[Int] = async { + val f = base(y) + var z = 0 + if (y > 0) { + z = await(f) + 2 + } else { + z = await(f) - 2 + } + z + } + + def m4(y: Int): Future[Int] = async { + val f = base(y) + val z = if (y > 0) { + await(f) + 2 + } else { + await(f) - 2 + } + z + 1 + } + + def futureUnitIfElse(y: Int): Future[Unit] = async { + val f = base(y) + if (y > 0) { + State.result = await(f) + 2 + } else { + State.result = await(f) - 2 + } + } +} + +object State { + @volatile var result: Int = 0 +} + +@RunWith(classOf[JUnit4]) +class AnfTransformSpec { + + @Test + def `simple ANF transform`() { + val o = new AnfTestClass + val fut = o.m(10) + val res = Await.result(fut, 2 seconds) + res mustBe (12) + } + + @Test + def `simple ANF transform 2`() { + val o = new AnfTestClass + val fut = o.m2(10) + val res = Await.result(fut, 2 seconds) + res mustBe (25) + } + + @Test + def `simple ANF transform 3`() { + val o = new AnfTestClass + val fut = o.m3(10) + val res = Await.result(fut, 2 seconds) + res mustBe (14) + } + + @Test + def `ANF transform of assigning the result of an if-else`() { + val o = new AnfTestClass + val fut = o.m4(10) + val res = Await.result(fut, 2 seconds) + res mustBe (15) + } + + @Test + def `Unit-typed if-else in tail position`() { + val o = new AnfTestClass + val fut = o.futureUnitIfElse(10) + Await.result(fut, 2 seconds) + State.result mustBe (14) + } + + @Test + def `inlining block produces duplicate definition`() { + import scala.async.AsyncId + + AsyncId.async { + val f = 12 + val x = AsyncId.await(f) + + { + val x = 42 + println(x) + } + + x + } + } + @Test + def `inlining block in tail position produces duplicate definition`() { + import scala.async.AsyncId + + AsyncId.async { + val f = 12 + val x = AsyncId.await(f) + + { + val x = 42 // TODO should we rename the symbols when we collapse them into the same scope? + x + } + } mustBe (42) + + } +} |