From ad86c52b02dc7cc14f2c2a917338df6054a7c07c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 20 Jul 2018 15:35:15 -0700 Subject: [PATCH] Port `Proxy` tests to `wasm` --- crates/js-sys/tests/all/Proxy.rs | 76 ------------------------------- crates/js-sys/tests/all/main.rs | 1 - crates/js-sys/tests/wasm/Proxy.js | 11 +++++ crates/js-sys/tests/wasm/Proxy.rs | 46 +++++++++++++++++++ crates/js-sys/tests/wasm/main.rs | 1 + 5 files changed, 58 insertions(+), 77 deletions(-) delete mode 100644 crates/js-sys/tests/all/Proxy.rs create mode 100644 crates/js-sys/tests/wasm/Proxy.js create mode 100644 crates/js-sys/tests/wasm/Proxy.rs diff --git a/crates/js-sys/tests/all/Proxy.rs b/crates/js-sys/tests/all/Proxy.rs deleted file mode 100644 index 562c3240..00000000 --- a/crates/js-sys/tests/all/Proxy.rs +++ /dev/null @@ -1,76 +0,0 @@ -#![allow(non_snake_case)] - -use project; - -#[test] -fn new() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn new_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() -} diff --git a/crates/js-sys/tests/all/main.rs b/crates/js-sys/tests/all/main.rs index 1c55acfe..e74db0b0 100644 --- a/crates/js-sys/tests/all/main.rs +++ b/crates/js-sys/tests/all/main.rs @@ -11,7 +11,6 @@ fn project() -> project_builder::Project { // Keep these tests in alphabetical order, just like the imports in `src/js.rs`. mod ArrayIterator; -mod Proxy; mod Reflect; mod Set; mod SetIterator; diff --git a/crates/js-sys/tests/wasm/Proxy.js b/crates/js-sys/tests/wasm/Proxy.js new file mode 100644 index 00000000..eb0a1258 --- /dev/null +++ b/crates/js-sys/tests/wasm/Proxy.js @@ -0,0 +1,11 @@ +exports.proxy_target = function() { + return { a: 100 }; +}; + +exports.proxy_handler = function() { + return { + get: function(obj, prop) { + return prop in obj ? obj[prop] : 37; + } + }; +}; diff --git a/crates/js-sys/tests/wasm/Proxy.rs b/crates/js-sys/tests/wasm/Proxy.rs new file mode 100644 index 00000000..b362b888 --- /dev/null +++ b/crates/js-sys/tests/wasm/Proxy.rs @@ -0,0 +1,46 @@ +use wasm_bindgen::prelude::*; +use wasm_bindgen_test::*; +use js_sys::*; + +#[wasm_bindgen(module = "tests/wasm/Proxy.js", version = "*")] +extern { + fn proxy_target() -> JsValue; + fn proxy_handler() -> Object; + + type Custom; + #[wasm_bindgen(method, getter, structural, catch)] + fn a(this: &Custom) -> Result; + #[wasm_bindgen(method, getter, structural, catch)] + fn b(this: &Custom) -> Result; + + + type RevocableResult; + #[wasm_bindgen(method, getter, structural)] + fn proxy(this: &RevocableResult) -> JsValue; + #[wasm_bindgen(method, getter, structural)] + fn revoke(this: &RevocableResult) -> Function; +} + +#[wasm_bindgen_test] +fn new() { + let proxy = Proxy::new(&proxy_target(), &proxy_handler()); + let proxy = Custom::from(JsValue::from(proxy)); + assert_eq!(proxy.a().unwrap(), 100); + assert_eq!(proxy.b().unwrap(), 37); +} + +#[wasm_bindgen_test] +fn revocable() { + let result = Proxy::revocable(&proxy_target(), &proxy_handler()); + let result = RevocableResult::from(JsValue::from(result)); + let proxy = result.proxy(); + let revoke = result.revoke(); + + let obj = Custom::from(proxy); + assert_eq!(obj.a().unwrap(), 100); + assert_eq!(obj.b().unwrap(), 37); + revoke.apply(&JsValue::undefined(), &Array::new()).unwrap(); + assert!(obj.a().is_err()); + assert!(obj.b().is_err()); + assert!(JsValue::from(obj).is_object()); +} diff --git a/crates/js-sys/tests/wasm/main.rs b/crates/js-sys/tests/wasm/main.rs index b02c738f..31c1cc98 100644 --- a/crates/js-sys/tests/wasm/main.rs +++ b/crates/js-sys/tests/wasm/main.rs @@ -22,3 +22,4 @@ pub mod MapIterator; pub mod Math; pub mod Number; pub mod Object; +pub mod Proxy;