From ff5e76ff98fe561a108178bf580e324c286c9cd9 Mon Sep 17 00:00:00 2001 From: Aidan Hahn Date: Wed, 3 Nov 2021 23:42:59 -0700 Subject: [PATCH] add str concat, refactor str tests --- src/str.rs | 32 ++++++- tests/test_lib_str.rs | 188 ++---------------------------------------- 2 files changed, 37 insertions(+), 183 deletions(-) diff --git a/src/str.rs b/src/str.rs index d0dfe6f..44cc125 100644 --- a/src/str.rs +++ b/src/str.rs @@ -43,11 +43,41 @@ pub fn get_echo() -> Function { Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()), Ctr::None => () } - string.push_str("\n"); + println!(string) return true; }) { eprintln!("circuit loop in echo should not have returned false") } + return Ctr::None; + } + ) + }; +} + +pub fn get_concat() -> Function { + return Function{ + name: String::from("concat"), + loose_syms: false, + eval_lazy: false, + args: Args::Lazy(-1), + function: Operation::Internal( + |a: Ast, _b: Rc>, _c: Rc>| -> Ctr { + let mut string = String::from(""); + if !circuit(a, &mut |arg: &Ctr| { + match arg { + // should be a thing here + Ctr::Symbol(_) => return false, + Ctr::String(s) => string.push_str(&s), + Ctr::Integer(i) => string.push_str(&i.to_string()), + Ctr::Float(f) => string.push_str(&f.to_string()), + Ctr::Bool(b) => string.push_str(&b.to_string()), + Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()), + Ctr::None => () + } + return true; + }) { + eprintln!("circuit loop in concat should not have returned false") + } return Ctr::String(string); } ) diff --git a/tests/test_lib_str.rs b/tests/test_lib_str.rs index 984625d..b568628 100644 --- a/tests/test_lib_str.rs +++ b/tests/test_lib_str.rs @@ -6,8 +6,8 @@ mod str_lib_tests { #[test] fn test_simple_echo() { - let document = "(echo 'test')"; - let result = "test\n"; + let document = "(concat 'test')"; + let result = "test"; let vt = Rc::new(RefCell::new(VTable::new())); let ft: Rc>; match get_stdlib() { @@ -50,8 +50,8 @@ mod str_lib_tests { #[test] fn test_poly_echo() { - let document = "(echo 'test' 1 2 3)"; - let result = "test\n1\n2\n3\n"; + let document = "(concat 'test' 1 2 3)"; + let result = "test123"; let vt = Rc::new(RefCell::new(VTable::new())); let ft: Rc>; match get_stdlib() { @@ -94,8 +94,8 @@ mod str_lib_tests { #[test] fn test_empty_echo() { - let document = "(echo)"; - let result = "\n"; + let document = "(concat)"; + let result = ""; let vt = Rc::new(RefCell::new(VTable::new())); let ft: Rc>; match get_stdlib() { @@ -135,180 +135,4 @@ mod str_lib_tests { } } } - - #[test] - fn test_append_to_empty_list() { - let document = "(append () 1 2 3)"; - let result = "(1 2 3)"; - let vt = Rc::new(RefCell::new(VTable::new())); - let ft: Rc>; - match get_stdlib() { - Ok(f) => ft = f, - Err(s) => { - ft = Rc::new(RefCell::new(FTable::new())); - println!("Couldnt get stdlib: {}!", s); - assert!(false) - } - } - - match lex(document.to_string()) { - Err(s) => { - println!("Couldnt lex {}: {}\n", document, s); - assert!(false); - }, - - Ok(tree) => { - match eval(tree, vt.clone(), ft.clone(), false) { - Err(s) => { - println!("Couldnt eval {}: {}\n", document, s); - assert!(false); - }, - - Ok(ctr) => { - match ctr { - Ctr::Symbol(_) => assert!(false), - Ctr::String(_) => assert!(false), - Ctr::Integer(_) => assert!(false), - Ctr::Float(_) => assert!(false), - Ctr::Bool(_) => assert!(false), - Ctr::Seg(s) => assert_eq!(ast_to_string(s), result), - Ctr::None => assert!(false) - } - } - } - } - } - } - - #[test] - fn test_append_to_full_list() { - let document = "(append (1 2) 3)"; - let result = "(1 2 3)"; - let vt = Rc::new(RefCell::new(VTable::new())); - let ft: Rc>; - match get_stdlib() { - Ok(f) => ft = f, - Err(s) => { - ft = Rc::new(RefCell::new(FTable::new())); - println!("Couldnt get stdlib: {}!", s); - assert!(false) - } - } - - match lex(document.to_string()) { - Err(s) => { - println!("Couldnt lex {}: {}\n", document, s); - assert!(false); - }, - - Ok(tree) => { - match eval(tree, vt.clone(), ft.clone(), false) { - Err(s) => { - println!("Couldnt eval {}: {}\n", document, s); - assert!(false); - }, - - Ok(ctr) => { - match ctr { - Ctr::Symbol(_) => assert!(false), - Ctr::String(_) => assert!(false), - Ctr::Integer(_) => assert!(false), - Ctr::Float(_) => assert!(false), - Ctr::Bool(_) => assert!(false), - Ctr::Seg(s) => assert_eq!(ast_to_string(s), result), - Ctr::None => assert!(false) - } - } - } - } - } - } - - #[test] - fn test_mono_append() { - let document = "(append)"; - let result = "()"; - let vt = Rc::new(RefCell::new(VTable::new())); - let ft: Rc>; - match get_stdlib() { - Ok(f) => ft = f, - Err(s) => { - ft = Rc::new(RefCell::new(FTable::new())); - println!("Couldnt get stdlib: {}!", s); - assert!(false) - } - } - - match lex(document.to_string()) { - Err(s) => { - println!("Couldnt lex {}: {}\n", document, s); - assert!(false); - }, - - Ok(tree) => { - match eval(tree, vt.clone(), ft.clone(), false) { - Err(s) => { - println!("Couldnt eval {}: {}\n", document, s); - assert!(false); - }, - - Ok(ctr) => { - match ctr { - Ctr::Symbol(_) => assert!(false), - Ctr::String(_) => assert!(false), - Ctr::Integer(_) => assert!(false), - Ctr::Float(_) => assert!(false), - Ctr::Bool(_) => assert!(false), - Ctr::Seg(s) => assert_eq!(ast_to_string(s), result), - Ctr::None => assert!(false) - } - } - } - } - } - } - - #[test] - fn test_append_no_list() { - let document = "(append 'test' 1 2 3)"; - let result = "('test' 1 2 3)"; - let vt = Rc::new(RefCell::new(VTable::new())); - let ft: Rc>; - match get_stdlib() { - Ok(f) => ft = f, - Err(s) => { - ft = Rc::new(RefCell::new(FTable::new())); - println!("Couldnt get stdlib: {}!", s); - assert!(false) - } - } - - match lex(document.to_string()) { - Err(s) => { - println!("Couldnt lex {}: {}\n", document, s); - assert!(false); - }, - - Ok(tree) => { - match eval(tree, vt.clone(), ft.clone(), false) { - Err(s) => { - println!("Couldnt eval {}: {}\n", document, s); - assert!(false); - }, - - Ok(ctr) => { - match ctr { - Ctr::Symbol(_) => assert!(false), - Ctr::String(_) => assert!(false), - Ctr::Integer(_) => assert!(false), - Ctr::Float(_) => assert!(false), - Ctr::Bool(_) => assert!(false), - Ctr::Seg(s) => assert_eq!(ast_to_string(s), result), - Ctr::None => assert!(false) - } - } - } - } - } - } }