From c70ed36be43938184b6fb0698509aa0a9c8d7731 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 30 Apr 2019 10:02:11 -0700 Subject: [PATCH] Add some tests which do computation in getters/setters --- tests/wasm/getters_and_setters.js | 8 +++++++ tests/wasm/getters_and_setters.rs | 38 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/tests/wasm/getters_and_setters.js b/tests/wasm/getters_and_setters.js index 5f9becf48..def62b39f 100644 --- a/tests/wasm/getters_and_setters.js +++ b/tests/wasm/getters_and_setters.js @@ -48,3 +48,11 @@ exports._7_js = (rules) => { rules.new_js_name__setter_with_name__no_setter_without_name_for_field = value * 2; return rules; } + +exports.test_getter_compute = x => { + assert.equal(x.foo, 3) +}; + +exports.test_setter_compute = x => { + x.foo = 97; +}; diff --git a/tests/wasm/getters_and_setters.rs b/tests/wasm/getters_and_setters.rs index ffb8e00ad..cb693e116 100644 --- a/tests/wasm/getters_and_setters.rs +++ b/tests/wasm/getters_and_setters.rs @@ -1,3 +1,5 @@ +use std::rc::Rc; +use std::cell::Cell; use wasm_bindgen::prelude::*; use wasm_bindgen_test::*; @@ -10,6 +12,9 @@ extern "C" { fn _5_js(rules: Rules) -> Rules; fn _6_js(rules: Rules) -> Rules; fn _7_js(rules: Rules) -> Rules; + + fn test_getter_compute(x: GetterCompute); + fn test_setter_compute(x: SetterCompute); } // Each getter/setter combination is derived @@ -20,6 +25,7 @@ pub struct Rules { } #[wasm_bindgen] +#[allow(non_snake_case)] impl Rules { #[wasm_bindgen] pub fn no_js_name__no_getter_with_name__no_getter_without_name(&self) -> i32 { @@ -123,3 +129,35 @@ fn _7_rust() { let rules = _7_js(Rules { field: 7 }); assert_eq!(rules.field, 14); } + +#[wasm_bindgen] +struct GetterCompute; + +#[wasm_bindgen] +impl GetterCompute { + #[wasm_bindgen(getter)] + pub fn foo(&self) -> u32 { 3 } +} + +#[wasm_bindgen_test] +fn getter_compute() { + test_getter_compute(GetterCompute); +} + +#[wasm_bindgen] +struct SetterCompute(Rc>); + +#[wasm_bindgen] +impl SetterCompute { + #[wasm_bindgen(setter)] + pub fn set_foo(&self, x: u32) { + self.0.set(x + 3); + } +} + +#[wasm_bindgen_test] +fn setter_compute() { + let r = Rc::new(Cell::new(3)); + test_setter_compute(SetterCompute(r.clone())); + assert_eq!(r.get(), 100); +}