test: 拆分 VM 测试并新增多项测试
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
use oxide_compiler::compiler::Compiler;
|
||||
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();
|
||||
match vm.run(&module) {
|
||||
Ok(result) => format!("{result}"),
|
||||
Err(e) => format!("vm error: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn object_create_and_read() {
|
||||
let allocator = Allocator::default();
|
||||
let source = "({a:1})";
|
||||
let program = oxide_parser::parse(&allocator, source).expect("parse failed");
|
||||
let module = Compiler::new().compile(&program).expect("compile failed");
|
||||
let mut vm = Vm::new();
|
||||
let obj = vm.run(&module).expect("vm run failed");
|
||||
assert!(obj.is_object());
|
||||
let obj_ref = unsafe { &*obj.as_js_object_ptr() };
|
||||
assert_eq!(obj_ref.prop_count(), 1, "object should have 1 property");
|
||||
assert!(obj_ref.get_inline_prop(0).is_int());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_object_property_read() {
|
||||
assert_eq!(eval("({a:1,b:2}).b"), "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_object_missing_property() {
|
||||
assert_eq!(eval("({a:1}).b"), "undefined");
|
||||
}
|
||||
Reference in New Issue
Block a user