add test for local x = x
This commit is contained in:
parent
2747749c37
commit
221cd6e149
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user