add test for local x = x

This commit is contained in:
numzero 2025-04-19 14:42:53 +03:00
parent 2747749c37
commit 221cd6e149
2 changed files with 39 additions and 0 deletions

View File

@ -59,6 +59,13 @@ impl Location {
pub fn new_variable(name: impl Into<String>) -> Self {
Self::Variable { name: name.into() }
}
pub fn new_field(table: impl Into<Expression>, name: impl Into<String>) -> Self {
Self::Field {
table: Box::new(table.into()),
index: Box::new(Expression::new_constant(name.into())),
}
}
}
impl Expression {

View File

@ -427,4 +427,36 @@ mod tests {
assert_eq!(ret, Some(3.into()));
assert_eq!(env.get("x".into()), Some(1.into()));
}
#[test]
fn test_self_assign() {
let testee = Function {
name: Some("testee".into()),
args: vec![],
body: vec![
Statement::Assign {
target: Location::new_variable("x"),
source: Box::new(Expression::new_constant(42)),
},
Statement::Local {
name: "x".into(),
init: Some(Box::new(Expression::new_variable("x"))),
},
Statement::Assign {
target: Location::new_field(Expression::new_variable("_ENV"), "x"),
source: Box::new(Expression::new_constant(666)),
},
Statement::Local {
name: "y".into(),
init: Some(Box::new(Expression::new_variable("x"))),
},
],
ret: Some(Box::new(Expression::new_variable("y"))),
};
let env = crate::types::Table::default();
let testee = build(&testee);
let ret = testee.call(Some(env.clone().into()), &[]);
assert_eq!(ret, Some(42.into()));
assert_eq!(env.get("x".into()), Some(666.into()));
}
}