diff options
author | phaller <philipp.haller@typesafe.com> | 2012-11-09 14:08:56 +0100 |
---|---|---|
committer | phaller <philipp.haller@typesafe.com> | 2012-11-09 14:08:56 +0100 |
commit | 87987b3619f44dba43466a908f1bee1b62c9d24a (patch) | |
tree | b1cf4ea41d6200e1d1ff31830c2f600aecd4288d /src/test | |
parent | bab1cbc10a4c15cefecda7435ffaf9be51fbe3c4 (diff) | |
download | scala-async-87987b3619f44dba43466a908f1bee1b62c9d24a.tar.gz scala-async-87987b3619f44dba43466a908f1bee1b62c9d24a.tar.bz2 scala-async-87987b3619f44dba43466a908f1bee1b62c9d24a.zip |
Reject local classes without crashing
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/scala/scala/async/neg/LocalClasses0Spec.scala | 83 | ||||
-rw-r--r-- | src/test/scala/scala/async/run/local-classes0/LocalClasses0Spec.scala | 45 |
2 files changed, 83 insertions, 45 deletions
diff --git a/src/test/scala/scala/async/neg/LocalClasses0Spec.scala b/src/test/scala/scala/async/neg/LocalClasses0Spec.scala new file mode 100644 index 0000000..4bf3ce8 --- /dev/null +++ b/src/test/scala/scala/async/neg/LocalClasses0Spec.scala @@ -0,0 +1,83 @@ +package scala.async +package neg + +/** + * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com> + */ + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.Test + +@RunWith(classOf[JUnit4]) +class LocalClasses0Spec { + + @Test + def `An async block should reject local classes 1`() { + expectError("Local class Person illegal within `async` block", "-cp target/scala-2.10/classes -deprecation -Xfatal-warnings") { + """ + | import scala.async.Async._ + | async { + | case class Person(name: String) + | } + """.stripMargin + } + } + + @Test + def `An async block should reject local classes 2`() { + expectError("Local class Person illegal within `async` block", "-cp target/scala-2.10/classes -deprecation -Xfatal-warnings") { + """ + | import scala.concurrent.{Future, ExecutionContext} + | import ExecutionContext.Implicits.global + | import scala.async.Async._ + | async { + | case class Person(name: String) + | val fut = Future { 5 } + | val x = await(fut) + | x + | } + """.stripMargin + } + } + + @Test + def `An async block should reject local classes 3`() { + expectError("Local class Person illegal within `async` block", "-cp target/scala-2.10/classes -deprecation -Xfatal-warnings") { + """ + | import scala.concurrent.{Future, ExecutionContext} + | import ExecutionContext.Implicits.global + | import scala.async.Async._ + | async { + | val fut = Future { 5 } + | val x = await(fut) + | case class Person(name: String) + | x + | } + """.stripMargin + } + } + + @Test + def `An async block should reject a nested local class`() { + expectError("Local class Person illegal within `async` block", "-cp target/scala-2.10/classes -deprecation -Xfatal-warnings") { + """ + | import scala.concurrent.{Future, ExecutionContext} + | import ExecutionContext.Implicits.global + | import scala.async.Async._ + | async { + | val fut = Future { 5 } + | val x = 2 + 2 + | var y = 0 + | if (x > 0) { + | case class Person(name: String) + | y = await(fut) + | } else { + | y = x + | } + | y + | } + """.stripMargin + } + } +} diff --git a/src/test/scala/scala/async/run/local-classes0/LocalClasses0Spec.scala b/src/test/scala/scala/async/run/local-classes0/LocalClasses0Spec.scala deleted file mode 100644 index 6b8fc4b..0000000 --- a/src/test/scala/scala/async/run/local-classes0/LocalClasses0Spec.scala +++ /dev/null @@ -1,45 +0,0 @@ -package scala.async -package run -package await0 - -/** - * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com> - */ - -import language.{reflectiveCalls, postfixOps} - -import scala.concurrent.{Future, ExecutionContext, future, Await} -import scala.concurrent.duration._ -import scala.async.Async.{async, await} -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 -import org.junit.Test - -class LocalClasses0Class { - - import ExecutionContext.Implicits.global - - def base(x: Int): Future[Int] = future { - x + 2 - } - - def methodWithLocalClass(): Future[Int] = async { - case class Person(name: String) - val fut = base(10) - val x = await(fut) - x + 1 - } -} - -@RunWith(classOf[JUnit4]) -class LocalClasses0Spec { - - @Test - def `An async method should reject local classes without crashing`() { - val o = new LocalClasses0Class - val fut = o.methodWithLocalClass() - val res = Await.result(fut, 2 seconds) - res mustBe (13) - } -} - |