Files
OxideJS-New/crates/oxide_vm/tests/vm_tests.rs
T

86 lines
2.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use std::sync::Arc;
use oxide_compiler::compiler::Compiler;
use oxide_compiler::DefaultCompilerService;
use oxide_parser::Allocator;
use oxide_vm::vm::Vm;
fn eval(source: &str) -> String {
let allocator = Allocator::default();
let program = match oxide_parser::parse(&allocator, source) {
Ok(p) => p,
Err(e) => return format!("parse error: {}", e[0].message),
};
let module = match Compiler::new().compile(&program) {
Ok(m) => m,
Err(e) => return format!("compile error: {e}"),
};
let mut vm = Vm::new();
vm.set_compiler_service(Arc::new(DefaultCompilerService));
match vm.run(&Arc::new(module)) {
Ok(result) => format!("{result}"),
Err(e) => format!("vm error: {e}"),
}
}
#[test]
fn test_throw_caught_by_catch() {
assert_eq!(eval("try { throw 42; } catch(e) { e; }"), "42");
}
#[test]
fn test_throw_without_catch() {
let result = eval("throw 'err';");
assert!(result.contains("uncaught"), "expected uncaught, got: {result}");
}
#[test]
fn test_try_finally_normal_path() {
assert_eq!(eval("var x = 1; try { x = 2; } finally { x = 3; } x;"), "3");
}
#[test]
fn test_try_finally_exception_path() {
let result = eval("var x = 0; try { throw 1; } finally { x = 2; }");
assert!(result.contains("uncaught"), "expected uncaught, got: {result}");
}
#[test]
fn test_nested_try_inner_catches() {
assert_eq!(eval("try { try { throw 1; } catch(e) { e; } } catch(e) { 2; }"), "1");
}
#[test]
fn test_nested_try_outer_catches() {
assert_eq!(eval("try { try { throw 1; } catch(e) { throw 2; } } catch(e) { e; }"), "2");
}
#[test]
fn test_try_catch_finally() {
assert_eq!(eval("var x = 0; try { x = 1; } catch(e) { x = 2; } finally { x = 3; } x;"), "3");
}
#[test]
fn test_catch_param_binding() {
assert_eq!(eval("try { throw 99; } catch(myErr) { myErr; }"), "99");
}
#[test]
fn test_throw_err_type_error_catchable() {
let result = eval("try { null.prop = 1; } catch(e) { 42; }");
assert_eq!(result, "42");
}
#[test]
fn test_throw_err_type_error_is_object() {
// 对 null 的 IC_GET_PROP 抛可捕获的 TypeError;catch 体执行。
let result = eval("try { null.prop; } catch(e) { 99; }");
assert_eq!(result, "99");
}
#[test]
fn test_uncaught_type_error_message() {
let result = eval("null.prop;");
assert!(result.contains("TypeError"), "expected TypeError in message, got: {result}");
}