summaryrefslogtreecommitdiff
path: root/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/compiler/RegressionTest.scala
blob: 19cceb26f0221e5082e9e1b1e6983b95f5c16613 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package scala.scalajs.testsuite.compiler

import scala.annotation.tailrec

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

object RegressionTest extends JasmineTest {

  class Bug218Foo[T](val x: T) extends AnyVal

  describe("Scala.js compiler regression tests") {

    it("Wrong division conversion (7 / 2.0) - #18") {
      val div = 7 / 2.0
      expect(div).toEqual(3.5)
      expect(div.getClass.getName).toEqual("double")

      val mod = 7 % 2.0
      expect(mod).toEqual(1.0)
      expect(mod.getClass.getName).toEqual("double")
    }

    it("js.prim.String + js.prim.String is ambiguous - #20") {
      val a: js.prim.String = "a"
      val b: js.prim.String = "b"
      val c: js.prim.String = a + b
      expect(c).toEqual("ab")
    }

    it("Abort with some pattern match guards - #22") {
      object PatternMatchGuards {
        def go(f: Int => Int) = f(1)
        def main(): Unit = {
          go {
            case x if false => x
          }
        }
      }
      // Nothing to check
    }

    it("Bad encoding for characters spanning 2 UTF-16 chars - #23") {
      val str = "A∀\uD835\uDCAB"
      var s: String = ""
      for (c <- str) {
        val code: Int = c
        s = s + code + " "
      }
      expect(s).toEqual("65 8704 55349 56491 ")
    }

    it("String concatenation with null - #26") {
      val x: Object = null
      expect(x + "check").toEqual("nullcheck")
    }

    class Bug66A(s: String, e: Object) {
      def this(e: Object) = this("", e)
      def this(s: String) = this(s, "")
    }
    class Bug66B(s: String, e: Object) extends Bug66A(s)

    it("should emit static calls when forwarding to another constructor - #66") {
      new Bug66B("", "")
    }

    it("should not swallow Unit expressions when converting to js.prim.Undefined - #83") {
      var effectHappened = false
      def doEffect(): Unit = effectHappened = true
      def f(): js.prim.Undefined = doEffect()
      f()
      expect(effectHappened).toBeTruthy
    }

    it("should correctly call subSequence on non-string CharSequences - #55") {
      val arr: CharSequence = Array('a','b','c','d')
      val ss = arr.subSequence(2,3)
      expect(ss.length()).toEqual(1)
      expect(ss.charAt(0)).toEqual('c')
    }

    it("should correctly concat primitive values to strings - #113") {
      expect(4 + "foo").toEqual("4foo")
      expect('a' + "foo").toEqual("afoo")
    }

    it("should resolve overloads on scala.Function.apply when converting to js.Function - #125") {
      class Fct extends Function1[Int,Any] {
        def apply(n: Int) = n
      }

      val scalaFunction = new Fct
      val jsFunction: js.Any = scalaFunction
      val thisFunction: js.ThisFunction = scalaFunction
    }

    it("should correctly dispatch calls on private functions - #165") {
      class A {
        private def x = 1
        def value = x
      }
      class B extends A {
        private def x = 2
      }
      expect(new B().value).toEqual(1)
    }

    it("should correctly mangle JavaScript reserved identifiers - #153") {
      // Class name
      class break {
        // class variable
        var continue = 1
        // method name
        def switch = {
          // local name
          val default = 2
          default
        }
      }
      trait Foo {
        // static member (through mixin)
        def function = 3
      }

      val x = new break with Foo
      expect(x.continue).toEqual(1)
      expect(x.switch).toEqual(2)
      expect(x.function).toEqual(3)
    }

    it("should correctly mangle identifiers starting with a digit - #153") {
      // Class name
      class `0` {
        // class variable
        var `1` = 1
        // method name
        def `2` = {
          // local name
          val `22` = 2
          `22`
        }
      }
      trait Foo {
        // static member (through mixin)
        def `3` = 3
      }

      val x = new `0` with Foo
      expect(x.`1`).toEqual(1)
      expect(x.`2`).toEqual(2)
      expect(x.`3`).toEqual(3)
    }

    it("should reserve `eval` and `arguments` - #743") {
      val eval = 5
      expect(eval).toEqual(5)
      val arguments = "hello"
      expect(arguments).toEqual("hello")
    }

    it("should support class literals for existential value types - #218") {
      expect(scala.reflect.classTag[Bug218Foo[_]].toString).toEqual(
          "scala.scalajs.testsuite.compiler.RegressionTest$Bug218Foo")
    }

    it("should support Buffer - #268") {
      val a = scala.collection.mutable.Buffer.empty[Int]
      a.insert(0, 0)
      a.remove(0)
      for (i <- 0 to 10) {
        a.insert(a.length / 2, i)
      }
      expect(a.mkString(", ")).toEqual("1, 3, 5, 7, 9, 10, 8, 6, 4, 2, 0")
    }

    it("should not call equals when comparing with a literal null - #362") {
      class A { override def equals(x: Any) = !(this == null) }

      val x = new A
      val y = new A

      // If the null comparisons actually call equals, the following two will
      // cause infinite recursion
      expect(x == y).toBeTruthy
      expect(y == x).toBeTruthy
    }

    it("should unbox null to the zero of types - #674") {
      class Box[A] {
        var value: A = _
      }
      def zero[A]: A = new Box[A].value

      /* Note: the same shape of test for Unit does not work, but it seems to
       * be a problem in scalac because it does not work on the JVM either.
       */

      val bool = zero[Boolean]
      expect((bool: Any).isInstanceOf[Boolean]).toBeTruthy
      expect(bool == false).toBeTruthy

      val char = zero[Char]
      expect((char: Any).isInstanceOf[Char]).toBeTruthy
      expect(char == '\u0000').toBeTruthy

      val byte = zero[Byte]
      expect((byte: Any).isInstanceOf[Byte]).toBeTruthy
      expect(byte == 0.toByte).toBeTruthy

      val short = zero[Short]
      expect((short: Any).isInstanceOf[Short]).toBeTruthy
      expect(short == 0.toShort).toBeTruthy

      val int = zero[Int]
      expect((int: Any).isInstanceOf[Int]).toBeTruthy
      expect(int == 0).toBeTruthy

      val long = zero[Long]
      expect((long: Any).isInstanceOf[Long]).toBeTruthy
      expect(long == 0L).toBeTruthy

      val float = zero[Float]
      expect((float: Any).isInstanceOf[Float]).toBeTruthy
      expect(float == 0.0f).toBeTruthy

      val double = zero[Double]
      expect((double: Any).isInstanceOf[Double]).toBeTruthy
      expect(double == 0.0).toBeTruthy

      val ref = zero[AnyRef]
      expect(ref == null).toBeTruthy
    }

    it("Param defs in tailrec methods should be considered mutable - #825") {
      @tailrec
      def foo(x: Int, y: Int): Unit = {
        if (x < y) foo(y, x)
        else {
          expect(x).toEqual(4)
          expect(y).toEqual(2)
        }
      }
      foo(2, 4)
    }

    it("null.synchronized should throw - #874") {
      expect(() => null.synchronized(5)).toThrow
    }

    it("x.synchronized should preserve side-effects of x") {
      var c = 0
      def x = { c += 1; this }
      expect(x.synchronized(5)).toEqual(5)
      expect(c).toEqual(1)
    }

    it("IR checker should allow Apply/Select on NullType and NothingType - #1123") {
      def giveMeANull(): Null = null
      expect(() => (giveMeANull(): StringBuilder).append(5)).toThrow
      expect(() => (giveMeANull(): scala.runtime.IntRef).elem).toThrow

      def giveMeANothing(): Nothing = sys.error("boom")
      expect(() => (giveMeANothing(): StringBuilder).append(5)).toThrow
      expect(() => (giveMeANothing(): scala.runtime.IntRef).elem).toThrow
    }

    it("should not put bad flags on caseaccessor export forwarders - #1191") {
      // This test used to choke patmat

      @scala.scalajs.js.annotation.JSExportAll
      case class T(one: Int, two: Int)

      val T(a, b) = T(1, 2)

      expect(a).toEqual(1)
      expect(b).toEqual(2)
    }
  }
}