aboutsummaryrefslogtreecommitdiff
path: root/test/files/run/if-else1/if-else1.scala
blob: 296a077eefb942bb2c445550adf4449fc01a25fc (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
/**
 * 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 }

object Test extends App {

  IfElse1Spec.check()

}

class TestIfElse1Class {
  import ExecutionContext.Implicits.global
  
  def base(x: Int): Future[Int] = future {
    Thread.sleep(1000)
    x + 2
  }
  
  def m1(y: Int): Future[Int] = async {
    val f = base(y)
    var z = 0
    if (y > 0) {
      if (y > 100)
        5
      else {
        val x1 = await(f)
        z = x1 + 2
      }
    } else {
      val x2 = await(f)
      z = x2 - 2
    }
    z
  }
  
  def m2(y: Int): Future[Int] = async {
    val f = base(y)
    var z = 0
    if (y > 0) {
      if (y < 100) {
        val x1 = await(f)
        z = x1 + 2
      }
      else
        5
    } else {
      val x2 = await(f)
      z = x2 - 2
    }
    z
  }
  
  def m3(y: Int): Future[Int] = async {
    val f = base(y)
    var z = 0
    if (y < 0) {
      val x2 = await(f)
      z = x2 - 2
    } else {
      if (y > 100)
        5
      else {
        val x1 = await(f)
        z = x1 + 2
      }
    }
    z
  }
  
  def m4(y: Int): Future[Int] = async {
    val f = base(y)
    var z = 0
    if (y < 0) {
      val x2 = await(f)
      z = x2 - 2
    } else {
      if (y < 100) {
        val x1 = await(f)
        z = x1 + 2
      } else
        5
    }
    z
  }

}


object IfElse1Spec extends MinimalScalaTest {

  "An async method" should {
    "support await in a nested if-else expression" in {
      val o = new TestIfElse1Class
      val fut = o.m1(10)
      val res = Await.result(fut, 2 seconds)
      res mustBe(14)
    }
  }

  "An async method" should {
    "support await in a nested if-else expression" in {
      val o = new TestIfElse1Class
      val fut = o.m2(10)
      val res = Await.result(fut, 2 seconds)
      res mustBe(14)
    }
  }

  "An async method" should {
    "support await in a nested if-else expression" in {
      val o = new TestIfElse1Class
      val fut = o.m3(10)
      val res = Await.result(fut, 2 seconds)
      res mustBe(14)
    }
  }

  "An async method" should {
    "support await in a nested if-else expression" in {
      val o = new TestIfElse1Class
      val fut = o.m4(10)
      val res = Await.result(fut, 2 seconds)
      res mustBe(14)
    }
  }

}