Skip to content

Commit

Permalink
feat(eval): add commands to parse in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lthoerner committed Sep 17, 2023
1 parent d20bc0d commit d67bb57
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 14 deletions.
130 changes: 118 additions & 12 deletions src/eval/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,129 @@ mod test {
use super::*;

#[test]
fn tokenizer_test_1() {
let cmd = "do the thing || ping pong | grep \"response and xyz\" | foo \'bar baz\'";
let parse_result = RushTokenizer::parse(Rule::input_line, cmd);
println!("{:#?}", parse_result);
fn simple_command_no_quotes() {
let command = r#"echo Hello world!"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 35 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn tokenizer_test_2() {
let cmd = "ping (getip \'google website\') | grep response";
let parse_result = RushTokenizer::parse(Rule::input_line, cmd);
println!("{:#?}", parse_result);
fn simple_command_double_quotes() {
let command = r#"echo "Hello world!""#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 41 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn tokenizer_test_3() {
let cmd = "test 1234";
let parse_result = RushTokenizer::parse(Rule::input_line, cmd);
println!("{:#?}", parse_result);
fn simple_command_single_quotes() {
let command = r#"echo 'Hello world!'"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 47 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn simple_command_escapes() {
let command = r#"echo Hello\ world!"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 53 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn simple_command_substitution() {
let command = r#"echo (date)"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 59 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn single_pipe() {
let command = r#"ls | sort"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 65 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn multi_pipe() {
let command = r#"ls | sort | grep rwx"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 71 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn single_logic_operation() {
let command = r#"mkdir testdir && cd testdir"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 77 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn multi_logic_operation() {
let command = r#"mkdir testdir && cd testdir && touch testfile"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 83 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn single_redirect() {
let command = r#"sort < file.txt"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);

Check warning on line 89 in src/eval/parser.rs

View workflow job for this annotation

GitHub Actions / Test and Compile shell

unused variable: `parse_result`
}

#[test]
fn multi_redirect() {
let command = r#"sort < unsorted.txt >> sorted.txt"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn special_chars() {
// TODO: Figure out whether EOF should be part of syntax
let command = r#"echo \nHello\ world!\nEOF"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn nested_substitution() {
let command = r#"echo (echo (date))"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn nested_substitution_with_pipe() {
let command = r#"echo (ls | wc -l)"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn logic_operation_with_substitution() {
let command = r#"test -d testdir && echo "(date): Directory exists"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn logic_operation_with_redirect() {
let command = r#"ls fake_dir 2>/dev/null || echo "(date): Directory does not exist"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn double_sided_redirect() {
let command = r#"command <> file.txt"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn pattern_matching() {
let command = r#"ls *.txt"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn multi_argument() {
let command = r#"command --option argument"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn double_quotes_with_escapes() {
let command = r#"echo "This is a \"test\""#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}

#[test]
fn double_quotes_with_special_chars() {
let command = r#"echo -e "First line\nSecond line"#;
let parse_result = RushTokenizer::parse(Rule::input_line, command);
}
}
4 changes: 2 additions & 2 deletions src/tokenizer.pest
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ literal_char = _{ char | "(" | ")" | "\\\'" | "\"" }

nonliteral_argument = @{ nonliteral_char+ }
literal_argument = @{ literal_char+ }
single_quoted_argument = { "\'" ~ literal_argument* ~ "\'" }
double_quoted_argument = { "\"" ~ nonliteral_argument* ~ "\"" }
single_quoted_argument = _{ "\'" ~ literal_argument* ~ "\'" }
double_quoted_argument = _{ "\"" ~ nonliteral_argument* ~ "\"" }

// A command substitution is a way to inject the output of a given command into the outer line of inputS
substitution = { "(" ~ command ~ ")" }
Expand Down

0 comments on commit d67bb57

Please sign in to comment.