diff --git a/tests/base64.rs b/tests/base64.rs index bfa23edec..1e9482e2d 100644 --- a/tests/base64.rs +++ b/tests/base64.rs @@ -18,13 +18,15 @@ fn test_encode() { #[test] fn test_decode() { - let (_, mut ucmd) = testing(UTIL_NAME); - let input = "aGVsbG8sIHdvcmxkIQ=="; - let result = ucmd.arg("-d").run_piped_stdin(input.as_bytes()); + for decode_param in vec!["-d", "--decode"] { + let (_, mut ucmd) = testing(UTIL_NAME); + let input = "aGVsbG8sIHdvcmxkIQ=="; + let result = ucmd.arg(decode_param).run_piped_stdin(input.as_bytes()); - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, "hello, world!"); + assert_empty_stderr!(result); + assert!(result.success); + assert_eq!(result.stdout, "hello, world!"); + } } #[test] @@ -40,23 +42,53 @@ fn test_garbage() { #[test] fn test_ignore_garbage() { - let (_, mut ucmd) = testing(UTIL_NAME); - let input = "aGVsbG8sIHdvcmxkIQ==\0"; - let result = ucmd.arg("-d").arg("-i").run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, "hello, world!"); + for ignore_garbage_param in vec!["-i", "--ignore-garbage"] { + let (_, mut ucmd) = testing(UTIL_NAME); + let input = "aGVsbG8sIHdvcmxkIQ==\0"; + let result = ucmd.arg("-d").arg(ignore_garbage_param).run_piped_stdin(input.as_bytes()); + assert_empty_stderr!(result); + assert!(result.success); + assert_eq!(result.stdout, "hello, world!"); + } } #[test] fn test_wrap() { - let (_, mut ucmd) = testing(UTIL_NAME); - let input = "The quick brown fox jumps over the lazy dog."; - let result = ucmd.arg("-w").arg("20").run_piped_stdin(input.as_bytes()); + for wrap_param in vec!["-w", "--wrap"] { + let (_, mut ucmd) = testing(UTIL_NAME); + let input = "The quick brown fox jumps over the lazy dog."; + let result = ucmd.arg(wrap_param).arg("20").run_piped_stdin(input.as_bytes()); - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, - "VGhlIHF1aWNrIGJyb3du\nIGZveCBqdW1wcyBvdmVy\nIHRoZSBsYXp5IGRvZy4=\n"); + assert_empty_stderr!(result); + assert!(result.success); + assert_eq!(result.stdout, + "VGhlIHF1aWNrIGJyb3du\nIGZveCBqdW1wcyBvdmVy\nIHRoZSBsYXp5IGRvZy4=\n"); + } +} + +#[test] +fn test_wrap_no_arg() { + for wrap_param in vec!["-w", "--wrap"] { + let (_, mut ucmd) = testing(UTIL_NAME); + let result = ucmd.arg(wrap_param).run(); + + assert!(!result.success); + assert!(result.stdout.len() == 0); + assert_eq!(result.stderr.trim_right(), + format!("base64: error: Argument to option '{}' missing.", + if wrap_param == "-w" { "w" } else { "wrap" })); + } +} + +#[test] +fn test_wrap_bad_arg() { + for wrap_param in vec!["-w", "--wrap"] { + let (_, mut ucmd) = testing(UTIL_NAME); + let result = ucmd.arg(wrap_param).arg("b").run(); + + assert!(!result.success); + assert!(result.stdout.len() == 0); + assert_eq!(result.stderr.trim_right(), + "base64: error: Argument to option 'wrap' improperly formatted: invalid digit found in string"); + } } diff --git a/tests/basename.rs b/tests/basename.rs index ec5decad4..4399e4d9c 100644 --- a/tests/basename.rs +++ b/tests/basename.rs @@ -5,40 +5,88 @@ use common::util::*; static UTIL_NAME: &'static str = "basename"; +fn expect_successful_stdout(input: Vec<&str>, expected: &str) { + let (_, mut ucmd) = testing(UTIL_NAME); + let results = ucmd.args(&input).run(); + assert_empty_stderr!(results); + assert!(results.success); + assert_eq!(expected, results.stdout.trim_right()); +} + #[test] fn test_directory() { - let (_, mut ucmd) = testing(UTIL_NAME); let dir = "/root/alpha/beta/gamma/delta/epsilon/omega/"; - ucmd.arg(dir); - - assert_eq!(ucmd.run().stdout.trim_right(), "omega"); + expect_successful_stdout(vec![dir], "omega"); } #[test] fn test_file() { - let (_, mut ucmd) = testing(UTIL_NAME); let file = "/etc/passwd"; - ucmd.arg(file); - - assert_eq!(ucmd.run().stdout.trim_right(), "passwd"); + expect_successful_stdout(vec![file], "passwd"); } #[test] fn test_remove_suffix() { - let (_, mut ucmd) = testing(UTIL_NAME); let path = "/usr/local/bin/reallylongexecutable.exe"; - ucmd.arg(path) - .arg(".exe"); - - assert_eq!(ucmd.run().stdout.trim_right(), "reallylongexecutable"); + expect_successful_stdout(vec![path, ".exe"], "reallylongexecutable"); } #[test] fn test_dont_remove_suffix() { - let (_, mut ucmd) = testing(UTIL_NAME); let path = "/foo/bar/baz"; - ucmd.arg(path) - .arg("baz"); - - assert_eq!(ucmd.run().stdout.trim_right(), "baz"); + expect_successful_stdout(vec![path, "baz"], "baz"); +} + +fn expect_error(input: Vec<&str>, expected_stdout: &str) { + let (_, mut ucmd) = testing(UTIL_NAME); + let results = ucmd.args(&input).run(); + assert!(!results.success); + assert!(results.stderr.len() > 0); + assert_eq!(expected_stdout, results.stdout.trim_right()); +} +#[cfg_attr(not(feature="test_unimplemented"),ignore)] +#[test] +fn test_multiple_param() { + for multiple_param in vec!["-a", "--multiple"] { + let path = "/foo/bar/baz"; + expect_successful_stdout(vec![multiple_param, path, path], "baz\nbaz"); + } +} + +#[cfg_attr(not(feature="test_unimplemented"),ignore)] +#[test] +fn test_suffix_param() { + for suffix_param in vec!["-s", "--suffix"] { + let path = "/foo/bar/baz.exe"; + let suffix = ".exe"; + expect_successful_stdout( + vec![suffix_param, suffix, path, path], + "baz\nbaz" + ); + } +} + +#[cfg_attr(not(feature="test_unimplemented"),ignore)] +#[test] +fn test_zero_param() { + for zero_param in vec!["-z", "--zero"] { + let path = "/foo/bar/baz"; + expect_successful_stdout(vec![zero_param, "-a", path, path], "baz\0baz\0"); + } +} + +#[test] +fn test_invalid_option() { + let path = "/foo/bar/baz"; + expect_error(vec!["-q", path], ""); +} + +#[test] +fn test_no_args() { + expect_error(vec![], ""); +} + +#[test] +fn test_too_many_args() { + expect_error(vec!["a", "b", "c"], ""); }