use javascript::{Value, evaluate_script};
#[test]
fn for_of_missing_iterator_throws() {
let res = evaluate_script("for (var x of {}) { }; 1", None::<&std::path::Path>);
assert!(res.is_err());
}
#[test]
fn iterator_next_returns_non_object_throws() {
let script = r#"
let s = Symbol.iterator;
let o = {};
o[s] = function() {
return { next: function() { return 42; } };
};
// Attempt to iterate will call next() which returns a non-object -> should error
for (let x of o) { }
1
"#;
let res = evaluate_script(script, None::<&std::path::Path>);
assert!(res.is_err());
}
#[test]
fn string_iteration_surrogate_pair_behaviour() {
let script = r#"
let s = "a😀b"; // contains an astral character
let acc = "";
for (let ch of s) { acc = acc + ch; }
acc
"#;
let res = evaluate_script(script, None::<&std::path::Path>);
match res {
Ok(Value::String(s)) => {
let rust_str = String::from_utf16_lossy(&s);
assert!(rust_str.starts_with("a"));
assert!(rust_str.ends_with("b"));
assert!(rust_str.len() >= 2);
}
other => panic!("Expected string result from iteration, got {:?}", other),
}
}