mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 04:21:21 +00:00
Move the js
module to a js_sys
crate (#512)
* Move the `js` module to a `js_sys` crate * Update js-sys tests to pass again * Update binding_to_unimplemented_apis_doesnt_break_everything Remove its dependency on the `js` module * Update metadata for js-sys * Fix the `closures` example
This commit is contained in:
23
crates/js-sys/Cargo.toml
Normal file
23
crates/js-sys/Cargo.toml
Normal file
@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "js-sys"
|
||||
version = "0.2.11"
|
||||
authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
||||
readme = "./README.md"
|
||||
categories = ["wasm"]
|
||||
repository = "https://github.com/rustwasm/wasm-bindgen/tree/master/crates/js-sys"
|
||||
homepage = "https://rustwasm.github.io/wasm-bindgen/"
|
||||
documentation = "https://docs.rs/wasm-bindgen"
|
||||
description = """
|
||||
Bindings for all JS global objects and functions in all JS environments like
|
||||
Node.js and browsers, built on `#[wasm_bindgen]` using the `wasm-bindgen` crate.
|
||||
"""
|
||||
|
||||
[lib]
|
||||
test = false
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
wasm-bindgen = { path = "../..", version = "0.2.11" }
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test-project-builder = { path = "../test-project-builder" }
|
8
crates/js-sys/README.md
Normal file
8
crates/js-sys/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
# `js-sys`
|
||||
|
||||
Raw bindings to JS global APIs for projects using `wasm-bindgen`. This crate is
|
||||
handwritten and intended to work in *all* JS environments like browsers and
|
||||
Node.js.
|
||||
|
||||
Progress for this crate can be tracked at
|
||||
[#275](https://github.com/rustwasm/wasm-bindgen/issues/275).
|
2395
crates/js-sys/src/lib.rs
Normal file
2395
crates/js-sys/src/lib.rs
Normal file
File diff suppressed because it is too large
Load Diff
1018
crates/js-sys/tests/all/Array.rs
Normal file
1018
crates/js-sys/tests/all/Array.rs
Normal file
File diff suppressed because it is too large
Load Diff
114
crates/js-sys/tests/all/ArrayBuffer.rs
Normal file
114
crates/js-sys/tests/all/ArrayBuffer.rs
Normal file
@ -0,0 +1,114 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use super::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::*;
|
||||
use js_sys::ArrayBuffer;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn new_arraybuffer() -> ArrayBuffer {
|
||||
ArrayBuffer::new(42)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(typeof wasm.new_arraybuffer(), "object");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_view() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use JsValue;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::ArrayBuffer;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn is_view(value: JsValue) -> bool {
|
||||
ArrayBuffer::is_view(value)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.is_view(new Uint8Array(42)), true);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::ArrayBuffer;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn slice(arraybuffer: &ArrayBuffer, begin: u32) -> ArrayBuffer {
|
||||
arraybuffer.slice(begin)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const arraybuffer = new ArrayBuffer(4);
|
||||
assert.equal(typeof wasm.slice(arraybuffer, 2), "object");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice_with_end() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::ArrayBuffer;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn slice_with_end(arraybuffer: &ArrayBuffer, begin: u32, end: u32) -> ArrayBuffer {
|
||||
arraybuffer.slice_with_end(begin, end)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const arraybuffer = new ArrayBuffer(4);
|
||||
assert.equal(typeof wasm.slice_with_end(arraybuffer, 1, 2), "object");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
128
crates/js-sys/tests/all/ArrayIterator.rs
Normal file
128
crates/js-sys/tests/all/ArrayIterator.rs
Normal file
@ -0,0 +1,128 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn keys() {
|
||||
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_keys(this: &js_sys::Array) -> js_sys::ArrayIterator {
|
||||
this.keys()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let numbers = [8, 5, 4, 3, 1, 2];
|
||||
let iterator = numbers.keys();
|
||||
let wasmIterator = wasm.get_keys(numbers);
|
||||
|
||||
assert.equal(iterator.toString(), wasmIterator.toString());
|
||||
assert.equal(Array.from(iterator)[0], Array.from(wasmIterator)[0]);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entries() {
|
||||
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_entries(this: &js_sys::Array) -> js_sys::ArrayIterator {
|
||||
this.entries()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let numbers = [8, 5, 4, 3, 1, 2];
|
||||
let iterator = numbers.entries();
|
||||
let wasmIterator = wasm.get_entries(numbers);
|
||||
let jsItem = iterator.next();
|
||||
let wasmItem = wasmIterator.next();
|
||||
|
||||
assert.equal(iterator.toString(), wasmIterator.toString());
|
||||
assert.equal(jsItem.value[1], wasmItem.value[1]);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values() {
|
||||
let mut project = project();
|
||||
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_values(this: &js_sys::Array) -> js_sys::ArrayIterator {
|
||||
this.values()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
if (typeof Array.prototype.values !== "function") {
|
||||
return;
|
||||
}
|
||||
|
||||
let numbers = [8, 3, 2];
|
||||
let wasmIterator = wasm.get_values(numbers);
|
||||
|
||||
assert.equal(wasmIterator.next().value, 8);
|
||||
assert.equal(wasmIterator.next().value, 3);
|
||||
assert.equal(wasmIterator.next().value, 2);
|
||||
assert.ok(wasmIterator.next().done);
|
||||
}
|
||||
"#,
|
||||
);
|
||||
|
||||
let mut headless = project.clone();
|
||||
headless.headless(true);
|
||||
|
||||
project.test();
|
||||
headless.test();
|
||||
}
|
67
crates/js-sys/tests/all/Boolean.rs
Normal file
67
crates/js-sys/tests/all/Boolean.rs
Normal file
@ -0,0 +1,67 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn new_undefined() {
|
||||
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_boolean() -> js_sys::Boolean {
|
||||
js_sys::Boolean::new(JsValue::undefined())
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.new_boolean().valueOf(), false);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_truely() {
|
||||
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_boolean() -> js_sys::Boolean {
|
||||
js_sys::Boolean::new(JsValue::from("foo"))
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.new_boolean().valueOf(), true);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
56
crates/js-sys/tests/all/DataView.rs
Normal file
56
crates/js-sys/tests/all/DataView.rs
Normal file
@ -0,0 +1,56 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use super::project;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::{ArrayBuffer, DataView};
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn test_data_view(buffer: &ArrayBuffer, offset: usize, len: usize) {
|
||||
let v = DataView::new(buffer, offset, len);
|
||||
assert_eq!(v.byte_offset(), offset);
|
||||
assert_eq!(v.byte_length(), len);
|
||||
assert_eq!(v.get_int8(0), 2);
|
||||
assert_eq!(v.get_uint8(0), 2);
|
||||
|
||||
v.set_int8(0, 42);
|
||||
assert_eq!(v.get_int8(0), 42);
|
||||
v.set_uint8(0, 255);
|
||||
assert_eq!(v.get_uint8(0), 255);
|
||||
v.set_int16(0, 32767);
|
||||
assert_eq!(v.get_int16(0), 32767);
|
||||
v.set_uint16(0, 65535);
|
||||
assert_eq!(v.get_uint16(0), 65535);
|
||||
v.set_int32(0, 123456789);
|
||||
assert_eq!(v.get_int32(0), 123456789);
|
||||
v.set_uint32(0, 3_123_456_789);
|
||||
assert_eq!(v.get_uint32(0), 3_123_456_789);
|
||||
v.set_float32(0, 100.123);
|
||||
assert_eq!(v.get_float32(0), 100.123);
|
||||
v.set_float64(0, 123456789.123456);
|
||||
assert_eq!(v.get_float64(0), 123456789.123456);
|
||||
|
||||
v.set_int8(0, 42);
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const bytes = new Int8Array(10);
|
||||
bytes[2] = 2;
|
||||
wasm.test_data_view(bytes.buffer, 2, 8);
|
||||
assert.equal(bytes[2], 42);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
1711
crates/js-sys/tests/all/Date.rs
Normal file
1711
crates/js-sys/tests/all/Date.rs
Normal file
File diff suppressed because it is too large
Load Diff
197
crates/js-sys/tests/all/Error.rs
Normal file
197
crates/js-sys/tests/all/Error.rs
Normal file
@ -0,0 +1,197 @@
|
||||
#![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::*;
|
||||
use js_sys::Error;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn new_error(message: &js_sys::JsString) -> Error {
|
||||
Error::new(message)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const message = 'any error message';
|
||||
const error = wasm.new_error(message);
|
||||
|
||||
assert.equal(error.message, message);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::Error;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn error_message(this: &Error) -> js_sys::JsString {
|
||||
this.message()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const message = 'any error message';
|
||||
const error = new Error(message);
|
||||
|
||||
assert.equal(wasm.error_message(error), message);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_message() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::Error;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn error_set_message(this: &Error, message: &js_sys::JsString) {
|
||||
this.set_message(message);
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const message = 'any error message';
|
||||
const error = new Error();
|
||||
wasm.error_set_message(error, message);
|
||||
|
||||
assert.equal(error.message, message);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn name() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::Error;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn error_name(this: &Error) -> js_sys::JsString {
|
||||
this.name()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const name = 'any error name';
|
||||
const error = new Error();
|
||||
error.name = name;
|
||||
|
||||
assert.equal(wasm.error_name(error), name);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_name() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::Error;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn error_set_name(this: &Error, name: &js_sys::JsString) {
|
||||
this.set_name(name);
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const name = 'any error name';
|
||||
const error = new Error();
|
||||
wasm.error_set_name(error, name);
|
||||
|
||||
assert.equal(error.name, name);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_string() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::Error;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn error_to_string(this: &Error) -> js_sys::JsString {
|
||||
this.to_string()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const error = new Error('error message 1');
|
||||
|
||||
assert.equal(wasm.error_to_string(error), 'Error: error message 1');
|
||||
|
||||
error.name = undefined;
|
||||
assert.equal(wasm.error_to_string(error), 'Error: error message 1');
|
||||
|
||||
error.name = 'error_name_1';
|
||||
assert.equal(wasm.error_to_string(error), 'error_name_1: error message 1');
|
||||
|
||||
error.message = undefined;
|
||||
assert.equal(wasm.error_to_string(error), 'error_name_1');
|
||||
|
||||
error.name = 'error_name_2';
|
||||
assert.equal(wasm.error_to_string(error), 'error_name_2');
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
208
crates/js-sys/tests/all/Function.rs
Normal file
208
crates/js-sys/tests/all/Function.rs
Normal file
@ -0,0 +1,208 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn apply() {
|
||||
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 apply(this: &js_sys::Function, context: &JsValue, args: &js_sys::Array) -> js_sys::Function {
|
||||
this.apply(context, args)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.apply(Math.max, {}, [1, 2, 3]), 3);
|
||||
|
||||
const arr = [1, 2];
|
||||
wasm.apply(Array.prototype.push, arr, [3]);
|
||||
assert.equal(arr[2], 3);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bind() {
|
||||
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 bind(this: &js_sys::Function, context: &JsValue) -> js_sys::Function {
|
||||
this.bind(context)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const obj = {
|
||||
a: 0,
|
||||
fn: function () {
|
||||
return this.a + 1;
|
||||
}
|
||||
}
|
||||
|
||||
const boundFn = wasm.bind(obj.fn, { a: 41 });
|
||||
assert.equal(boundFn(), 42);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn length() {
|
||||
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 fn_length(this: &js_sys::Function) -> u32 {
|
||||
this.length()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.fn_length(() => {}), 0);
|
||||
assert.equal(wasm.fn_length(a => console.log(a)), 1);
|
||||
assert.equal(wasm.fn_length((a, b) => console.log({ a, b })), 2);
|
||||
|
||||
function fn0() {}
|
||||
function fn1(a) {
|
||||
console.log(a);
|
||||
}
|
||||
function fn2(a, b) {
|
||||
console.log({ a, b });
|
||||
}
|
||||
|
||||
assert.equal(wasm.fn_length(fn0), 0);
|
||||
assert.equal(wasm.fn_length(fn1), 1);
|
||||
assert.equal(wasm.fn_length(fn2), 2);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn name() {
|
||||
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 fn_name(this: &js_sys::Function) -> js_sys::JsString {
|
||||
this.name()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
function namedFn() {}
|
||||
assert.equal(wasm.fn_name(namedFn), 'namedFn');
|
||||
|
||||
assert.equal(wasm.fn_name(namedFn.bind({})), 'bound namedFn');
|
||||
|
||||
const obj = {
|
||||
method: () => {}
|
||||
}
|
||||
assert.equal(wasm.fn_name(obj.method), 'method');
|
||||
|
||||
assert.equal(wasm.fn_name(new Function()), 'anonymous');
|
||||
|
||||
assert.equal(wasm.fn_name(() => {}), '');
|
||||
|
||||
const closure = () => {};
|
||||
assert.equal(wasm.fn_name(closure), 'closure');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_string() {
|
||||
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_source_code(this: &js_sys::Function) -> js_sys::JsString {
|
||||
this.to_string()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
function fn1(a, b) { return a + b; }
|
||||
const fn2 = a => console.log(a);
|
||||
|
||||
assert.equal(wasm.get_source_code(fn1), 'function fn1(a, b) { return a + b; }');
|
||||
assert.equal(wasm.get_source_code(fn2), 'a => console.log(a)');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
146
crates/js-sys/tests/all/Generator.rs
Normal file
146
crates/js-sys/tests/all/Generator.rs
Normal file
@ -0,0 +1,146 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn return_() {
|
||||
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 gen_return(this: &js_sys::Generator, value: &JsValue) -> JsValue {
|
||||
this.return_(value)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
function* generator() {
|
||||
yield 1;
|
||||
yield 2;
|
||||
}
|
||||
|
||||
const gen = generator();
|
||||
gen.next();
|
||||
|
||||
const res = wasm.gen_return(gen, 42);
|
||||
assert.deepEqual(res, { value: 42, done: true });
|
||||
|
||||
const next = gen.next();
|
||||
assert.deepEqual(next, { value: undefined, done: true });
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn next() {
|
||||
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 next(this: &js_sys::Generator, value: &JsValue) -> JsValue {
|
||||
this.next(value)
|
||||
.ok()
|
||||
.expect("generator throws an error")
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn next_throws_error(this: &js_sys::Generator, value: &JsValue) -> bool {
|
||||
this.next(value).is_err()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
function* generator() {
|
||||
const reply = yield '2 * 2';
|
||||
|
||||
return reply === 4;
|
||||
}
|
||||
|
||||
const gen = generator();
|
||||
|
||||
const q = wasm.next(gen, undefined);
|
||||
assert.deepEqual(q, { value: '2 * 2', done: false });
|
||||
|
||||
const a = wasm.next(gen, 4);
|
||||
assert.deepEqual(a, { value: true, done: true });
|
||||
|
||||
function* brokenGenerator() {
|
||||
throw new Error('Something went wrong');
|
||||
yield 1;
|
||||
}
|
||||
|
||||
assert.ok(wasm.next_throws_error(brokenGenerator(), undefined));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn throw() {
|
||||
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 gen_throws_error(this: &js_sys::Generator, error: &js_sys::Error) -> bool {
|
||||
this.throw(error).is_err()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
function* generator() {
|
||||
yield 1;
|
||||
yield 2;
|
||||
}
|
||||
|
||||
const gen = generator();
|
||||
gen.next();
|
||||
|
||||
assert.ok(wasm.gen_throws_error(gen, new Error('Something went wrong')));
|
||||
assert.deepEqual(gen.next(), { value: undefined, done: true });
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
34
crates/js-sys/tests/all/Intl.rs
Normal file
34
crates/js-sys/tests/all/Intl.rs
Normal file
@ -0,0 +1,34 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn get_canonical_locales() {
|
||||
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_canonical_locales(v: &JsValue) -> js_sys::Array {
|
||||
js_sys::Intl::get_canonical_locales(v)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let locales = ["EN-US", "Fr"];
|
||||
let canonical_locales = wasm.get_canonical_locales(locales);
|
||||
assert.deepStrictEqual(canonical_locales, ["en-US", "fr"]);
|
||||
|
||||
let single_locale = wasm.get_canonical_locales("EN-US");
|
||||
assert.equal(single_locale, "en-US");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
850
crates/js-sys/tests/all/JsString.rs
Normal file
850
crates/js-sys/tests/all/JsString.rs
Normal file
@ -0,0 +1,850 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn length() {
|
||||
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 string_length(this: &js_sys::JsString) -> u32 {
|
||||
this.length()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let x = 'Mozilla';
|
||||
assert.equal(wasm.string_length(x), 7);
|
||||
|
||||
let empty = '';
|
||||
assert.equal(wasm.string_length(empty), 0);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_at() {
|
||||
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 string_char_at(this: &js_sys::JsString, index: u32) -> js_sys::JsString {
|
||||
this.char_at(index)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
var anyString = 'Brave new world';
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.string_char_at(anyString, 0), "B");
|
||||
assert.equal(wasm.string_char_at(anyString, 999), "");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_code_at() {
|
||||
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 string_char_code_at(this: &js_sys::JsString, index: u32) -> f64 {
|
||||
this.char_code_at(index)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
var anyString = 'Brave new world';
|
||||
|
||||
export function test() {
|
||||
for (let i = 0; i < anyString.length; i++) {
|
||||
assert.equal(wasm.string_char_code_at(anyString, i),
|
||||
anyString.charCodeAt(i),
|
||||
`charCodeAt(${i})`);
|
||||
}
|
||||
|
||||
const outOfBounds = wasm.string_char_code_at(anyString, 999);
|
||||
assert.ok(Number.isNaN(outOfBounds));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn code_point_at() {
|
||||
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 string_code_point_at(this: &js_sys::JsString, pos: u32) -> JsValue {
|
||||
this.code_point_at(pos)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.string_code_point_at('ABC', 1), 66);
|
||||
assert.equal(wasm.string_code_point_at('\uD800\uDC00', 0), 65536);
|
||||
assert.equal(wasm.string_code_point_at('XYZ', 42), undefined);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concat() {
|
||||
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 string_concat(this: &js_sys::JsString, string_2: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.concat(string_2)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
// TODO: Implement ability to receive multiple optional arguments
|
||||
assert.equal(wasm.string_concat('Hello ', 'World'), 'Hello World');
|
||||
assert.equal(wasm.string_concat('foo', {}), 'foo[object Object]');
|
||||
assert.equal(wasm.string_concat('foo', []), 'foo');
|
||||
assert.equal(wasm.string_concat('foo', null), 'foonull');
|
||||
assert.equal(wasm.string_concat('foo', true), 'footrue');
|
||||
assert.equal(wasm.string_concat('foo', 1234), 'foo1234');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ends_with() {
|
||||
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 ends_with(this: &js_sys::JsString, search_value: &js_sys::JsString, length: i32) -> bool {
|
||||
this.ends_with(search_value, length)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let str = "To be, or not to be, that is the question.";
|
||||
let len = str.length;
|
||||
|
||||
// TODO: remove third parameter once we have optional parameters
|
||||
assert.equal(wasm.ends_with(str, "question.", len), true);
|
||||
assert.equal(wasm.ends_with(str, "to be", len), false);
|
||||
assert.equal(wasm.ends_with(str, "to be", 19), true);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn includes() {
|
||||
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 string_includes(this: &js_sys::JsString, search_value: &js_sys::JsString, position: i32) -> bool {
|
||||
this.includes(search_value, position)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let str = "Blue Whale";
|
||||
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_includes(str, 'Blue', 0), true);
|
||||
assert.equal(wasm.string_includes(str, 'Blute', 0), false);
|
||||
assert.equal(wasm.string_includes(str, 'Whale', 0), true);
|
||||
assert.equal(wasm.string_includes(str, 'Whale', 5), true);
|
||||
assert.equal(wasm.string_includes(str, 'Whale', 7), false);
|
||||
assert.equal(wasm.string_includes(str, '', 0), true);
|
||||
assert.equal(wasm.string_includes(str, '', 16), true);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn index_of() {
|
||||
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 string_index_of(this: &js_sys::JsString, search_value: &js_sys::JsString, from_index: i32) -> i32 {
|
||||
this.index_of(search_value, from_index)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let str = "Blue Whale";
|
||||
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_index_of(str, 'Blue', 0), 0);
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_index_of(str, 'Blute', 0), -1);
|
||||
assert.equal(wasm.string_index_of(str, 'Whale', 0), 5);
|
||||
assert.equal(wasm.string_index_of(str, 'Whale', 5), 5);
|
||||
assert.equal(wasm.string_index_of(str, 'Whale', 7), -1);
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_index_of(str, '', 0), 0);
|
||||
assert.equal(wasm.string_index_of(str, '', 9), 9);
|
||||
assert.equal(wasm.string_index_of(str, '', 10), 10);
|
||||
assert.equal(wasm.string_index_of(str, '', 11), 10);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn last_index_of() {
|
||||
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 string_last_index_of(this: &js_sys::JsString, search_value: &js_sys::JsString, from_index: i32) -> i32 {
|
||||
this.last_index_of(search_value, from_index)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let str = "canal";
|
||||
let len = str.length;
|
||||
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_last_index_of(str, 'a', len), 3);
|
||||
assert.equal(wasm.string_last_index_of(str, 'a', 2), 1);
|
||||
assert.equal(wasm.string_last_index_of(str, 'a', 0), -1);
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_last_index_of(str, 'x', len), -1);
|
||||
assert.equal(wasm.string_last_index_of(str, 'c', -5), 0);
|
||||
assert.equal(wasm.string_last_index_of(str, 'c', 0), 0);
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_last_index_of(str, '', len), 5);
|
||||
assert.equal(wasm.string_last_index_of(str, '', 2), 2);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize() {
|
||||
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 string_normalize(this: &js_sys::JsString, form: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.normalize(form)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let str = "\u1E9B\u0323";
|
||||
|
||||
// TODO: Handle undefined
|
||||
assert.equal(wasm.string_normalize(str, "NFC"), "\u1E9B\u0323");
|
||||
assert.equal(wasm.string_normalize(str, "NFD"), "\u017F\u0323\u0307");
|
||||
assert.equal(wasm.string_normalize(str, "NFKC"), "\u1E69");
|
||||
assert.equal(wasm.string_normalize(str, "NFKD"), "\u0073\u0323\u0307");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pad_end() {
|
||||
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 string_pad_end(
|
||||
this: &js_sys::JsString,
|
||||
target_length: u32,
|
||||
pad_string: &js_sys::JsString
|
||||
) -> js_sys::JsString
|
||||
{
|
||||
this.pad_end(target_length, pad_string)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let str = "abc";
|
||||
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_pad_end(str, 10, " "), "abc ");
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_pad_end(str, 10, " "), "abc ");
|
||||
assert.equal(wasm.string_pad_end(str, 10, "foo"), "abcfoofoof");
|
||||
assert.equal(wasm.string_pad_end(str, 6, "123456"), "abc123");
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_pad_end(str, 1, " "), "abc");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pad_start() {
|
||||
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 string_pad_start(
|
||||
this: &js_sys::JsString,
|
||||
target_length: u32,
|
||||
pad_string: &js_sys::JsString
|
||||
) -> js_sys::JsString
|
||||
{
|
||||
this.pad_start(target_length, pad_string)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let str = "abc";
|
||||
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_pad_start(str, 10, " "), " abc");
|
||||
assert.equal(wasm.string_pad_start(str, 10, "foo"), "foofoofabc");
|
||||
assert.equal(wasm.string_pad_start(str, 6, "123465"), "123abc");
|
||||
assert.equal(wasm.string_pad_start(str, 8, "0"), "00000abc");
|
||||
// TODO: remove second parameter once we have optional parameters
|
||||
assert.equal(wasm.string_pad_start(str, 1, " "), "abc");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeat() {
|
||||
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 string_repeat(this: &js_sys::JsString, count: i32) -> js_sys::JsString {
|
||||
this.repeat(count)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let str = "test";
|
||||
assert.equal(wasm.string_repeat(str, 3), "testtesttest");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice() {
|
||||
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 create_slice(this: &js_sys::JsString, start: u32, end: u32) -> js_sys::JsString {
|
||||
this.slice(start, end)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let characters = "acxn18";
|
||||
let subset = wasm.create_slice(characters, 1, 3);
|
||||
|
||||
assert.equal(subset, "cx");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starts_with() {
|
||||
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 string_starts_with(this: &js_sys::JsString, search_string: &js_sys::JsString, position: u32) -> bool {
|
||||
this.starts_with(search_string, position)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let str = "To be, or not to be, that is the question.";
|
||||
|
||||
// TODO: remove second parameter for both assertions once we have optional parameters
|
||||
assert.ok(wasm.string_starts_with(str, 'To be', 0));
|
||||
assert.ok(!wasm.string_starts_with(str, 'not to be', 0));
|
||||
assert.ok(wasm.string_starts_with(str, 'not to be', 10));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn substring() {
|
||||
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 string_substring(this: &js_sys::JsString, index_start: u32, index_end: u32) -> js_sys::JsString {
|
||||
this.substring(index_start, index_end)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let anyString = "Mozilla";
|
||||
|
||||
assert.equal(wasm.string_substring(anyString, 0, 1), 'M');
|
||||
assert.equal(wasm.string_substring(anyString, 1, 0), 'M');
|
||||
|
||||
assert.equal(wasm.string_substring(anyString, 0, 6), 'Mozill');
|
||||
|
||||
// TODO: Add test once we have optional parameters
|
||||
// assert.equal(wasm.string_substring(anyString, 4), 'lla');
|
||||
assert.equal(wasm.string_substring(anyString, 4, 7), 'lla');
|
||||
assert.equal(wasm.string_substring(anyString, 7, 4), 'lla');
|
||||
|
||||
assert.equal(wasm.string_substring(anyString, 0, 7), 'Mozilla');
|
||||
assert.equal(wasm.string_substring(anyString, 0, 10), 'Mozilla');
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn substr() {
|
||||
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 create_substr(this: &js_sys::JsString, start: i32, length: i32) -> js_sys::JsString {
|
||||
this.substr(start, length)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let aString = "Mozilla";
|
||||
|
||||
assert.equal(wasm.create_substr(aString, 0, 1), "M");
|
||||
assert.equal(wasm.create_substr(aString, 1, 0), "");
|
||||
assert.equal(wasm.create_substr(aString, -1, 1), "a");
|
||||
assert.equal(wasm.create_substr(aString, 1, -1), "");
|
||||
// TODO: Uncomment and test these assertions, once we have support for optional parameters
|
||||
// assert.equal(wasm.create_substr(aString, -3), "lla");
|
||||
// assert.equal(wasm.create_substr(aString, 1), "ozilla");
|
||||
assert.equal(wasm.create_substr(aString, -20, 2), "Mo");
|
||||
assert.equal(wasm.create_substr(aString, 20, 2), "");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_lower_case() {
|
||||
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 string_to_lower_case(this: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.to_lower_case()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.string_to_lower_case("Mozilla"), "mozilla");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_string() {
|
||||
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 string_to_string(this: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.to_string()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let greeting = 'Hello world!';
|
||||
assert.equal(wasm.string_to_string(greeting), 'Hello world!');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_upper_case() {
|
||||
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 string_to_upper_case(this: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.to_upper_case()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.string_to_upper_case("Mozilla"), "MOZILLA");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trim() {
|
||||
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 string_trim(this: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.trim()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.string_trim(' foo '), 'foo');
|
||||
// Another example of .trim() removing whitespace from just one side.
|
||||
assert.equal(wasm.string_trim('foo '), 'foo');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trim_end_and_trim_right() {
|
||||
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 string_trim_end(this: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.trim_end()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn string_trim_right(this: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.trim_right()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let greeting = ' Hello world! ';
|
||||
let trimmed = ' Hello world!';
|
||||
assert.equal(wasm.string_trim_end(greeting), trimmed);
|
||||
assert.equal(wasm.string_trim_right(greeting), trimmed);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trim_start_and_trim_left() {
|
||||
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 string_trim_start(this: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.trim_start()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn string_trim_left(this: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.trim_left()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let greeting = ' Hello world! ';
|
||||
let trimmed = 'Hello world! ';
|
||||
assert.equal(wasm.string_trim_start(greeting), trimmed);
|
||||
assert.equal(wasm.string_trim_left(greeting), trimmed);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_of() {
|
||||
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 string_value_of(this: &js_sys::JsString) -> js_sys::JsString {
|
||||
this.value_of()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let greeting = new String('Hello world!');
|
||||
assert.equal(wasm.string_value_of(greeting), 'Hello world!');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
258
crates/js-sys/tests/all/Map.rs
Normal file
258
crates/js-sys/tests/all/Map.rs
Normal file
@ -0,0 +1,258 @@
|
||||
#![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()
|
||||
}
|
100
crates/js-sys/tests/all/MapIterator.rs
Normal file
100
crates/js-sys/tests/all/MapIterator.rs
Normal file
@ -0,0 +1,100 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
|
||||
#[test]
|
||||
fn entries() {
|
||||
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_entries(this: &js_sys::Map) -> js_sys::MapIterator {
|
||||
this.entries()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const map = new Map();
|
||||
const iterator = map.entries();
|
||||
const wasmIterator = wasm.get_entries(map);
|
||||
map.set('foo', 'bar');
|
||||
map.set('bar', 'baz');
|
||||
|
||||
assert.equal(iterator.toString(), wasmIterator.toString());
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keys() {
|
||||
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_keys(this: &js_sys::Map) -> js_sys::MapIterator {
|
||||
this.keys()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const map = new Map();
|
||||
const iterator = map.keys();
|
||||
const wasmIterator = wasm.get_keys(map);
|
||||
map.set('foo', 'bar');
|
||||
map.set('bar', 'baz');
|
||||
|
||||
assert.equal(iterator.toString(), wasmIterator.toString());
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values() {
|
||||
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_values(this: &js_sys::Map) -> js_sys::MapIterator {
|
||||
this.values()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const map = new Map();
|
||||
const iterator = map.keys();
|
||||
const wasmIterator = wasm.get_values(map);
|
||||
map.set('foo', 'bar');
|
||||
map.set('bar', 'baz');
|
||||
|
||||
assert.equal(iterator.toString(), wasmIterator.toString());
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
971
crates/js-sys/tests/all/Math.rs
Normal file
971
crates/js-sys/tests/all/Math.rs
Normal file
@ -0,0 +1,971 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use super::project;
|
||||
|
||||
#[test]
|
||||
fn abs() {
|
||||
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 abs(x: f64) -> f64 {
|
||||
js_sys::Math::abs(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.abs(-32), Math.abs(-32));
|
||||
assert.equal(wasm.abs(32), 32);
|
||||
assert.equal(wasm.abs(-4.7), Math.abs(-4.7));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acos() {
|
||||
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 acos(x: f64) -> f64 {
|
||||
js_sys::Math::acos(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.acos(-1), Math.PI);
|
||||
assert.equal(wasm.acos(0.5), 1.0471975511965979);
|
||||
assert.ok(Number.isNaN(wasm.acos(2)));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acosh() {
|
||||
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 acosh(x: f64) -> f64 {
|
||||
js_sys::Math::acosh(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.acosh(1), 0);
|
||||
assert.equal(wasm.acosh(2), Math.acosh(2));
|
||||
assert.ok(Number.isNaN(wasm.acosh(0.5)));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn asin() {
|
||||
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 asin(x: f64) -> f64 {
|
||||
js_sys::Math::asin(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.asin(1), Math.asin(1));
|
||||
assert.equal(wasm.asin(0.5), Math.asin(0.5));
|
||||
assert.ok(Number.isNaN(wasm.asin(2)));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn asinh() {
|
||||
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 asinh(x: f64) -> f64 {
|
||||
js_sys::Math::asinh(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.asinh(1), Math.asinh(1));
|
||||
assert.equal(wasm.asinh(0.5), Math.asinh(0.5));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atan() {
|
||||
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 atan(x: f64) -> f64 {
|
||||
js_sys::Math::atan(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.atan(1), Math.atan(1));
|
||||
assert.equal(wasm.atan(0.5), Math.atan(0.5));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atan2() {
|
||||
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 atan2(y: f64, x: f64) -> f64 {
|
||||
js_sys::Math::atan2(y, x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.atan2(1, 2), Math.atan2(1, 2));
|
||||
assert.equal(wasm.atan2(0.7, 3.8), Math.atan2(0.7, 3.8));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atanh() {
|
||||
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 atanh(x: f64) -> f64 {
|
||||
js_sys::Math::atanh(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.atanh(1), Math.atanh(1));
|
||||
assert.equal(wasm.atanh(0.5), Math.atanh(0.5));
|
||||
assert.ok(Number.isNaN(wasm.atanh(2)));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cbrt() {
|
||||
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 cbrt(x: f64) -> f64 {
|
||||
js_sys::Math::cbrt(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.cbrt(27), 3);
|
||||
assert.equal(wasm.cbrt(12.3), Math.cbrt(12.3));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ceil() {
|
||||
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 ceil(x: f64) -> i32 {
|
||||
js_sys::Math::ceil(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.ceil(1.1), 2);
|
||||
assert.equal(wasm.ceil(-1.1), -1);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clz32() {
|
||||
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 clz32(x: i32) -> u32 {
|
||||
js_sys::Math::clz32(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.clz32(1), 31);
|
||||
assert.equal(wasm.clz32(1000), 22);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cos() {
|
||||
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 cos(x: f64) -> f64 {
|
||||
js_sys::Math::cos(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.cos(0), 1);
|
||||
assert.equal(wasm.cos(1.5), Math.cos(1.5));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cosh() {
|
||||
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 cosh(x: f64) -> f64 {
|
||||
js_sys::Math::cosh(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.cosh(0), 1);
|
||||
assert.equal(wasm.cosh(2), 3.7621956910836314);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exp() {
|
||||
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 exp(x: f64) -> f64 {
|
||||
js_sys::Math::exp(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.exp(0), 1);
|
||||
assert.equal(wasm.exp(-1), 0.36787944117144233);
|
||||
assert.equal(wasm.exp(2), 7.38905609893065);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expm1() {
|
||||
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 expm1(x: f64) -> f64 {
|
||||
js_sys::Math::expm1(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.expm1(0), 0);
|
||||
assert.equal(wasm.expm1(1), 1.718281828459045);
|
||||
assert.equal(wasm.expm1(-1), -0.6321205588285577);
|
||||
assert.equal(wasm.expm1(2), 6.38905609893065);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn floor() {
|
||||
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 floor(x: f64) -> i32 {
|
||||
js_sys::Math::floor(x)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.floor(5.95), 5);
|
||||
assert.equal(wasm.floor(-5.05), -6);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fround() {
|
||||
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 fround(x: f64) -> f32 {
|
||||
js_sys::Math::fround(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.fround(5.5), 5.5);
|
||||
assert.equal(wasm.fround(5.05), 5.050000190734863);
|
||||
assert.equal(wasm.fround(5), 5);
|
||||
assert.equal(wasm.fround(-5.05), -5.050000190734863);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imul() {
|
||||
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 imul(x: i32, y:i32) -> i32 {
|
||||
js_sys::Math::imul(x, y)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.imul(3, 4), 12);
|
||||
assert.equal(wasm.imul(-5, 12), -60);
|
||||
assert.equal(wasm.imul(0xffffffff, 5), Math.imul(0xffffffff, 5));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn log() {
|
||||
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 log(x: f64) -> f64 {
|
||||
js_sys::Math::log(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.log(8) / wasm.log(2), 3);
|
||||
assert.equal(wasm.log(625) / wasm.log(5), 4);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn log10() {
|
||||
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 log10(x: f64) -> f64 {
|
||||
js_sys::Math::log10(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.log10(100000), 5);
|
||||
assert.equal(wasm.log10(1), 0);
|
||||
assert.equal(wasm.log10(2), 0.3010299956639812);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn log1p() {
|
||||
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 log1p(x: f64) -> f64 {
|
||||
js_sys::Math::log1p(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.log1p(1), 0.6931471805599453);
|
||||
assert.equal(wasm.log1p(0), 0);
|
||||
assert.equal(wasm.log1p(-1), -Infinity);
|
||||
assert.ok(isNaN(wasm.log1p(-2)));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn log2() {
|
||||
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 log2(x: f64) -> f64 {
|
||||
js_sys::Math::log2(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.log2(3), 1.584962500721156);
|
||||
assert.equal(wasm.log2(2), 1);
|
||||
assert.equal(wasm.log2(1), 0);
|
||||
assert.equal(wasm.log2(0), -Infinity);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pow() {
|
||||
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 pow(base: f64, exponent: f64) -> f64 {
|
||||
js_sys::Math::pow(base, exponent)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.pow(7, 2), 49);
|
||||
assert.equal(wasm.pow(3.8, 0.5), Math.pow(3.8, 0.5));
|
||||
assert.ok(Number.isNaN(wasm.pow(-2, 0.5)));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random() {
|
||||
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 random() -> f64 {
|
||||
js_sys::Math::random()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.ok(wasm.random() < 1);
|
||||
assert.ok(wasm.random() >= 0);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round() {
|
||||
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 round(x: f64) -> i32 {
|
||||
js_sys::Math::round(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.round(20.49), 20);
|
||||
assert.equal(wasm.round(20.5), 21);
|
||||
assert.equal(wasm.round(42), 42);
|
||||
assert.equal(wasm.round(-20.5), -20);
|
||||
assert.equal(wasm.round(-20.51), -21);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sign() {
|
||||
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 sign(x: f64) -> f64 {
|
||||
js_sys::Math::sign(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.sign(3), 1);
|
||||
assert.equal(wasm.sign(-3), -1);
|
||||
assert.equal(wasm.sign(2.3), 1);
|
||||
assert.equal(wasm.sign(0), 0);
|
||||
assert.ok(Number.isNaN(wasm.sign(NaN)));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sin() {
|
||||
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 sin(x: f64) -> f64 {
|
||||
js_sys::Math::sin(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.sin(0), 0);
|
||||
assert.equal(wasm.sin(1), Math.sin(1));
|
||||
assert.equal(wasm.sin(Math.PI / 2), 1);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sinh() {
|
||||
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 sinh(x: f64) -> f64 {
|
||||
js_sys::Math::sinh(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.sinh(0), 0);
|
||||
assert.equal(wasm.sinh(1), Math.sinh(1));
|
||||
assert.equal(wasm.sinh(2.3), Math.sinh(2.3));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqrt() {
|
||||
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 sqrt(x: f64) -> f64 {
|
||||
js_sys::Math::sqrt(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.sqrt(9), 3);
|
||||
assert.equal(wasm.sqrt(2), Math.sqrt(2));
|
||||
assert.equal(wasm.sqrt(42.42), Math.sqrt(42.42));
|
||||
assert.equal(wasm.sqrt(1), 1);
|
||||
assert.ok(Number.isNaN(wasm.sqrt(-1)));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tan() {
|
||||
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 tan(x: f64) -> f64 {
|
||||
js_sys::Math::tan(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.tan(0), 0);
|
||||
assert.equal(wasm.tan(1), Math.tan(1));
|
||||
assert.equal(wasm.tan(0.5), Math.tan(0.5));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tanh() {
|
||||
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 tanh(x: f64) -> f64 {
|
||||
js_sys::Math::tanh(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.tanh(0), 0);
|
||||
assert.equal(wasm.tanh(1), Math.tanh(1));
|
||||
assert.equal(wasm.tanh(0.5), Math.tanh(0.5));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trunc() {
|
||||
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 trunc(x: f64) -> i32 {
|
||||
js_sys::Math::trunc(x)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.trunc(13.37), 13);
|
||||
assert.equal(wasm.trunc(42.84), 42);
|
||||
assert.equal(wasm.trunc(0.123), 0);
|
||||
assert.equal(wasm.trunc(-0.123), 0);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
379
crates/js-sys/tests/all/Number.rs
Normal file
379
crates/js-sys/tests/all/Number.rs
Normal file
@ -0,0 +1,379 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use super::project;
|
||||
|
||||
#[test]
|
||||
fn is_finite() {
|
||||
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 is_finite(value: &JsValue) -> bool {
|
||||
js_sys::Number::is_finite(value)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.is_finite(42), true);
|
||||
assert.equal(wasm.is_finite(42.1), true);
|
||||
assert.equal(wasm.is_finite('42'), false);
|
||||
assert.equal(wasm.is_finite(NaN), false);
|
||||
assert.equal(wasm.is_finite(Infinity), false);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_integer() {
|
||||
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 is_integer(value: &JsValue) -> bool {
|
||||
js_sys::Number::is_integer(value)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.ok(wasm.is_integer(123));
|
||||
assert.ok(!wasm.is_integer(123.45));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_safe_integer() {
|
||||
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 is_safe_integer(value: &JsValue) -> bool {
|
||||
js_sys::Number::is_safe_integer(value)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.is_safe_integer(42), true);
|
||||
assert.equal(wasm.is_safe_integer(Math.pow(2, 53) - 1), true);
|
||||
assert.equal(wasm.is_safe_integer(Math.pow(2, 53)), false);
|
||||
assert.equal(wasm.is_safe_integer('42'), false);
|
||||
assert.equal(wasm.is_safe_integer(42.1), false);
|
||||
assert.equal(wasm.is_safe_integer(NaN), false);
|
||||
assert.equal(wasm.is_safe_integer(Infinity), 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::*;
|
||||
use js_sys::Number;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn new_number() -> Number {
|
||||
Number::new(JsValue::from(42))
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(typeof wasm.new_number(), "object");
|
||||
assert.equal(wasm.new_number(), 42);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_int_float() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::Number;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn parse_int(text: &str, radix: u8) -> Number {
|
||||
Number::parse_int(text, radix)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn parse_float(text: &str) -> Number {
|
||||
Number::parse_float(text)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.parse_int("42", 10), 42);
|
||||
assert.equal(wasm.parse_int("42", 16), 66); // 0x42 == 66
|
||||
assert.ok(Number.isNaN(wasm.parse_int("invalid int", 10)), "should be NaN");
|
||||
|
||||
assert.equal(wasm.parse_float("123456.789"), 123456.789);
|
||||
assert.ok(Number.isNaN(wasm.parse_float("invalid float")), "should be NaN");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_locale_string() {
|
||||
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 to_locale_string(this: &js_sys::Number, locale: &str) -> js_sys::JsString {
|
||||
this.to_locale_string(locale)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let number = 1234.45;
|
||||
assert.equal(wasm.to_locale_string(number, "en-US"), "1,234.45");
|
||||
// TODO: these tests seems to be system dependent, disable for now
|
||||
// assert.equal(wasm.to_locale_string(number, "de-DE"), "1,234.45");
|
||||
// assert.equal(wasm.to_locale_string(number, "zh-Hans-CN-u-nu-hanidec"), "1,234.45");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_precision() {
|
||||
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 to_precision(this: &js_sys::Number, precision: u8) -> js_sys::JsString {
|
||||
let result = this.to_precision(precision);
|
||||
let result = match result {
|
||||
Ok(num) => num,
|
||||
Err(_err) => "RangeError".into()
|
||||
};
|
||||
result
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.to_precision(0.1, 3), "0.100");
|
||||
assert.equal(wasm.to_precision(10, 101), "RangeError");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_string() {
|
||||
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 to_string(this: &js_sys::Number, radix: u8) -> js_sys::JsString {
|
||||
let result = this.to_string(radix);
|
||||
let result = match result {
|
||||
Ok(num) => num,
|
||||
Err(_err) => "RangeError".into()
|
||||
};
|
||||
result
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let number = 42;
|
||||
assert.equal(wasm.to_string(number, 10), "42");
|
||||
assert.equal(wasm.to_string(233, 16), "e9");
|
||||
assert.equal(wasm.to_string(number, 100), "RangeError");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_of() {
|
||||
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 js_value_of(this: &js_sys::Number) -> f64 {
|
||||
this.value_of()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let number = 42;
|
||||
assert.equal(wasm.js_value_of(number), 42);
|
||||
assert.equal(typeof wasm.js_value_of(number), "number");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_fixed() {
|
||||
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 to_fixed(this: &js_sys::Number, digits: u8) -> js_sys::JsString {
|
||||
let result = this.to_fixed(digits);
|
||||
let result = match result {
|
||||
Ok(num) => num,
|
||||
Err(_err) => "RangeError".into()
|
||||
};
|
||||
result
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.to_fixed(123.456, 2), "123.46");
|
||||
assert.equal(wasm.to_fixed(10, 101), "RangeError");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_exponential() {
|
||||
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 to_exponential(this: &js_sys::Number, fraction_digits: u8) -> js_sys::JsString {
|
||||
let result = this.to_exponential(fraction_digits);
|
||||
let result = match result {
|
||||
Ok(num) => num,
|
||||
Err(_err) => "RangeError".into()
|
||||
};
|
||||
result
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.to_exponential(123456, 2), "1.23e+5");
|
||||
assert.equal(wasm.to_exponential(10, 101), "RangeError");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
522
crates/js-sys/tests/all/Object.rs
Normal file
522
crates/js-sys/tests/all/Object.rs
Normal file
@ -0,0 +1,522 @@
|
||||
#![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_object() -> js_sys::Object {
|
||||
js_sys::Object::new()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(typeof wasm.new_object(), "object");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn has_own_property() {
|
||||
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_own_foo_property(obj: &js_sys::Object, property: &JsValue) -> bool {
|
||||
obj.has_own_property(&property)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.ok(wasm.has_own_foo_property({ foo: 42 }, "foo"));
|
||||
assert.ok(wasm.has_own_foo_property({ 42: "foo" }, 42));
|
||||
assert.ok(!wasm.has_own_foo_property({ foo: 42 }, "bar"));
|
||||
|
||||
const s = Symbol();
|
||||
assert.ok(wasm.has_own_foo_property({ [s]: "foo" }, s));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_string() {
|
||||
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 to_string(obj: &js_sys::Object) -> js_sys::JsString {
|
||||
obj.to_string()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn test() {
|
||||
let object = js_sys::Object::new();
|
||||
assert_eq!(String::from(object.to_string()), "[object Object]");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.to_string({ foo: 42 }), "[object Object]");
|
||||
wasm.test();
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_extensible() {
|
||||
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 is_extensible(obj: &js_sys::Object) -> bool {
|
||||
js_sys::Object::is_extensible(&obj)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {};
|
||||
assert.ok(wasm.is_extensible(object));
|
||||
|
||||
Object.preventExtensions(object);
|
||||
assert.ok(!wasm.is_extensible(object));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_frozen() {
|
||||
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 is_frozen(obj: &js_sys::Object) -> bool {
|
||||
js_sys::Object::is_frozen(&obj)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {};
|
||||
assert.ok(!wasm.is_frozen(object));
|
||||
|
||||
Object.freeze(object);
|
||||
assert.ok(wasm.is_frozen(object));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_sealed() {
|
||||
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 is_sealed(obj: &js_sys::Object) -> bool {
|
||||
js_sys::Object::is_sealed(&obj)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {};
|
||||
assert.ok(!wasm.is_sealed(object));
|
||||
|
||||
Object.seal(object);
|
||||
assert.ok(wasm.is_sealed(object));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_prototype_of() {
|
||||
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 obj_is_prototype_of_value(obj: &js_sys::Object, value: &JsValue) -> bool {
|
||||
obj.is_prototype_of(&value)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
class Foo {}
|
||||
class Bar {}
|
||||
|
||||
export function test() {
|
||||
const foo = new Foo();
|
||||
assert.ok(wasm.obj_is_prototype_of_value(Foo.prototype, foo));
|
||||
assert.ok(!wasm.obj_is_prototype_of_value(Bar.prototype, foo));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keys() {
|
||||
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 keys(obj: &js_sys::Object) -> js_sys::Array {
|
||||
js_sys::Object::keys(obj)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const obj = { a: 1, b: 2, c: 3 };
|
||||
assert.deepStrictEqual(wasm.keys(obj), ["a", "b", "c"]);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prevent_extensions() {
|
||||
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 prevent_extensions(obj: &js_sys::Object) {
|
||||
js_sys::Object::prevent_extensions(obj);
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {};
|
||||
wasm.prevent_extensions(object);
|
||||
|
||||
assert.ok(!Object.isExtensible(object));
|
||||
assert.throws(() => {
|
||||
'use strict';
|
||||
Object.defineProperty(object, 'foo', { value: 42 });
|
||||
}, TypeError);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn property_is_enumerable() {
|
||||
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 property_is_enumerable(obj: &js_sys::Object, property: &JsValue) -> bool {
|
||||
obj.property_is_enumerable(&property)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.ok(wasm.property_is_enumerable({ foo: 42 }, "foo"));
|
||||
assert.ok(wasm.property_is_enumerable({ 42: "foo" }, 42));
|
||||
assert.ok(!wasm.property_is_enumerable({}, 42));
|
||||
|
||||
const obj = {};
|
||||
Object.defineProperty(obj, "foo", { enumerable: false });
|
||||
assert.ok(!wasm.property_is_enumerable(obj, "foo"));
|
||||
|
||||
const s = Symbol();
|
||||
assert.ok(wasm.property_is_enumerable({ [s]: true }, s));
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seal() {
|
||||
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 seal(value: &JsValue) -> JsValue {
|
||||
js_sys::Object::seal(&value)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = { foo: 'bar' };
|
||||
const sealedObject = wasm.seal(object);
|
||||
assert.strictEqual(object, sealedObject);
|
||||
assert.throws(() => {
|
||||
'use strict';
|
||||
sealedObject.bar = 'foo';
|
||||
}, TypeError);
|
||||
assert.throws(() => {
|
||||
'use strict';
|
||||
delete sealedObject.foo;
|
||||
}, TypeError);
|
||||
|
||||
const primitive = 42;
|
||||
assert.doesNotThrow(() => {
|
||||
'use strict';
|
||||
// according to ES2015, this should not throw anymore
|
||||
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal#Notes
|
||||
wasm.seal(primitive);
|
||||
});
|
||||
|
||||
const array = [1, 2, 3];
|
||||
const sealedArray = wasm.seal(array);
|
||||
assert.throws(() => {
|
||||
'use strict';
|
||||
sealedArray.push(42);
|
||||
}, TypeError);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_prototype_of() {
|
||||
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_prototype_of(object: &js_sys::Object, prototype: &js_sys::Object) -> js_sys::Object {
|
||||
js_sys::Object::set_prototype_of(&object, &prototype)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = { foo: 42 };
|
||||
const newPrototype = { bar: 'baz' };
|
||||
|
||||
const modifiedObject = wasm.set_prototype_of(object, newPrototype);
|
||||
assert.ok(newPrototype.isPrototypeOf(modifiedObject));
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_locale_string() {
|
||||
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 to_locale_string() -> js_sys::JsString {
|
||||
let object = js_sys::Object::new();
|
||||
object.to_locale_string()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.to_locale_string(), "[object Object]");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_of() {
|
||||
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 value_of(obj: &js_sys::Object) -> js_sys::Object {
|
||||
obj.value_of()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const obj = { foo: 42 };
|
||||
assert.strictEqual(wasm.value_of(obj), obj);
|
||||
assert.notStrictEqual(wasm.value_of(obj), { foo: 42 });
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values() {
|
||||
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 values(obj: &js_sys::Object) -> js_sys::Array {
|
||||
js_sys::Object::values(&obj)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = { foo: 'bar', baz: 'qux' };
|
||||
const values = wasm.values(object);
|
||||
assert.equal(values.length, 2);
|
||||
assert.deepEqual(values.sort(), ['bar', 'qux']);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
76
crates/js-sys/tests/all/Proxy.rs
Normal file
76
crates/js-sys/tests/all/Proxy.rs
Normal file
@ -0,0 +1,76 @@
|
||||
#![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_proxy(target: JsValue, handler: js_sys::Object) -> js_sys::Proxy {
|
||||
js_sys::Proxy::new(&target, &handler)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const target = { a: 100 };
|
||||
const handler = {
|
||||
get: function(obj, prop) {
|
||||
return prop in obj ? obj[prop] : 37;
|
||||
}
|
||||
};
|
||||
const proxy = wasm.new_proxy(target, handler);
|
||||
assert.equal(proxy.a, 100);
|
||||
assert.equal(proxy.b, 37);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn revocable() {
|
||||
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_revocable_proxy(target: JsValue, handler: js_sys::Object) -> js_sys::Object {
|
||||
js_sys::Proxy::revocable(&target, &handler)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const target = { a: 100 };
|
||||
const handler = {
|
||||
get: function(obj, prop) {
|
||||
return prop in obj ? obj[prop] : 37;
|
||||
}
|
||||
};
|
||||
const { proxy, revoke } =
|
||||
wasm.new_revocable_proxy(target, handler);
|
||||
assert.equal(proxy.a, 100);
|
||||
assert.equal(proxy.b, 37);
|
||||
revoke();
|
||||
assert.throws(() => { proxy.a }, TypeError);
|
||||
assert.throws(() => { proxy.b }, TypeError);
|
||||
assert.equal(typeof proxy, "object");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
601
crates/js-sys/tests/all/Reflect.rs
Normal file
601
crates/js-sys/tests/all/Reflect.rs
Normal file
@ -0,0 +1,601 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn apply() {
|
||||
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 apply(target: &js_sys::Function, this_argument: &JsValue, arguments_list: &js_sys::Array) -> JsValue {
|
||||
js_sys::Reflect::apply(target, this_argument, arguments_list)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.apply("".charAt, "ponies", [3]), "i");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn construct() {
|
||||
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 construct(target: &js_sys::Function, arguments_list: &js_sys::Array) -> JsValue {
|
||||
js_sys::Reflect::construct(target, arguments_list)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
class Rectangle {
|
||||
constructor(x, y){
|
||||
this.x = x,
|
||||
this.y = y
|
||||
}
|
||||
|
||||
static eq(x, y) {
|
||||
return x === y;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const args = [10, 10];
|
||||
|
||||
assert.equal(wasm.construct(Rectangle, args).x, 10);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn construct_with_new_target() {
|
||||
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 construct_with_new_target(target: &js_sys::Function, arguments_list: &js_sys::Array, new_target: &js_sys::Function) -> JsValue {
|
||||
js_sys::Reflect::construct_with_new_target(target, arguments_list, new_target)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
class Rectangle {
|
||||
constructor(x, y){
|
||||
this.x = x,
|
||||
this.y = y
|
||||
}
|
||||
|
||||
static eq(x, y) {
|
||||
return x === y;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Rectangle2 {
|
||||
constructor(x, y){
|
||||
this.x = x,
|
||||
this.y = y
|
||||
}
|
||||
|
||||
static eq(x, y) {
|
||||
return x === y;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const args = [10, 10];
|
||||
|
||||
assert.equal(wasm.construct_with_new_target(Rectangle, args, Rectangle2).x, 10);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn define_property() {
|
||||
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 define_property(target: &js_sys::Object, property_key: &JsValue, attributes: &js_sys::Object) -> bool {
|
||||
js_sys::Reflect::define_property(target, property_key, attributes)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {};
|
||||
|
||||
assert.equal(wasm.define_property(object, "key", { value: 42}), true)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_property() {
|
||||
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_property(target: &js_sys::Object, property_key: &JsValue) -> bool {
|
||||
js_sys::Reflect::delete_property(target, property_key)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {
|
||||
property: 42
|
||||
};
|
||||
|
||||
wasm.delete_property(object, "property");
|
||||
|
||||
assert.equal(object.property, undefined);
|
||||
|
||||
const array = [1, 2, 3, 4, 5];
|
||||
wasm.delete_property(array, 3);
|
||||
|
||||
assert.equal(array[3], undefined);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.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 get(target: &js_sys::Object, property_key: &JsValue) -> JsValue {
|
||||
js_sys::Reflect::get(target, property_key)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {
|
||||
property: 42
|
||||
};
|
||||
|
||||
assert.equal(wasm.get(object, "property"), 42);
|
||||
|
||||
const array = [1, 2, 3, 4, 5];
|
||||
|
||||
assert.equal(wasm.get(array, 3), 4);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_own_property_descriptor() {
|
||||
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_own_property_descriptor(target: &js_sys::Object, property_key: &JsValue) -> JsValue {
|
||||
js_sys::Reflect::get_own_property_descriptor(target, property_key)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {
|
||||
property: 42
|
||||
};
|
||||
|
||||
assert.equal(wasm.get_own_property_descriptor(object, "property").value, 42);
|
||||
assert.equal(wasm.get_own_property_descriptor(object, "property1"), undefined);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_prototype_of() {
|
||||
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_prototype_of(target: &js_sys::Object) -> js_sys::Object {
|
||||
js_sys::Reflect::get_prototype_of(target)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {
|
||||
property: 42
|
||||
};
|
||||
const array = [1, 2, 3];
|
||||
|
||||
assert.equal(wasm.get_prototype_of(object), Object.prototype);
|
||||
assert.equal(wasm.get_prototype_of(array), Array.prototype);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.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(target: &js_sys::Object, property_key: &JsValue) -> bool {
|
||||
js_sys::Reflect::has(target, property_key)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {
|
||||
property: 42
|
||||
};
|
||||
const array = [1, 2, 3, 4]
|
||||
|
||||
assert.equal(wasm.has(object, "property"), true);
|
||||
assert.equal(wasm.has(object, "foo"), false);
|
||||
assert.equal(wasm.has(array, 3), true);
|
||||
assert.equal(wasm.has(array, 10), false);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_extensible() {
|
||||
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 is_extensible(target: &js_sys::Object) -> bool {
|
||||
js_sys::Reflect::is_extensible(target)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {
|
||||
property: 42
|
||||
};
|
||||
|
||||
assert.equal(wasm.is_extensible(object), true);
|
||||
|
||||
Reflect.preventExtensions(object);
|
||||
|
||||
assert.equal(wasm.is_extensible(object), false);
|
||||
|
||||
const object2 = Object.seal({});
|
||||
|
||||
assert.equal(wasm.is_extensible(object2), false);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn own_keys() {
|
||||
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 own_keys(target: &js_sys::Object) -> js_sys::Array {
|
||||
js_sys::Reflect::own_keys(target)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {
|
||||
property: 42
|
||||
};
|
||||
const array = [];
|
||||
|
||||
assert.equal(wasm.own_keys(object)[0], "property");
|
||||
assert.equal(wasm.own_keys(array)[0], "length");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prevent_extensions() {
|
||||
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 prevent_extensions(target: &js_sys::Object) -> bool {
|
||||
js_sys::Reflect::prevent_extensions(target)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object1 = {};
|
||||
|
||||
wasm.prevent_extensions(object1);
|
||||
|
||||
assert.equal(Reflect.isExtensible(object1), false);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.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(target: &js_sys::Object, property_key: &JsValue, value: &JsValue) -> bool {
|
||||
js_sys::Reflect::set(target, property_key, value)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {};
|
||||
const array = [1, 2, 3, 4];
|
||||
assert.equal(wasm.set(object, "key", "value"), true);
|
||||
assert.equal(wasm.set(array, 0, 100), true);
|
||||
|
||||
assert.equal(Reflect.get(object, "key"), "value");
|
||||
assert.equal(array[0], 100);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_with_receiver() {
|
||||
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_with_receiver(target: &js_sys::Object, property_key: &JsValue, value: &JsValue, receiver: &js_sys::Object) -> bool {
|
||||
js_sys::Reflect::set_with_receiver(target, property_key, value, receiver)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {};
|
||||
const array = [1, 2, 3, 4];
|
||||
assert.equal(wasm.set_with_receiver({}, "key", "value", object), true);
|
||||
assert.equal(wasm.set_with_receiver([], 0, 100, array), true);
|
||||
|
||||
assert.equal(Reflect.get(object, "key"), "value");
|
||||
assert.equal(array[0], 100);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_prototype_of() {
|
||||
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_prototype_of(target: &js_sys::Object, prototype: &JsValue) -> bool {
|
||||
js_sys::Reflect::set_prototype_of(target, prototype)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object = {};
|
||||
assert.equal(wasm.set_prototype_of(object, Object.prototype), true);
|
||||
assert.equal(Object.getPrototypeOf(object), Object.prototype);
|
||||
assert.equal(wasm.set_prototype_of(object, null), true);
|
||||
assert.equal(Object.getPrototypeOf(object), null);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
228
crates/js-sys/tests/all/Set.rs
Normal file
228
crates/js-sys/tests/all/Set.rs
Normal file
@ -0,0 +1,228 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[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(this: &js_sys::Set, value: &JsValue) -> js_sys::Set {
|
||||
this.add(value)
|
||||
}
|
||||
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let set = new Set([]);
|
||||
|
||||
wasm.add(set, 100);
|
||||
|
||||
|
||||
assert.equal(set.size, 1);
|
||||
assert.equal(Array.from(set)[0], 100);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[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 clear(this: &js_sys::Set) {
|
||||
this.clear();
|
||||
}
|
||||
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let set = new Set([1, 2, 3]);
|
||||
|
||||
wasm.clear(set);
|
||||
|
||||
assert.equal(set.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 set_delete(this: &js_sys::Set, value: &JsValue) -> bool {
|
||||
this.delete(value)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let set = new Set([1, 2, 3]);
|
||||
|
||||
assert.equal(wasm.set_delete(set, 4), false);
|
||||
assert.equal(wasm.set_delete(set, 2), true);
|
||||
}
|
||||
"#)
|
||||
.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 count_evens(set: &js_sys::Set) -> u32 {
|
||||
let mut res = 0;
|
||||
set.for_each(&mut |value, _, _| {
|
||||
match value.as_f64() {
|
||||
Some(val) if val % 2. == 0. => res += 1,
|
||||
_ => { }
|
||||
}
|
||||
});
|
||||
res
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let setEven = new Set([2, 4, 6, 8]);
|
||||
let setEvenExpected = 4;
|
||||
let setEvenActual = wasm.count_evens(setEven);
|
||||
assert.equal(setEvenExpected, setEvenActual);
|
||||
|
||||
let setOdd = new Set([1, 3, 5, 7]);
|
||||
let setOddExpected = 0;
|
||||
let setOddActual = wasm.count_evens(setOdd);
|
||||
assert.equal(setOddExpected, setOddActual);
|
||||
|
||||
let setMixed = new Set([3, 5, 7, 10]);
|
||||
let setMixedExpected = 1;
|
||||
let setMixedActual = wasm.count_evens(setMixed);
|
||||
assert.equal(setMixedExpected, setMixedActual);
|
||||
}
|
||||
"#)
|
||||
.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::Set, value: &JsValue) -> bool {
|
||||
this.has(value)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let set = new Set([1, 2, 3]);
|
||||
|
||||
assert.equal(wasm.has(set, 4), false);
|
||||
assert.equal(wasm.has(set, 2), true);
|
||||
}
|
||||
"#)
|
||||
.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_set() -> js_sys::Set {
|
||||
js_sys::Set::new()
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let set = wasm.new_set();
|
||||
|
||||
assert.equal(set.size, 0);
|
||||
}
|
||||
"#)
|
||||
.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 size(this: &js_sys::Set) -> u32 {
|
||||
this.size()
|
||||
}
|
||||
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let set = new Set([8, 5, 4, 3, 1, 2]);
|
||||
|
||||
assert.equal(wasm.size(set), 6);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
97
crates/js-sys/tests/all/SetIterator.rs
Normal file
97
crates/js-sys/tests/all/SetIterator.rs
Normal file
@ -0,0 +1,97 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn entries() {
|
||||
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 entries(this: &js_sys::Set) -> js_sys::SetIterator {
|
||||
this.entries()
|
||||
}
|
||||
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let set = new Set([8, 5, 4, 3, 1, 2]);
|
||||
let wasmIterator = wasm.entries(set);
|
||||
let nextValue = wasmIterator.next().value;
|
||||
|
||||
assert.equal(nextValue[0], 8);
|
||||
assert.equal(nextValue[1], 8);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keys() {
|
||||
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 keys(this: &js_sys::Set) -> js_sys::SetIterator {
|
||||
this.keys()
|
||||
}
|
||||
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let set = new Set([8, 5, 4, 3, 1, 2]);
|
||||
let wasmIterator = wasm.keys(set);
|
||||
let nextValue = wasmIterator.next().value;
|
||||
|
||||
assert.equal(nextValue, 8);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values() {
|
||||
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 values(this: &js_sys::Set) -> js_sys::SetIterator {
|
||||
this.values()
|
||||
}
|
||||
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let set = new Set([8, 5, 4, 3, 1, 2]);
|
||||
let wasmIterator = wasm.values(set);
|
||||
let nextValue = wasmIterator.next().value;
|
||||
|
||||
assert.equal(nextValue, 8);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
571
crates/js-sys/tests/all/Symbol.rs
Normal file
571
crates/js-sys/tests/all/Symbol.rs
Normal file
@ -0,0 +1,571 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn has_instance() {
|
||||
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 symbol_has_instance() -> js_sys::Symbol {
|
||||
js_sys::Symbol::has_instance()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
class Array1 {
|
||||
static [wasm.symbol_has_instance()](instance) {
|
||||
return Array.isArray(instance);
|
||||
}
|
||||
}
|
||||
|
||||
export function test() {
|
||||
assert.equal(typeof wasm.symbol_has_instance(), "symbol");
|
||||
assert.ok([] instanceof Array1);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_concat_spreadable() {
|
||||
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 symbol_is_cancat_spreadable() -> js_sys::Symbol {
|
||||
js_sys::Symbol::is_concat_spreadable()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const alpha = ['a', 'b', 'c'];
|
||||
const numeric = [1, 2, 3];
|
||||
let alphaNumeric = alpha.concat(numeric);
|
||||
|
||||
assert.deepEqual(alphaNumeric, ["a", "b", "c", 1, 2, 3]);
|
||||
|
||||
numeric[wasm.symbol_is_cancat_spreadable()] = false;
|
||||
alphaNumeric = alpha.concat(numeric);
|
||||
|
||||
assert.deepEqual(alphaNumeric, ["a", "b", "c", [1, 2, 3]]);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iterator() {
|
||||
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 symbol_iterator() -> js_sys::Symbol {
|
||||
js_sys::Symbol::iterator()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const iterable1 = new Object();
|
||||
|
||||
iterable1[wasm.symbol_iterator()] = function* () {
|
||||
yield 1;
|
||||
yield 2;
|
||||
yield 3;
|
||||
};
|
||||
|
||||
assert.deepEqual([...iterable1], [1, 2, 3]);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_() {
|
||||
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 symbol_match() -> js_sys::Symbol {
|
||||
js_sys::Symbol::match_()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const regexp1 = /foo/;
|
||||
assert.throws(() => '/foo/'.startsWith(regexp1));
|
||||
|
||||
regexp1[wasm.symbol_match()] = false;
|
||||
|
||||
assert.ok('/foo/'.startsWith(regexp1));
|
||||
|
||||
assert.equal('/baz/'.endsWith(regexp1), false);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replace() {
|
||||
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 symbol_replace() -> js_sys::Symbol {
|
||||
js_sys::Symbol::replace()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
class Replace1 {
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
[wasm.symbol_replace()](string) {
|
||||
return `s/${string}/${this.value}/g`;
|
||||
}
|
||||
}
|
||||
|
||||
export function test() {
|
||||
assert.equal('foo'.replace(new Replace1('bar')), 's/foo/bar/g');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search() {
|
||||
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 symbol_search() -> js_sys::Symbol {
|
||||
js_sys::Symbol::search()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
class Search1 {
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
[wasm.symbol_search()](string) {
|
||||
return string.indexOf(this.value);
|
||||
}
|
||||
}
|
||||
|
||||
export function test() {
|
||||
assert.equal('foobar'.search(new Search1('bar')), 3);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn species() {
|
||||
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 symbol_species() -> js_sys::Symbol {
|
||||
js_sys::Symbol::species()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
class Array1 extends Array {
|
||||
static get [wasm.symbol_species()]() { return Array; }
|
||||
}
|
||||
|
||||
export function test() {
|
||||
const a = new Array1(1, 2, 3);
|
||||
const mapped = a.map(x => x * x);
|
||||
|
||||
assert.equal(mapped instanceof Array1, false);
|
||||
|
||||
assert.ok(mapped instanceof Array);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split() {
|
||||
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 symbol_split() -> js_sys::Symbol {
|
||||
js_sys::Symbol::split()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
class Split1 {
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
[wasm.symbol_split()](string) {
|
||||
var index = string.indexOf(this.value);
|
||||
return this.value + string.substr(0, index) + "/"
|
||||
+ string.substr(index + this.value.length);
|
||||
}
|
||||
}
|
||||
|
||||
export function test() {
|
||||
assert.equal('foobar'.split(new Split1('foo')), 'foo/bar');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_primitive() {
|
||||
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 symbol_to_primitive() -> js_sys::Symbol {
|
||||
js_sys::Symbol::to_primitive()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object1 = {
|
||||
[wasm.symbol_to_primitive()](hint) {
|
||||
if (hint == 'number') {
|
||||
return 42;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
assert.equal(+object1, 42);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_string_tag() {
|
||||
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 symbol_to_string_tag() -> js_sys::Symbol {
|
||||
js_sys::Symbol::to_string_tag()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
class ValidatorClass {
|
||||
get [wasm.symbol_to_string_tag()]() {
|
||||
return 'Validator';
|
||||
}
|
||||
}
|
||||
|
||||
export function test() {
|
||||
assert.equal(Object.prototype.toString.call(new ValidatorClass()), '[object Validator]');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn for_() {
|
||||
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 symbol_for(key: &js_sys::JsString) -> js_sys::Symbol {
|
||||
js_sys::Symbol::for_(key)
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.strictEqual(wasm.symbol_for("bar"), wasm.symbol_for("bar"));
|
||||
assert.strictEqual(wasm.symbol_for("bar"), Symbol.for("bar"));
|
||||
|
||||
assert.notStrictEqual(wasm.symbol_for("foo"), Symbol("bar"));
|
||||
assert.notStrictEqual(wasm.symbol_for("foo"), wasm.symbol_for("bar"));
|
||||
|
||||
var sym = wasm.symbol_for("mario");
|
||||
assert.equal(sym.toString(), "Symbol(mario)");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_for() {
|
||||
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 symbol_key_for(sym: &js_sys::Symbol) -> js_sys::JsString {
|
||||
js_sys::Symbol::key_for(sym)
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
var globalSym = Symbol.for("foo");
|
||||
assert.strictEqual(wasm.symbol_key_for(globalSym), 'foo');
|
||||
|
||||
var localSym = Symbol();
|
||||
assert.strictEqual(wasm.symbol_key_for(localSym), undefined);
|
||||
|
||||
assert.strictEqual(wasm.symbol_key_for(Symbol.iterator), undefined);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_string() {
|
||||
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 symbol_to_string(this: &js_sys::Symbol) -> js_sys::JsString {
|
||||
js_sys::Symbol::to_string(this)
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.strictEqual(wasm.symbol_to_string(Symbol('desc')), 'Symbol(desc)');
|
||||
|
||||
assert.strictEqual(wasm.symbol_to_string(Symbol.iterator), 'Symbol(Symbol.iterator)');
|
||||
|
||||
assert.strictEqual(wasm.symbol_to_string(Symbol.for('foo')), 'Symbol(foo)');
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_of() {
|
||||
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 symbol_value_of(this: &js_sys::Symbol) -> js_sys::Symbol {
|
||||
js_sys::Symbol::value_of(this)
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
var globalSym = Symbol.for("foo");
|
||||
assert.strictEqual(wasm.symbol_value_of(globalSym), globalSym);
|
||||
|
||||
var localSym = Symbol();
|
||||
assert.strictEqual(wasm.symbol_value_of(localSym), localSym);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
302
crates/js-sys/tests/all/TypedArray.rs
Normal file
302
crates/js-sys/tests/all/TypedArray.rs
Normal file
@ -0,0 +1,302 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
use std::string::String;
|
||||
|
||||
fn new_undefined_lib(array_type: &str) -> String {
|
||||
format!(r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn new_array() -> js_sys::{} {{
|
||||
js_sys::{}::new(JsValue::undefined())
|
||||
}}
|
||||
"#, array_type, array_type)
|
||||
}
|
||||
|
||||
fn new_undefined_test_js() -> &'static str {
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.new_array().length, 0);
|
||||
}
|
||||
"#
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Uint8Array_undefined() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_undefined_lib("Uint8Array"),)
|
||||
.file("test.js", new_undefined_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Uint8ClampedArray_undefined() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_undefined_lib("Uint8ClampedArray"),)
|
||||
.file("test.js", new_undefined_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Uint16Array_undefined() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_undefined_lib("Uint16Array"),)
|
||||
.file("test.js", new_undefined_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Uint32Array_undefined() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_undefined_lib("Uint32Array"),)
|
||||
.file("test.js", new_undefined_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Int8Array_undefined() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_undefined_lib("Int8Array"),)
|
||||
.file("test.js", new_undefined_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Int16Array_undefined() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_undefined_lib("Int16Array"),)
|
||||
.file("test.js", new_undefined_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Int32Array_undefined() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_undefined_lib("Int32Array"),)
|
||||
.file("test.js", new_undefined_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Float32Array_undefined() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_undefined_lib("Float32Array"),)
|
||||
.file("test.js", new_undefined_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Float64Array_undefined() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_undefined_lib("Float64Array"),)
|
||||
.file("test.js", new_undefined_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
fn new_length_lib(array_type: &str) -> String {
|
||||
format!(r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn new_array() -> js_sys::{} {{
|
||||
js_sys::{}::new(JsValue::from_f64(4.0))
|
||||
}}
|
||||
"#, array_type, array_type)
|
||||
}
|
||||
|
||||
fn new_length_test_js() -> &'static str {
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.new_array().length, 4);
|
||||
}
|
||||
"#
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Uint8Array_length() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_length_lib("Uint8Array"),)
|
||||
.file("test.js", new_length_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Uint8ClampedArray_length() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_length_lib("Uint8ClampedArray"),)
|
||||
.file("test.js", new_length_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Uint16Array_length() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_length_lib("Uint16Array"),)
|
||||
.file("test.js", new_length_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Uint32Array_length() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_length_lib("Uint32Array"),)
|
||||
.file("test.js", new_length_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Int8Array_length() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_length_lib("Int8Array"),)
|
||||
.file("test.js", new_length_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Int16Array_length() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_length_lib("Int16Array"),)
|
||||
.file("test.js", new_length_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Int32Array_length() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_length_lib("Int32Array"),)
|
||||
.file("test.js", new_length_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Float32Array_length() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_length_lib("Float32Array"),)
|
||||
.file("test.js", new_length_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_Float64Array_length() {
|
||||
project()
|
||||
.file("src/lib.rs", &new_length_lib("Float64Array"),)
|
||||
.file("test.js", new_length_test_js(),)
|
||||
.test()
|
||||
}
|
||||
|
||||
fn fill_lib(array_type: &str, el_type: &str) -> String {
|
||||
format!(r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn fill_with(this: &js_sys::{}, value: {}, start: u32, end: u32) -> js_sys::{} {{
|
||||
this.fill(value, start, end)
|
||||
}}
|
||||
"#, array_type, el_type, array_type)
|
||||
}
|
||||
|
||||
fn fill_test_js(array_type: &str) -> String {
|
||||
format!(r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {{
|
||||
let characters = new {}([0, 0, 0, 0, 0, 0]);
|
||||
let subset = wasm.fill_with(characters, 1, 0, 3);
|
||||
|
||||
assert.equal(subset[0], 1);
|
||||
assert.equal(subset[4], 0);
|
||||
}}
|
||||
"#, array_type)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_Uint8Array() {
|
||||
project()
|
||||
.file("src/lib.rs", &fill_lib("Uint8Array", "u8"),)
|
||||
.file("test.js", &fill_test_js("Uint8Array"))
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_Uint8ClampedArray() {
|
||||
project()
|
||||
.file("src/lib.rs", &fill_lib("Uint8ClampedArray", "u8"),)
|
||||
.file("test.js", &fill_test_js("Uint8ClampedArray"))
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_Uint16Array() {
|
||||
project()
|
||||
.file("src/lib.rs", &fill_lib("Uint16Array", "u16"),)
|
||||
.file("test.js", &fill_test_js("Uint16Array"))
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_Uint32Array() {
|
||||
project()
|
||||
.file("src/lib.rs", &fill_lib("Uint32Array", "u32"),)
|
||||
.file("test.js", &fill_test_js("Uint32Array"))
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_Int8Array() {
|
||||
project()
|
||||
.file("src/lib.rs", &fill_lib("Int8Array", "i8"),)
|
||||
.file("test.js", &fill_test_js("Int8Array"))
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_Int16Array() {
|
||||
project()
|
||||
.file("src/lib.rs", &fill_lib("Int16Array", "i16"),)
|
||||
.file("test.js", &fill_test_js("Int16Array"))
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_Int32Array() {
|
||||
project()
|
||||
.file("src/lib.rs", &fill_lib("Int32Array", "i32"),)
|
||||
.file("test.js", &fill_test_js("Int32Array"))
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_Float32Array() {
|
||||
project()
|
||||
.file("src/lib.rs", &fill_lib("Float32Array", "f32"),)
|
||||
.file("test.js", &fill_test_js("Float32Array"))
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_Float64Array() {
|
||||
project()
|
||||
.file("src/lib.rs", &fill_lib("Float64Array", "f64"),)
|
||||
.file("test.js", &fill_test_js("Float64Array"))
|
||||
.test()
|
||||
}
|
183
crates/js-sys/tests/all/WeakMap.rs
Normal file
183
crates/js-sys/tests/all/WeakMap.rs
Normal file
@ -0,0 +1,183 @@
|
||||
#![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_map() -> js_sys::WeakMap {
|
||||
js_sys::WeakMap::new()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(typeof wasm.new_weak_map(), "object");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.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 get_value(this: &js_sys::WeakMap, key: js_sys::Object) -> JsValue {
|
||||
this.get(key)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let map = new WeakMap();
|
||||
let key = {some: "key"};
|
||||
map.set(key, "value");
|
||||
assert.equal(wasm.get_value(map, key), "value");
|
||||
|
||||
let undef = "unexisting_key";
|
||||
assert.equal(typeof wasm.get_value(map, undef), "undefined");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.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_value(this: &js_sys::WeakMap, key: js_sys::Object, value: JsValue) -> js_sys::WeakMap {
|
||||
this.set(key, value)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let map = new WeakMap();
|
||||
let key = {some: "key"};
|
||||
wasm.set_value(map, key, "value");
|
||||
assert.equal(map.get(key), "value");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.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::WeakMap, key: js_sys::Object) -> bool {
|
||||
this.has(key)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let map = new WeakMap();
|
||||
let key = {some: "key"};
|
||||
map.set(key, "value");
|
||||
assert.equal(wasm.has_value(map, key), true);
|
||||
|
||||
let undef = "unexisting_key";
|
||||
assert.equal(wasm.has_value(map, undef), false);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.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_key(this: &js_sys::WeakMap, key: js_sys::Object) -> bool {
|
||||
this.delete(key)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let map = new WeakMap();
|
||||
let key = {some: "key"};
|
||||
map.set(key, "value");
|
||||
assert.equal(wasm.delete_key(map, key), true);
|
||||
assert.equal(map.has(key), false);
|
||||
assert.equal(wasm.delete_key(map, key), false);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
157
crates/js-sys/tests/all/WeakSet.rs
Normal file
157
crates/js-sys/tests/all/WeakSet.rs
Normal file
@ -0,0 +1,157 @@
|
||||
#![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()
|
||||
}
|
70
crates/js-sys/tests/all/WebAssembly.rs
Normal file
70
crates/js-sys/tests/all/WebAssembly.rs
Normal file
@ -0,0 +1,70 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use super::project;
|
||||
|
||||
#[test]
|
||||
fn validate() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use JsValue;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::WebAssembly;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn validate_wasm(wasm: JsValue) -> JsValue {
|
||||
match WebAssembly::validate(wasm) {
|
||||
Ok(value) => value.into(),
|
||||
Err(err) => err
|
||||
}
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.validate_wasm(new ArrayBuffer(42)), false);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_with_invalid_input() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use JsValue;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::WebAssembly;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn validate_wasm(wasm: JsValue) -> JsValue {
|
||||
match WebAssembly::validate(wasm) {
|
||||
Ok(value) => value.into(),
|
||||
Err(err) => err
|
||||
}
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
try {
|
||||
wasm.validate_wasm(42);
|
||||
assert.ok(false);
|
||||
} catch (e) {
|
||||
assert.ok(true);
|
||||
}
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
246
crates/js-sys/tests/all/main.rs
Normal file
246
crates/js-sys/tests/all/main.rs
Normal file
@ -0,0 +1,246 @@
|
||||
extern crate wasm_bindgen_test_project_builder as project_builder;
|
||||
|
||||
fn project() -> project_builder::Project {
|
||||
let mut p = project_builder::project();
|
||||
p.add_local_dependency("js-sys", env!("CARGO_MANIFEST_DIR"));
|
||||
return p
|
||||
}
|
||||
|
||||
// Keep these tests in alphabetical order, just like the imports in `src/js.rs`.
|
||||
|
||||
mod Array;
|
||||
mod ArrayBuffer;
|
||||
mod ArrayIterator;
|
||||
mod Boolean;
|
||||
mod DataView;
|
||||
mod Date;
|
||||
mod Error;
|
||||
mod Function;
|
||||
mod Generator;
|
||||
mod Intl;
|
||||
mod JsString;
|
||||
mod Map;
|
||||
mod MapIterator;
|
||||
mod Math;
|
||||
mod Number;
|
||||
mod Object;
|
||||
mod Proxy;
|
||||
mod Reflect;
|
||||
mod Set;
|
||||
mod SetIterator;
|
||||
mod Symbol;
|
||||
mod TypedArray;
|
||||
mod WeakMap;
|
||||
mod WeakSet;
|
||||
mod WebAssembly;
|
||||
|
||||
#[test]
|
||||
fn decode_uri() {
|
||||
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 test() {
|
||||
let x = js_sys::decode_uri("https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B")
|
||||
.ok()
|
||||
.expect("should decode URI OK");
|
||||
assert_eq!(String::from(x), "https://mozilla.org/?x=шеллы");
|
||||
|
||||
assert!(js_sys::decode_uri("%E0%A4%A").is_err());
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_uri_component() {
|
||||
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 test() {
|
||||
let x = js_sys::decode_uri_component("%3Fx%3Dtest")
|
||||
.ok()
|
||||
.expect("should decode URI OK");
|
||||
assert_eq!(String::from(x), "?x=test");
|
||||
|
||||
assert!(js_sys::decode_uri_component("%E0%A4%A").is_err());
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_uri() {
|
||||
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 test() {
|
||||
let x = js_sys::encode_uri("ABC abc 123");
|
||||
assert_eq!(String::from(x), "ABC%20abc%20123");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_uri_component() {
|
||||
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 test() {
|
||||
let x = js_sys::encode_uri_component("?x=шеллы");
|
||||
assert_eq!(String::from(x), "%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval() {
|
||||
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 test() {
|
||||
let x = js_sys::eval("42").ok().expect("should eval OK");
|
||||
assert_eq!(x.as_f64().unwrap(), 42.0);
|
||||
|
||||
let err = js_sys::eval("(function () { throw 42; }())")
|
||||
.err()
|
||||
.expect("eval should throw");
|
||||
assert_eq!(err.as_f64().unwrap(), 42.0);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_finite() {
|
||||
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 is_finite(value: &JsValue) -> bool {
|
||||
js_sys::is_finite(value)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.is_finite(42), true);
|
||||
assert.equal(wasm.is_finite(42.1), true);
|
||||
assert.equal(wasm.is_finite('42'), true);
|
||||
assert.equal(wasm.is_finite(NaN), false);
|
||||
assert.equal(wasm.is_finite(Infinity), false);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_int_float() {
|
||||
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 test() {
|
||||
let i = js_sys::parse_int("42", 10);
|
||||
assert_eq!(i as i64, 42);
|
||||
|
||||
let i = js_sys::parse_int("42", 16);
|
||||
assert_eq!(i as i64, 66); // 0x42 == 66
|
||||
|
||||
let i = js_sys::parse_int("invalid int", 10);
|
||||
assert!(i.is_nan());
|
||||
|
||||
let f = js_sys::parse_float("123456.789");
|
||||
assert_eq!(f, 123456.789);
|
||||
|
||||
let f = js_sys::parse_float("invalid float");
|
||||
assert!(f.is_nan());
|
||||
}
|
||||
"#)
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape() {
|
||||
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 test() {
|
||||
assert_eq!(String::from(js_sys::escape("test")), "test");
|
||||
assert_eq!(String::from(js_sys::escape("äöü")), "%E4%F6%FC");
|
||||
assert_eq!(String::from(js_sys::escape("ć")), "%u0107");
|
||||
assert_eq!(String::from(js_sys::escape("@*_+-./")), "@*_+-./");
|
||||
}
|
||||
"#)
|
||||
.test();
|
||||
}
|
Reference in New Issue
Block a user