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

702 lines
23 KiB
Rust
Raw Permalink Normal View History

use expect_test::{expect, Expect};
use oyster_parser::ast;
2022-07-14 22:26:57 +00:00
fn check(s: &str, expect: Expect) {
let actual = ast::Code::try_from(s);
expect.assert_debug_eq(&actual);
2022-07-14 22:26:57 +00:00
}
#[test]
fn empty() {
check(
"",
expect![[r#"
Ok(
Code(
[],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn word() {
check(
"word",
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"word",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn word_with_escape() {
check(
r"hello\#world",
expect![[r##"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"hello",
),
Text(
"#",
),
Text(
"world",
),
],
),
],
None,
),
],
),
),
],
),
)
"##]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn unicode() {
check(
"谚语",
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"谚语",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn simple_command() {
check(
"echo hi",
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"echo",
),
],
),
Word(
[
Text(
"hi",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn semicolon() {
check(
"hello; hi",
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"hello",
),
],
),
],
None,
),
],
),
),
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"hi",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn comment() {
let source = r"# a
# b";
check(
source,
expect![[r#"
Ok(
Code(
[],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn inline_comment() {
check(
"whoami # hello",
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"whoami",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn pipeline() {
check(
"whoami | cat",
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"whoami",
),
],
),
],
Stdout,
),
Command(
[
Word(
[
Text(
"cat",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn multiline_pipeline() {
let source = r"whoami |
wc -l";
check(
source,
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"whoami",
),
],
),
],
Stdout,
),
Command(
[
Word(
[
Text(
"wc",
),
],
),
Word(
[
Text(
"-l",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn comment_in_pipeline() {
let source = r"whoami | # comment
wc -l";
check(
source,
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"whoami",
),
],
),
],
Stdout,
),
Command(
[
Word(
[
Text(
"wc",
),
],
),
Word(
[
Text(
"-l",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn reject_leading_pipe() {
let source = r"whoami
| cat";
check(
source,
expect![[r#"
Err(
UnexpectedPipe,
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn reject_trailing_pipe() {
check(
"whoami |",
expect![[r#"
Err(
UnexpectedEof,
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn reject_pipe_semicolon() {
let source = "whoami | ; cat";
check(
source,
expect![[r#"
Err(
UnexpectedSemicolon,
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn reject_double_pipe() {
check(
"whoami | | cat",
expect![[r#"
Err(
UnexpectedPipe,
)
"#]],
);
2022-07-14 22:26:57 +00:00
}
#[test]
fn double_quote_string() {
check(
r#""hello world""#,
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"hello world",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
}
#[test]
fn escaped_char_in_double_quotes() {
check(
r#""hello \" world""#,
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"hello ",
),
Text(
"\"",
),
Text(
" world",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
}
#[test]
fn unterminated_double_quotes() {
check(
r#""hello world"#,
expect![[r#"
Err(
UnexpectedEof,
)
"#]],
);
}
#[test]
fn command_substitution() {
check(
r#"echo (whoami)"#,
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"echo",
),
],
),
Word(
[
CommandSubstitution(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"whoami",
),
],
),
],
None,
),
],
),
),
],
),
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
}
#[test]
fn quoted_command_substitution() {
let source = r#"echo "(whoami)
""#;
check(
source,
expect![[r#"
Ok(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"echo",
),
],
),
Word(
[
CommandSubstitution(
Code(
[
Pipeline(
Pipeline(
[
Command(
[
Word(
[
Text(
"whoami",
),
],
),
],
None,
),
],
),
),
],
),
),
Text(
"\n ",
),
],
),
],
None,
),
],
),
),
],
),
)
"#]],
);
}
#[test]
fn empty_pipeline() {
check(
";",
expect![[r#"
Ok(
Code(
[],
),
)
"#]],
);
}