240 lines
3.8 KiB
Rust
240 lines
3.8 KiB
Rust
use expect_test::expect;
|
|
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);
|
|
}};
|
|
}
|
|
|
|
#[test]
|
|
fn eof() {
|
|
check!(
|
|
"",
|
|
expect![[r#"
|
|
Token {
|
|
kind: Eof,
|
|
len: 0,
|
|
}
|
|
"#]],
|
|
Command
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn whitespace() {
|
|
check!(
|
|
" \t \t\t",
|
|
expect![[r#"
|
|
Token {
|
|
kind: Whitespace,
|
|
len: 6,
|
|
}
|
|
"#]],
|
|
Command
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn newlines() {
|
|
check!(
|
|
"\n\n\n",
|
|
expect![[r#"
|
|
Token {
|
|
kind: Newlines,
|
|
len: 3,
|
|
}
|
|
"#]],
|
|
Command
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn semicolon() {
|
|
check!(
|
|
";",
|
|
expect![[r#"
|
|
Token {
|
|
kind: Semicolon,
|
|
len: 1,
|
|
}
|
|
"#]],
|
|
Command
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn pipe() {
|
|
check!(
|
|
"|",
|
|
expect![[r#"
|
|
Token {
|
|
kind: Pipe,
|
|
len: 1,
|
|
}
|
|
"#]],
|
|
Command
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn plain_word() {
|
|
check!(
|
|
"whoami",
|
|
expect![[r#"
|
|
Token {
|
|
kind: PlainText,
|
|
len: 6,
|
|
}
|
|
"#]],
|
|
Command
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn word_with_hash() {
|
|
check!(
|
|
"nixpkgs#hello",
|
|
expect![[r#"
|
|
Token {
|
|
kind: PlainText,
|
|
len: 13,
|
|
}
|
|
"#]],
|
|
Command
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn escaped_hash() {
|
|
check!(
|
|
r"\#",
|
|
expect![[r#"
|
|
Token {
|
|
kind: EscapedChar,
|
|
len: 2,
|
|
}
|
|
"#]],
|
|
Command
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn comment() {
|
|
check!(
|
|
"# hey",
|
|
expect![[r#"
|
|
Token {
|
|
kind: Comment,
|
|
len: 5,
|
|
}
|
|
"#]],
|
|
Command
|
|
);
|
|
}
|
|
|
|
#[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
|
|
);
|
|
}
|