mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
numfmt: implement --format
This commit is contained in:
parent
4e72e284b5
commit
ee13bc41e7
5 changed files with 659 additions and 11 deletions
|
@ -673,3 +673,241 @@ fn test_valid_but_forbidden_suffix() {
|
|||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=--%f--", "50"])
|
||||
.succeeds()
|
||||
.stdout_is("--50--\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_separate_value() {
|
||||
new_ucmd!()
|
||||
.args(&["--format", "--%f--", "50"])
|
||||
.succeeds()
|
||||
.stdout_is("--50--\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_padding_with_prefix_and_suffix() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=--%6f--", "50"])
|
||||
.succeeds()
|
||||
.stdout_is("-- 50--\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_negative_padding_with_prefix_and_suffix() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=--%-6f--", "50"])
|
||||
.succeeds()
|
||||
.stdout_is("--50 --\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_format_padding_overriding_padding_option() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%6f", "--padding=10", "1234"])
|
||||
.succeeds()
|
||||
.stdout_is(" 1234\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_format_padding_overriding_implicit_padding() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%6f", " 1234"])
|
||||
.succeeds()
|
||||
.stdout_is(" 1234\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_negative_format_padding_and_suffix() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%-6f", "1234 ?"])
|
||||
.succeeds()
|
||||
.stdout_is("1234 ?\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_zero_padding() {
|
||||
let formats = vec!["%06f", "%0 6f"];
|
||||
|
||||
for format in formats {
|
||||
new_ucmd!()
|
||||
.args(&[format!("--format={}", format), String::from("1234")])
|
||||
.succeeds()
|
||||
.stdout_is("001234\n");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_zero_padding_and_padding_option() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%06f", "--padding=8", "1234"])
|
||||
.succeeds()
|
||||
.stdout_is(" 001234\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_zero_padding_and_negative_padding_option() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%06f", "--padding=-8", "1234"])
|
||||
.succeeds()
|
||||
.stdout_is("001234 \n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_zero_padding_and_implicit_padding() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%06f", " 1234"])
|
||||
.succeeds()
|
||||
.stdout_is(" 001234\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_zero_padding_and_suffix() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%06f", "1234 ?"])
|
||||
.succeeds()
|
||||
.stdout_is("001234 ?\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_precision() {
|
||||
let values = vec![("0.99", "1.0"), ("1", "1.0"), ("1.01", "1.1")];
|
||||
|
||||
for (input, expected) in values {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%.1f", input])
|
||||
.succeeds()
|
||||
.stdout_is(format!("{}\n", expected));
|
||||
}
|
||||
|
||||
let values = vec![("0.99", "0.99"), ("1", "1.00"), ("1.01", "1.01")];
|
||||
|
||||
for (input, expected) in values {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%.2f", input])
|
||||
.succeeds()
|
||||
.stdout_is(format!("{}\n", expected));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_precision_and_down_rounding() {
|
||||
let values = vec![("0.99", "0.9"), ("1", "1.0"), ("1.01", "1.0")];
|
||||
|
||||
for (input, expected) in values {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%.1f", input, "--round=down"])
|
||||
.succeeds()
|
||||
.stdout_is(format!("{}\n", expected));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_precision_and_to_arg() {
|
||||
let values = vec![("%.1f", "10.0G"), ("%.4f", "9.9913G")];
|
||||
|
||||
for (format, expected) in values {
|
||||
new_ucmd!()
|
||||
.args(&[
|
||||
format!("--format={}", format),
|
||||
"9991239123".to_string(),
|
||||
"--to=si".to_string(),
|
||||
])
|
||||
.succeeds()
|
||||
.stdout_is(format!("{}\n", expected));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_without_percentage_directive() {
|
||||
let invalid_formats = vec!["", "hello"];
|
||||
|
||||
for invalid_format in invalid_formats {
|
||||
new_ucmd!()
|
||||
.arg(format!("--format={}", invalid_format))
|
||||
.fails()
|
||||
.code_is(1)
|
||||
.stderr_contains(format!("format '{}' has no % directive", invalid_format));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_percentage_directive_at_end() {
|
||||
let invalid_format = "hello%";
|
||||
|
||||
new_ucmd!()
|
||||
.arg(format!("--format={}", invalid_format))
|
||||
.fails()
|
||||
.code_is(1)
|
||||
.stderr_contains(format!("format '{}' ends in %", invalid_format));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_too_many_percentage_directives() {
|
||||
let invalid_format = "%f %f";
|
||||
|
||||
new_ucmd!()
|
||||
.arg(format!("--format={}", invalid_format))
|
||||
.fails()
|
||||
.code_is(1)
|
||||
.stderr_contains(format!(
|
||||
"format '{}' has too many % directives",
|
||||
invalid_format
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_invalid_format() {
|
||||
let invalid_formats = vec!["%d", "% -43 f"];
|
||||
|
||||
for invalid_format in invalid_formats {
|
||||
new_ucmd!()
|
||||
.arg(format!("--format={}", invalid_format))
|
||||
.fails()
|
||||
.code_is(1)
|
||||
.stderr_contains(format!(
|
||||
"invalid format '{}', directive must be %[0]['][-][N][.][N]f",
|
||||
invalid_format
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_width_overflow() {
|
||||
let invalid_format = "%18446744073709551616f";
|
||||
new_ucmd!()
|
||||
.arg(format!("--format={}", invalid_format))
|
||||
.fails()
|
||||
.code_is(1)
|
||||
.stderr_contains(format!(
|
||||
"invalid format '{}' (width overflow)",
|
||||
invalid_format
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_with_invalid_precision() {
|
||||
let invalid_formats = vec!["%.-1f", "%.+1f", "%. 1f", "%.18446744073709551616f"];
|
||||
|
||||
for invalid_format in invalid_formats {
|
||||
new_ucmd!()
|
||||
.arg(format!("--format={}", invalid_format))
|
||||
.fails()
|
||||
.code_is(1)
|
||||
.stderr_contains(format!("invalid precision in format '{}'", invalid_format));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_grouping_conflicts_with_to_option() {
|
||||
new_ucmd!()
|
||||
.args(&["--format=%'f", "--to=si"])
|
||||
.fails()
|
||||
.code_is(1)
|
||||
.stderr_contains("grouping cannot be combined with --to");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue