1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

numfmt: implement --field

This commit is contained in:
Daniel Rocco 2021-02-28 13:06:58 -05:00
parent 200310be18
commit 0e02607dc7
8 changed files with 238 additions and 61 deletions

View file

@ -315,3 +315,71 @@ fn test_to_iec_i_should_truncate_output() {
.succeeds()
.stdout_is_fixture("gnutest_iec-i_result.txt");
}
#[test]
fn test_format_selected_field() {
new_ucmd!()
.args(&["--from=auto", "--field", "3", "1K 2K 3K"])
.succeeds()
.stdout_only("1K 2K 3000\n");
new_ucmd!()
.args(&["--from=auto", "--field", "2", "1K 2K 3K"])
.succeeds()
.stdout_only("1K 2000 3K\n");
}
#[test]
fn test_format_selected_fields() {
new_ucmd!()
.args(&["--from=auto", "--field", "1,4,3", "1K 2K 3K 4K 5K 6K"])
.succeeds()
.stdout_only("1000 2K 3000 4000 5K 6K\n");
}
#[test]
fn test_should_succeed_if_selected_field_out_of_range() {
new_ucmd!()
.args(&["--from=auto", "--field", "9", "1K 2K 3K"])
.succeeds()
.stdout_only("1K 2K 3K\n");
}
#[test]
fn test_format_selected_field_range() {
new_ucmd!()
.args(&["--from=auto", "--field", "2-5", "1K 2K 3K 4K 5K 6K"])
.succeeds()
.stdout_only("1K 2000 3000 4000 5000 6K\n");
}
#[test]
fn test_should_succeed_if_range_out_of_bounds() {
new_ucmd!()
.args(&["--from=auto", "--field", "5-10", "1K 2K 3K 4K 5K 6K"])
.succeeds()
.stdout_only("1K 2K 3K 4K 5000 6000\n");
}
#[test]
fn test_implied_initial_field_value() {
new_ucmd!()
.args(&["--from=auto", "--field", "-2", "1K 2K 3K"])
.succeeds()
.stdout_only("1000 2000 3K\n");
// same as above but with the equal sign
new_ucmd!()
.args(&["--from=auto", "--field=-2", "1K 2K 3K"])
.succeeds()
.stdout_only("1000 2000 3K\n");
}
#[test]
fn test_field_df_example() {
// df -B1 | numfmt --header --field 2-4 --to=si
new_ucmd!()
.args(&["--header", "--field", "2-4", "--to=si"])
.pipe_in_fixture("df_input.txt")
.succeeds()
.stdout_is_fixture("df_expected.txt");
}

8
tests/fixtures/numfmt/df_expected.txt vendored Normal file
View file

@ -0,0 +1,8 @@
Filesystem 1B-blocks Used Available Use% Mounted on
udev 8.2G 0 8.2G 0% /dev
tmpfs 1.7G 2.1M 1.7G 1% /run
/dev/nvme0n1p2 1.1T 433G 523G 46% /
tmpfs 8.3G 145M 8.1G 2% /dev/shm
tmpfs 5.3M 4.1K 5.3M 1% /run/lock
tmpfs 8.3G 0 8.3G 0% /sys/fs/cgroup
/dev/nvme0n1p1 536M 8.2M 528M 2% /boot/efi

8
tests/fixtures/numfmt/df_input.txt vendored Normal file
View file

@ -0,0 +1,8 @@
Filesystem 1B-blocks Used Available Use% Mounted on
udev 8192688128 0 8192688128 0% /dev
tmpfs 1643331584 2015232 1641316352 1% /run
/dev/nvme0n1p2 1006530654208 432716689408 522613624832 46% /
tmpfs 8216649728 144437248 8072212480 2% /dev/shm
tmpfs 5242880 4096 5238784 1% /run/lock
tmpfs 8216649728 0 8216649728 0% /sys/fs/cgroup
/dev/nvme0n1p1 535805952 8175616 527630336 2% /boot/efi