oysh/crates/oyster_parser/tests/it/lexer.rs

241 lines
3.8 KiB
Rust
Raw Normal View History

use expect_test::expect;
2022-07-14 22:26:57 +00:00
use oyster_parser::Lexer;
macro_rules! check {
($s:expr, $expect:expr, Command) => {{
let actual = Lexer::new($s).next_command_token();
$expect.assert_debug_eq(&actual);
}};
($s:expr, $expect:expr, String) => {{
let actual = Lexer::new($s).next_string_token();
let actual = format!("{:?}", actual);
$expect.assert_debug_eq(&actual);
}};
}
2022-07-14 22:26:57 +00:00
#[test]
fn eof() {
check!(
"",
expect![[r#"
Token {
kind: Eof,
len: 0,
}
"#]],
Command
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn whitespace() {
check!(
" \t \t\t",
expect![[r#"
Token {
kind: Whitespace,
len: 6,
}
"#]],
Command
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn newlines() {
check!(
"\n\n\n",
expect![[r#"
Token {
kind: Newlines,
len: 3,
}
"#]],
Command
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn semicolon() {
check!(
";",
expect![[r#"
Token {
kind: Semicolon,
len: 1,
}
"#]],
Command
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn pipe() {
check!(
"|",
expect![[r#"
Token {
kind: Pipe,
len: 1,
}
"#]],
Command
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn plain_word() {
check!(
"whoami",
expect![[r#"
Token {
kind: PlainText,
len: 6,
}
"#]],
Command
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn word_with_hash() {
check!(
"nixpkgs#hello",
expect![[r#"
Token {
kind: PlainText,
len: 13,
}
"#]],
Command
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn escaped_hash() {
check!(
r"\#",
expect![[r#"
Token {
kind: EscapedChar,
len: 2,
}
"#]],
Command
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn comment() {
check!(
"# hey",
expect![[r#"
Token {
kind: Comment,
len: 5,
}
"#]],
Command
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn double_quotes_in_command() {
check!(
r#"""#,
expect![[r#"
Token {
kind: DoubleQuote,
len: 1,
}
"#]],
Command
);
}
#[test]
fn double_quotes_in_string() {
check!(
r#"""#,
expect![[r#"
"Token { kind: DoubleQuote, len: 1 }"
"#]],
String
);
}
#[test]
fn escaped_quotes_in_string() {
check!(
r#"\""#,
expect![[r#"
"Token { kind: EscapedChar, len: 2 }"
"#]],
String
);
}
#[test]
fn opening_parenthesis_command_mode() {
check!(
r"(",
expect![[r#"
Token {
kind: OpeningParenthesis,
len: 1,
}
"#]],
Command
);
}
#[test]
fn closing_parenthesis_command_mode() {
check!(
r")",
expect![[r#"
Token {
kind: ClosingParenthesis,
len: 1,
}
"#]],
Command
);
}
#[test]
fn opening_parenthesis_string_mode() {
check!(
r"(",
expect![[r#"
"Token { kind: OpeningParenthesis, len: 1 }"
"#]],
String
);
}
#[test]
fn closing_parenthesis_string_mode() {
check!(
r")",
expect![[r#"
"Token { kind: ClosingParenthesis, len: 1 }"
"#]],
String
);
}
#[test]
fn escape_newline() {
check!(
"\\\n",
expect![[r#"
Token {
kind: Whitespace,
len: 2,
}
"#]],
Command
);
}