-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: added tests of Cmd#parse and errors
- Loading branch information
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#[cfg(test)] | ||
mod tests_of_parse { | ||
use cliargs; | ||
use std::ffi; | ||
|
||
#[test] | ||
fn it_should_parse_command_line_arguments() { | ||
let mut cmd = cliargs::Cmd::new().unwrap(); | ||
match cmd.parse() { | ||
Ok(_) => {} | ||
Err(_) => assert!(false), | ||
} | ||
println!("cmd = {cmd:?}"); | ||
assert!(cmd.name().starts_with("parse_test-")); | ||
} | ||
|
||
#[test] | ||
fn it_should_parse_strings_as_command_line_arguments() { | ||
let mut cmd = cliargs::Cmd::with_strings([ | ||
"/path/to/app".to_string(), | ||
"--foo-bar=123".to_string(), | ||
"bar".to_string(), | ||
"--baz".to_string(), | ||
"qux".to_string(), | ||
]); | ||
match cmd.parse() { | ||
Ok(_) => {} | ||
Err(_) => assert!(false), | ||
} | ||
println!("cmd = {cmd:?}"); | ||
assert_eq!(cmd.name(), "app"); | ||
assert_eq!(cmd.args(), ["bar", "qux"]); | ||
assert_eq!(cmd.has_opt("foo-bar"), true); | ||
assert_eq!(cmd.opt_arg("foo-bar"), Some("123")); | ||
assert_eq!(cmd.opt_args("foo-bar"), Some(&["123"] as &[&str])); | ||
assert_eq!(cmd.has_opt("baz"), true); | ||
assert_eq!(cmd.opt_arg("baz"), None); | ||
assert_eq!(cmd.opt_args("baz"), Some(&[] as &[&str])); | ||
} | ||
|
||
#[test] | ||
fn it_should_parse_os_strings_as_command_line_arguments() { | ||
let mut cmd = cliargs::Cmd::with_os_strings([ | ||
ffi::OsString::from("/path/to/app"), | ||
ffi::OsString::from("--foo-bar=123"), | ||
ffi::OsString::from("bar"), | ||
ffi::OsString::from("--baz"), | ||
ffi::OsString::from("qux"), | ||
]) | ||
.unwrap(); | ||
match cmd.parse() { | ||
Ok(_) => {} | ||
Err(_) => assert!(false), | ||
} | ||
println!("cmd = {cmd:?}"); | ||
assert_eq!(cmd.name(), "app"); | ||
assert_eq!(cmd.args(), ["bar", "qux"]); | ||
assert_eq!(cmd.has_opt("foo-bar"), true); | ||
assert_eq!(cmd.opt_arg("foo-bar"), Some("123")); | ||
assert_eq!(cmd.opt_args("foo-bar"), Some(&["123"] as &[&str])); | ||
assert_eq!(cmd.has_opt("baz"), true); | ||
assert_eq!(cmd.opt_arg("baz"), None); | ||
assert_eq!(cmd.opt_args("baz"), Some(&[] as &[&str])); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests_of_errors { | ||
use cliargs; | ||
use std::ffi; | ||
|
||
#[test] | ||
fn it_should_parse_but_fail_because_command_line_arguments_contain_invalid_unicode() { | ||
let bad_arg = b"bar\xFF"; | ||
let bad_os_str = unsafe { ffi::OsStr::from_encoded_bytes_unchecked(bad_arg) }; | ||
let bad_os_string = bad_os_str.to_os_string(); | ||
|
||
match cliargs::Cmd::with_os_strings([ | ||
ffi::OsString::from("/path/to/app"), | ||
ffi::OsString::from("--foo-bar=123"), | ||
bad_os_string.clone(), | ||
ffi::OsString::from("--baz"), | ||
ffi::OsString::from("qux"), | ||
]) { | ||
Ok(_) => assert!(false), | ||
Err(cliargs::Error::OsArgsContainInvalidUnicode { index, os_arg }) => { | ||
assert_eq!(index, 2); | ||
assert_eq!(os_arg, bad_os_string); | ||
} | ||
Err(_) => assert!(false), | ||
} | ||
} | ||
|
||
#[test] | ||
fn it_should_parse_but_fail_because_option_contains_invalid_char() { | ||
let mut cmd = cliargs::Cmd::with_strings([ | ||
"/path/to/app".to_string(), | ||
"--foo-bar=123".to_string(), | ||
"--b@z".to_string(), | ||
"qux".to_string(), | ||
]); | ||
match cmd.parse() { | ||
Ok(_) => assert!(false), | ||
Err(cliargs::Error::InvalidOption( | ||
cliargs::OptionError::OptionContainsInvalidChar { option }, | ||
)) => { | ||
assert_eq!(option, "b@z"); | ||
} | ||
Err(_) => assert!(false), | ||
} | ||
} | ||
} |