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
|
/* __ *\
** ________ ___ / / ___ __ ____ 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.collection.mutable
import scala.reflect.ClassTag
object WrappedDictionaryTest extends JasmineTest {
describe("scala.scalajs.js.WrappedDictionary") {
// Methods we actually implement
it("should implement get") {
val map: mutable.Map[String, Any] =
js.Dictionary("a" -> "a", "b" -> 6, "e" -> js.undefined)
expect(map.get("a") == Some("a")).toBeTruthy
expect(map.get("b") == Some(6)).toBeTruthy
expect(map.get("e") == Some(())).toBeTruthy
expect(map.get("f") == None).toBeTruthy
}
it("should implement += and -=") {
val dict = js.Dictionary[String]()
val map: mutable.Map[String, String] = dict
expect(js.Object.properties(dict)).toEqual(js.Array())
map += "hello" -> "world"
expect(dict("hello")).toEqual("world")
map += "foo" -> "bar"
expect(dict("foo")).toEqual("bar")
map -= "hello"
expect(dict.get("hello").isDefined).toBeFalsy
expect(js.Object.properties(dict)).toEqual(js.Array("foo"))
}
it("should implement iterator") {
val elems = ('a' to 'e').map(_.toString).zip(1 to 5)
val dict = js.Dictionary[Int]()
val map: mutable.Map[String, Int] = dict
dict ++= elems
expect(map.iterator.toList.sorted.sameElements(elems)).toBeTruthy
}
// Some arbitrary methods to test the builders
it("should implement map") {
def ct[A : ClassTag](x: A) = implicitly[ClassTag[A]]
val dict = js.Dictionary[Int]()
dict ++= Seq("one" -> 1, "two" -> 2, "three" -> 3)
val mapChr = dict.map { case (k,v) => k(0) -> v * 2 }
val mapStr = dict.map { case (k,v) => k(0).toString -> v * 2 }
expect(ct(mapChr).runtimeClass == classOf[js.WrappedDictionary[_]]).toBeFalsy
expect(ct(mapStr).runtimeClass == classOf[js.WrappedDictionary[_]]).toBeTruthy
expect(mapChr.size).toBe(2)
expect(mapStr.size).toBe(2)
}
it("should implement withFilter") {
val dict = js.Dictionary[Int]()
val flt = dict.withFilter { case (k,v) => v > 5 || k == "a" }
def size = flt.map(x => x).size
expect(size).toBe(0)
dict += "a" -> 1
expect(size).toBe(1)
dict += "b" -> 2
expect(size).toBe(1)
dict += "c" -> 6
expect(size).toBe(2)
dict += "b" -> 7
expect(size).toBe(3)
dict -= "a"
expect(size).toBe(2)
}
it("should implement toList") {
val dict = js.Dictionary("a" -> "a", "b" -> 6, "e" -> js.undefined)
val list = dict.toList
expect(list.size).toBe(3)
}
it("should implement to[T]") {
val dict = js.Dictionary("a" -> "a", "b" -> 6, "e" -> js.undefined)
val list = dict.to[List]
expect(list.size).toBe(3)
}
}
}
|