Skip to content

Commit

Permalink
od: use clap options instead of custom mock options for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Jan 17, 2022
1 parent 8872485 commit 55893f0
Showing 1 changed file with 45 additions and 74 deletions.
119 changes: 45 additions & 74 deletions src/uu/od/src/parse_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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!(
Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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();
}

Expand Down

0 comments on commit 55893f0

Please sign in to comment.