From 8e13b5b87f11f77f13f1c0439d2f32bc618d4c50 Mon Sep 17 00:00:00 2001 From: Ava Hahn Date: Fri, 24 Feb 2023 15:29:17 -0800 Subject: [PATCH] tests function now Signed-off-by: Ava Hahn --- tests/test_eval.rs | 82 ++++++++++++++++++++--------------------- tests/test_func.rs | 91 +++++++++++----------------------------------- 2 files changed, 61 insertions(+), 112 deletions(-) diff --git a/tests/test_eval.rs b/tests/test_eval.rs index 5bbf559..a9d3cc2 100644 --- a/tests/test_eval.rs +++ b/tests/test_eval.rs @@ -1,5 +1,5 @@ mod eval_tests { - use relish::ast::{eval, lex, SYM_TABLE}; + use relish::ast::{eval, lex, SymTable}; use relish::ast::{Args, Symbol, Ctr, Seg, ValueType, UserFn}; // TODO: write generalized testing routine on top of list of inputs @@ -7,13 +7,14 @@ mod eval_tests { #[test] fn eval_singlet() { let test_doc = "(1)".to_string(); + let mut syms = SymTable::new(); match lex(&test_doc) { Err(e) => { println!("Lexing error: {}\n", e); assert!(false) } - Ok(ref initial_ast) => match eval(initial_ast, true, true) { + Ok(ref initial_ast) => match eval(initial_ast, &mut syms) { Err(e) => { println!("Evaluation error: {}\n", e); assert!(false) @@ -29,14 +30,14 @@ mod eval_tests { #[test] fn eval_embedded_lists_no_funcs() { let test_doc = "(1 (1 2 3 4 5) 5)".to_string(); - + let mut syms = SymTable::new(); match lex(&test_doc) { Err(e) => { println!("Lexing error: {}\n", e); assert!(false) } - Ok(initial_ast) => match eval(&initial_ast, true, true) { + Ok(initial_ast) => match eval(&initial_ast, &mut syms) { Err(e) => { println!("Evaluation error: {}\n", e); assert!(false) @@ -53,33 +54,32 @@ mod eval_tests { fn eval_function_call() { let test_doc = "('one' (echo 'unwrap_me'))".to_string(); let output = "('one' 'unwrap_me')"; + let mut syms = SymTable::new(); - { // we want the write lock to expire before eval - let mut table_handle = SYM_TABLE.write().unwrap(); - let test_external_func: Symbol = Symbol { - name: String::from("echo"), - args: Args::Lazy(1), - has_undefined_symbols: false, - value: ValueType::FuncForm( UserFn { - arg_syms: vec!["input".to_string()], - ast: Box::new(Seg::from( - Box::new(Ctr::Seg(Seg::from( - Box::from(Ctr::Symbol("input".to_string())), - Box::from(Ctr::None)))), - Box::new(Ctr::None), - )), - }), - }; + let test_external_func: Symbol = Symbol { + name: String::from("echo"), + args: Args::Lazy(1), + conditional_branches: false, + value: ValueType::FuncForm( UserFn { + arg_syms: vec!["input".to_string()], + ast: Box::new(Seg::from( + Box::new(Ctr::Seg(Seg::from( + Box::from(Ctr::Symbol("input".to_string())), + Box::from(Ctr::None)))), + Box::new(Ctr::None), + )), + }), + }; + + syms.insert(String::from("echo"), test_external_func); - table_handle.insert(String::from("echo"), test_external_func); - } match lex(&test_doc) { Err(e) => { println!("Lexing error: {}\n", e); assert!(false) } - Ok(initial_ast) => match eval(&initial_ast, true, true) { + Ok(initial_ast) => match eval(&initial_ast, &mut syms) { Err(e) => { println!("Evaluation error: {}\n", e); assert!(false) @@ -96,33 +96,31 @@ mod eval_tests { fn eval_embedded_func_calls() { let test_doc = "('one' (echo (echo 'unwrap_me')))".to_string(); let output = "('one' 'unwrap_me')"; + let mut syms = SymTable::new(); - { - let mut table_handle = SYM_TABLE.write().unwrap(); - let test_external_func: Symbol = Symbol{ - name: String::from("echo"), - args: Args::Lazy(1), - has_undefined_symbols: false, - value: ValueType::FuncForm( UserFn { - arg_syms: vec!["input".to_string()], - ast: Box::new(Seg::from( - Box::new(Ctr::Seg(Seg::from( - Box::from(Ctr::Symbol("input".to_string())), - Box::from(Ctr::None)))), - Box::new(Ctr::None), - )), - }), - }; + let test_external_func: Symbol = Symbol{ + name: String::from("echo"), + args: Args::Lazy(1), + conditional_branches: false, + value: ValueType::FuncForm( UserFn { + arg_syms: vec!["input".to_string()], + ast: Box::new(Seg::from( + Box::new(Ctr::Seg(Seg::from( + Box::from(Ctr::Symbol("input".to_string())), + Box::from(Ctr::None)))), + Box::new(Ctr::None), + )), + }), + }; - table_handle.insert(String::from("echo"), test_external_func); - } + syms.insert(String::from("echo"), test_external_func); match lex(&test_doc) { Err(e) => { println!("Lexing error: {}\n", e); assert!(false) } - Ok(initial_ast) => match eval(&initial_ast, true, true) { + Ok(initial_ast) => match eval(&initial_ast, &mut syms) { Err(e) => { println!("Evaluation error: {}\n", e); assert!(false) diff --git a/tests/test_func.rs b/tests/test_func.rs index 9063da1..52204af 100644 --- a/tests/test_func.rs +++ b/tests/test_func.rs @@ -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"), _ => {