test closures!

This commit is contained in:
numzero 2025-04-19 15:24:55 +03:00
parent af28442913
commit 2167b524ec

View File

@ -548,4 +548,51 @@ mod tests {
let the_answerer = remember.call(None, &[Some(42.into())]);
assert_eq!(call(&the_answerer, &[]), Some(42.into()));
}
#[test]
fn test_rw() {
let make_cell = Function {
name: Some("make_cell".into()),
args: vec![],
body: vec![Statement::Local {
name: "data".into(),
init: None,
}],
ret: Some(Box::new(Expression::new_table(
[
(
"get",
Expression::Function(Function {
name: None,
args: vec![],
body: vec![],
ret: Some(Box::new(Expression::new_variable("data"))),
}),
),
(
"set",
Expression::Function(Function {
name: None,
args: vec!["value".into()],
body: vec![Statement::Assign {
target: Location::new_variable("data"),
source: Box::new(Expression::new_variable("value")),
}],
ret: None,
}),
),
]
.into_iter(),
))),
};
let make_cell = build(&make_cell);
let cell1 = make_cell.call(None, &[]);
let cell2 = make_cell.call(None, &[]);
assert_eq!(call(&get(&cell1, "get"), &[]), None);
call(&get(&cell1, "set"), &[Some(42.into())]);
call(&get(&cell2, "set"), &[Some("foobar".into())]);
assert_eq!(call(&get(&cell1, "get"), &[]), Some(42.into()));
call(&get(&cell1, "set"), &[call(&get(&cell2, "get"), &[])]);
assert_eq!(call(&get(&cell1, "get"), &[]), Some("foobar".into()));
}
}