2021-01-24 12:34:58 -08:00
|
|
|
mod lex_tests {
|
2023-01-27 17:45:19 -08:00
|
|
|
use relish::ast::lex;
|
2021-01-24 12:34:58 -08:00
|
|
|
|
2021-01-24 22:04:26 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_lex_basic_pair() {
|
2023-02-15 23:27:00 -08:00
|
|
|
let document = String::from("(hello 'world')");
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), document);
|
2021-01-24 22:04:26 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-24 12:34:58 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_lex_basic_list() {
|
2023-02-15 23:27:00 -08:00
|
|
|
let document = String::from("(hello 'world' 1 2 3)");
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), document);
|
2021-01-24 22:04:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 13:16:25 -08:00
|
|
|
fn test_bad_symbol() {
|
2023-03-24 18:14:33 -07:00
|
|
|
let document = String::from("(as@dd)");
|
|
|
|
|
let output: &str = "Problem lexing document: \"Unparsable token: as@dd\"";
|
2023-03-10 13:16:25 -08:00
|
|
|
assert_eq!(lex(&document).err().unwrap(), output.to_string(),);
|
2021-01-24 12:34:58 -08:00
|
|
|
}
|
2021-01-24 22:32:09 -08:00
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 13:16:25 -08:00
|
|
|
fn test_lex_complex_list() {
|
|
|
|
|
let document = String::from("(hello 'world' (1 2 (1 2 3)) 1 2 3)");
|
|
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), document);
|
2021-01-24 22:32:09 -08:00
|
|
|
}
|
|
|
|
|
|
2021-09-18 16:48:24 -07:00
|
|
|
#[test]
|
|
|
|
|
fn test_list_delim_in_str() {
|
2023-02-15 23:27:00 -08:00
|
|
|
let document = String::from("('(')");
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), document);
|
2021-09-18 16:48:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_empty_string() {
|
2023-02-15 23:27:00 -08:00
|
|
|
let document = String::from("('')");
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), document);
|
2021-09-18 16:48:24 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-24 22:32:09 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_unmatched_list_delim_flat() {
|
2023-02-15 23:27:00 -08:00
|
|
|
let document = String::from("(one two");
|
2021-01-24 22:32:09 -08:00
|
|
|
let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\"";
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).err().unwrap(), output.to_string(),);
|
2021-01-24 22:32:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_unmatched_list_delim_complex() {
|
2023-02-15 23:27:00 -08:00
|
|
|
let document = String::from("(one two (three)");
|
2021-01-24 22:32:09 -08:00
|
|
|
let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\"";
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).err().unwrap(), output.to_string(),);
|
2021-01-24 22:32:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 13:16:25 -08:00
|
|
|
fn test_comment_1() {
|
2023-02-15 23:27:00 -08:00
|
|
|
let document = String::from("#!/bin/relish\n(one two)");
|
2021-01-25 20:55:16 -08:00
|
|
|
let output: &str = "(one two)";
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), output.to_string(),);
|
2021-01-24 22:32:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 13:16:25 -08:00
|
|
|
fn test_comment_2() {
|
|
|
|
|
let document = String::from(";; big doc string\n(one two)");
|
|
|
|
|
let output: &str = "(one two)";
|
|
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), output.to_string(),);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_postline_comment_1() {
|
|
|
|
|
let document =
|
|
|
|
|
String::from("#!/bin/relish\n((one two)# another doc comment\n('three' four))");
|
|
|
|
|
let output: &str = "((one two) ('three' four))";
|
|
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), output.to_string(),);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_postline_comment_2() {
|
2023-03-01 11:38:02 -08:00
|
|
|
let document =
|
2023-03-10 13:16:25 -08:00
|
|
|
String::from("#!/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))";
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), output.to_string(),);
|
2021-01-24 22:32:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 13:16:25 -08:00
|
|
|
fn test_inline_comment_1() {
|
|
|
|
|
let document = String::from("#!/bin/relish\n((one two)\n# another comment\nthree)");
|
|
|
|
|
let output: &str = "((one two) three)";
|
|
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), output.to_string(),);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_inline_comment_2() {
|
|
|
|
|
let document = String::from("# head\n((one two)\n;; another comment\nthree)");
|
2021-01-25 20:55:16 -08:00
|
|
|
let output: &str = "((one two) three)";
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).unwrap().to_string(), output.to_string(),);
|
2021-01-24 22:32:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_bad_token_list() {
|
2023-02-15 23:27:00 -08:00
|
|
|
let document = String::from("(one t(wo)");
|
2021-01-24 22:32:09 -08:00
|
|
|
let output: &str = "Problem lexing document: \"list started in middle of another token\"";
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(lex(&document).err().unwrap(), output.to_string(),);
|
2021-01-24 22:32:09 -08:00
|
|
|
}
|
2021-01-24 12:34:58 -08:00
|
|
|
}
|