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

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

import scala.reflect.ClassTag

import js.JSConverters._

object ArrayOpsTest extends JasmineTest {

  describe("scala.scalajs.js.ArrayOps") {

    // Methods we actually implement

    it("should implement apply") {
      val array = js.Array(3,4,5,6,3,4)
      val ops: js.ArrayOps[Int] = array

      expect(ops(0)).toEqual(3)
      expect(ops(3)).toEqual(6)

      array(0) = 4
      expect(ops(0)).toEqual(4)
    }

    it("should implement update") {
      val array = js.Array(3,4,5,6,3,4)
      val ops: js.ArrayOps[Int] = array

      expect(array(1)).toEqual(4)
      ops(1) = 5
      expect(array(1)).toEqual(5)

      ops(5) = 10
      expect(array(5)).toEqual(10)
    }

    it("should implement length") {
      val array = js.Array(3,4,5,6,3,4)
      val ops: js.ArrayOps[Int] = array

      expect(ops.length).toEqual(6)
      array.push(1)
      expect(ops.length).toEqual(7)
    }

    it("should implement seq") {
      val array = js.Array(3,4,5,6,3,4)
      val ops: js.ArrayOps[Int] = array
      val seq = ops.seq

      expect(seq.toList == List(3,4,5,6,3,4)).toBeTruthy
    }

    it("should implement reduceLeft") {
      val array = js.Array(100, 6, 2, 56, -1)
      expect(array.reduceLeft(_ - _)).toEqual(37)
      expect(() => js.Array[Int]().reduceLeft(_ + _)).toThrow
    }

    it("should implement reduceRight") {
      val array = js.Array("hello", "world")
      expect(array.reduceRight(_ + ", " + _)).toEqual("hello, world")
      expect(() => js.Array[Int]().reduceRight(_ + _)).toThrow
    }

    it("should implement ++") {
      val left = js.Array("hello", "world")
      val right = js.Array("and", "everyone", "else")
      expect(left ++ right).toEqual(
          js.Array("hello", "world", "and", "everyone", "else"))

      val ints = js.Array(4, 3)
      expect(left ++ ints).toEqual(js.Array("hello", "world", 4, 3))
    }

    // Some arbitrary methods to test the builders

    it("should implement collect") {
      def ct[A : ClassTag](x: A) = implicitly[ClassTag[A]]
      val array = js.Array(3,4,5,6,3,4)
      val res = array.collect {
        case x if x > 4 => 2*x
      }

      expect(ct(res).runtimeClass == classOf[js.Array[Int]]).toBeTruthy
      expect(res).toEqual(js.Array(10, 12))
    }

    it("should implement diff") {
      val array = js.Array(1,2,1,3,1,10,9)
      val diff = array.diff(Seq(1,3,9))
      expect(diff).toEqual(js.Array(2,1,1,10))
    }

    it("should implement toList - #843") {
      val array = js.Array(1,2,1,3,1,10,9)
      val list = array.toList
      expect(list.toJSArray).toEqual(array)
    }

    it("should implement to[T] - #843") {
      val array = js.Array(1,2,1,3,1,10,9)
      val list = array.to[List]
      expect(list.toJSArray).toEqual(array)
    }

  }
}