all tests but vars tests refactored
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
7555a90328
commit
b20f64b038
4 changed files with 120 additions and 333 deletions
|
|
@ -1,24 +1,20 @@
|
|||
mod func_tests {
|
||||
use relish::ast::VTable;
|
||||
use relish::ast::{
|
||||
func_call, func_declare, lex, Args, ExternalOperation, FTable, Function, Operation,
|
||||
};
|
||||
use relish::ast::{new_ast, Ast, Ctr, Type};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use relish::ast::lex;
|
||||
use relish::ast::{SYM_TABLE, Type, UserFn};
|
||||
use relish::ast::{Args, Symbol, Ctr, Seg, ValueType};
|
||||
|
||||
#[test]
|
||||
fn decl_and_call_internal_func() {
|
||||
let test_internal_func: Function = Function {
|
||||
let mut table_handle = SYM_TABLE.lock().unwrap();
|
||||
let test_internal_func: Symbol = Symbol {
|
||||
name: String::from("test_func_in"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
has_undefined_symbols: false,
|
||||
args: Args::Strict(vec![Type::Bool]),
|
||||
function: Operation::Internal(Box::new(
|
||||
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
|
||||
let inner = a.borrow();
|
||||
value: ValueType::Internal(Box::new(
|
||||
|a: &Seg| -> Ctr {
|
||||
let inner = a;
|
||||
let mut is_bool = false;
|
||||
if let Ctr::Bool(_) = &inner.car {
|
||||
if let Ctr::Bool(_) = *inner.car {
|
||||
is_bool = true;
|
||||
}
|
||||
|
||||
|
|
@ -26,25 +22,23 @@ mod func_tests {
|
|||
},
|
||||
)),
|
||||
};
|
||||
let ft = Rc::new(RefCell::new(FTable::new()));
|
||||
let vt = Rc::new(RefCell::new(VTable::new()));
|
||||
let args = new_ast(Ctr::Bool(true), Ctr::None);
|
||||
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_internal_func))) {
|
||||
print!("Error declaring internal func: {}", s);
|
||||
assert!(false);
|
||||
}
|
||||
let args = Seg::from(
|
||||
Box::new(Ctr::Bool(true)),
|
||||
Box::new(Ctr::None)
|
||||
);
|
||||
|
||||
let func: Rc<RefCell<Function>>;
|
||||
if let Some(f) = ft.borrow().get(&"test_func_in".to_string()) {
|
||||
func = f.clone();
|
||||
table_handle.insert(String::from("test_func_in"), test_internal_func);
|
||||
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(func, args, vt, ft) {
|
||||
match ret {
|
||||
if let Ok(ret) = func.call(&args) {
|
||||
match *ret {
|
||||
Ctr::Bool(b) => assert!(b),
|
||||
_ => {
|
||||
print!("invalid return from func!");
|
||||
|
|
@ -59,39 +53,37 @@ mod func_tests {
|
|||
|
||||
#[test]
|
||||
fn decl_and_call_external_func_singlet() {
|
||||
match lex("((input))".to_string()) {
|
||||
let mut table_handle = SYM_TABLE.lock().unwrap();
|
||||
match lex(&"((input))".to_string()) {
|
||||
Err(e) => panic!("{}", e),
|
||||
Ok(finner) => {
|
||||
let test_external_func: Function = Function {
|
||||
let test_external_func: Symbol = Symbol {
|
||||
name: String::from("echo"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
has_undefined_symbols: false,
|
||||
args: Args::Lazy(1),
|
||||
function: Operation::External(ExternalOperation {
|
||||
value: ValueType::FuncForm(UserFn{
|
||||
arg_syms: vec!["input".to_string()],
|
||||
ast: finner,
|
||||
}),
|
||||
};
|
||||
let ft = Rc::new(RefCell::new(FTable::new()));
|
||||
let vt = Rc::new(RefCell::new(VTable::new()));
|
||||
let args = new_ast(Ctr::String("test".to_string()), Ctr::None);
|
||||
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func)))
|
||||
{
|
||||
print!("Error declaring external func: {}", s);
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
let func: Rc<RefCell<Function>>;
|
||||
if let Some(f) = ft.borrow().get(&"echo".to_string()) {
|
||||
func = f.clone();
|
||||
let args = Seg::from(
|
||||
Box::new(Ctr::String("test".to_string())),
|
||||
Box::new(Ctr::None)
|
||||
);
|
||||
|
||||
table_handle.insert(String::from("test_func_in"), test_external_func);
|
||||
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(func, args, vt, ft) {
|
||||
Ok(ret) => match ret {
|
||||
match func.call(&args) {
|
||||
Ok(ret) => match *ret {
|
||||
Ctr::String(b) => assert!(b == "test"),
|
||||
_ => {
|
||||
print!("Invalid return from func. Got {:?}\n", ret);
|
||||
|
|
@ -109,48 +101,37 @@ mod func_tests {
|
|||
|
||||
#[test]
|
||||
fn decl_and_call_external_func_multi_body() {
|
||||
match lex("((input) (input))".to_string()) {
|
||||
let mut table_handle = SYM_TABLE.lock().unwrap();
|
||||
match lex(&"((input) (input))".to_string()) {
|
||||
Err(e) => panic!("{}", e),
|
||||
Ok(finner) => {
|
||||
let test_external_func: Function = Function {
|
||||
let test_external_func: Symbol = Symbol{
|
||||
name: String::from("echo_2"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
has_undefined_symbols: false,
|
||||
args: Args::Lazy(1),
|
||||
function: Operation::External(ExternalOperation {
|
||||
value: ValueType::FuncForm(UserFn{
|
||||
arg_syms: vec!["input".to_string()],
|
||||
ast: finner,
|
||||
}),
|
||||
};
|
||||
let ft = Rc::new(RefCell::new(FTable::new()));
|
||||
let vt = Rc::new(RefCell::new(VTable::new()));
|
||||
let args = new_ast(Ctr::String("test".to_string()), Ctr::None);
|
||||
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func)))
|
||||
{
|
||||
print!("Error declaring external func: {}", s);
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
let func: Rc<RefCell<Function>>;
|
||||
if let Some(f) = ft.borrow().get(&"echo_2".to_string()) {
|
||||
func = f.clone();
|
||||
let args = Seg::from(
|
||||
Box::new(Ctr::String("test".to_string())),
|
||||
Box::new(Ctr::None)
|
||||
);
|
||||
|
||||
table_handle.insert(String::from("echo_2"), test_external_func);
|
||||
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(func, args, vt, ft) {
|
||||
Ok(ret) => match ret {
|
||||
Ctr::String(s) => {
|
||||
assert!(s == "test");
|
||||
}
|
||||
_ => {
|
||||
print!("Invalid return from function {:#?}. Should have recieved single string", ret);
|
||||
assert!(false);
|
||||
return;
|
||||
}
|
||||
},
|
||||
match func.call(&args) {
|
||||
Ok(ret) => assert_eq!(ret.to_string(), "(\"test\" \"test\")"),
|
||||
Err(e) => {
|
||||
print!("Call to function failed: {}\n", e);
|
||||
assert!(false);
|
||||
|
|
@ -162,16 +143,16 @@ mod func_tests {
|
|||
|
||||
#[test]
|
||||
fn decl_and_call_func_with_nested_call() {
|
||||
let inner_func: Function = Function {
|
||||
let mut table_handle = SYM_TABLE.lock().unwrap();
|
||||
let inner_func: Symbol = Symbol {
|
||||
name: String::from("test_inner"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
has_undefined_symbols: false,
|
||||
args: Args::Strict(vec![Type::Bool]),
|
||||
function: Operation::Internal(Box::new(
|
||||
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
|
||||
let inner = a.borrow();
|
||||
if let Ctr::Bool(b) = &inner.car {
|
||||
if *b {
|
||||
value: ValueType::Internal(Box::new(
|
||||
|a: &Seg| -> Ctr {
|
||||
let inner = a;
|
||||
if let Ctr::Bool(b) = *inner.car {
|
||||
if b {
|
||||
Ctr::String("test".to_string())
|
||||
} else {
|
||||
Ctr::None
|
||||
|
|
@ -183,44 +164,39 @@ mod func_tests {
|
|||
)),
|
||||
};
|
||||
|
||||
match lex("((test_inner true))".to_string()) {
|
||||
match lex(&"((test_inner true))".to_string()) {
|
||||
Err(e) => panic!("{}", e),
|
||||
Ok(finner) => {
|
||||
let outer_func: Function = Function {
|
||||
let outer_func: Symbol = Symbol {
|
||||
name: String::from("test_outer"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
has_undefined_symbols: false,
|
||||
args: Args::Lazy(1),
|
||||
function: Operation::External(ExternalOperation {
|
||||
value: ValueType::FuncForm(UserFn{
|
||||
arg_syms: vec!["input".to_string()],
|
||||
ast: finner,
|
||||
}),
|
||||
};
|
||||
|
||||
let ft = Rc::new(RefCell::new(FTable::new()));
|
||||
let vt = Rc::new(RefCell::new(VTable::new()));
|
||||
let args = new_ast(Ctr::Bool(true), Ctr::None);
|
||||
|
||||
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(inner_func))) {
|
||||
print!("Error declaring inner func: {}", s);
|
||||
assert!(false);
|
||||
}
|
||||
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(outer_func))) {
|
||||
print!("Error declaring outer func: {}", s);
|
||||
assert!(false);
|
||||
}
|
||||
let args = Seg::from(
|
||||
Box::new(Ctr::Bool(true)),
|
||||
Box::new(Ctr::None)
|
||||
);
|
||||
|
||||
let func: Rc<RefCell<Function>>;
|
||||
if let Some(f) = ft.borrow().get(&"test_outer".to_string()) {
|
||||
func = f.clone();
|
||||
table_handle.insert(String::from("test_inner"), inner_func);
|
||||
table_handle.insert(String::from("test_outer"), outer_func);
|
||||
|
||||
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(func, args, vt, ft) {
|
||||
Ok(ret) => match ret {
|
||||
match func.call(&args) {
|
||||
Ok(ret) => match *ret {
|
||||
Ctr::String(b) => assert!(b == "test"),
|
||||
_ => {
|
||||
print!("Invalid return from func. Got {:?}\n", ret);
|
||||
|
|
@ -236,92 +212,6 @@ mod func_tests {
|
|||
}
|
||||
}
|
||||
|
||||
/* This test removed because it would make more sense to test this functionality in eval tests
|
||||
* this function tested the evaluation of a complex argument ast that could be reduced in eval
|
||||
|
||||
#[test]
|
||||
fn decl_and_call_func_eval_arg() {
|
||||
let is_true_func: Function = Function{
|
||||
name: String::from("is_true?"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
args: Args::Strict(vec![Type::Bool]),
|
||||
function: Operation::Internal(
|
||||
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
|
||||
let inner = a.borrow();
|
||||
if let Ctr::Bool(b) = &inner.car {
|
||||
if *b {
|
||||
Ctr::String("test".to_string())
|
||||
} else {
|
||||
Ctr::None
|
||||
}
|
||||
} else {
|
||||
Ctr::None
|
||||
}
|
||||
}
|
||||
)
|
||||
};
|
||||
|
||||
let echo_func: Function = Function{
|
||||
name: String::from("echo"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
args: Args::Lazy(1),
|
||||
function: Operation::External(
|
||||
ExternalOperation{
|
||||
arg_syms: vec!["input".to_string()],
|
||||
ast: new_ast(Ctr::Symbol("input".to_string()), Ctr::None)
|
||||
}
|
||||
)
|
||||
};
|
||||
|
||||
let ft = Rc::new(RefCell::new(FTable::new()));
|
||||
let vt = Rc::new(RefCell::new(VTable::new()));
|
||||
let args = new_ast(
|
||||
Ctr::Seg(new_ast(
|
||||
Ctr::Symbol("is_true?".to_string()),
|
||||
Ctr::Seg(new_ast(
|
||||
Ctr::Bool(true),
|
||||
Ctr::None)))),
|
||||
Ctr::None);
|
||||
|
||||
if let Some(s) = func_declare(ft.clone(),
|
||||
Rc::new(RefCell::new(is_true_func))) {
|
||||
print!("Error declaring inner func: {}", s);
|
||||
assert!(false);
|
||||
}
|
||||
if let Some(s) = func_declare(ft.clone(),
|
||||
Rc::new(RefCell::new(echo_func))) {
|
||||
print!("Error declaring outer func: {}", s);
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
let func: Rc<RefCell<Function>>;
|
||||
if let Some(f) = ft.borrow().get(&"echo".to_string()) {
|
||||
func = f.clone();
|
||||
} else {
|
||||
print!("failed to retrieve function!");
|
||||
assert!(false);
|
||||
return;
|
||||
}
|
||||
|
||||
match func_call(func, args, vt, ft) {
|
||||
Ok(ret) => {
|
||||
match ret {
|
||||
Ctr::String(b) => assert!(b == "test"),
|
||||
_ => {
|
||||
print!("Invalid return from func. Got {:?}\n", ret);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
print!("Call to function failed: {}\n", e);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
// TODO: These tests need completion!
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue