aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/scala/async/neg/NakedAwait.scala
blob: f4a10ddce8b5dbdc2cab87fc04b42f7484b29aef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2012-2014 Lightbend Inc. <http://www.lightbend.com>
 */

package scala.async
package neg

import org.junit.Test

class NakedAwait {
  @Test
  def `await only allowed in async neg`(): Unit = {
    expectError("`await` must be enclosed in an `async` block") {
      """
        | import _root_.scala.async.Async._
        | await[Any](null)
      """.stripMargin
    }
  }

  @Test
  def `await not allowed in by-name argument`(): Unit = {
    expectError("await must not be used under a by-name argument.") {
      """
        | import _root_.scala.async.internal.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`(): Unit = {
    expectError("await must not be used under a by-name argument.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { true && await(false) }
      """.stripMargin
    }
  }

  @Test
  def `await not allowed in boolean short circuit argument 2`(): Unit = {
    expectError("await must not be used under a by-name argument.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { true || await(false) }
      """.stripMargin
    }
  }

  @Test
  def nestedObject(): Unit = {
    expectError("await must not be used under a nested object.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { object Nested { await(false) } }
      """.stripMargin
    }
  }

  @Test
  def nestedTrait(): Unit = {
    expectError("await must not be used under a nested trait.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { trait Nested { await(false) } }
      """.stripMargin
    }
  }

  @Test
  def nestedClass(): Unit = {
    expectError("await must not be used under a nested class.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { class Nested { await(false) } }
      """.stripMargin
    }
  }

  @Test
  def nestedFunction(): Unit = {
    expectError("await must not be used under a nested function.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { () => { await(false) } }
      """.stripMargin
    }
  }

  @Test
  def nestedPatMatFunction(): Unit = {
    expectError("await must not be used under a nested class.") { // TODO more specific error message
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { { case x => { await(false) } } : PartialFunction[Any, Any] }
      """.stripMargin
    }
  }

  @Test
  def tryBody(): Unit = {
    expectError("await must not be used under a try/catch.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { try { await(false) } catch { case _ => } }
      """.stripMargin
    }
  }

  @Test
  def catchBody(): Unit = {
    expectError("await must not be used under a try/catch.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { try { () } catch { case _ => await(false) } }
      """.stripMargin
    }
  }

  @Test
  def finallyBody(): Unit = {
    expectError("await must not be used under a try/catch.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { try { () } finally { await(false) } }
      """.stripMargin
    }
  }

  @Test
  def guard(): Unit = {
    expectError("await must not be used under a pattern guard.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { 1 match { case _ if await(true) => } }
      """.stripMargin
    }
  }

  @Test
  def nestedMethod(): Unit = {
    expectError("await must not be used under a nested method.") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | async { def foo = await(false) }
      """.stripMargin
    }
  }

  @Test
  def returnIllegal(): Unit = {
    expectError("return is illegal") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | def foo(): Any = async { return false }
        | ()
        |
        |""".stripMargin
    }
  }

  @Test
  def lazyValIllegal(): Unit = {
    expectError("await must not be used under a lazy val initializer") {
      """
        | import _root_.scala.async.internal.AsyncId._
        | def foo(): Any = async { val x = { lazy val y = await(0); y } }
        | ()
        |
        |""".stripMargin
    }
  }
}