summaryrefslogtreecommitdiff
path: root/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/UndefOrTest.scala
blob: 28eae1562936b3ed35a9999e2a0bd2a2953be86f (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package scala.scalajs.testsuite.jsinterop

import scala.scalajs.js
import org.scalajs.jasminetest.JasmineTest

import js.annotation.JSExport

object UndefOrTest extends JasmineTest {

  def some[A](v: A): js.UndefOr[A] = v
  def none[A]: js.UndefOr[A] = js.undefined

  describe("scala.scalajs.js.UndefOr[A]") {

    it("convert A to js.UndefOr[A]") {
      val x: js.UndefOr[Int] = 42
      expect(x.isEmpty).toBeFalsy
      expect(x.isDefined).toBeTruthy
      expect(x.nonEmpty).toBeTruthy
      expect(x.get).toEqual(42)
    }

    it("convert undefined to js.UndefOr[A]") {
      val x: js.UndefOr[Int] = js.undefined
      expect(x.isEmpty).toBeTruthy
      expect(x.isDefined).toBeFalsy
      expect(x.nonEmpty).toBeFalsy
      expect(() => x.get).toThrow
    }

    it("convert to js.Any when A <% js.Any") {
      val x: js.UndefOr[Int] = 42
      expect(x).toEqual(42)

      val y: js.UndefOr[String] = js.undefined
      expect(y).toBeUndefined
    }

    it("getOrElse") {
      expect(some("hello").getOrElse("ko")).toEqual("hello")
      expect(none[String].getOrElse("ok")).toEqual("ok")

      var defaultComputed = false
      expect(some("test") getOrElse {
        defaultComputed = true
        "ko"
      }).toEqual("test")
      expect(defaultComputed).toBeFalsy
    }

    it("orNull") {
      expect(some("hello").orNull).toEqual("hello")
      expect(none[String].orNull).toBeNull
    }

    it("map") {
      expect(some(62).map(_ / 3)).toEqual(62 / 3)
      expect(none[Int].map(_ / 3)).toBeUndefined
    }

    it("fold") {
      expect(some(3).fold(10)(_ * 2)).toEqual(6)
      expect(none[Int].fold(10)(_ * 2)).toEqual(10)
    }

    it("flatMap") {
      def f(x: Int): js.UndefOr[Int] = if (x > 0) x+3 else js.undefined
      expect(some(6).flatMap(f)).toEqual(9)
      expect(some(-6).flatMap(f)).toBeUndefined
      expect(none[Int].flatMap(f)).toBeUndefined
    }

    it("flatten") {
      expect(some(some(7)).flatten.isDefined).toBeTruthy
      expect(some(some(7)).flatten.get).toEqual(7)
      expect(some(none[Int]).flatten.isDefined).toBeFalsy
      expect(none[js.UndefOr[Int]].flatten.isDefined).toBeFalsy
    }

    it("filter") {
      expect(some(7).filter(_ > 0).isDefined).toBeTruthy
      expect(some(7).filter(_ > 0).get).toEqual(7)
      expect(some(7).filter(_ < 0).isDefined).toBeFalsy
      expect(none[Int].filter(_ < 0).isDefined).toBeFalsy
    }

    it("filterNot") {
      expect(some(7).filterNot(_ < 0).isDefined).toBeTruthy
      expect(some(7).filterNot(_ < 0).get).toEqual(7)
      expect(some(7).filterNot(_ > 0).isDefined).toBeFalsy
      expect(none[Int].filterNot(_ > 0).isDefined).toBeFalsy
    }

    it("exists") {
      expect(some(7).exists(_ > 0)).toBeTruthy
      expect(some(7).exists(_ < 0)).toBeFalsy
      expect(none[Int].exists(_ > 0)).toBeFalsy
    }

    it("forall") {
      expect(some(7).forall(_ > 0)).toBeTruthy
      expect(some(7).forall(_ < 0)).toBeFalsy
      expect(none[Int].forall(_ > 0)).toBeTruthy
    }

    it("foreach") {
      var witness1 = 3
      some(42).foreach(witness1 = _)
      expect(witness1).toEqual(42)

      var witness2 = 3
      none[Int].foreach(witness2 = _)
      expect(witness2).toEqual(3)
    }

    it("collect") {
      expect(some("hello") collect {
        case "hello" => "ok"
      }).toEqual("ok")
      expect(some("hello") collect {
        case "notthis" => "ko"
      }).toBeUndefined
      expect(none[String] collect {
        case "hello" => "ko"
      }).toBeUndefined
    }

    it("collect should call guard at most once") {
      var witness = 0
      def guard(x: String) = {
        witness += 1
        true
      }
      expect(some("hello") collect {
        case x @ "hello" if guard(x) => "ok"
      }).toEqual("ok")
      expect(witness).toEqual(1)
    }

    it("orElse") {
      expect(some(true) orElse some(false)).toBeTruthy
      expect(some("ok") orElse none).toEqual("ok")
      expect(none orElse some("yes")).toEqual("yes")
      expect(none orElse none).toBeUndefined
    }

    it("toList") {
      import scala.scalajs.js.JSConverters._

      expect(some("hello").toList.toJSArray).toEqual(js.Array("hello"))
      expect(none[String].toList.toJSArray).toEqual(js.Array())
    }

    it("toLeft and toRight") {
      expect(some("left").toLeft("right").isInstanceOf[Left[_, _]]).toBeTruthy
      expect(none[String].toLeft("right").isInstanceOf[Right[_, _]]).toBeTruthy
      expect(some("right").toRight("left").isInstanceOf[Right[_, _]]).toBeTruthy
      expect(none[String].toRight("left").isInstanceOf[Left[_, _]]).toBeTruthy
    }

    it("toOption") {
      expect(some("foo").toOption == Some("foo")).toBeTruthy
      expect(none.toOption == None).toBeTruthy
    }

  }

  describe("scala.scalajs.js.JSConverters.JSRichOption") {

    import js.JSConverters._

    it("should provide orUndefined") {
      expect(Some("asdf").orUndefined).toEqual("asdf")
      expect((None: Option[String]).orUndefined).toBeUndefined

      // This doesn't work on 2.10, since it doesn't infer
      // Nothing <:< js.Any to implicitly convert UndefOr[Nothing] to
      // js.Any
      // expect(None.orUndefined).toBeUndefined
    }

  }

}