Skip to content

Commit

Permalink
new: added tests of Cmd#parse and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed May 26, 2024
1 parent 4ed6be6 commit 41a1437
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions tests/parse_test.rs
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),
}
}
}

0 comments on commit 41a1437

Please sign in to comment.