mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 02:00:51 +00:00
Added tests for bind1()
This commit is contained in:
parent
caa86a07a0
commit
06d0704cf8
@ -1072,6 +1072,14 @@ extern "C" {
|
||||
arg3: &JsValue,
|
||||
) -> Result<JsValue, JsValue>;
|
||||
|
||||
/// The bind() method creates a new function that, when called, has its this keyword set to the provided value,
|
||||
/// with a given sequence of arguments preceding any provided when the new function is called.
|
||||
///
|
||||
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)
|
||||
#[wasm_bindgen(method, js_name = bind)]
|
||||
pub fn bind(this: &Function, context: &JsValue) -> Function;
|
||||
|
||||
|
||||
/// The bind() method creates a new function that, when called, has its this keyword set to the provided value,
|
||||
/// with a given sequence of arguments preceding any provided when the new function is called.
|
||||
///
|
||||
|
@ -5,6 +5,15 @@ exports.get_function_to_bind = function() {
|
||||
exports.get_value_to_bind_to = function() {
|
||||
return { x: 2 };
|
||||
};
|
||||
exports.list = function() {
|
||||
return Array.prototype.slice.call(arguments);
|
||||
};
|
||||
exports.add_arguments = function() {
|
||||
return function(arg1, arg2) {return arg1 + arg2}
|
||||
};
|
||||
exports.call_function = function(f) {
|
||||
return f();
|
||||
};
|
||||
exports.call_function_arg = function(f, arg1) {
|
||||
return f(arg1);
|
||||
};
|
||||
|
@ -34,17 +34,42 @@ fn apply() {
|
||||
extern "C" {
|
||||
fn get_function_to_bind() -> Function;
|
||||
fn get_value_to_bind_to() -> JsValue;
|
||||
fn list() -> Function;
|
||||
fn add_arguments() -> Function;
|
||||
fn call_function(f: Function) -> JsValue;
|
||||
fn call_function_arg(f: &Function, arg0: JsValue) -> JsValue;
|
||||
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn bind() {
|
||||
let f = get_function_to_bind();
|
||||
let new_f = f.bind(&get_value_to_bind_to());
|
||||
assert_eq!(call_function(f), 1);
|
||||
assert_eq!(call_function(new_f), 2);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn bind0() {
|
||||
let f = get_function_to_bind();
|
||||
let new_f = f.bind0(&get_value_to_bind_to());
|
||||
assert_eq!(call_function(f), 1);
|
||||
assert_eq!(call_function(new_f), 2);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn bind1() {
|
||||
//let a_list = list();
|
||||
//let prepended_list = a_list.bind1(&JsValue::NULL, &JsValue::from(2));
|
||||
|
||||
let adder = add_arguments();
|
||||
let add_42 = adder.bind1(&JsValue::NULL, &JsValue::from(42));
|
||||
|
||||
assert_eq!(call_function_arg(&add_42, JsValue::from(1)), 43);
|
||||
assert_eq!(call_function_arg(&add_42, JsValue::from(378)), 420);
|
||||
}
|
||||
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn length() {
|
||||
assert_eq!(MAX.length(), 2);
|
||||
|
Loading…
x
Reference in New Issue
Block a user