aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/scala/async/run/match0/Match0.scala
blob: 8eead712f2625b8910ea19343c26ec9562eea52c (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
/*
 * Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
 */

package scala.async
package run
package match0

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 scala.async.internal.AsyncId


class TestMatchClass {

  import ExecutionContext.Implicits.global

  def m1(x: Int): Future[Int] = future {
    x + 2
  }

  def m2(y: Int): Future[Int] = async {
    val f = m1(y)
    var z = 0
    y match {
      case 10 =>
        val x1 = await(f)
        z = x1 + 2
      case 20 =>
        val x2 = await(f)
        z = x2 - 2
    }
    z
  }

  def m3(y: Int): Future[Int] = async {
    val f = m1(y)
    var z = 0
    y match {
      case 0 =>
        val x2 = await(f)
        z = x2 - 2
      case 1 =>
        val x1 = await(f)
        z = x1 + 2
    }
    z
  }
}


class MatchSpec {

  @Test def `support await in a simple match expression`() {
    val o = new TestMatchClass
    val fut = o.m2(10) // matches first case
    val res = Await.result(fut, 2 seconds)
    res mustBe (14)
  }

  @Test def `support await in a simple match expression 2`() {
    val o = new TestMatchClass
    val fut = o.m3(1) // matches second case
    val res = Await.result(fut, 2 seconds)
    res mustBe (5)
  }

  @Test def `support await in a match expression with binds`() {
    val result = AsyncId.async {
      val x = 1
      Option(x) match {
        case op @ Some(x) =>
          assert(op == Some(1))
          x + AsyncId.await(x)
        case None => AsyncId.await(0)
      }
    }
    result mustBe (2)
  }

  @Test def `support await referring to pattern matching vals`() {
    import AsyncId.{async, await}
    val result = async {
      val x = 1
      val opt = Some("")
      await(0)
      val o @ Some(y) = opt

      {
        val o @ Some(y) = Some(".")
      }

      await(0)
      await((o, y.isEmpty))
    }
    result mustBe ((Some(""), true))
  }

  @Test def `await in scrutinee`() {
    import AsyncId.{async, await}
    val result = async {
      await(if ("".isEmpty) await(1) else ???) match {
        case x if x < 0 => ???
        case y: Int => y * await(3)
      }
    }
    result mustBe (3)
  }

  @Test def duplicateBindName() {
    import AsyncId.{async, await}
    def m4(m: Any) = async {
      m match {
        case buf: String =>
          await(0)
        case buf: Double =>
          await(2)
      }
    }
    m4("") mustBe 0
  }

  @Test def bugCastBoxedUnitToStringMatch() {
    import scala.async.internal.AsyncId.{async, await}
    def foo = async {
      val p2 = await(5)
      "foo" match {
        case p3: String =>
          p2.toString
      }
    }
    foo mustBe "5"
  }

  @Test def bugCastBoxedUnitToStringIf() {
    import scala.async.internal.AsyncId.{async, await}
    def foo = async {
      val p2 = await(5)
      if (true) p2.toString else p2.toString
    }
    foo mustBe "5"
  }
}