diff --git a/src/run.rs b/src/run.rs index c84a4c6..8f3cf68 100644 --- a/src/run.rs +++ b/src/run.rs @@ -88,7 +88,7 @@ impl Eval for Expression { #[cfg(test)] mod tests { use super::*; - use crate::ast; + use crate::{ast, types}; #[test] fn test_consts() { @@ -104,4 +104,37 @@ mod tests { Some("foobar".into()) ); } + + #[test] + fn test_vars() { + let mut scope = Scope::default(); + let foo = ast::Location::Variable { name: "foo".into() }; + let bar = ast::Location::Variable { name: "bar".into() }; + assert_eq!(foo.eval(&mut scope), None); + assert_eq!(bar.eval(&mut scope), None); + scope.variables.insert("bar".into(), 42.into()); + assert_eq!(foo.eval(&mut scope), None); + assert_eq!(bar.eval(&mut scope), Some(42.into())); + } + + #[test] + fn test_fields() { + let mut scope = Scope::default(); + let loc = ast::Location::Field { + table: Box::new(ast::Location::Variable { name: "foo".into() }), + index: Box::new(ast::Expression::Constant(ast::Constant::String( + "bar".into(), + ))), + }; + let foo = types::Table::default(); + let bar = types::Table::default(); + scope.variables.insert("foo".into(), foo.clone().into()); + assert_eq!(loc.eval(&mut scope), None); + scope.variables.insert("bar".into(), bar.clone().into()); + assert_eq!(loc.eval(&mut scope), None); + bar.set("foo".into(), Some(666.into())); + assert_eq!(loc.eval(&mut scope), None); + foo.set("bar".into(), Some(42.into())); + assert_eq!(loc.eval(&mut scope), Some(42.into())); + } }