2018-09-26 18:26:00 +03:00
|
|
|
use js_sys::*;
|
2018-07-21 03:01:26 +03:00
|
|
|
use wasm_bindgen::prelude::*;
|
2018-08-09 05:49:06 +03:00
|
|
|
use wasm_bindgen::JsCast;
|
2018-09-26 18:26:00 +03:00
|
|
|
use wasm_bindgen_test::*;
|
2018-07-21 03:01:26 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-09-26 18:26:00 +03:00
|
|
|
extern "C" {
|
2018-07-21 03:01:26 +03:00
|
|
|
type SomeKey;
|
|
|
|
#[wasm_bindgen(method, setter, structural)]
|
|
|
|
fn set_some(this: &SomeKey, val: JsValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn some_key() -> Object {
|
|
|
|
let key = SomeKey::from(JsValue::from(Object::new()));
|
|
|
|
key.set_some("key".into());
|
|
|
|
Object::from(JsValue::from(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn new() {
|
|
|
|
assert!(JsValue::from(WeakMap::new()).is_object());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn get_and_set() {
|
|
|
|
let map = WeakMap::new();
|
|
|
|
let key = some_key();
|
2018-07-26 00:33:44 +03:00
|
|
|
map.set(&key, &"value".into());
|
2018-07-21 03:01:26 +03:00
|
|
|
assert_eq!(map.get(&key), "value");
|
|
|
|
assert_eq!(map.get(&Object::new()), JsValue::undefined());
|
|
|
|
assert_eq!(map.get(&some_key()), JsValue::undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn has() {
|
|
|
|
let map = WeakMap::new();
|
|
|
|
let key = some_key();
|
2018-07-26 00:33:44 +03:00
|
|
|
map.set(&key, &"value".into());
|
2018-07-21 03:01:26 +03:00
|
|
|
assert!(map.has(&key));
|
|
|
|
assert!(!map.has(&Object::new()));
|
|
|
|
assert!(!map.has(&some_key()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn delete() {
|
|
|
|
let map = WeakMap::new();
|
|
|
|
let key = some_key();
|
|
|
|
assert!(!map.has(&key));
|
2018-07-26 00:33:44 +03:00
|
|
|
map.set(&key, &"value".into());
|
2018-07-21 03:01:26 +03:00
|
|
|
assert!(map.has(&key));
|
|
|
|
map.delete(&key);
|
|
|
|
assert!(!map.has(&key));
|
|
|
|
}
|
2018-08-09 05:49:06 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn weakmap_inheritance() {
|
|
|
|
let map = WeakMap::new();
|
|
|
|
assert!(map.is_instance_of::<WeakMap>());
|
|
|
|
assert!(map.is_instance_of::<Object>());
|
2018-08-17 23:09:30 +03:00
|
|
|
let _: &Object = map.as_ref();
|
2018-08-09 05:49:06 +03:00
|
|
|
}
|