flesh/tests/test_lib_control.rs
2023-03-09 16:03:06 -08:00

220 lines
6.9 KiB
Rust

mod control_lib_tests {
use relish::ast::{eval, lex, 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();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[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();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_complex_case_call() {
let document = "(if true (cons () 1) 2)";
let result = "(1)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_let_multiphase_locals() {
let document = "(let (
(temp '1')
(temp (cons () temp '2')))
temp)";
let result = "('1' '2')";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_let_multibody_evals() {
let document = "(let ((temp '1')) temp (cons () temp '2'))";
let result = "('1' '2')";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_let_multiphase_local_multibody_evals() {
let document = "(let (
(temp '1')
(temp (cons () temp '2')))
(echo 'first body')
(cons temp '3'))";
let result = "('1' '2' '3')";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_while_basic() {
let switch_dec = "(def switch '' true)";
// if prev is true, switch looped once and only once
// else prev will have a problematic type
let while_loop = "
(while switch
(def prev '' switch)
(toggle switch)
(if switch
(def '' prev)
()))";
let test_check = "prev";
let switch_tree = lex(&switch_dec.to_string()).unwrap();
let while_tree = lex(&while_loop.to_string()).unwrap();
let check_tree = lex(&test_check.to_string()).unwrap();
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
eval(&switch_tree, &mut syms).unwrap();
eval(&while_tree, &mut syms).unwrap();
eval(&check_tree, &mut syms).unwrap();
}
#[test]
fn test_while_eval_cond() {
let switch_dec = "(def switch '' true)";
// if prev is true, switch looped once and only once
// else prev will have a problematic type
let while_loop = "
(while (or switch switch)
(def prev '' switch)
(toggle switch)
(if switch
(def '' prev)
()))";
let test_check = "prev";
let switch_tree = lex(&switch_dec.to_string()).unwrap();
let while_tree = lex(&while_loop.to_string()).unwrap();
let check_tree = lex(&test_check.to_string()).unwrap();
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
eval(&switch_tree, &mut syms).unwrap();
eval(&while_tree, &mut syms).unwrap();
eval(&check_tree, &mut syms).unwrap();
}
#[test]
fn test_while_2_iter() {
let additional = "(def sw1 '' true)";
let switch_dec = "(def sw2 '' true)";
// while should loop twice and define result
let while_loop = "
(while sw1
(toggle sw2)
(if (and sw1 sw2)
(def sw1 '' false)
(def result '' 'yay')))";
let test_check = "result";
let another_tree = lex(&additional.to_string()).unwrap();
let switch_tree = lex(&switch_dec.to_string()).unwrap();
let while_tree = lex(&while_loop.to_string()).unwrap();
let check_tree = lex(&test_check.to_string()).unwrap();
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
eval(&another_tree, &mut syms).unwrap();
eval(&switch_tree, &mut syms).unwrap();
eval(&while_tree, &mut syms).unwrap();
eval(&check_tree, &mut syms).unwrap();
}
#[test]
fn test_circuit_basic() {
let document = "(if (circuit true (and true true) true) (def result '' 'passed') ())";
let test = "result";
let doc_tree = lex(&document.to_string()).unwrap();
let test_tree = lex(&test.to_string()).unwrap();
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
eval(&doc_tree, &mut syms).unwrap();
let res = eval(&test_tree, &mut syms);
res.unwrap();
}
#[test]
fn test_circuit_fail() {
let document = "(if (circuit true (and false true) true) (def result '' 'passed') ())";
let test = "result";
let doc_tree = lex(&document.to_string()).unwrap();
let test_tree = lex(&test.to_string()).unwrap();
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
eval(&doc_tree, &mut syms).unwrap();
assert_eq!(
eval(&test_tree, &mut syms).err().unwrap(),
"error in call to result: undefined symbol: result".to_string()
);
}
}