diff options
Diffstat (limited to 'examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/DictionaryTest.scala')
-rw-r--r-- | examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/DictionaryTest.scala | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/DictionaryTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/DictionaryTest.scala new file mode 100644 index 0000000..8b45395 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/DictionaryTest.scala @@ -0,0 +1,79 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ 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 + +object DictionaryTest extends JasmineTest { + + describe("scala.scalajs.js.Dictionary") { + + it("should provide an equivalent of the JS delete keyword - #255") { + val obj = js.Dictionary.empty[js.Any] + obj("foo") = 42 + obj("bar") = "foobar" + + expect(obj("foo")).toEqual(42) + expect(obj("bar")).toEqual("foobar") + obj.delete("foo") + expect(obj("foo")).toBeUndefined + expect(obj.asInstanceOf[js.Object].hasOwnProperty("foo")).toBeFalsy + expect(obj("bar")).toEqual("foobar") + } + + // This doesn't work on Rhino due to lack of full strict mode support - #679 + unless("rhino"). + it("should behave as specified when deleting a non-configurable property - #461 - #679") { + val obj = js.Dictionary.empty[js.Any] + js.Object.defineProperty(obj.asInstanceOf[js.Object], "nonconfig", + js.Dynamic.literal(value = 4, writable = false).asInstanceOf[js.PropertyDescriptor]) + expect(obj("nonconfig")).toEqual(4) + expect(() => obj.delete("nonconfig")).toThrow + expect(obj("nonconfig")).toEqual(4) + } + + it("should provide `get`") { + val obj = js.Dictionary.empty[Int] + obj("hello") = 1 + + expect(obj.get("hello").isDefined).toBeTruthy + expect(obj.get("world").isDefined).toBeFalsy + } + + it("should treat delete as a statement - #907") { + val obj = js.Dictionary("a" -> "A") + obj.delete("a") + } + + it("should desugar arguments to delete statements - #908") { + val kh = js.Dynamic.literal( key = "a" ).asInstanceOf[KeyHolder] + val dict = js.Dictionary[String]("a" -> "A") + def a[T](foo: String) = dict.asInstanceOf[T] + a[js.Dictionary[String]]("foo").delete(kh.key) + } + + } + + trait KeyHolder extends js.Object { + def key: String = js.native + } + + describe("scala.scalajs.js.JSConverters.JSRichGenMap") { + + import js.JSConverters._ + + it("should provide toJSDictionary") { + expect(Map("a" -> 1, "b" -> 2).toJSDictionary).toEqual( + js.Dynamic.literal(a = 1, b = 2)) + expect(Map("a" -> "foo", "b" -> "bar").toJSDictionary).toEqual( + js.Dynamic.literal(a = "foo", b = "bar")) + } + + } +} |