2022-10-26 19:31:39 +00:00
|
|
|
use expect_test::expect;
|
2022-07-14 22:26:57 +00:00
|
|
|
use oyster_parser::Lexer;
|
|
|
|
|
2022-10-26 19:31:39 +00:00
|
|
|
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() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
"",
|
|
|
|
expect![[r#"
|
2022-11-05 13:32:38 +00:00
|
|
|
Token {
|
|
|
|
kind: Eof,
|
|
|
|
len: 0,
|
|
|
|
}
|
|
|
|
"#]],
|
2022-10-26 19:31:39 +00:00
|
|
|
Command
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whitespace() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
" \t \t\t",
|
|
|
|
expect![[r#"
|
|
|
|
Token {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 6,
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
Command
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn newlines() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
"\n\n\n",
|
|
|
|
expect![[r#"
|
|
|
|
Token {
|
|
|
|
kind: Newlines,
|
|
|
|
len: 3,
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
Command
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn semicolon() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
";",
|
|
|
|
expect![[r#"
|
2022-11-05 13:32:38 +00:00
|
|
|
Token {
|
|
|
|
kind: Semicolon,
|
|
|
|
len: 1,
|
|
|
|
}
|
|
|
|
"#]],
|
2022-10-26 19:31:39 +00:00
|
|
|
Command
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pipe() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
"|",
|
|
|
|
expect![[r#"
|
2022-11-05 13:32:38 +00:00
|
|
|
Token {
|
|
|
|
kind: Pipe,
|
|
|
|
len: 1,
|
|
|
|
}
|
|
|
|
"#]],
|
2022-10-26 19:31:39 +00:00
|
|
|
Command
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn plain_word() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
"whoami",
|
|
|
|
expect![[r#"
|
|
|
|
Token {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
Command
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn word_with_hash() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
"nixpkgs#hello",
|
|
|
|
expect![[r#"
|
|
|
|
Token {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 13,
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
Command
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn escaped_hash() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
r"\#",
|
|
|
|
expect![[r#"
|
|
|
|
Token {
|
|
|
|
kind: EscapedChar,
|
|
|
|
len: 2,
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
Command
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comment() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
"# hey",
|
|
|
|
expect![[r#"
|
2022-11-05 13:32:38 +00:00
|
|
|
Token {
|
|
|
|
kind: Comment,
|
|
|
|
len: 5,
|
|
|
|
}
|
|
|
|
"#]],
|
2022-10-26 19:31:39 +00:00
|
|
|
Command
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
2022-09-24 17:17:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_quotes_in_command() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
r#"""#,
|
|
|
|
expect![[r#"
|
|
|
|
Token {
|
|
|
|
kind: DoubleQuote,
|
|
|
|
len: 1,
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
Command
|
|
|
|
);
|
2022-09-24 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_quotes_in_string() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
r#"""#,
|
|
|
|
expect![[r#"
|
|
|
|
"Token { kind: DoubleQuote, len: 1 }"
|
|
|
|
"#]],
|
|
|
|
String
|
|
|
|
);
|
2022-09-24 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn escaped_quotes_in_string() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
r#"\""#,
|
|
|
|
expect![[r#"
|
|
|
|
"Token { kind: EscapedChar, len: 2 }"
|
|
|
|
"#]],
|
|
|
|
String
|
|
|
|
);
|
2022-09-24 17:17:56 +00:00
|
|
|
}
|
2022-10-24 19:27:04 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opening_parenthesis_command_mode() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
r"(",
|
|
|
|
expect![[r#"
|
|
|
|
Token {
|
|
|
|
kind: OpeningParenthesis,
|
|
|
|
len: 1,
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
Command
|
|
|
|
);
|
2022-10-24 19:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn closing_parenthesis_command_mode() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
r")",
|
|
|
|
expect![[r#"
|
|
|
|
Token {
|
|
|
|
kind: ClosingParenthesis,
|
|
|
|
len: 1,
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
Command
|
|
|
|
);
|
2022-10-24 19:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opening_parenthesis_string_mode() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
r"(",
|
|
|
|
expect![[r#"
|
|
|
|
"Token { kind: OpeningParenthesis, len: 1 }"
|
|
|
|
"#]],
|
|
|
|
String
|
|
|
|
);
|
2022-10-24 19:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn closing_parenthesis_string_mode() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
r")",
|
|
|
|
expect![[r#"
|
|
|
|
"Token { kind: ClosingParenthesis, len: 1 }"
|
|
|
|
"#]],
|
|
|
|
String
|
|
|
|
);
|
2022-10-24 19:27:04 +00:00
|
|
|
}
|
2022-10-26 14:46:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn escape_newline() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check!(
|
|
|
|
"\\\n",
|
|
|
|
expect![[r#"
|
|
|
|
Token {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 2,
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
Command
|
|
|
|
);
|
2022-10-26 14:46:13 +00:00
|
|
|
}
|