add more unit tests for function calls
This commit is contained in:
parent
a6abc993a5
commit
9b981921b4
4 changed files with 339 additions and 19 deletions
|
|
@ -3,14 +3,14 @@ mod func_tests {
|
|||
use std::cell::RefCell;
|
||||
use relish::ast::{Ast, Type, Ctr, new_ast};
|
||||
use relish::ast::VTable;
|
||||
use relish::ast::{Function, Operation, FTable, Args, func_declare, func_call};
|
||||
use relish::ast::{Function, Operation, FTable, Args, func_declare, func_call, ExternalOperation};
|
||||
|
||||
#[test]
|
||||
fn decl_and_call_internal_func() {
|
||||
let test_internal_func: Function = Function{
|
||||
name: String::from("test_func_in"),
|
||||
name: String::from("test_func_in"),
|
||||
loose_syms: false,
|
||||
eval_lazy: true,
|
||||
eval_lazy: false,
|
||||
args: Args::Strict(vec![Type::Bool]),
|
||||
function: Operation::Internal(
|
||||
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
|
||||
|
|
@ -29,7 +29,7 @@ mod func_tests {
|
|||
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!("{}", s);
|
||||
print!("Error declaring internal func: {}", s);
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
|
|
@ -55,4 +55,306 @@ mod func_tests {
|
|||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decl_and_call_external_func_singlet() {
|
||||
let test_external_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::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();
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decl_and_call_external_func_dual_ret() {
|
||||
let test_external_func: Function = Function{
|
||||
name: String::from("echox2"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
args: Args::Lazy(1),
|
||||
function: Operation::External(
|
||||
ExternalOperation{
|
||||
arg_syms: vec!["input".to_string()],
|
||||
ast: new_ast(Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()),
|
||||
Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()), Ctr::None)))),
|
||||
Ctr::None)
|
||||
}
|
||||
)
|
||||
};
|
||||
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(&"echox2".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::Seg(b) => {
|
||||
match b.borrow().clone().car {
|
||||
Ctr::String(s) => assert!(s == "test"),
|
||||
_ => {
|
||||
print!("first elem wrong. expected \"test\", got: {:?}\n", b.borrow().car);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
match b.borrow().clone().cdr {
|
||||
Ctr::Seg(b2) => {
|
||||
match b2.borrow().clone().car {
|
||||
Ctr::String(s) => assert!(s == "test"),
|
||||
_ => {
|
||||
print!("second elem wrong. expected \"test\", got: {:?}\n", b2.borrow().car);
|
||||
assert!(false)
|
||||
}
|
||||
}
|
||||
|
||||
match b2.borrow().cdr {
|
||||
Ctr::None => (),
|
||||
_ => {
|
||||
print!("there should not be a third element\n");
|
||||
assert!(false)
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
print!("first elem cdr should be a list. got: {:?}\n", b.borrow().cdr)
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
print!("Invalid return from func. Got {:?}\n", ret);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
print!("Call to function failed: {}\n", e);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decl_and_call_func_with_nested_call() {
|
||||
let inner_func: Function = Function{
|
||||
name: String::from("test_inner"),
|
||||
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 outer_func: Function = Function{
|
||||
name: String::from("test_outer"),
|
||||
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("test_inner".to_string()),
|
||||
Ctr::Seg(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::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 func: Rc<RefCell<Function>>;
|
||||
if let Some(f) = ft.borrow().get(&"test_outer".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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/* TESTING TODO:
|
||||
* - Test functions that eval_lazy
|
||||
* - Test functions that sym_loose
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue