Skip to content

Commit

Permalink
csplit: fix up tests and error message for filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Feb 16, 2024
1 parent 50cab57 commit fc82360
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
19 changes: 11 additions & 8 deletions src/uu/csplit/src/split_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -52,8 +52,11 @@ impl SplitName {
None => format!("%0{n_digits}u"),
};

let format = Format::<UnsignedInt>::parse(format_string)
.map_err(|_| CsplitError::SuffixFormatIncorrect)?;
let format = match Format::<UnsignedInt>::parse(format_string) {
Ok(format) => Ok(format),
Err(FormatError::TooManySpecs(_)) => Err(CsplitError::SuffixFormatTooManyPercents),
Err(_) => Err(CsplitError::SuffixFormatIncorrect),

Check warning on line 58 in src/uu/csplit/src/split_name.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/csplit/src/split_name.rs#L57-L58

Added lines #L57 - L58 were not covered by tests
}?;

Ok(Self {
prefix: prefix.as_bytes().to_owned(),
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
39 changes: 13 additions & 26 deletions tests/by-util/test_csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

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));
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));
}
}

0 comments on commit fc82360

Please sign in to comment.