Commit 3207cf48 authored by stratact's avatar stratact Committed by Michael Aaron Murphy
Browse files

Fix tests

parent 8d5eac76
......@@ -155,42 +155,42 @@ fn test_evaluate_arguments() {
assert_eq!(evaluate_arguments(&[], &shell), Ok(false));
// multiple arguments
// ignores all but the first argument
assert_eq!(evaluate_arguments(&["foo", "bar"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["foo".to_owned(), "bar".to_owned()], &shell), Ok(true));
// check `exists STRING`
assert_eq!(evaluate_arguments(&[""], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["string"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["string with space"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-startswithdash"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["".to_owned()], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["string".to_owned()], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["string with space".to_owned()], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-startswithdash".to_owned()], &shell), Ok(true));
// check `exists -a`
// no argument means we treat it as a string
assert_eq!(evaluate_arguments(&["-a"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-a".to_owned()], &shell), Ok(true));
shell
.variables
.set_array("emptyarray", SmallVec::from_vec(Vec::new()));
assert_eq!(evaluate_arguments(&["-a", "emptyarray"], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["-a".to_owned(), "emptyarray".to_owned()], &shell), Ok(false));
let mut vec = Vec::new();
vec.push("element".to_owned());
shell.variables.set_array("array", SmallVec::from_vec(vec));
assert_eq!(evaluate_arguments(&["-a", "array"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-a".to_owned(), "array".to_owned()], &shell), Ok(true));
shell.variables.unset_array("array");
assert_eq!(evaluate_arguments(&["-a", "array"], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["-a".to_owned(), "array".to_owned()], &shell), Ok(false));
// check `exists -b`
// TODO: see test_binary_is_in_path()
// no argument means we treat it as a string
assert_eq!(evaluate_arguments(&["-b"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-b".to_owned()], &shell), Ok(true));
let oldpath = shell.get_var("PATH").unwrap_or("/usr/bin".to_owned());
shell.set_var("PATH", "testing/");
assert_eq!(
evaluate_arguments(&["-b", "executable_file"], &shell),
evaluate_arguments(&["-b".to_owned(), "executable_file".to_owned()], &shell),
Ok(true)
);
assert_eq!(evaluate_arguments(&["-b", "empty_file"], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["-b".to_owned(), "empty_file".to_owned()], &shell), Ok(false));
assert_eq!(
evaluate_arguments(&["-b", "file_does_not_exist"], &shell),
evaluate_arguments(&["-b".to_owned(), "file_does_not_exist".to_owned()], &shell),
Ok(false)
);
......@@ -200,45 +200,45 @@ fn test_evaluate_arguments() {
// check `exists -d`
// no argument means we treat it as a string
assert_eq!(evaluate_arguments(&["-d"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-d", "testing/"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-d".to_owned()], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-d".to_owned(), "testing/".to_owned()], &shell), Ok(true));
assert_eq!(
evaluate_arguments(&["-d", "testing/empty_file"], &shell),
evaluate_arguments(&["-d".to_owned(), "testing/empty_file".to_owned()], &shell),
Ok(false)
);
assert_eq!(
evaluate_arguments(&["-d", "does/not/exist/"], &shell),
evaluate_arguments(&["-d".to_owned(), "does/not/exist/".to_owned()], &shell),
Ok(false)
);
// check `exists -f`
// no argument means we treat it as a string
assert_eq!(evaluate_arguments(&["-f"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-f", "testing/"], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["-f".to_owned()], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-f".to_owned(), "testing/".to_owned()], &shell), Ok(false));
assert_eq!(
evaluate_arguments(&["-f", "testing/empty_file"], &shell),
evaluate_arguments(&["-f".to_owned(), "testing/empty_file".to_owned()], &shell),
Ok(true)
);
assert_eq!(
evaluate_arguments(&["-f", "does-not-exist"], &shell),
evaluate_arguments(&["-f".to_owned(), "does-not-exist".to_owned()], &shell),
Ok(false)
);
// check `exists -s`
// no argument means we treat it as a string
assert_eq!(evaluate_arguments(&["-s"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-s".to_owned()], &shell), Ok(true));
shell.set_var("emptyvar", "");
assert_eq!(evaluate_arguments(&["-s", "emptyvar"], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["-s".to_owned(), "emptyvar".to_owned()], &shell), Ok(false));
shell.set_var("testvar", "foobar");
assert_eq!(evaluate_arguments(&["-s", "testvar"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-s".to_owned(), "testvar".to_owned()], &shell), Ok(true));
shell.variables.unset_var("testvar");
assert_eq!(evaluate_arguments(&["-s", "testvar"], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["-s".to_owned(), "testvar".to_owned()], &shell), Ok(false));
// also check that it doesn't trigger on arrays
let mut vec = Vec::new();
vec.push("element".to_owned());
shell.variables.unset_var("array");
shell.variables.set_array("array", SmallVec::from_vec(vec));
assert_eq!(evaluate_arguments(&["-s", "array"], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["-s".to_owned(), "array".to_owned()], &shell), Ok(false));
// check `exists --fn`
let name_str = "test_function";
......@@ -257,14 +257,14 @@ fn test_evaluate_arguments() {
Function::new(Some(description), name, args, statements),
);
assert_eq!(evaluate_arguments(&["--fn", name_str], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["--fn".to_owned(), name_str.to_owned()], &shell), Ok(true));
shell.functions.remove(name_str);
assert_eq!(evaluate_arguments(&["--fn", name_str], &shell), Ok(false));
assert_eq!(evaluate_arguments(&["--fn".to_owned(), name_str.to_owned()], &shell), Ok(false));
// check invalid flags / parameters (should all be treated as strings and
// therefore succeed)
assert_eq!(evaluate_arguments(&["--foo"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-x"], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["--foo".to_owned()], &shell), Ok(true));
assert_eq!(evaluate_arguments(&["-x".to_owned()], &shell), Ok(true));
}
#[test]
......
......@@ -45,6 +45,9 @@ fn get_var_string(name: &str, shell: &mut Shell) -> String {
#[test]
fn test_is() {
fn vec_string(args: &[&str]) -> Vec<String> {
args.iter().map(|s| (*s).to_owned()).collect::<Vec<String>>()
}
use shell::ShellBuilder;
let mut shell = ShellBuilder::new().as_library();
shell.set_var("x", "value");
......@@ -52,36 +55,36 @@ fn test_is() {
// Four arguments
assert_eq!(
is(&["is", " ", " ", " "], &mut shell),
is(&vec_string(&["is", " ", " ", " "]), &mut shell),
Err("Expected 'not' instead found ' '\n".to_string())
);
assert_eq!(
is(&["is", "not", " ", " "], &mut shell),
is(&vec_string(&["is", "not", " ", " "]), &mut shell),
Err("".to_string())
);
assert_eq!(
is(&["is", "not", "$x", "$x"], &mut shell),
is(&vec_string(&["is", "not", "$x", "$x"]), &mut shell),
Err("".to_string())
);
assert_eq!(is(&["is", "not", "2", "1"], &mut shell), Ok(()));
assert_eq!(is(&["is", "not", "$x", "$y"], &mut shell), Ok(()));
assert_eq!(is(&vec_string(&["is", "not", "2", "1"]), &mut shell), Ok(()));
assert_eq!(is(&vec_string(&["is", "not", "$x", "$y"]), &mut shell), Ok(()));
// Three arguments
assert_eq!(is(&["is", "1", "2"], &mut shell), Err("".to_string()));
assert_eq!(is(&["is", "$x", "$y"], &mut shell), Err("".to_string()));
assert_eq!(is(&["is", " ", " "], &mut shell), Ok(()));
assert_eq!(is(&["is", "$x", "$x"], &mut shell), Ok(()));
assert_eq!(is(&vec_string(&["is", "1", "2"]), &mut shell), Err("".to_string()));
assert_eq!(is(&vec_string(&["is", "$x", "$y"]), &mut shell), Err("".to_string()));
assert_eq!(is(&vec_string(&["is", " ", " "]), &mut shell), Ok(()));
assert_eq!(is(&vec_string(&["is", "$x", "$x"]), &mut shell), Ok(()));
// Two arguments
assert_eq!(
is(&["is", " "], &mut shell),
is(&vec_string(&["is", " "]), &mut shell),
Err("is needs 3 or 4 arguments\n".to_string())
);
assert_eq!(is(&["is", "-h"], &mut shell), Ok(()));
assert_eq!(is(&vec_string(&["is", "-h"]), &mut shell), Ok(()));
// One argument
assert_eq!(
is(&["is"], &mut shell),
is(&vec_string(&["is"]), &mut shell),
Err("is needs 3 or 4 arguments\n".to_string())
);
}
......@@ -266,52 +266,55 @@ fn test_strings() {
#[test]
fn test_empty_str() {
let eval = |args: Vec<&str>| evaluate_arguments(&args);
assert_eq!(eval(vec![""]), Ok(false));
assert_eq!(eval(vec!["c", "=", ""]), Ok(false));
let eval = |args: Vec<String>| evaluate_arguments(&args);
assert_eq!(eval(vec!["".to_owned()]), Ok(false));
assert_eq!(eval(vec!["c".to_owned(), "=".to_owned(), "".to_owned()]), Ok(false));
}
#[test]
fn test_integers_arguments() {
fn vec_string(args: &[&str]) -> Vec<String> {
args.iter().map(|s| (*s).to_owned()).collect::<Vec<String>>()
}
// Equal To
assert_eq!(evaluate_arguments(&["10", "-eq", "10"]), Ok(true));
assert_eq!(evaluate_arguments(&["10", "-eq", "5"]), Ok(false));
assert_eq!(evaluate_arguments(&["-10", "-eq", "-10"]), Ok(true));
assert_eq!(evaluate_arguments(&["-10", "-eq", "10"]), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-eq", "10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-eq", "5"])), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["-10", "-eq", "-10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["-10", "-eq", "10"])), Ok(false));
// Greater Than or Equal To
assert_eq!(evaluate_arguments(&["10", "-ge", "10"]), Ok(true));
assert_eq!(evaluate_arguments(&["10", "-ge", "5"]), Ok(true));
assert_eq!(evaluate_arguments(&["5", "-ge", "10"]), Ok(false));
assert_eq!(evaluate_arguments(&["-9", "-ge", "-10"]), Ok(true));
assert_eq!(evaluate_arguments(&["-10", "-ge", "-10"]), Ok(true));
assert_eq!(evaluate_arguments(&["-10", "-ge", "10"]), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-ge", "10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-ge", "5"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["5", "-ge", "10"])), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["-9", "-ge", "-10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["-10", "-ge", "-10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["-10", "-ge", "10"])), Ok(false));
// Less Than or Equal To
assert_eq!(evaluate_arguments(&["5", "-le", "5"]), Ok(true));
assert_eq!(evaluate_arguments(&["5", "-le", "10"]), Ok(true));
assert_eq!(evaluate_arguments(&["10", "-le", "5"]), Ok(false));
assert_eq!(evaluate_arguments(&["-11", "-le", "-10"]), Ok(true));
assert_eq!(evaluate_arguments(&["-10", "-le", "-10"]), Ok(true));
assert_eq!(evaluate_arguments(&["10", "-le", "-10"]), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["5", "-le", "5"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["5", "-le", "10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-le", "5"])), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["-11", "-le", "-10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["-10", "-le", "-10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-le", "-10"])), Ok(false));
// Less Than
assert_eq!(evaluate_arguments(&["5", "-lt", "10"]), Ok(true));
assert_eq!(evaluate_arguments(&["10", "-lt", "5"]), Ok(false));
assert_eq!(evaluate_arguments(&["-11", "-lt", "-10"]), Ok(true));
assert_eq!(evaluate_arguments(&["10", "-lt", "-10"]), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["5", "-lt", "10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-lt", "5"])), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["-11", "-lt", "-10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-lt", "-10"])), Ok(false));
// Greater Than
assert_eq!(evaluate_arguments(&["10", "-gt", "5"]), Ok(true));
assert_eq!(evaluate_arguments(&["5", "-gt", "10"]), Ok(false));
assert_eq!(evaluate_arguments(&["-9", "-gt", "-10"]), Ok(true));
assert_eq!(evaluate_arguments(&["-10", "-gt", "10"]), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-gt", "5"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["5", "-gt", "10"])), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["-9", "-gt", "-10"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["-10", "-gt", "10"])), Ok(false));
// Not Equal To
assert_eq!(evaluate_arguments(&["10", "-ne", "5"]), Ok(true));
assert_eq!(evaluate_arguments(&["5", "-ne", "5"]), Ok(false));
assert_eq!(evaluate_arguments(&["-10", "-ne", "-10"]), Ok(false));
assert_eq!(evaluate_arguments(&["-10", "-ne", "10"]), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["10", "-ne", "5"])), Ok(true));
assert_eq!(evaluate_arguments(&vec_string(&["5", "-ne", "5"])), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["-10", "-ne", "-10"])), Ok(false));
assert_eq!(evaluate_arguments(&vec_string(&["-10", "-ne", "10"])), Ok(true));
}
#[test]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment