mod control_lib_tests { use relish::ast::{eval, lex, Ctr, SymTable}; use relish::stdlib::{dynamic_stdlib, static_stdlib}; #[test] fn test_if_first_case_singlet() { let document = "(if true 1 2)"; let result = 1; let mut syms = SymTable::new(); static_stdlib(&mut syms).unwrap(); dynamic_stdlib(&mut syms).unwrap(); if let Ok(tree) = lex(&document.to_string()) { if let Ctr::Integer(i) = *eval(&tree, &mut syms).unwrap() { assert_eq!(i, result); } else { assert!(false); } } else { assert!(false); } } #[test] fn test_if_second_case_singlet() { let document = "(if false 1 2)"; let result = 2; let mut syms = SymTable::new(); static_stdlib(&mut syms).unwrap(); dynamic_stdlib(&mut syms).unwrap(); if let Ok(tree) = lex(&document.to_string()) { if let Ctr::Integer(i) = *eval(&tree, &mut syms).unwrap() { assert_eq!(i, result); } else { eprintln!("{}", *eval(&tree, &mut syms).unwrap()); assert!(false); } } else { assert!(false); } } #[test] fn test_complex_case_call() { let document = "(if true (append () 1) 2)"; let result = "(1)"; let mut syms = SymTable::new(); static_stdlib(&mut syms).unwrap(); dynamic_stdlib(&mut syms).unwrap(); if let Ok(tree) = lex(&document.to_string()) { if let Ctr::Seg(ref i) = *eval(&tree, &mut syms).unwrap() { assert_eq!(i.to_string(), result); } else { assert!(false); } } else { assert!(false); } } #[test] fn test_let_multiphase_locals() { let document = "(let ( (temp '1') (temp (append () temp '2'))) temp)"; let result = "('1' '2')"; let mut syms = SymTable::new(); static_stdlib(&mut syms).unwrap(); dynamic_stdlib(&mut syms).unwrap(); if let Ok(tree) = lex(&document.to_string()) { if let Ctr::Seg(ref i) = *eval(&tree, &mut syms).unwrap() { assert_eq!(i.to_string(), result); } else { assert!(false); } } else { assert!(false); } } #[test] fn test_let_multibody_evals() { let document = "(let ((temp '1')) temp (append () temp '2'))"; let result = "('1' '2')"; let mut syms = SymTable::new(); static_stdlib(&mut syms).unwrap(); dynamic_stdlib(&mut syms).unwrap(); if let Ok(tree) = lex(&document.to_string()) { if let Ctr::Seg(ref i) = *eval(&tree, &mut syms).unwrap() { assert_eq!(i.to_string(), result); } else { assert!(false); } } else { assert!(false); } } #[test] fn test_let_multiphase_local_multibody_evals() { // prints 'first body' and then returns ('1' '2' '3') let document = "(let ( (temp '1') (temp (append () temp '2'))) (echo 'first body') (append temp '3'))"; let result = "('1' '2' '3')"; let mut syms = SymTable::new(); static_stdlib(&mut syms).unwrap(); dynamic_stdlib(&mut syms).unwrap(); if let Ok(tree) = lex(&document.to_string()) { if let Ctr::Seg(ref i) = *eval(&tree, &mut syms).unwrap() { assert_eq!(i.to_string(), result); } else { assert!(false); } } else { assert!(false); } } }