From 2bad30b2b0aa0b785f9b1500b92461a02087b1e8 Mon Sep 17 00:00:00 2001 From: Nathan Ross Date: Mon, 15 Feb 2016 19:06:30 -0500 Subject: [PATCH 1/5] base64: in tests, test both option forms --- tests/base64.rs | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/tests/base64.rs b/tests/base64.rs index bfa23edec..2e977e731 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,26 @@ 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"); + } } From 962fcd71835797eaf1c1d9884296402971db95d3 Mon Sep 17 00:00:00 2001 From: Nathan Ross Date: Mon, 15 Feb 2016 20:38:03 -0500 Subject: [PATCH 2/5] base64: tests for incorrect wrap args --- tests/base64.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/base64.rs b/tests/base64.rs index 2e977e731..1e9482e2d 100644 --- a/tests/base64.rs +++ b/tests/base64.rs @@ -65,3 +65,30 @@ fn test_wrap() { "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"); + } +} From 21cee1556b409a9187b893e15b728f22dc890778 Mon Sep 17 00:00:00 2001 From: Nathan Ross Date: Tue, 16 Feb 2016 02:15:13 -0500 Subject: [PATCH 3/5] basename: in tests, assert error-free in usage --- tests/basename.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/tests/basename.rs b/tests/basename.rs index ec5decad4..2b8977722 100644 --- a/tests/basename.rs +++ b/tests/basename.rs @@ -5,40 +5,34 @@ 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"); } From 8190c30a85ff1b24a4e421860c18c740a6f75fdb Mon Sep 17 00:00:00 2001 From: Nathan Ross Date: Tue, 16 Feb 2016 02:16:19 -0500 Subject: [PATCH 4/5] basename: tests for remaining options --- tests/basename.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/basename.rs b/tests/basename.rs index 2b8977722..f59c62190 100644 --- a/tests/basename.rs +++ b/tests/basename.rs @@ -36,3 +36,41 @@ fn test_dont_remove_suffix() { let path = "/foo/bar/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"); + } +} From e44f29a02023ec87bcfa44bc229e7e5d9472151e Mon Sep 17 00:00:00 2001 From: Nathan Ross Date: Tue, 16 Feb 2016 02:17:04 -0500 Subject: [PATCH 5/5] basename: tests for bad inputs --- tests/basename.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/basename.rs b/tests/basename.rs index f59c62190..4399e4d9c 100644 --- a/tests/basename.rs +++ b/tests/basename.rs @@ -74,3 +74,19 @@ fn test_zero_param() { 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"], ""); +}