blob: 9d1df21475fbc5b264d2acb60c4fc5f3c906cfd8 (
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
|
/*
* Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com>
*/
package scala.async
package run
package hygiene
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
@RunWith(classOf[JUnit4])
class HygieneSpec {
import scala.async.AsyncId.{async, await}
@Test
def `is hygenic`() {
val state = 23
val result: Any = "result"
def resume(): Any = "resume"
val res = async {
val f1 = state + 2
val x = await(f1)
val y = await(result)
val z = await(resume())
(x, y, z)
}
res mustBe ((25, "result", "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)
}
@Test
def `is hygenic nested`() {
val state = 23
val result: Any = "result"
def resume(): Any = "resume"
import AsyncId.{await, async}
val res = async {
val f1 = async { state + 2 }
val x = await(f1)
val y = await(async { result })
val z = await(async(await(async { resume() })))
(x, y, z)
}
res._1 mustBe (25)
res._2 mustBe ("result")
res._3 mustBe ("resume")
}
}
|