diff --git a/src/uu/csplit/src/split_name.rs b/src/uu/csplit/src/split_name.rs index a24a1cba6..e2432f3ce 100644 --- a/src/uu/csplit/src/split_name.rs +++ b/src/uu/csplit/src/split_name.rs @@ -4,7 +4,7 @@ // file that was distributed with this source code. // spell-checker:ignore (regex) diuox -use uucore::format::{num_format::UnsignedInt, Format}; +use uucore::format::{num_format::UnsignedInt, Format, FormatError}; use crate::csplit_error::CsplitError; @@ -52,8 +52,11 @@ impl SplitName { None => format!("%0{n_digits}u"), }; - let format = Format::::parse(format_string) - .map_err(|_| CsplitError::SuffixFormatIncorrect)?; + let format = match Format::::parse(format_string) { + Ok(format) => Ok(format), + Err(FormatError::TooManySpecs(_)) => Err(CsplitError::SuffixFormatTooManyPercents), + Err(_) => Err(CsplitError::SuffixFormatIncorrect), + }?; Ok(Self { prefix: prefix.as_bytes().to_owned(), @@ -187,7 +190,7 @@ mod tests { #[test] fn alternate_form_octal() { let split_name = SplitName::new(None, Some(String::from("cst-%#10o-")), None).unwrap(); - assert_eq!(split_name.get(42), "xxcst- 0o52-"); + assert_eq!(split_name.get(42), "xxcst- 052-"); } #[test] @@ -199,7 +202,7 @@ mod tests { #[test] fn alternate_form_upper_hex() { let split_name = SplitName::new(None, Some(String::from("cst-%#10X-")), None).unwrap(); - assert_eq!(split_name.get(42), "xxcst- 0x2A-"); + assert_eq!(split_name.get(42), "xxcst- 0X2A-"); } #[test] @@ -223,19 +226,19 @@ mod tests { #[test] fn left_adjusted_octal() { let split_name = SplitName::new(None, Some(String::from("cst-%-10o-")), None).unwrap(); - assert_eq!(split_name.get(42), "xxcst-0o52 -"); + assert_eq!(split_name.get(42), "xxcst-52 -"); } #[test] fn left_adjusted_lower_hex() { let split_name = SplitName::new(None, Some(String::from("cst-%-10x-")), None).unwrap(); - assert_eq!(split_name.get(42), "xxcst-0x2a -"); + assert_eq!(split_name.get(42), "xxcst-2a -"); } #[test] fn left_adjusted_upper_hex() { let split_name = SplitName::new(None, Some(String::from("cst-%-10X-")), None).unwrap(); - assert_eq!(split_name.get(42), "xxcst-0x2A -"); + assert_eq!(split_name.get(42), "xxcst-2A -"); } #[test] diff --git a/tests/by-util/test_csplit.rs b/tests/by-util/test_csplit.rs index df1034436..fb4f4cc2a 100644 --- a/tests/by-util/test_csplit.rs +++ b/tests/by-util/test_csplit.rs @@ -1345,30 +1345,17 @@ fn test_line_num_range_with_up_to_match3() { #[test] fn precision_format() { - let (at, mut ucmd) = at_and_ucmd!(); - ucmd.args(&["numbers50.txt", "10", "--suffix-format", "%#6.3x"]) - .succeeds() - .stdout_only("18\n123\n"); + for f in ["%#6.3x", "%0#6.3x"] { + let (at, mut ucmd) = at_and_ucmd!(); + ucmd.args(&["numbers50.txt", "10", "--suffix-format", f]) + .succeeds() + .stdout_only("18\n123\n"); - let count = glob(&at.plus_as_string("xx*")) - .expect("there should be splits created") - .count(); - assert_eq!(count, 2); - assert_eq!(at.read("xx 000"), generate(1, 10)); - assert_eq!(at.read("xx 0x001"), generate(10, 51)); -} - -#[test] -fn precision_format2() { - let (at, mut ucmd) = at_and_ucmd!(); - ucmd.args(&["numbers50.txt", "10", "--suffix-format", "%0#6.3x"]) - .succeeds() - .stdout_only("18\n123\n"); - - let count = glob(&at.plus_as_string("xx*")) - .expect("there should be splits created") - .count(); - assert_eq!(count, 2); - assert_eq!(at.read("xx 000"), generate(1, 10)); - assert_eq!(at.read("xx 0x001"), generate(10, 51)); + let count = glob(&at.plus_as_string("xx*")) + .expect("there should be splits created") + .count(); + assert_eq!(count, 2); + assert_eq!(at.read("xx 000"), generate(1, 10)); + assert_eq!(at.read("xx 0x001"), generate(10, 51)); + } }