2023-02-27 22:53:54 -08:00
|
|
|
mod control_lib_tests {
|
2023-03-05 22:18:49 -08:00
|
|
|
use relish::ast::{eval, lex, SymTable};
|
2023-03-01 11:38:02 -08:00
|
|
|
use relish::stdlib::{dynamic_stdlib, static_stdlib};
|
2023-02-27 22:53:54 -08:00
|
|
|
|
|
|
|
|
#[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();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-02-27 22:53:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-02-27 22:53:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_complex_case_call() {
|
2023-03-09 16:03:06 -08:00
|
|
|
let document = "(if true (cons () 1) 2)";
|
2023-02-27 22:53:54 -08:00
|
|
|
let result = "(1)";
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-02-27 22:53:54 -08:00
|
|
|
}
|
2023-03-01 15:17:50 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_let_multiphase_locals() {
|
|
|
|
|
let document = "(let (
|
|
|
|
|
(temp '1')
|
2023-03-09 16:03:06 -08:00
|
|
|
(temp (cons () temp '2')))
|
2023-03-01 15:17:50 -08:00
|
|
|
temp)";
|
|
|
|
|
let result = "('1' '2')";
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
2023-03-17 12:01:43 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_let_def_escapes_locals() {
|
|
|
|
|
let document1 = "(let (
|
|
|
|
|
(temp 'hello')
|
|
|
|
|
(temp (concat temp ' ' 'world')))
|
|
|
|
|
(def global '' temp))";
|
|
|
|
|
let document2 = "global";
|
|
|
|
|
let result = "('hello world')";
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-17 12:01:43 -07:00
|
|
|
eval(&lex(&document1.to_string()).unwrap(), &mut syms).unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&lex(&document2.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
|
|
|
|
result.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
);
|
2023-03-01 15:17:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_let_multibody_evals() {
|
2023-03-09 16:03:06 -08:00
|
|
|
let document = "(let ((temp '1')) temp (cons () temp '2'))";
|
2023-03-01 15:17:50 -08:00
|
|
|
let result = "('1' '2')";
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-03-01 15:17:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_let_multiphase_local_multibody_evals() {
|
|
|
|
|
let document = "(let (
|
|
|
|
|
(temp '1')
|
2023-03-09 16:03:06 -08:00
|
|
|
(temp (cons () temp '2')))
|
2023-03-01 15:17:50 -08:00
|
|
|
(echo 'first body')
|
2023-03-09 16:03:06 -08:00
|
|
|
(cons temp '3'))";
|
2023-03-01 15:17:50 -08:00
|
|
|
|
|
|
|
|
let result = "('1' '2' '3')";
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-03-01 15:17:50 -08:00
|
|
|
}
|
2023-03-02 15:29:50 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_while_basic() {
|
2023-03-05 22:18:49 -08:00
|
|
|
let switch_dec = "(def switch '' true)";
|
2023-03-02 15:29:50 -08:00
|
|
|
// if prev is true, switch looped once and only once
|
|
|
|
|
// else prev will have a problematic type
|
|
|
|
|
let while_loop = "
|
|
|
|
|
(while switch
|
2023-03-05 22:18:49 -08:00
|
|
|
(def prev '' switch)
|
2023-03-02 15:29:50 -08:00
|
|
|
(toggle switch)
|
|
|
|
|
(if switch
|
2023-03-05 22:18:49 -08:00
|
|
|
(def '' prev)
|
2023-03-02 15:29:50 -08:00
|
|
|
()))";
|
|
|
|
|
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();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-02 15:29:50 -08:00
|
|
|
|
|
|
|
|
eval(&switch_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&while_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&check_tree, &mut syms).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_while_eval_cond() {
|
2023-03-05 22:18:49 -08:00
|
|
|
let switch_dec = "(def switch '' true)";
|
2023-03-02 15:29:50 -08:00
|
|
|
// if prev is true, switch looped once and only once
|
|
|
|
|
// else prev will have a problematic type
|
|
|
|
|
let while_loop = "
|
|
|
|
|
(while (or switch switch)
|
2023-03-05 22:18:49 -08:00
|
|
|
(def prev '' switch)
|
2023-03-02 15:29:50 -08:00
|
|
|
(toggle switch)
|
|
|
|
|
(if switch
|
2023-03-05 22:18:49 -08:00
|
|
|
(def '' prev)
|
2023-03-02 15:29:50 -08:00
|
|
|
()))";
|
|
|
|
|
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();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-02 15:29:50 -08:00
|
|
|
|
|
|
|
|
eval(&switch_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&while_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&check_tree, &mut syms).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_while_2_iter() {
|
2023-03-05 22:18:49 -08:00
|
|
|
let additional = "(def sw1 '' true)";
|
|
|
|
|
let switch_dec = "(def sw2 '' true)";
|
2023-03-02 15:29:50 -08:00
|
|
|
// while should loop twice and define result
|
|
|
|
|
let while_loop = "
|
|
|
|
|
(while sw1
|
|
|
|
|
(toggle sw2)
|
|
|
|
|
(if (and sw1 sw2)
|
2023-03-05 22:18:49 -08:00
|
|
|
(def sw1 '' false)
|
|
|
|
|
(def result '' 'yay')))";
|
2023-03-02 15:29:50 -08:00
|
|
|
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();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-02 15:29:50 -08:00
|
|
|
|
|
|
|
|
eval(&another_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&switch_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&while_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&check_tree, &mut syms).unwrap();
|
|
|
|
|
}
|
2023-03-03 14:29:53 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_circuit_basic() {
|
2023-03-05 22:18:49 -08:00
|
|
|
let document = "(if (circuit true (and true true) true) (def result '' 'passed') ())";
|
2023-03-03 14:29:53 -08:00
|
|
|
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();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-03 14:29:53 -08:00
|
|
|
|
|
|
|
|
eval(&doc_tree, &mut syms).unwrap();
|
|
|
|
|
let res = eval(&test_tree, &mut syms);
|
|
|
|
|
res.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_circuit_fail() {
|
2023-03-05 22:18:49 -08:00
|
|
|
let document = "(if (circuit true (and false true) true) (def result '' 'passed') ())";
|
2023-03-03 14:29:53 -08:00
|
|
|
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();
|
2023-03-24 18:14:33 -07:00
|
|
|
dynamic_stdlib(&mut syms, None).unwrap();
|
2023-03-03 14:29:53 -08:00
|
|
|
|
|
|
|
|
eval(&doc_tree, &mut syms).unwrap();
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
|
|
|
|
eval(&test_tree, &mut syms).err().unwrap(),
|
|
|
|
|
"error in call to result: undefined symbol: result".to_string()
|
|
|
|
|
);
|
2023-03-03 14:29:53 -08:00
|
|
|
}
|
2023-02-27 22:53:54 -08:00
|
|
|
}
|