tests function now

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-02-24 15:29:17 -08:00
parent c70cbc701d
commit 8e13b5b87f
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
2 changed files with 61 additions and 112 deletions

View file

@ -1,16 +1,17 @@
mod func_tests {
use relish::ast::lex;
use relish::ast::{SYM_TABLE, Type, UserFn};
use relish::ast::{SymTable, Type, UserFn};
use relish::ast::{Args, Symbol, Ctr, Seg, ValueType};
#[test]
fn decl_and_call_internal_func() {
let mut syms = SymTable::new();
let test_internal_func: Symbol = Symbol {
name: String::from("test_func_in"),
has_undefined_symbols: false,
conditional_branches: false,
args: Args::Strict(vec![Type::Bool]),
value: ValueType::Internal(Box::new(
|a: &Seg| -> Ctr {
|a: &Seg, _: &mut SymTable| -> Ctr {
let inner = a;
let mut is_bool = false;
if let Ctr::Bool(_) = *inner.car {
@ -25,22 +26,9 @@ mod func_tests {
Box::new(Ctr::None)
);
{
let mut table_handle = SYM_TABLE.write().unwrap();
table_handle.insert(String::from("test_func_in"), test_internal_func);
}
syms.insert(String::from("test_func_in"), test_internal_func);
let table_handle = SYM_TABLE.read().unwrap();
let func: &Symbol;
if let Some(f) = table_handle.get(&"test_func_in".to_string()) {
func = f;
} else {
print!("failed to retrieve function!");
assert!(false);
return;
}
if let Ok(ret) = func.call(&args) {
if let Ok(ret) = syms.call_symbol(&"test_func_in".to_string(), &args, true) {
match *ret {
Ctr::Bool(b) => assert!(b),
_ => {
@ -56,12 +44,13 @@ mod func_tests {
#[test]
fn decl_and_call_external_func_singlet() {
let mut syms = SymTable::new();
match lex(&"((input))".to_string()) {
Err(e) => panic!("{}", e),
Ok(finner) => {
let test_external_func: Symbol = Symbol {
name: String::from("echo"),
has_undefined_symbols: false,
conditional_branches: false,
args: Args::Lazy(1),
value: ValueType::FuncForm(UserFn{
arg_syms: vec!["input".to_string()],
@ -74,22 +63,9 @@ mod func_tests {
Box::new(Ctr::None)
);
{
let mut table_handle = SYM_TABLE.write().unwrap();
table_handle.insert(String::from("test_func_in"), test_external_func);
}
syms.insert(String::from("test_func_in"), test_external_func);
let table_handle = SYM_TABLE.read().unwrap();
let func: &Symbol;
if let Some(f) = table_handle.get(&"test_func_in".to_string()) {
func = f;
} else {
print!("failed to retrieve function!");
assert!(false);
return;
}
match func.call(&args) {
match syms.call_symbol(&"test_func_in".to_string(), &args, true) {
Ok(ret) => match *ret {
Ctr::String(b) => assert!(b == "test"),
_ => {
@ -108,12 +84,13 @@ mod func_tests {
#[test]
fn decl_and_call_external_func_multi_body() {
let mut syms = SymTable::new();
match lex(&"((input) (input))".to_string()) {
Err(e) => panic!("{}", e),
Ok(finner) => {
let test_external_func: Symbol = Symbol{
name: String::from("echo_2"),
has_undefined_symbols: false,
conditional_branches: false,
args: Args::Lazy(1),
value: ValueType::FuncForm(UserFn{
arg_syms: vec!["input".to_string()],
@ -126,22 +103,9 @@ mod func_tests {
Box::new(Ctr::None)
);
{
let mut table_handle = SYM_TABLE.write().unwrap();
table_handle.insert(String::from("echo_2"), test_external_func);
}
syms.insert(String::from("echo_2"), test_external_func);
let table_handle = SYM_TABLE.read().unwrap();
let func: &Symbol;
if let Some(f) = table_handle.get(&"echo_2".to_string()) {
func = f;
} else {
print!("failed to retrieve function!");
assert!(false);
return;
}
match func.call(&args) {
match syms.call_symbol(&"echo_2".to_string(), &args, true) {
Ok(ret) => assert_eq!(ret.to_string(), "(\"test\" \"test\")"),
Err(e) => {
print!("Call to function failed: {}\n", e);
@ -154,12 +118,13 @@ mod func_tests {
#[test]
fn decl_and_call_func_with_nested_call() {
let mut syms = SymTable::new();
let inner_func: Symbol = Symbol {
name: String::from("test_inner"),
has_undefined_symbols: false,
conditional_branches: false,
args: Args::Strict(vec![Type::Bool]),
value: ValueType::Internal(Box::new(
|a: &Seg| -> Ctr {
|a: &Seg, _: &mut SymTable| -> Ctr {
let inner = a;
if let Ctr::Bool(b) = *inner.car {
if b {
@ -179,7 +144,7 @@ mod func_tests {
Ok(finner) => {
let outer_func: Symbol = Symbol {
name: String::from("test_outer"),
has_undefined_symbols: false,
conditional_branches: false,
args: Args::Lazy(1),
value: ValueType::FuncForm(UserFn{
arg_syms: vec!["input".to_string()],
@ -192,24 +157,10 @@ mod func_tests {
Box::new(Ctr::None)
);
{
let mut table_handle = SYM_TABLE.write().unwrap();
syms.insert(String::from("test_inner"), inner_func);
syms.insert(String::from("test_outer"), outer_func);
table_handle.insert(String::from("test_inner"), inner_func);
table_handle.insert(String::from("test_outer"), outer_func);
}
let table_handle = SYM_TABLE.read().unwrap();
let func: &Symbol;
if let Some(f) = table_handle.get(&"test_outer".to_string()) {
func = f;
} else {
print!("failed to retrieve function!");
assert!(false);
return;
}
match func.call(&args) {
match syms.call_symbol(&"test_outer".to_string(), &args, true) {
Ok(ret) => match *ret {
Ctr::String(b) => assert!(b == "test"),
_ => {