wasm-bindgen/tests/all/js_globals/Map.rs

213 lines
5.5 KiB
Rust
Raw Normal View History

2018-06-28 22:51:39 +03:00
#![allow(non_snake_case)]
use project;
#[test]
fn clear() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn map_clear(this: &js::Map) {
this.clear();
}
"#)
.file("test.ts", 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()
2018-06-28 22:52:27 +03:00
}
#[test]
fn delete() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn map_delete(this: &js::Map, key: &str) -> bool {
this.delete(key)
}
"#)
.file("test.ts", 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()
2018-06-28 22:53:20 +03:00
}
#[test]
fn get() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn map_get(this: &js::Map, key: &JsValue) -> JsValue {
this.get(key)
}
"#)
.file("test.ts", 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()
2018-06-28 22:54:10 +03:00
}
#[test]
fn has() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn has(this: &js::Map, key: &JsValue) -> bool {
this.has(key)
}
"#)
.file("test.ts", 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()
2018-06-28 22:55:10 +03:00
}
#[test]
fn new() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn new_map() -> js::Map {
js::Map::new()
}
"#)
.file("test.ts", 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()
2018-06-28 22:55:55 +03:00
}
#[test]
fn set() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn set(this: &js::Map, key: &JsValue, value: &JsValue) -> js::Map {
this.set(key, value)
}
"#)
.file("test.ts", 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()
2018-06-28 22:56:49 +03:00
}
#[test]
fn size() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn map_size(this: &js::Map) -> js::Number {
this.size()
}
"#)
.file("test.ts", 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()
2018-06-28 22:51:39 +03:00
}