mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-03 19:01:06 +00:00
Port WeakSet
tests to wasm
This commit is contained in:
parent
1d1df09e60
commit
9f7eea1e75
@ -2293,20 +2293,20 @@ extern "C" {
|
|||||||
///
|
///
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/has
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/has
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn has(this: &WeakSet, value: Object) -> bool;
|
pub fn has(this: &WeakSet, value: &Object) -> bool;
|
||||||
|
|
||||||
/// The `add()` method appends a new object to the end of a WeakSet object.
|
/// The `add()` method appends a new object to the end of a WeakSet object.
|
||||||
///
|
///
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/add
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/add
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn add(this: &WeakSet, value: Object) -> WeakSet;
|
pub fn add(this: &WeakSet, value: &Object) -> WeakSet;
|
||||||
|
|
||||||
/// The `delete()` method removes the specified element from a WeakSet
|
/// The `delete()` method removes the specified element from a WeakSet
|
||||||
/// object.
|
/// object.
|
||||||
///
|
///
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/delete
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/delete
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn delete(this: &WeakSet, value: Object) -> bool;
|
pub fn delete(this: &WeakSet, value: &Object) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
// WebAssembly
|
// WebAssembly
|
||||||
|
@ -1,157 +0,0 @@
|
|||||||
#![allow(non_snake_case)]
|
|
||||||
|
|
||||||
use project;
|
|
||||||
|
|
||||||
#[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_weak_set() -> js_sys::WeakSet {
|
|
||||||
js_sys::WeakSet::new()
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.file(
|
|
||||||
"test.js",
|
|
||||||
r#"
|
|
||||||
import * as assert from "assert";
|
|
||||||
import * as wasm from "./out";
|
|
||||||
|
|
||||||
export function test() {
|
|
||||||
assert.equal(typeof wasm.new_weak_set(), "object");
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.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_value(this: &js_sys::WeakSet, value: js_sys::Object) -> bool {
|
|
||||||
this.has(value)
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.file(
|
|
||||||
"test.js",
|
|
||||||
r#"
|
|
||||||
import * as assert from "assert";
|
|
||||||
import * as wasm from "./out";
|
|
||||||
|
|
||||||
export function test() {
|
|
||||||
let set = new WeakSet();
|
|
||||||
let value = {some: "value"};
|
|
||||||
set.add(value);
|
|
||||||
assert.equal(wasm.has_value(set, value), true);
|
|
||||||
|
|
||||||
let nonex = {nonexistent: "value"};
|
|
||||||
assert.equal(wasm.has_value(set, nonex), false);
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.test()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn add() {
|
|
||||||
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 add_value(this: &js_sys::WeakSet, value: js_sys::Object) -> js_sys::WeakSet {
|
|
||||||
this.add(value)
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.file(
|
|
||||||
"test.js",
|
|
||||||
r#"
|
|
||||||
import * as assert from "assert";
|
|
||||||
import * as wasm from "./out";
|
|
||||||
|
|
||||||
export function test() {
|
|
||||||
let set = new WeakSet();
|
|
||||||
let value = {some: "value"};
|
|
||||||
wasm.add_value(set, value);
|
|
||||||
assert.equal(set.has(value), true);
|
|
||||||
|
|
||||||
assert.throws(() => { wasm.add_value(set, 1) }, TypeError);
|
|
||||||
assert.throws(() => { wasm.add_value(set, true) }, TypeError);
|
|
||||||
assert.throws(() => { wasm.add_value(set, "fail") }, TypeError);
|
|
||||||
assert.throws(() => { wasm.add_value(set, null) }, TypeError);
|
|
||||||
assert.throws(() => { wasm.add_value(set, undefined) }, TypeError);
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.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 delete_value(this: &js_sys::WeakSet, value: js_sys::Object) -> bool {
|
|
||||||
this.delete(value)
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.file(
|
|
||||||
"test.js",
|
|
||||||
r#"
|
|
||||||
import * as assert from "assert";
|
|
||||||
import * as wasm from "./out";
|
|
||||||
|
|
||||||
export function test() {
|
|
||||||
let set = new WeakSet();
|
|
||||||
let value = {some: "value"};
|
|
||||||
set.add(value);
|
|
||||||
assert.equal(wasm.delete_value(set, value), true);
|
|
||||||
assert.equal(set.has(value), false);
|
|
||||||
assert.equal(wasm.delete_value(set, value), false);
|
|
||||||
|
|
||||||
assert.equal(wasm.delete_value(set, 1), false);
|
|
||||||
assert.equal(wasm.delete_value(set, true), false);
|
|
||||||
assert.equal(wasm.delete_value(set, "false"), false);
|
|
||||||
assert.equal(wasm.delete_value(set, null), false);
|
|
||||||
assert.equal(wasm.delete_value(set, undefined), false);
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.test()
|
|
||||||
}
|
|
@ -12,7 +12,6 @@ fn project() -> project_builder::Project {
|
|||||||
|
|
||||||
mod ArrayIterator;
|
mod ArrayIterator;
|
||||||
mod Reflect;
|
mod Reflect;
|
||||||
mod WeakSet;
|
|
||||||
mod WebAssembly;
|
mod WebAssembly;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
42
crates/js-sys/tests/wasm/WeakSet.rs
Normal file
42
crates/js-sys/tests/wasm/WeakSet.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen_test::*;
|
||||||
|
use js_sys::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
extern {
|
||||||
|
type SomeValue;
|
||||||
|
#[wasm_bindgen(method, setter, structural)]
|
||||||
|
fn set_some(this: &SomeValue, val: JsValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn some_value() -> Object {
|
||||||
|
let value = SomeValue::from(JsValue::from(Object::new()));
|
||||||
|
value.set_some("value".into());
|
||||||
|
Object::from(JsValue::from(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn new() {
|
||||||
|
assert!(JsValue::from(WeakSet::new()).is_object())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn has() {
|
||||||
|
let set = WeakSet::new();
|
||||||
|
let value = some_value();
|
||||||
|
assert!(!set.has(&value));
|
||||||
|
set.add(&value);
|
||||||
|
assert!(set.has(&value));
|
||||||
|
assert!(!set.has(&some_value()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn delete() {
|
||||||
|
let set = WeakSet::new();
|
||||||
|
let value = some_value();
|
||||||
|
set.add(&value);
|
||||||
|
assert!(set.has(&value));
|
||||||
|
assert!(set.delete(&value));
|
||||||
|
assert!(!set.has(&value));
|
||||||
|
assert!(!set.delete(&value));
|
||||||
|
}
|
@ -28,3 +28,4 @@ pub mod SetIterator;
|
|||||||
pub mod Symbol;
|
pub mod Symbol;
|
||||||
pub mod TypedArray;
|
pub mod TypedArray;
|
||||||
pub mod WeakMap;
|
pub mod WeakMap;
|
||||||
|
pub mod WeakSet;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user