1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

numfmt: handle leading whitespace & implied padding (#1721)

Align with GNU numfmt by trimming leading whitespace from supplied values.
If the user did not specify a padding, calculate an implied padding from
the leading whitespace and the value.

Also track closer to GNU numfmt’s error message format.
This commit is contained in:
Daniel Rocco 2021-02-14 03:04:29 -05:00 committed by GitHub
parent 842b6dd75f
commit f8006f47df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 204 additions and 23 deletions

View file

@ -145,3 +145,114 @@ fn test_si_to_iec() {
.run()
.stdout_is("13.9T\n");
}
#[test]
fn test_should_report_invalid_empty_number_on_empty_stdin() {
new_ucmd!()
.args(&["--from=auto"])
.pipe_in("\n")
.run()
.stderr_is("numfmt: invalid number: \n");
}
#[test]
fn test_should_report_invalid_empty_number_on_blank_stdin() {
new_ucmd!()
.args(&["--from=auto"])
.pipe_in(" \t \n")
.run()
.stderr_is("numfmt: invalid number: \n");
}
#[test]
fn test_should_report_invalid_suffix_on_stdin() {
new_ucmd!()
.args(&["--from=auto"])
.pipe_in("1k")
.run()
.stderr_is("numfmt: invalid suffix in input: 1k\n");
// GNU numfmt reports this one as “invalid number”
new_ucmd!()
.args(&["--from=auto"])
.pipe_in("NaN")
.run()
.stderr_is("numfmt: invalid suffix in input: NaN\n");
}
#[test]
fn test_should_report_invalid_number_with_interior_junk() {
// GNU numfmt reports this as “invalid suffix”
new_ucmd!()
.args(&["--from=auto"])
.pipe_in("1x0K")
.run()
.stderr_is("numfmt: invalid number: 1x0K\n");
}
#[test]
fn test_should_skip_leading_space_from_stdin() {
new_ucmd!()
.args(&["--from=auto"])
.pipe_in(" 2Ki")
.run()
.stdout_is("2048\n");
// multiline
new_ucmd!()
.args(&["--from=auto"])
.pipe_in("\t1Ki\n 2K")
.run()
.stdout_is("1024\n2000\n");
}
#[test]
fn test_should_convert_only_first_number_in_line() {
new_ucmd!()
.args(&["--from=auto"])
.pipe_in("1Ki 2M 3G")
.run()
.stdout_is("1024 2M 3G\n");
}
#[test]
fn test_leading_whitespace_should_imply_padding() {
new_ucmd!()
.args(&["--from=auto"])
.pipe_in(" 1K")
.run()
.stdout_is(" 1000\n");
new_ucmd!()
.args(&["--from=auto"])
.pipe_in(" 202Ki")
.run()
.stdout_is(" 206848\n");
}
#[test]
fn test_should_calculate_implicit_padding_per_line() {
new_ucmd!()
.args(&["--from=auto"])
.pipe_in(" 1Ki\n 2K")
.run()
.stdout_is(" 1024\n 2000\n");
}
#[test]
fn test_leading_whitespace_in_free_argument_should_imply_padding() {
new_ucmd!()
.args(&["--from=auto", " 1Ki"])
.run()
.stdout_is(" 1024\n");
}
#[test]
fn test_should_calculate_implicit_padding_per_free_argument() {
new_ucmd!()
.args(&["--from=auto", " 1Ki", " 2K"])
.pipe_in(" 1Ki\n 2K")
.run()
.stdout_is(" 1024\n 2000\n");
}