From 2c9a606c3d247a81f72bce9426ecda44d1509bea Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 20 Jul 2018 13:46:22 -0700 Subject: [PATCH] Port `Map` tests to `wasm` --- crates/js-sys/src/lib.rs | 2 +- crates/js-sys/tests/all/Map.rs | 258 ------------------------------- crates/js-sys/tests/all/main.rs | 1 - crates/js-sys/tests/wasm/Map.rs | 88 +++++++++++ crates/js-sys/tests/wasm/main.rs | 1 + 5 files changed, 90 insertions(+), 260 deletions(-) delete mode 100644 crates/js-sys/tests/all/Map.rs create mode 100644 crates/js-sys/tests/wasm/Map.rs diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 84f90ebe..10abef37 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -869,7 +869,7 @@ extern { /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete #[wasm_bindgen(method)] - pub fn delete(this: &Map, key: &str) -> bool; + pub fn delete(this: &Map, key: &JsValue) -> bool; /// The forEach() method executes a provided function once per each /// key/value pair in the Map object, in insertion order. diff --git a/crates/js-sys/tests/all/Map.rs b/crates/js-sys/tests/all/Map.rs deleted file mode 100644 index 3b871306..00000000 --- a/crates/js-sys/tests/all/Map.rs +++ /dev/null @@ -1,258 +0,0 @@ -#![allow(non_snake_case)] - -use project; - -#[test] -fn clear() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn map_clear(this: &js_sys::Map) { - this.clear(); - } - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const map = new Map(); - map.set('foo', 'bar'); - map.set('bar', 'baz'); - assert.equal(map.size, 2); - wasm.map_clear(map); - assert.equal(map.size, 0); - - } - "#) - .test() -} - -#[test] -fn delete() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn map_delete(this: &js_sys::Map, key: &str) -> bool { - this.delete(key) - } - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const map = new Map(); - map.set('foo', 'bar'); - assert.equal(map.size, 1); - assert.equal(wasm.map_delete(map, 'foo'), true); - assert.equal(wasm.map_delete(map, 'bar'), false); - assert.equal(map.size, 0); - - } - "#) - .test() -} - -#[test] -fn for_each() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn get_bool_vals(this: &js_sys::Map) -> js_sys::Map { - let res = js_sys::Map::new(); - this.for_each(&mut |value, key| { - if value.as_bool().is_some() { - res.set(&key, &value); - } - }); - res - } - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const map = new Map(); - map.set(1, true); - map.set(2, false); - map.set(3, "awoo"); - map.set(4, 100); - map.set(5, []); - map.set(6, {}); - - const res = wasm.get_bool_vals(map); - - assert.equal(map.size, 6); - assert.equal(res.size, 2); - assert.equal(res.get(1), true); - assert.equal(res.get(2), false); - } - "#) - .test() -} - -#[test] -fn get() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn map_get(this: &js_sys::Map, key: &JsValue) -> JsValue { - this.get(key) - } - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const map = new Map(); - map.set('foo', 'bar'); - map.set(1, 2) - assert.equal(wasm.map_get(map, 'foo'), 'bar'); - assert.equal(wasm.map_get(map, 1), 2); - assert.equal(wasm.map_get(map, 2), undefined); - } - "#) - .test() -} - -#[test] -fn has() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn has(this: &js_sys::Map, key: &JsValue) -> bool { - this.has(key) - } - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const map = new Map(); - map.set('foo', 'bar'); - assert.equal(wasm.has(map, 'foo'), true); - assert.equal(wasm.has(map, 'bar'), false); - } - "#) - .test() -} - -#[test] -fn new() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn new_map() -> js_sys::Map { - js_sys::Map::new() - } - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const map = wasm.new_map(); - - assert.equal(map.size, 0); - } - "#) - .test() -} - -#[test] -fn set() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn set(this: &js_sys::Map, key: &JsValue, value: &JsValue) -> js_sys::Map { - this.set(key, value) - } - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const map = new Map(); - const newMap = wasm.set(map, 'foo', 'bar'); - assert.equal(map.has('foo'), true); - assert.equal(newMap.has('foo'), true); - } - "#) - .test() -} - -#[test] -fn size() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn map_size(this: &js_sys::Map) -> u32 { - this.size() - } - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const map = new Map(); - map.set('foo', 'bar'); - map.set('bar', 'baz'); - assert.equal(wasm.map_size(map), 2); - } - "#) - .test() -} diff --git a/crates/js-sys/tests/all/main.rs b/crates/js-sys/tests/all/main.rs index a50ee833..c3ffcb44 100644 --- a/crates/js-sys/tests/all/main.rs +++ b/crates/js-sys/tests/all/main.rs @@ -11,7 +11,6 @@ fn project() -> project_builder::Project { // Keep these tests in alphabetical order, just like the imports in `src/js.rs`. mod ArrayIterator; -mod Map; mod MapIterator; mod Math; mod Number; diff --git a/crates/js-sys/tests/wasm/Map.rs b/crates/js-sys/tests/wasm/Map.rs new file mode 100644 index 00000000..6395b6bf --- /dev/null +++ b/crates/js-sys/tests/wasm/Map.rs @@ -0,0 +1,88 @@ +use wasm_bindgen_test::*; +use js_sys::*; + +#[wasm_bindgen_test] +fn clear() { + let map = Map::new(); + map.set(&"foo".into(), &"bar".into()); + map.set(&"bar".into(), &"baz".into()); + assert_eq!(map.size(), 2); + map.clear(); + assert_eq!(map.size(), 0); + map.clear(); + assert_eq!(map.size(), 0); +} + +#[wasm_bindgen_test] +fn delete() { + let map = Map::new(); + map.set(&"foo".into(), &"bar".into()); + assert_eq!(map.size(), 1); + assert_eq!(map.delete(&"foo".into()), true); + assert_eq!(map.delete(&"bar".into()), false); + assert_eq!(map.size(), 0); +} + +#[wasm_bindgen_test] +fn for_each() { + let map = Map::new(); + map.set(&1.into(), &true.into()); + map.set(&2.into(), &false.into()); + map.set(&3.into(), &"awoo".into()); + map.set(&4.into(), &100.into()); + map.set(&5.into(), &Array::new().into()); + map.set(&6.into(), &Object::new().into()); + + let mut res = Vec::new(); + map.for_each(&mut |value, key| { + if value.as_bool().is_some() { + res.push((key, value)); + } + }); + + assert_eq!(map.size(), 6); + assert_eq!(res.len(), 2); + assert_eq!(res[0].0, 1); + assert_eq!(res[0].1, true); + assert_eq!(res[1].0, 2); + assert_eq!(res[1].1, false); +} + +#[wasm_bindgen_test] +fn get() { + let map = Map::new(); + map.set(&"foo".into(), &"bar".into()); + map.set(&1.into(), &2.into()); + assert_eq!(map.get(&"foo".into()), "bar"); + assert_eq!(map.get(&1.into()), 2); + assert!(map.get(&2.into()).is_undefined()); +} + +#[wasm_bindgen_test] +fn has() { + let map = Map::new(); + map.set(&"foo".into(), &"bar".into()); + assert_eq!(map.has(&"foo".into()), true); + assert_eq!(map.has(&"bar".into()), false); +} + +#[wasm_bindgen_test] +fn new() { + assert_eq!(Map::new().size(), 0); +} + +#[wasm_bindgen_test] +fn set() { + let map = Map::new(); + let new = map.set(&"foo".into(), &"bar".into()); + assert_eq!(map.has(&"foo".into()), true); + assert_eq!(new.has(&"foo".into()), true); +} + +#[wasm_bindgen_test] +fn size() { + let map = Map::new(); + map.set(&"foo".into(), &"bar".into()); + map.set(&"bar".into(), &"baz".into()); + assert_eq!(map.size(), 2); +} diff --git a/crates/js-sys/tests/wasm/main.rs b/crates/js-sys/tests/wasm/main.rs index d679268f..b14ea339 100644 --- a/crates/js-sys/tests/wasm/main.rs +++ b/crates/js-sys/tests/wasm/main.rs @@ -17,3 +17,4 @@ pub mod Function; pub mod Generator; pub mod Intl; pub mod JsString; +pub mod Map;