1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
use super::*;
fn parse(x: &[u8]) -> Ast<PreExpansion> {
do_parse(x).unwrap()
}
fn parse_test(l: Ast<PreExpansion>, r: Ast<PreExpansion>) {
if l != r {
let mut left = Vec::new();
l.cdisplay(&mut left).unwrap();
let mut right = Vec::new();
r.cdisplay(&mut right).unwrap();
let left = String::from_utf8_lossy(&left);
let right = String::from_utf8_lossy(&right);
panic!("parse equality error\nleft: {left}\nright: {right}")
}
}
#[test]
fn command_interp() {
parse_test(
parse(br#""$(echo echo)""#),
pipes([cmd([str([cmdp(pipes([cmd([
estr(b"echo"),
estr(b"echo"),
])]))])])]),
)
}
#[test]
fn string_concat() {
parse_test(
parse(br#" foo'bar'"baz" "#),
pipes([cmd([estr(b"foobarbaz")])]),
);
}
#[test]
fn simple_string() {
parse_test(parse(b"foo"), pipes([cmd([estr(b"foo")])]));
}
#[test]
fn simple_var() {
parse_test(parse(b"$foo"), pipes([cmd([str([var(b"foo")])])]));
}
#[test]
fn ls_pipe_cat() {
parse_test(
parse(b"ls | cat"),
pipes([cmd([estr(b"ls")]), cmd([estr(b"cat")])]),
);
}
#[test]
fn ls_pipe_cat_nospace() {
parse_test(
parse(b"ls|cat"),
pipes([cmd([estr(b"ls")]), cmd([estr(b"cat")])]),
);
}
#[test]
fn unclosed_single_quote() {
assert!(do_parse(b"x'").is_err())
}
#[test]
fn unclosed_double_quote() {
assert!(do_parse(b"x\"").is_err())
}
#[test]
fn tilde() {
parse_test(
parse(b"echo ~"),
pipes([cmd([estr(b"echo"), str([var(b"HOME")])])]),
);
}
#[test]
fn set_variable_in_fun() {
parse_test(
parse(b"fun setter { set x = 1 }"),
decl(estr(b"setter"), assign(estr(b"x"), estr(b"1"))),
);
}
#[test]
fn variable_with_defaults() {
parse_test(
parse(b"${x:-y}"),
pipes([cmd([str([var_default(b"x", estr(b"y"))])])]),
);
}
#[test]
fn escape_newline() {
parse_test(parse(b"\"\\n\""), pipes([cmd([estr(b"\n")])]));
}
#[test]
fn escape_carriage_return() {
parse_test(parse(b"\"\\r\""), pipes([cmd([estr(b"\r")])]));
}
#[test]
fn escape_tab() {
parse_test(parse(b"\"\\t\""), pipes([cmd([estr(b"\t")])]));
}
#[test]
fn escape_hex_1() {
parse_test(parse(b"\\x41"), pipes([cmd([estr(b"A")])]));
}
#[test]
fn escape_hex_2() {
parse_test(parse(b"\\x0a"), pipes([cmd([estr(b"\n")])]));
}
|