2023-02-27 22:53:54 -08:00
|
|
|
mod control_lib_tests {
|
2024-02-06 22:39:08 +00:00
|
|
|
use flesh::ast::{eval, lex, SymTable};
|
2024-07-10 13:22:28 -07:00
|
|
|
use flesh::stdlib::static_stdlib;
|
2023-02-27 22:53:54 -08:00
|
|
|
|
2023-06-20 01:25:19 +00:00
|
|
|
#[test]
|
|
|
|
|
fn test_assert_t() {
|
|
|
|
|
let document = "(assert true)";
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms);
|
|
|
|
|
eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_assert_f() {
|
|
|
|
|
let document = "(assert false)";
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms);
|
|
|
|
|
assert!(eval(&lex(&document.to_string()).unwrap(), &mut syms).is_err())
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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 (
|
2024-07-10 13:22:28 -07:00
|
|
|
(temp \"1\")
|
|
|
|
|
(temp (cons () temp \"2\")))
|
2023-03-01 15:17:50 -08:00
|
|
|
temp)";
|
2024-07-10 13:22:28 -07:00
|
|
|
let result = "(\"1\" \"2\")";
|
2023-03-01 15:17:50 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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 (
|
2024-07-10 13:22:28 -07:00
|
|
|
(temp \"hello\")
|
|
|
|
|
(temp (concat temp \" \" \"world\")))
|
|
|
|
|
(def global \"\" temp))";
|
2023-03-17 12:01:43 -07:00
|
|
|
let document2 = "global";
|
2024-07-10 13:22:28 -07:00
|
|
|
let result = "(\"hello world\")";
|
2023-03-17 12:01:43 -07:00
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(let ((temp \"1\")) temp (cons () temp \"2\"))";
|
|
|
|
|
let result = "(\"1\" \"2\")";
|
2023-03-01 15:17:50 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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 (
|
2024-07-10 13:22:28 -07:00
|
|
|
(temp \"1\")
|
|
|
|
|
(temp (cons () temp \"2\")))
|
|
|
|
|
(echo \"first body\")
|
|
|
|
|
(cons temp \"3\"))";
|
2023-03-01 15:17:50 -08:00
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
let result = "(\"1\" \"2\" \"3\")";
|
2023-03-01 15:17:50 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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() {
|
2024-07-10 13:22:28 -07: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
|
2024-07-10 13:22:28 -07:00
|
|
|
(def prev \"\" switch)
|
2023-03-02 15:29:50 -08:00
|
|
|
(toggle switch)
|
|
|
|
|
(if switch
|
2024-07-10 13:22:28 -07: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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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() {
|
2024-07-10 13:22:28 -07: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)
|
2024-07-10 13:22:28 -07:00
|
|
|
(def prev \"\" switch)
|
2023-03-02 15:29:50 -08:00
|
|
|
(toggle switch)
|
|
|
|
|
(if switch
|
2024-07-10 13:22:28 -07: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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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() {
|
2024-07-10 13:22:28 -07: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)
|
2024-07-10 13:22:28 -07: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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(if (circuit true (and true true) 0 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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-03 14:29:53 -08:00
|
|
|
|
|
|
|
|
eval(&doc_tree, &mut syms).unwrap();
|
|
|
|
|
let res = eval(&test_tree, &mut syms);
|
|
|
|
|
res.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
#[cfg(not(feature = "implicit-load"))]
|
2023-03-03 14:29:53 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_circuit_fail() {
|
2024-07-10 13:22:28 -07: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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-03 14:29:53 -08:00
|
|
|
|
|
|
|
|
eval(&doc_tree, &mut syms).unwrap();
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-05-23 22:06:11 +00:00
|
|
|
eval(&test_tree, &mut syms).err().unwrap().0.first().unwrap().message,
|
|
|
|
|
"(is an undefined symbol)".to_string()
|
2023-03-05 22:18:49 -08:00
|
|
|
);
|
2023-03-03 14:29:53 -08:00
|
|
|
}
|
2023-02-27 22:53:54 -08:00
|
|
|
}
|