mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
od: use clap options instead of custom mock options for unit tests
This commit is contained in:
parent
8872485922
commit
55893f0e3d
1 changed files with 45 additions and 74 deletions
|
@ -56,7 +56,7 @@ pub fn parse_inputs(matches: &dyn CommandLineOpts) -> Result<CommandLineInputs,
|
|||
if !matches.opts_present(&[
|
||||
options::ADDRESS_RADIX,
|
||||
options::READ_BYTES,
|
||||
options::READ_BYTES,
|
||||
options::SKIP_BYTES,
|
||||
options::FORMAT,
|
||||
options::OUTPUT_DUPLICATES,
|
||||
options::WIDTH,
|
||||
|
@ -173,61 +173,28 @@ pub fn parse_offset_operand(s: &str) -> Result<usize, &'static str> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A mock for the command line options type
|
||||
///
|
||||
/// `inputs` are all command line parameters which do not belong to an option.
|
||||
/// `option_names` are the names of the options on the command line.
|
||||
struct MockOptions<'a> {
|
||||
inputs: Vec<String>,
|
||||
option_names: Vec<&'a str>,
|
||||
}
|
||||
|
||||
impl<'a> MockOptions<'a> {
|
||||
fn new(inputs: Vec<&'a str>, option_names: Vec<&'a str>) -> MockOptions<'a> {
|
||||
MockOptions {
|
||||
inputs: inputs.iter().map(|&s| s.to_string()).collect::<Vec<_>>(),
|
||||
option_names,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> CommandLineOpts for MockOptions<'a> {
|
||||
fn inputs(&self) -> Vec<&str> {
|
||||
self.inputs.iter().map(|s| s.as_str()).collect()
|
||||
}
|
||||
fn opts_present(&self, opts: &[&str]) -> bool {
|
||||
for expected in opts.iter() {
|
||||
for actual in self.option_names.iter() {
|
||||
if *expected == *actual {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
use crate::uu_app;
|
||||
|
||||
#[test]
|
||||
fn test_parse_inputs_normal() {
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["-".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec![], vec![])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od"])).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["-".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["-"], vec![])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "-"])).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["file1".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["file1"], vec![])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1"])).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["file1".to_string(), "file2".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["file1", "file2"], vec![])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1", "file2"])).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
|
@ -236,7 +203,7 @@ mod tests {
|
|||
"file1".to_string(),
|
||||
"file2".to_string(),
|
||||
]),
|
||||
parse_inputs(&MockOptions::new(vec!["-", "file1", "file2"], vec![])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "-", "file1", "file2"])).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -245,58 +212,58 @@ mod tests {
|
|||
// offset is found without filename, so stdin will be used.
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileAndOffset(("-".to_string(), 8, None)),
|
||||
parse_inputs(&MockOptions::new(vec!["+10"], vec![])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "+10"])).unwrap()
|
||||
);
|
||||
|
||||
// offset must start with "+" if no input is specified.
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["10".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["10"], vec![""])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "10"])).unwrap()
|
||||
);
|
||||
|
||||
// offset is not valid, so it is considered a filename.
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["+10a".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["+10a"], vec![""])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "+10a"])).unwrap()
|
||||
);
|
||||
|
||||
// if -j is included in the command line, there cannot be an offset.
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["+10".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["+10"], vec!["j"])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "-j10", "+10"])).unwrap()
|
||||
);
|
||||
|
||||
// if -v is included in the command line, there cannot be an offset.
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["+10".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["+10"], vec!["o", "v"])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "-o", "-v", "+10"])).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileAndOffset(("file1".to_string(), 8, None)),
|
||||
parse_inputs(&MockOptions::new(vec!["file1", "+10"], vec![])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1", "+10"])).unwrap()
|
||||
);
|
||||
|
||||
// offset does not need to start with "+" if a filename is included.
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileAndOffset(("file1".to_string(), 8, None)),
|
||||
parse_inputs(&MockOptions::new(vec!["file1", "10"], vec![])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1", "10"])).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["file1".to_string(), "+10a".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["file1", "+10a"], vec![""])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1", "+10a"])).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["file1".to_string(), "+10".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["file1", "+10"], vec!["j"])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "-j10", "file1", "+10"])).unwrap()
|
||||
);
|
||||
|
||||
// offset must be last on the command line
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["+10".to_string(), "file1".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["+10", "file1"], vec![""])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "+10", "file1"])).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -305,59 +272,63 @@ mod tests {
|
|||
// it should not return FileAndOffset to signal no offset was entered on the command line.
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["-".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec![], vec!["traditional"])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional"])).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileNames(vec!["file1".to_string()]),
|
||||
parse_inputs(&MockOptions::new(vec!["file1"], vec!["traditional"])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "file1"])).unwrap()
|
||||
);
|
||||
|
||||
// offset does not need to start with a +
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileAndOffset(("-".to_string(), 8, None)),
|
||||
parse_inputs(&MockOptions::new(vec!["10"], vec!["traditional"])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10"])).unwrap()
|
||||
);
|
||||
|
||||
// valid offset and valid label
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileAndOffset(("-".to_string(), 8, Some(8))),
|
||||
parse_inputs(&MockOptions::new(vec!["10", "10"], vec!["traditional"])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10", "10"]))
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileAndOffset(("file1".to_string(), 8, None)),
|
||||
parse_inputs(&MockOptions::new(vec!["file1", "10"], vec!["traditional"])).unwrap()
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "file1", "10"]))
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
// only one file is allowed, it must be the first
|
||||
parse_inputs(&MockOptions::new(vec!["10", "file1"], vec!["traditional"])).unwrap_err();
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10", "file1"]))
|
||||
.unwrap_err();
|
||||
|
||||
assert_eq!(
|
||||
CommandLineInputs::FileAndOffset(("file1".to_string(), 8, Some(8))),
|
||||
parse_inputs(&MockOptions::new(
|
||||
vec!["file1", "10", "10"],
|
||||
vec!["traditional"]
|
||||
))
|
||||
parse_inputs(&uu_app().get_matches_from(vec![
|
||||
"od",
|
||||
"--traditional",
|
||||
"file1",
|
||||
"10",
|
||||
"10"
|
||||
]))
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
parse_inputs(&MockOptions::new(
|
||||
vec!["10", "file1", "10"],
|
||||
vec!["traditional"],
|
||||
))
|
||||
.unwrap_err();
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10", "file1", "10"]))
|
||||
.unwrap_err();
|
||||
|
||||
parse_inputs(&MockOptions::new(
|
||||
vec!["10", "10", "file1"],
|
||||
vec!["traditional"],
|
||||
))
|
||||
.unwrap_err();
|
||||
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10", "10", "file1"]))
|
||||
.unwrap_err();
|
||||
|
||||
parse_inputs(&MockOptions::new(
|
||||
vec!["10", "10", "10", "10"],
|
||||
vec!["traditional"],
|
||||
))
|
||||
parse_inputs(&uu_app().get_matches_from(vec![
|
||||
"od",
|
||||
"--traditional",
|
||||
"10",
|
||||
"10",
|
||||
"10",
|
||||
"10",
|
||||
]))
|
||||
.unwrap_err();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue