summaryrefslogtreecommitdiffstats
path: root/src/parse.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-01-07 19:47:41 -0500
committerJesse Luehrs <doy@tozt.net>2022-01-07 19:47:41 -0500
commit75d1c3aa223f150153d810939b8353f1bc7839b8 (patch)
tree92a4770e80bbd654f4366a2a801cd4235f6e9778 /src/parse.rs
parent0766964a90bef03df35de35d672995a385fc7172 (diff)
downloadnbsh-75d1c3aa223f150153d810939b8353f1bc7839b8.tar.gz
nbsh-75d1c3aa223f150153d810939b8353f1bc7839b8.zip
split tests out into their own file
Diffstat (limited to 'src/parse.rs')
-rw-r--r--src/parse.rs294
1 files changed, 2 insertions, 292 deletions
diff --git a/src/parse.rs b/src/parse.rs
index 72de364..1438281 100644
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -363,295 +363,5 @@ impl std::error::Error for Error {
}
#[cfg(test)]
-mod test {
- use super::*;
-
- impl From<std::os::unix::io::RawFd> for RedirectTarget {
- fn from(fd: std::os::unix::io::RawFd) -> Self {
- Self::Fd(fd)
- }
- }
-
- impl From<std::path::PathBuf> for RedirectTarget {
- fn from(path: std::path::PathBuf) -> Self {
- Self::File(path)
- }
- }
-
- #[allow(clippy::fallible_impl_from)]
- impl From<&str> for RedirectTarget {
- fn from(path: &str) -> Self {
- Self::File(path.try_into().unwrap())
- }
- }
-
- macro_rules! c {
- ($input_string:expr, $($pipelines:expr),*) => {
- Commands {
- pipelines: vec![$($pipelines),*],
- input_string: $input_string.to_string(),
- }
- };
- }
-
- macro_rules! p {
- ($input_string:expr, $($exes:expr),*) => {
- Pipeline {
- exes: vec![$($exes),*],
- input_string: $input_string.to_string(),
- }
- };
- }
-
- macro_rules! e {
- ($word:expr) => {
- Exe {
- exe: $word,
- args: vec![],
- redirects: vec![],
- }
- };
- ($word:expr, $($args:expr),*) => {
- Exe {
- exe: $word,
- args: vec![$($args),*],
- redirects: vec![],
- }
- };
- ($word:expr ; $($redirects:expr),*) => {
- Exe {
- exe: $word,
- args: vec![],
- redirects: vec![$($redirects),*],
- }
- };
- ($word:expr, $($args:expr),* ; $($redirects:expr),*) => {
- Exe {
- exe: $word,
- args: vec![$($args),*],
- redirects: vec![$($redirects),*],
- }
- };
- }
-
- macro_rules! r {
- ($from:literal, $to:literal, $dir:ident) => {
- Redirect {
- from: $from,
- to: $to.into(),
- dir: Direction::$dir,
- }
- };
- }
-
- macro_rules! w {
- ($word:expr) => {
- Word {
- word: $word.to_string(),
- interpolate: true,
- quoted: false,
- }
- };
- ($word:expr, $interpolate:expr) => {
- Word {
- word: $word.to_string(),
- interpolate: $interpolate,
- quoted: false,
- }
- };
- ($word:expr, $interpolate:expr, $quoted:expr) => {
- Word {
- word: $word.to_string(),
- interpolate: $interpolate,
- quoted: $quoted,
- }
- };
- }
-
- macro_rules! parse_eq {
- ($line:literal, $parsed:expr) => {
- assert_eq!(&Commands::parse($line).unwrap(), &$parsed)
- };
- }
-
- #[test]
- fn test_basic() {
- parse_eq!("foo", c!("foo", p!("foo", e!(w!("foo")))));
- parse_eq!(
- "foo bar",
- c!("foo bar", p!("foo bar", e!(w!("foo"), w!("bar"))))
- );
- parse_eq!(
- "foo bar baz",
- c!(
- "foo bar baz",
- p!("foo bar baz", e!(w!("foo"), w!("bar"), w!("baz")))
- )
- );
- parse_eq!(
- "foo | bar",
- c!("foo | bar", p!("foo | bar", e!(w!("foo")), e!(w!("bar"))))
- );
- parse_eq!(
- "command ls; perl -E 'say foo' | tr a-z A-Z; builtin echo bar",
- c!(
- "command ls; perl -E 'say foo' | tr a-z A-Z; builtin echo bar",
- p!(
- "command ls",
- e!(w!("command"), w!("ls"))
- ),
- p!(
- "perl -E 'say foo' | tr a-z A-Z",
- e!(w!("perl"), w!("-E"), w!("say foo", false, true)),
- e!(w!("tr"), w!("a-z"), w!("A-Z"))
- ),
- p!(
- "builtin echo bar",
- e!(w!("builtin"), w!("echo"), w!("bar"))
- )
- )
- );
- }
-
- #[test]
- fn test_whitespace() {
- parse_eq!(" foo ", c!("foo", p!("foo", e!(w!("foo")))));
- parse_eq!(
- " foo # this is a comment",
- c!("foo", p!("foo", e!(w!("foo"))))
- );
- parse_eq!("foo#comment", c!("foo", p!("foo", e!(w!("foo")))));
- parse_eq!(
- "foo;bar|baz;quux#comment",
- c!(
- "foo;bar|baz;quux",
- p!("foo", e!(w!("foo"))),
- p!("bar|baz", e!(w!("bar")), e!(w!("baz"))),
- p!("quux", e!(w!("quux")))
- )
- );
- parse_eq!(
- "foo | bar ",
- c!(
- "foo | bar",
- p!("foo | bar", e!(w!("foo")), e!(w!("bar")))
- )
- );
- parse_eq!(
- " abc def ghi |jkl mno| pqr stu; vwxyz # comment",
- c!(
- "abc def ghi |jkl mno| pqr stu; vwxyz",
- p!(
- "abc def ghi |jkl mno| pqr stu",
- e!(w!("abc"), w!("def"), w!("ghi")),
- e!(w!("jkl"), w!("mno")),
- e!(w!("pqr"), w!("stu"))
- ),
- p!("vwxyz", e!(w!("vwxyz")))
- )
- );
- parse_eq!(
- "foo 'bar # baz' \"quux # not a comment\" # comment",
- c!(
- "foo 'bar # baz' \"quux # not a comment\"",
- p!(
- "foo 'bar # baz' \"quux # not a comment\"",
- e!(
- w!("foo"),
- w!("bar # baz", false, true),
- w!("quux # not a comment", true, true)
- )
- )
- )
- );
- }
-
- #[test]
- fn test_redirect() {
- parse_eq!(
- "foo > bar",
- c!(
- "foo > bar",
- p!("foo > bar", e!(w!("foo") ; r!(1, "bar", Out)))
- )
- );
- parse_eq!(
- "foo <bar",
- c!("foo <bar", p!("foo <bar", e!(w!("foo") ; r!(0, "bar", In))))
- );
- parse_eq!(
- "foo > /dev/null 2>&1",
- c!(
- "foo > /dev/null 2>&1",
- p!(
- "foo > /dev/null 2>&1",
- e!(w!("foo") ; r!(1, "/dev/null", Out), r!(2, 1, Out))
- )
- )
- );
- parse_eq!(
- "foo >>bar",
- c!(
- "foo >>bar",
- p!("foo >>bar", e!(w!("foo") ; r!(1, "bar", Append)))
- )
- );
- parse_eq!(
- "foo >> bar",
- c!(
- "foo >> bar",
- p!("foo >> bar", e!(w!("foo") ; r!(1, "bar", Append)))
- )
- );
- parse_eq!(
- "foo > 'bar baz'",
- c!(
- "foo > 'bar baz'",
- p!("foo > 'bar baz'", e!(w!("foo") ; r!(1, "bar baz", Out)))
- )
- );
- }
-
- #[test]
- fn test_escape() {
- parse_eq!(
- "foo\\ bar",
- c!("foo\\ bar", p!("foo\\ bar", e!(w!("foo bar"))))
- );
- parse_eq!(
- "'foo\\ bar'",
- c!(
- "'foo\\ bar'",
- p!("'foo\\ bar'", e!(w!("foo\\ bar", false, true)))
- )
- );
- parse_eq!(
- "\"foo\\ bar\"",
- c!(
- "\"foo\\ bar\"",
- p!("\"foo\\ bar\"", e!(w!("foo bar", true, true)))
- )
- );
- parse_eq!(
- "\"foo\\\"bar\"",
- c!(
- "\"foo\\\"bar\"",
- p!("\"foo\\\"bar\"", e!(w!("foo\"bar", true, true)))
- )
- );
- parse_eq!(
- "'foo\\'bar\\\\'",
- c!(
- "'foo\\'bar\\\\'",
- p!("'foo\\'bar\\\\'", e!(w!("foo'bar\\", false, true)))
- )
- );
- parse_eq!(
- "foo > bar\\ baz",
- c!(
- "foo > bar\\ baz",
- p!("foo > bar\\ baz", e!(w!("foo") ; r!(1, "bar baz", Out)))
- )
- );
- }
-}
+#[path = "test_parse.rs"]
+mod test;