Files

58 lines
1.4 KiB
Rust

use myc::ast::environment::Environment;
use myc::ast::types::Value;
#[test]
fn repl_preserves_variable_bindings() {
let env = Environment::new();
let r1 = env.run_script("(def x 42)");
assert!(r1.is_ok(), "def should succeed: {:?}", r1.err());
let r2 = env.run_script("(+ x 1)");
assert_eq!(r2.unwrap(), Value::Int(43));
}
#[test]
fn repl_preserves_function_bindings() {
let env = Environment::new();
let r1 = env.run_script("(def double (fn [n] (* n 2)))");
assert!(r1.is_ok(), "def fn should succeed: {:?}", r1.err());
let r2 = env.run_script("(double 21)");
assert_eq!(r2.unwrap(), Value::Int(42));
}
#[test]
fn repl_function_uses_prior_variable() {
let env = Environment::new();
env.run_script("(def x 10)").unwrap();
env.run_script("(def double (fn [n] (* n 2)))").unwrap();
let result = env.run_script("(double x)");
assert_eq!(result.unwrap(), Value::Int(20));
}
#[test]
fn repl_reassign_variable() {
let env = Environment::new();
env.run_script("(def x 1)").unwrap();
env.run_script("(assign x 99)").unwrap();
let result = env.run_script("x");
assert_eq!(result.unwrap(), Value::Int(99));
}
#[test]
fn repl_macro_from_prior_call() {
let env = Environment::new();
env.run_script("(macro unless [c body] `(if ~c ... ~body))")
.unwrap();
let result = env.run_script(r#"(unless false "yes")"#);
assert_eq!(result.unwrap(), Value::Text("yes".into()));
}