2023-03-02 09:43:12 -08:00
|
|
|
mod bool_lib_tests {
|
2024-02-06 22:39:08 +00:00
|
|
|
use flesh::ast::{eval, lex, Ctr, SymTable};
|
2024-07-10 13:22:28 -07:00
|
|
|
use flesh::stdlib::static_stdlib;
|
2023-03-02 09:43:12 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_and_true_chain() {
|
|
|
|
|
let document = "(and true true true true true)";
|
2023-03-05 22:18:49 -08:00
|
|
|
let result = "true";
|
2023-03-02 09:43:12 -08:00
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
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-02 09:43:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_and_true_chain_with_false() {
|
|
|
|
|
let document = "(and true true false true true)";
|
2023-03-05 22:18:49 -08:00
|
|
|
let result = "false";
|
2023-03-02 09:43:12 -08:00
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
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-02 09:43:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_and_false_chain() {
|
|
|
|
|
let document = "(and false false false false false)";
|
2023-03-05 22:18:49 -08:00
|
|
|
let result = "false";
|
2023-03-02 09:43:12 -08:00
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
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-02 09:43:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_or_true_chain() {
|
|
|
|
|
let document = "(or true true true true true)";
|
2023-03-05 22:18:49 -08:00
|
|
|
let result = "true";
|
2023-03-02 09:43:12 -08:00
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
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-02 09:43:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_or_true_chain_with_false() {
|
|
|
|
|
let document = "(or true true false true true)";
|
2023-03-05 22:18:49 -08:00
|
|
|
let result = "true";
|
2023-03-02 09:43:12 -08:00
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
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-02 09:43:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_or_false_chain() {
|
2023-03-05 22:18:49 -08:00
|
|
|
let document = "(or false false false false false)";
|
|
|
|
|
let result = "false";
|
2023-03-02 09:43:12 -08:00
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
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-02 09:43:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_not() {
|
|
|
|
|
let document = "(not true)";
|
2023-03-05 22:18:49 -08:00
|
|
|
let result = "false";
|
2023-03-02 09:43:12 -08:00
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
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-02 09:43:12 -08:00
|
|
|
}
|
2023-03-02 12:15:42 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_toggle_a_bool() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(def tester \"\" true)";
|
2023-03-02 12:15:42 -08:00
|
|
|
let change = "(toggle tester)";
|
|
|
|
|
let check = "(tester)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:15:42 -08:00
|
|
|
|
|
|
|
|
let doc_tree = lex(&document.to_string()).unwrap();
|
|
|
|
|
let change_tree = lex(&change.to_string()).unwrap();
|
|
|
|
|
let check_tree = lex(&check.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
eval(&doc_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&change_tree, &mut syms).unwrap();
|
|
|
|
|
|
|
|
|
|
if let Ctr::Seg(ref s) = *eval(&check_tree, &mut syms).unwrap() {
|
2023-03-03 14:29:53 -08:00
|
|
|
if let Ctr::Bool(ref b) = *s.car {
|
2023-03-02 12:15:42 -08:00
|
|
|
assert_eq!(false, *b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eval(&change_tree, &mut syms).unwrap();
|
|
|
|
|
if let Ctr::Seg(ref s) = *eval(&check_tree, &mut syms).unwrap() {
|
2023-03-03 14:29:53 -08:00
|
|
|
if let Ctr::Bool(ref b) = *s.car {
|
2023-03-02 12:15:42 -08:00
|
|
|
assert_eq!(true, *b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_toggle_errors_dont_lose_vars() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(def tester \"\" \"oops\")";
|
2023-03-02 12:15:42 -08:00
|
|
|
let change = "(toggle tester)";
|
|
|
|
|
let check = "(tester)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:15:42 -08:00
|
|
|
|
|
|
|
|
let doc_tree = lex(&document.to_string()).unwrap();
|
|
|
|
|
let change_tree = lex(&change.to_string()).unwrap();
|
|
|
|
|
let check_tree = lex(&check.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
eval(&doc_tree, &mut syms).unwrap();
|
|
|
|
|
if let Err(s) = eval(&change_tree, &mut syms) {
|
2023-03-03 14:29:53 -08:00
|
|
|
assert_eq!(
|
2023-05-23 22:06:11 +00:00
|
|
|
s.0.first().unwrap().message,
|
|
|
|
|
"can only toggle a boolean".to_string()
|
2023-03-03 14:29:53 -08:00
|
|
|
);
|
2023-03-02 12:15:42 -08:00
|
|
|
let intermediate = *eval(&check_tree, &mut syms).unwrap();
|
|
|
|
|
if let Ctr::Seg(ref s) = intermediate {
|
2024-07-10 13:22:28 -07:00
|
|
|
assert_eq!(s.to_string(), "(\"oops\")".to_string());
|
2023-03-02 12:15:42 -08:00
|
|
|
} else {
|
|
|
|
|
eprintln!("did not expect: {}", intermediate);
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("shouldn't have succeeded!");
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_toggle_errors_dont_lose_funcs() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(def tester \"\" (oops) oops)";
|
2023-03-02 12:15:42 -08:00
|
|
|
let change = "(toggle tester)";
|
2024-07-10 13:22:28 -07:00
|
|
|
let check = "(tester \"1\")";
|
2023-03-02 12:15:42 -08:00
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:15:42 -08:00
|
|
|
|
|
|
|
|
let doc_tree = lex(&document.to_string()).unwrap();
|
|
|
|
|
let change_tree = lex(&change.to_string()).unwrap();
|
|
|
|
|
let check_tree = lex(&check.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
eval(&doc_tree, &mut syms).unwrap();
|
|
|
|
|
if let Err(s) = eval(&change_tree, &mut syms) {
|
2023-03-03 14:29:53 -08:00
|
|
|
assert_eq!(
|
2023-05-23 22:06:11 +00:00
|
|
|
s.0.first().unwrap().message,
|
|
|
|
|
"cannot toggle a function".to_string()
|
2023-03-03 14:29:53 -08:00
|
|
|
);
|
2023-03-02 12:15:42 -08:00
|
|
|
if let Ctr::String(ref s) = *eval(&check_tree, &mut syms).unwrap() {
|
|
|
|
|
assert_eq!(*s, "1".to_string());
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("shouldn't have succeeded!");
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-02 12:48:26 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_iseq_basic_t() {
|
|
|
|
|
let document = "(eq? true true)";
|
|
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:48:26 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_iseq_basic_f() {
|
|
|
|
|
let document = "(eq? true false)";
|
|
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:48:26 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(!b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_iseq_basic_f_mixed_data() {
|
|
|
|
|
let document = "(eq? true 1)";
|
|
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:48:26 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(!b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_iseq_long_f() {
|
|
|
|
|
let document = "(eq? true true true true true false)";
|
|
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:48:26 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(!b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_iseq_long_t_str() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(eq? \"1\" \"1\" \"1\" \"1\" \"1\" \"1\" \"1\" \"1\" \"1\" \"1\" \"1\")";
|
2023-03-02 12:48:26 -08:00
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:48:26 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_iseq_t_mixed_numerals() {
|
|
|
|
|
let document = "(eq? 1 1.0)";
|
|
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:48:26 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_iseq_f_wrong_type() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(eq? 1 \"1\")";
|
2023-03-02 12:48:26 -08:00
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-02 12:48:26 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(!b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-09 17:28:17 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_boolcast_str_t() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(bool \"true\")";
|
2023-03-09 17:28:17 -08:00
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-09 17:28:17 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_boolcast_str_f() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(bool \"false\")";
|
2023-03-09 17:28:17 -08:00
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-09 17:28:17 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(!b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_boolcast_int_t() {
|
|
|
|
|
let document = "(bool 0)";
|
|
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-09 17:28:17 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_boolcast_int_f() {
|
|
|
|
|
let document = "(bool 12)";
|
|
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-09 17:28:17 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(!b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_boolcast_float_t() {
|
|
|
|
|
let document = "(bool 0.0)";
|
|
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-09 17:28:17 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_boolcast_float_f() {
|
|
|
|
|
let document = "(bool 1.2)";
|
|
|
|
|
let test = lex(&document.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2024-07-26 22:16:21 -07:00
|
|
|
static_stdlib(&mut syms, |_: &String| (), || String::new());
|
2023-03-09 17:28:17 -08:00
|
|
|
|
|
|
|
|
if let Ctr::Bool(b) = *eval(&test, &mut syms).unwrap() {
|
|
|
|
|
assert!(!b)
|
|
|
|
|
} else {
|
|
|
|
|
panic!()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-02 09:43:12 -08:00
|
|
|
}
|