From 55893f0e3d21c437da18d2da7fa36196a62b17af Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 17 Jan 2022 15:19:15 +0100 Subject: [PATCH] od: use clap options instead of custom mock options for unit tests --- src/uu/od/src/parse_inputs.rs | 119 +++++++++++++--------------------- 1 file changed, 45 insertions(+), 74 deletions(-) diff --git a/src/uu/od/src/parse_inputs.rs b/src/uu/od/src/parse_inputs.rs index 3c5dcef1942..a5634c0aa8f 100644 --- a/src/uu/od/src/parse_inputs.rs +++ b/src/uu/od/src/parse_inputs.rs @@ -56,7 +56,7 @@ pub fn parse_inputs(matches: &dyn CommandLineOpts) -> Result Result { #[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, - 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::>(), - 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(); }