mod lex_tests { use relish::ast::{lex}; #[test] fn test_lex_basic_pair() { let document: &str = "(hello 'world')"; let output: &str = "(hello 'world' nil)"; match lex(document.to_string()) { Ok(box_cell) => { assert_eq!(format!("{}", *box_cell), output.to_string()); }, Err(s) => { print!("{}\n", s); assert!(false); } } } #[test] fn test_lex_basic_list() { let document: &str = "(hello 'world' 1 2 3)"; let output: &str = "(hello 'world' 1 2 3 nil)"; match lex(document.to_string()) { Ok(box_cell) => { assert_eq!(format!("{}", *box_cell), output.to_string()); }, Err(s) => { print!("{}\n", s); assert!(false); } } } #[test] fn test_lex_complex_list() { let document: &str = "(hello 'world' (1 2 (1 2 3)) 1 2 3)"; let output: &str = "(hello 'world' (1 2 (1 2 3 nil) nil) 1 2 3 nil)"; match lex(document.to_string()) { Ok(box_cell) => { assert_eq!(format!("{}", *box_cell), output.to_string()); }, Err(s) => { print!("{}\n", s); assert!(false); } } } #[test] fn test_bad_symbol() { let document: &str = "(as;dd)"; let output: &str = "Problem lexing document: \"Unparsable token:as;dd\""; match lex(document.to_string()) { Ok(box_cell) => { print!("Bad token yielded: {}\n", *box_cell); assert!(false); }, Err(s) => { assert_eq!(s, output.to_string()); } } } #[test] fn test_unmatched_list_delim_flat() { let document: &str = "(one two"; let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\""; match lex(document.to_string()) { Ok(box_cell) => { print!("Bad token yielded: {}\n", *box_cell); assert!(false); }, Err(s) => { assert_eq!(s, output.to_string()); } } } #[test] fn test_unmatched_list_delim_complex() { let document: &str = "(one two (three)"; let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\""; match lex(document.to_string()) { Ok(box_cell) => { print!("Bad token yielded: {}\n", *box_cell); assert!(false); }, Err(s) => { assert_eq!(s, output.to_string()); } } } #[test] fn test_comment() { let document: &str = "#!/bin/relish\n(one two)"; let output: &str = "(one two nil)"; match lex(document.to_string()) { Ok(box_cell) => { assert_eq!(format!("{}", *box_cell), output.to_string()); }, Err(s) => { print!("{}\n", s); assert!(false); } } } #[test] fn test_postline_comment() { let document: &str = "#!/bin/relish\n((one two)# another doc comment\n(three four))"; let output: &str = "(one two nil)"; match lex(document.to_string()) { Ok(box_cell) => { assert_eq!(format!("{}", *box_cell), output.to_string()); }, Err(s) => { print!("{}\n", s); assert!(false); } } } #[test] fn test_inline_comment() { let document: &str = "#!/bin/relish\n((one two)\n# another doc comment\nthree)"; let output: &str = "(one two nil)"; match lex(document.to_string()) { Ok(box_cell) => { assert_eq!(format!("{}", *box_cell), output.to_string()); }, Err(s) => { print!("{}\n", s); assert!(false); } } } #[test] fn test_bad_token_list() { let document: &str = "(one t(wo)"; let output: &str = "Problem lexing document: \"list started in middle of another token\""; match lex(document.to_string()) { Ok(box_cell) => { print!("Bad token yielded: {}\n", *box_cell); assert!(false); }, Err(s) => { assert_eq!(s, output.to_string()); } } } }