more complex tests
This commit is contained in:
parent
df5cb47cb4
commit
2c30975571
6 changed files with 179 additions and 320 deletions
|
|
@ -3,7 +3,7 @@ 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, ExternalOperation};
|
||||
use relish::ast::{Function, Operation, FTable, Args, func_declare, func_call, ExternalOperation, lex};
|
||||
|
||||
#[test]
|
||||
fn decl_and_call_internal_func() {
|
||||
|
|
@ -58,131 +58,111 @@ mod func_tests {
|
|||
|
||||
#[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)
|
||||
match lex("((input))".to_string()) {
|
||||
Err(e) => panic!(e),
|
||||
Ok(finner) => {
|
||||
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: 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 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;
|
||||
}
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
},
|
||||
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);
|
||||
}
|
||||
fn decl_and_call_external_func_multi_body() {
|
||||
match lex("((input) (input))".to_string()) {
|
||||
Err(e) => panic!(e),
|
||||
Ok(finner) => {
|
||||
let test_external_func: Function = Function{
|
||||
name: String::from("echo_2"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
args: Args::Lazy(1),
|
||||
function: Operation::External(
|
||||
ExternalOperation{
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
let func: Rc<RefCell<Function>>;
|
||||
if let Some(f) = ft.borrow().get(&"echo_2".to_string()) {
|
||||
func = f.clone();
|
||||
} else {
|
||||
print!("failed to retrieve function!");
|
||||
assert!(false);
|
||||
return;
|
||||
}
|
||||
|
||||
match b2.borrow().cdr {
|
||||
Ctr::None => (),
|
||||
_ => {
|
||||
print!("there should not be a third element\n");
|
||||
assert!(false)
|
||||
}
|
||||
}
|
||||
match func_call(func, args, vt, ft) {
|
||||
Ok(ret) => {
|
||||
match ret {
|
||||
Ctr::String(s) => {
|
||||
assert!(s == "test");
|
||||
},
|
||||
_ => {
|
||||
print!("first elem cdr should be a list. got: {:?}\n", b.borrow().cdr)
|
||||
print!("Invalid return from function {:#?}. Should have recieved single string", ret);
|
||||
assert!(false);
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
print!("Invalid return from func. Got {:?}\n", ret);
|
||||
Err(e) => {
|
||||
print!("Call to function failed: {}\n", e);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
print!("Call to function failed: {}\n", e);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -210,58 +190,61 @@ mod func_tests {
|
|||
)
|
||||
};
|
||||
|
||||
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)))
|
||||
match lex("((test_inner true))".to_string()) {
|
||||
Err(e) => panic!(e),
|
||||
Ok(finner) => {
|
||||
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: 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 ft = Rc::new(RefCell::new(FTable::new()));
|
||||
let vt = Rc::new(RefCell::new(VTable::new()));
|
||||
let args = new_ast(Ctr::Bool(true), Ctr::None);
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
print!("Call to function failed: {}\n", e);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -350,9 +333,10 @@ mod func_tests {
|
|||
assert!(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}*/
|
||||
|
||||
/*
|
||||
// TODO: These tests need completion!
|
||||
#[test]
|
||||
fn eval_lazy_func_call() {
|
||||
|
||||
|
|
@ -381,10 +365,5 @@ mod func_tests {
|
|||
#[test]
|
||||
fn bad_eval_fn_body() {
|
||||
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
/* TESTING TODO:
|
||||
* - Test functions that eval_lazy
|
||||
* - Test functions that sym_loose
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue