2021-01-24 12:34:58 -08:00
|
|
|
mod lex_tests {
|
2021-03-14 16:14:57 -07:00
|
|
|
use relish::ast::{lex, ast_to_string};
|
2021-01-24 12:34:58 -08:00
|
|
|
|
2021-01-24 22:04:26 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_lex_basic_pair() {
|
|
|
|
|
let document: &str = "(hello 'world')";
|
|
|
|
|
match lex(document.to_string()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
assert_eq!(ast_to_string(tree), document);
|
2021-01-24 22:04:26 -08:00
|
|
|
},
|
|
|
|
|
Err(s) => {
|
|
|
|
|
print!("{}\n", s);
|
|
|
|
|
assert!(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 12:34:58 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_lex_basic_list() {
|
2021-01-24 22:04:26 -08:00
|
|
|
let document: &str = "(hello 'world' 1 2 3)";
|
|
|
|
|
match lex(document.to_string()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
assert_eq!(ast_to_string(tree), document);
|
2021-01-24 22:04:26 -08:00
|
|
|
},
|
|
|
|
|
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)";
|
2021-01-24 12:34:58 -08:00
|
|
|
match lex(document.to_string()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
assert_eq!(ast_to_string(tree), document);
|
2021-01-24 12:34:58 -08:00
|
|
|
},
|
2021-01-24 22:04:26 -08:00
|
|
|
Err(s) => {
|
|
|
|
|
print!("{}\n", s);
|
|
|
|
|
assert!(false);
|
|
|
|
|
}
|
2021-01-24 12:34:58 -08:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-24 22:32:09 -08:00
|
|
|
|
|
|
|
|
#[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()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
print!("Bad token yielded: {}\n", ast_to_string(tree));
|
2021-01-24 22:32:09 -08:00
|
|
|
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()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
print!("Bad token yielded: {}\n", ast_to_string(tree));
|
2021-01-24 22:32:09 -08:00
|
|
|
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()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
print!("Bad token yielded: {}\n", ast_to_string(tree));
|
2021-01-24 22:32:09 -08:00
|
|
|
assert!(false);
|
|
|
|
|
},
|
|
|
|
|
Err(s) => {
|
|
|
|
|
assert_eq!(s, output.to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_comment() {
|
|
|
|
|
let document: &str = "#!/bin/relish\n(one two)";
|
2021-01-25 20:55:16 -08:00
|
|
|
let output: &str = "(one two)";
|
2021-01-24 22:32:09 -08:00
|
|
|
match lex(document.to_string()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
assert_eq!(ast_to_string(tree), output.to_string());
|
2021-01-24 22:32:09 -08:00
|
|
|
},
|
|
|
|
|
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))";
|
2021-01-25 20:55:16 -08:00
|
|
|
let output: &str = "((one two) (three four))";
|
2021-01-24 22:32:09 -08:00
|
|
|
match lex(document.to_string()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
assert_eq!(ast_to_string(tree), output.to_string());
|
2021-01-24 22:32:09 -08:00
|
|
|
},
|
|
|
|
|
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)";
|
2021-01-25 20:55:16 -08:00
|
|
|
let output: &str = "((one two) three)";
|
2021-01-24 22:32:09 -08:00
|
|
|
match lex(document.to_string()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
assert_eq!(ast_to_string(tree), output.to_string());
|
2021-01-24 22:32:09 -08:00
|
|
|
},
|
|
|
|
|
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()) {
|
2021-03-14 16:14:57 -07:00
|
|
|
Ok(tree) => {
|
|
|
|
|
print!("Bad token yielded: {}\n", ast_to_string(tree));
|
2021-01-24 22:32:09 -08:00
|
|
|
assert!(false);
|
|
|
|
|
},
|
|
|
|
|
Err(s) => {
|
|
|
|
|
assert_eq!(s, output.to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-24 12:34:58 -08:00
|
|
|
}
|