use javascript::{Value, evaluate_script};
#[ctor::ctor]
fn __init_test_logger() {
let _ = env_logger::Builder::from_env(env_logger::Env::default()).is_test(true).try_init();
}
#[test]
fn object_literal_named_function_has_name() {
let script = r#"
(function(){
let o = { x: function foo() { return 1; } };
console.log(o.x());
return o.x.name;
})()
"#;
let res = evaluate_script(script, None::<&std::path::Path>);
match res {
Ok(Value::String(s)) => {
let s = String::from_utf16_lossy(&s);
assert_eq!(s, "foo");
}
other => panic!("Expected string result, got {:?}", other),
}
}
#[test]
fn assigned_generator_named_function_has_name() {
let script = r#"
(function(){
let o = {};
o.x = function* foo() { yield 1; };
console.log(o.x());
console.log(o.x().next());
console.log(o.x().next().value);
return o.x.name;
})()
"#;
let res = evaluate_script(script, None::<&std::path::Path>);
match res {
Ok(Value::String(s)) => {
let s = String::from_utf16_lossy(&s);
assert_eq!(s, "foo");
}
other => panic!("Expected string result, got {:?}", other),
}
}
#[test]
fn assigned_async_named_function_has_name() {
let script = r#"
(function(){
let o = {};
o.x = async function foo() { return 99; };
console.log(await o.x());
o.x().then(v => console.log(v));
return o.x.name;
})()
"#;
let res = evaluate_script(script, None::<&std::path::Path>);
match res {
Ok(Value::String(s)) => {
let s = String::from_utf16_lossy(&s);
assert_eq!(s, "foo");
}
other => panic!("Expected string result, got {:?}", other),
}
}