summaryrefslogtreecommitdiff
path: root/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/compiler/InstanceTestsHijackedBoxedClassesTest.scala
blob: 1379b80f3ce7dfd1362cd64f6f90859311616443 (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
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package scala.scalajs.testsuite.compiler

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

object InstanceTestsHijackedBoxedClassesTest extends JasmineTest {

  describe("Instance tests for hijacked boxed classes") {

    it("should support isInstanceOf (positive)") {
      expect((()         : Any).isInstanceOf[Unit]   ).toBeTruthy
      expect((false      : Any).isInstanceOf[Boolean]).toBeTruthy
      expect(('a'        : Any).isInstanceOf[Char]   ).toBeTruthy
      expect((65.toByte  : Any).isInstanceOf[Byte]   ).toBeTruthy
      expect((654.toShort: Any).isInstanceOf[Short]  ).toBeTruthy
      expect((-4321      : Any).isInstanceOf[Int]    ).toBeTruthy
      expect((684321L    : Any).isInstanceOf[Long]   ).toBeTruthy
      expect((3.14f      : Any).isInstanceOf[Float]  ).toBeTruthy
      expect((3.14       : Any).isInstanceOf[Double] ).toBeTruthy
    }

    it("should support isInstanceOf (negative)") {
      expect((12345: Any).isInstanceOf[Unit]   ).toBeFalsy
      expect((12345: Any).isInstanceOf[Boolean]).toBeFalsy
      expect((12345: Any).isInstanceOf[Char]   ).toBeFalsy
      expect(('a'  : Any).isInstanceOf[Byte]   ).toBeFalsy
      expect(('b'  : Any).isInstanceOf[Short]  ).toBeFalsy
      expect(('c'  : Any).isInstanceOf[Int]    ).toBeFalsy
      expect(('d'  : Any).isInstanceOf[Long]   ).toBeFalsy
      expect(('f'  : Any).isInstanceOf[Float]  ).toBeFalsy
      expect(('g'  : Any).isInstanceOf[Double] ).toBeFalsy
    }

    it("should support asInstanceOf (positive)") {
      def swallow(x: Any): Unit = ()
      swallow((()         : Any).asInstanceOf[Unit]   )
      swallow((false      : Any).asInstanceOf[Boolean])
      swallow(('a'        : Any).asInstanceOf[Char]   )
      swallow((65.toByte  : Any).asInstanceOf[Byte]   )
      swallow((654.toShort: Any).asInstanceOf[Short]  )
      swallow((-4321      : Any).asInstanceOf[Int]    )
      swallow((684321L    : Any).asInstanceOf[Long]   )
      swallow((3.14f      : Any).asInstanceOf[Float]  )
      swallow((3.14       : Any).asInstanceOf[Double] )
    }

    when("compliant-asinstanceof").
    it("should support asInstanceOf (negative)") {
      expect(() => (12345: Any).asInstanceOf[Unit]   ).toThrow
      expect(() => (12345: Any).asInstanceOf[Boolean]).toThrow
      expect(() => (12345: Any).asInstanceOf[Char]   ).toThrow
      expect(() => ('a'  : Any).asInstanceOf[Byte]   ).toThrow
      expect(() => ('b'  : Any).asInstanceOf[Short]  ).toThrow
      expect(() => ('c'  : Any).asInstanceOf[Int]    ).toThrow
      expect(() => ('d'  : Any).asInstanceOf[Long]   ).toThrow
      expect(() => ('f'  : Any).asInstanceOf[Float]  ).toThrow
      expect(() => ('g'  : Any).asInstanceOf[Double] ).toThrow
    }

    it("should support isInstanceOf via java.lang.Class (positive)") {
      def test(x: Any, clazz: Class[_]): Unit =
        expect(clazz.isInstance(x)).toBeTruthy

      test(()         , classOf[scala.runtime.BoxedUnit])
      test(false      , classOf[java.lang.Boolean])
      test('a'        , classOf[java.lang.Character])
      test(65.toByte  , classOf[java.lang.Byte])
      test(654.toShort, classOf[java.lang.Short])
      test(-4321      , classOf[java.lang.Integer])
      test(684321L    , classOf[java.lang.Long])
      test(3.14f      , classOf[java.lang.Float])
      test(3.14       , classOf[java.lang.Double])
    }

    it("should support isInstanceOf via java.lang.Class (negative)") {
      def test(x: Any, clazz: Class[_]): Unit =
        expect(clazz.isInstance(x)).toBeFalsy

      test(12345, classOf[scala.runtime.BoxedUnit])
      test(12345, classOf[java.lang.Boolean])
      test(12345, classOf[java.lang.Character])
      test('a'  , classOf[java.lang.Byte])
      test('b'  , classOf[java.lang.Short])
      test('c'  , classOf[java.lang.Integer])
      test('d'  , classOf[java.lang.Long])
      test('e'  , classOf[java.lang.Float])
      test('f'  , classOf[java.lang.Double])
    }

  }
}