2022-10-26 19:31:39 +00:00
|
|
|
use expect_test::{expect, Expect};
|
2022-07-14 22:26:57 +00:00
|
|
|
use oyster_parser::ParseTree;
|
|
|
|
|
2022-10-26 19:31:39 +00:00
|
|
|
fn check(s: &str, expect: Expect) {
|
|
|
|
let actual = ParseTree::from(s);
|
|
|
|
expect.assert_debug_eq(&actual);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
"",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn word() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
"word",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 4,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn word_with_escape() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
r"hello\#world",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 5,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: EscapedChar,
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 5,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unicode() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
"谚语",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_command() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
"echo hi",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 4,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn semicolon() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
"hello; hi",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 5,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2022-11-07 13:41:28 +00:00
|
|
|
Leaf {
|
|
|
|
kind: Semicolon,
|
|
|
|
len: 1,
|
|
|
|
},
|
2022-10-26 19:31:39 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comment() {
|
|
|
|
let source = r"# a
|
|
|
|
# b";
|
|
|
|
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
source,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: Comment,
|
|
|
|
len: 3,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Newlines,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Comment,
|
|
|
|
len: 3,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_comment() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
"whoami # hello",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Comment,
|
|
|
|
len: 7,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pipeline() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
"whoami | cat",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Pipe,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 3,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiline_pipeline() {
|
|
|
|
let source = r"whoami |
|
|
|
|
wc -l";
|
|
|
|
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
source,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Pipe,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Newlines,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comment_in_pipeline() {
|
|
|
|
let source = r"whoami | # comment
|
|
|
|
wc -l";
|
|
|
|
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
source,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Pipe,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Comment,
|
|
|
|
len: 9,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Newlines,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reject_leading_pipe() {
|
|
|
|
let source = r"whoami
|
|
|
|
| cat";
|
|
|
|
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
source,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Newlines,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Error {
|
|
|
|
kind: UnexpectedPipe,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 3,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reject_trailing_pipe() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
"whoami |",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Pipe,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Error {
|
|
|
|
kind: UnexpectedEof,
|
|
|
|
len: 0,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reject_pipe_semicolon() {
|
|
|
|
let source = "whoami | ; cat";
|
|
|
|
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
source,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Pipe,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Error {
|
|
|
|
kind: UnexpectedSemicolon,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 3,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reject_double_pipe() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
"whoami | | cat",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Pipe,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Error {
|
|
|
|
kind: UnexpectedPipe,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 3,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-07-14 22:26:57 +00:00
|
|
|
}
|
2022-09-24 17:17:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_quote_string() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
r#""hello world""#,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: DQuotedString,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: DoubleQuote,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 11,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: DoubleQuote,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-09-24 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn escaped_char_in_double_quotes() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
r#""hello \" world""#,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: DQuotedString,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: DoubleQuote,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: EscapedChar,
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: DoubleQuote,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-09-24 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unterminated_double_quotes() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
r#""hello world"#,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: DQuotedString,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: DoubleQuote,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 11,
|
|
|
|
},
|
|
|
|
Error {
|
|
|
|
kind: UnexpectedEof,
|
|
|
|
len: 0,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-09-24 17:17:56 +00:00
|
|
|
}
|
2022-10-24 19:27:04 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn command_substitution() {
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
r#"echo (whoami)"#,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 4,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: CommandSubstitution,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: OpeningParenthesis,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: ClosingParenthesis,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-10-24 19:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn quoted_command_substitution() {
|
|
|
|
let source = r#"echo "(whoami)
|
|
|
|
""#;
|
|
|
|
|
2022-10-26 19:31:39 +00:00
|
|
|
check(
|
|
|
|
source,
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 4,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: Whitespace,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: DQuotedString,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: DoubleQuote,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: CommandSubstitution,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: OpeningParenthesis,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Tree {
|
|
|
|
kind: Pipeline,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Command,
|
|
|
|
children: [
|
|
|
|
Tree {
|
|
|
|
kind: Word,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: ClosingParenthesis,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: PlainText,
|
|
|
|
len: 9,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
kind: DoubleQuote,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2022-10-24 19:27:04 +00:00
|
|
|
}
|
2022-11-07 13:41:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_pipeline() {
|
|
|
|
check(
|
|
|
|
";",
|
|
|
|
expect![[r#"
|
|
|
|
Tree {
|
|
|
|
kind: Program,
|
|
|
|
children: [
|
|
|
|
Leaf {
|
|
|
|
kind: Semicolon,
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|