Port Symbol and SetIterator tests to wasm

This commit is contained in:
Alex Crichton 2018-07-20 16:19:10 -07:00
parent 54abca7344
commit df3530a9c2
8 changed files with 292 additions and 671 deletions

View File

@ -2582,7 +2582,7 @@ extern "C" {
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/keyFor
#[wasm_bindgen(static_method_of = Symbol, js_name = keyFor)]
pub fn key_for(sym: &Symbol) -> JsString;
pub fn key_for(sym: &Symbol) -> JsValue;
/// The toString() method returns a string representing the specified Symbol object.
///

View File

@ -1,97 +0,0 @@
#![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()
}

View File

@ -1,571 +0,0 @@
#![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();
}

View File

@ -12,8 +12,6 @@ fn project() -> project_builder::Project {
mod ArrayIterator;
mod Reflect;
mod SetIterator;
mod Symbol;
mod TypedArray;
mod WeakMap;
mod WeakSet;

View File

@ -0,0 +1,54 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use js_sys::*;
#[wasm_bindgen]
extern {
type GenericIterator;
#[wasm_bindgen(method, structural)]
fn next(this: &GenericIterator) -> IteratorNext;
type IteratorNext;
#[wasm_bindgen(method, structural, getter)]
fn value(this: &IteratorNext) -> JsValue;
#[wasm_bindgen(method, structural, getter)]
fn done(this: &IteratorNext) -> bool;
}
#[wasm_bindgen_test]
fn entries() {
let s = Set::new(&JsValue::undefined());
s.add(&1.into());
let iter = GenericIterator::from(JsValue::from(s.entries()));
let obj = iter.next();
assert!(!obj.done());
let array = Array::from(obj.value());
assert_eq!(array.length(), 2);
array.for_each(&mut |a, _, _| {
assert_eq!(a, 1);
});
assert!(iter.next().done());
}
#[wasm_bindgen_test]
fn keys() {
let s = Set::new(&JsValue::undefined());
s.add(&1.into());
let iter = GenericIterator::from(JsValue::from(s.keys()));
let obj = iter.next();
assert!(!obj.done());
assert_eq!(obj.value(), 1);
assert!(iter.next().done());
}
#[wasm_bindgen_test]
fn values() {
let s = Set::new(&JsValue::undefined());
s.add(&1.into());
let iter = GenericIterator::from(JsValue::from(s.values()));
let obj = iter.next();
assert!(!obj.done());
assert_eq!(obj.value(), 1);
assert!(iter.next().done());
}

View File

@ -0,0 +1,127 @@
const assert = require('assert');
exports.test_has_instance = function(sym) {
class Array1 {
static [sym](instance) {
return Array.isArray(instance);
}
}
assert.equal(typeof sym, "symbol");
assert.ok([] instanceof Array1);
};
exports.test_is_concat_spreadable = function(sym) {
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[sym] = false;
alphaNumeric = alpha.concat(numeric);
assert.deepEqual(alphaNumeric, ["a", "b", "c", [1, 2, 3]]);
};
exports.test_iterator = function(sym) {
const iterable1 = new Object();
iterable1[sym] = function* () {
yield 1;
yield 2;
yield 3;
};
assert.deepEqual([...iterable1], [1, 2, 3]);
};
exports.test_match = function(sym) {
const regexp1 = /foo/;
assert.throws(() => '/foo/'.startsWith(regexp1));
regexp1[sym] = false;
assert.ok('/foo/'.startsWith(regexp1));
assert.equal('/baz/'.endsWith(regexp1), false);
};
exports.test_replace = function(sym) {
class Replace1 {
constructor(value) {
this.value = value;
}
[sym](string) {
return `s/${string}/${this.value}/g`;
}
}
assert.equal('foo'.replace(new Replace1('bar')), 's/foo/bar/g');
};
exports.test_search = function(sym) {
class Search1 {
constructor(value) {
this.value = value;
}
[sym](string) {
return string.indexOf(this.value);
}
}
assert.equal('foobar'.search(new Search1('bar')), 3);
};
exports.test_species = function(sym) {
class Array1 extends Array {
static get [sym]() { return Array; }
}
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);
};
exports.test_split = function(sym) {
class Split1 {
constructor(value) {
this.value = value;
}
[sym](string) {
var index = string.indexOf(this.value);
return this.value + string.substr(0, index) + "/"
+ string.substr(index + this.value.length);
}
}
assert.equal('foobar'.split(new Split1('foo')), 'foo/bar');
};
exports.test_to_primitive = function(sym) {
const object1 = {
[sym](hint) {
if (hint == 'number') {
return 42;
}
return null;
}
};
assert.equal(+object1, 42);
};
exports.test_to_string_tag = function(sym) {
class ValidatorClass {
get [sym]() {
return 'Validator';
}
}
assert.equal(Object.prototype.toString.call(new ValidatorClass()), '[object Validator]');
};

View File

@ -0,0 +1,108 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use js_sys::*;
#[wasm_bindgen(module = "tests/wasm/Symbol.js", version = "*")]
extern {
fn test_has_instance(sym: &Symbol);
fn test_is_concat_spreadable(sym: &Symbol);
fn test_iterator(sym: &Symbol);
fn test_match(sym: &Symbol);
fn test_replace(sym: &Symbol);
fn test_search(sym: &Symbol);
fn test_species(sym: &Symbol);
fn test_split(sym: &Symbol);
fn test_to_primitive(sym: &Symbol);
fn test_to_string_tag(sym: &Symbol);
}
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_name = Symbol)]
fn gensym(val: JsValue) -> Symbol;
}
#[wasm_bindgen_test]
fn has_instance() {
test_has_instance(&Symbol::has_instance());
}
#[wasm_bindgen_test]
fn is_concat_spreadable() {
test_is_concat_spreadable(&Symbol::is_concat_spreadable());
}
#[wasm_bindgen_test]
fn iterator() {
test_iterator(&Symbol::iterator());
}
#[wasm_bindgen_test]
fn match_() {
test_match(&Symbol::match_());
}
#[wasm_bindgen_test]
fn replace() {
test_replace(&Symbol::replace());
}
#[wasm_bindgen_test]
fn search() {
test_search(&Symbol::search());
}
#[wasm_bindgen_test]
fn species() {
test_species(&Symbol::species());
}
#[wasm_bindgen_test]
fn split() {
test_split(&Symbol::split());
}
#[wasm_bindgen_test]
fn to_primitive() {
test_to_primitive(&Symbol::to_primitive());
}
#[wasm_bindgen_test]
fn to_string_tag() {
test_to_string_tag(&Symbol::to_string_tag());
}
#[wasm_bindgen_test]
fn for_() {
let foo = JsValue::from(Symbol::for_(&"foo".into()));
let bar = JsValue::from(Symbol::for_(&"bar".into()));
assert_eq!(foo, foo);
assert_eq!(bar, bar);
assert_ne!(foo, bar);
assert_ne!(bar, foo);
assert_eq!(Symbol::for_(&"mario".into()).to_string(), "Symbol(mario)");
}
#[wasm_bindgen_test]
fn key_for() {
let sym = Symbol::for_(&"foo".into());
assert_eq!(Symbol::key_for(&sym), "foo");
assert!(Symbol::key_for(&Symbol::iterator()).is_undefined());
assert!(Symbol::key_for(&gensym(JsValue::undefined())).is_undefined());
}
#[wasm_bindgen_test]
fn to_string() {
assert_eq!(Symbol::iterator().to_string(), "Symbol(Symbol.iterator)");
assert_eq!(Symbol::for_(&"foo".into()).to_string(), "Symbol(foo)");
assert_eq!(gensym("desc".into()).to_string(), "Symbol(desc)");
}
#[wasm_bindgen_test]
fn value_of() {
let a = Symbol::for_(&"foo".into());
assert_eq!(JsValue::from(a.value_of()), JsValue::from(a));
let a = gensym(JsValue::undefined());
assert_eq!(JsValue::from(a.value_of()), JsValue::from(a));
}

View File

@ -24,3 +24,5 @@ pub mod Number;
pub mod Object;
pub mod Proxy;
pub mod Set;
pub mod SetIterator;
pub mod Symbol;