Skip to content

Commit

Permalink
fix: Environment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 29, 2023
1 parent 38afef2 commit 0373ff3
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions server/src/runtime/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,35 @@ impl Environment {
panic!("Invalid variable distance.");
}
}

#[cfg(test)]
mod test{
use crate::runtime::{types::RV, interpreter::HaltReason};

#[test]
fn test_read_basic() {
let env = super::Environment::new(None);
env.borrow_mut().declare("five".to_string(), RV::Num(5.0));
assert_eq!(env.borrow().read("five").unwrap(), RV::Num(5.0));
}

#[test]
fn test_read_from_parent() {
let parent = super::Environment::new(None);
parent.borrow_mut().declare("five".to_string(), RV::Num(5.0));
let child = super::Environment::new(Some(parent.clone()));
assert_eq!(child.borrow().read("five").unwrap(), RV::Num(5.0));
}

#[test]
fn test_read_undefined_variable() {
let env = super::Environment::new(None);
assert!(env.borrow().read("five").is_err());
}

#[test]
fn test_assign_to_undefined_variable() {
let env = super::Environment::new(None);
assert!(env.borrow_mut().assign("five".to_string(), RV::Num(5.0)).is_err());
}
}

0 comments on commit 0373ff3

Please sign in to comment.