more complex tests

This commit is contained in:
Aidan Hahn 2021-07-19 23:59:03 -07:00
parent df5cb47cb4
commit 2c30975571
No known key found for this signature in database
GPG key ID: 327711E983899316
6 changed files with 179 additions and 320 deletions

View file

@ -1,4 +1,4 @@
mod func_tests {
mod eval_tests {
use std::rc::Rc;
use std::cell::RefCell;
use relish::ast::{Ctr, Function, Operation, ExternalOperation};
@ -117,12 +117,30 @@ mod func_tests {
}
}
/*
#[test]
fn eval_embedded_func_calls() {
let test_doc = "1".to_string();
let test_doc = "('one' (echo (echo 'unwrap_me')))".to_string();
let output = "('one' 'unwrap_me')";
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::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()));
if let Some(s) = func_declare(ft.clone(),
Rc::new(RefCell::new(test_external_func))) {
print!("Error declaring external func: {}", s);
assert!(false);
}
match lex(test_doc) {
Err(e) => {
@ -137,14 +155,22 @@ mod func_tests {
assert!(false)
},
Ok(reduced_ast) => {
// write tests here
Ok(reduced) => {
if let Ctr::Seg(reduced_ast) = reduced {
let out_doc = ast_to_string(reduced_ast);
if out_doc != output {
print!("Erroneous output: {}\n", out_doc);
assert!(false)
}
}
}
}
}
}
}
/*
#[test]
fn eval_bad_vars() {
let test_doc = "1".to_string();

View file

@ -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
*/

View file

@ -46,7 +46,7 @@ mod lex_tests {
#[test]
fn test_bad_symbol() {
let document: &str = "(as;dd)";
let output: &str = "Problem lexing document: \"Unparsable token:as;dd\"";
let output: &str = "Problem lexing document: \"Unparsable token: as;dd\"";
match lex(document.to_string()) {
Ok(tree) => {
print!("Bad token yielded: {}\n", ast_to_string(tree));