| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore (paths) gnutest ronna quetta unitless |
| 7 |
|
| 8 |
use uutests::new_ucmd; |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn test_invalid_arg() { |
| 12 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 13 |
} |
| 14 |
|
| 15 |
// This test failed when fixing #11653. |
| 16 |
// Add a `--` separator to ensure floats are not rounded(it match the gnu pattern). |
| 17 |
#[test] |
| 18 |
fn test_should_not_round_floats() { |
| 19 |
new_ucmd!() |
| 20 |
.args(&["--", "0.99", "1.01", "1.1", "1.22", ".1", "-0.1"]) |
| 21 |
.succeeds() |
| 22 |
.stdout_is("0.99\n1.01\n1.1\n1.22\n0.1\n-0.1\n"); |
| 23 |
} |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn test_should_preserve_trailing_zeros() { |
| 27 |
new_ucmd!() |
| 28 |
.args(&["0.1000", "10.00"]) |
| 29 |
.succeeds() |
| 30 |
.stdout_is("0.1000\n10.00\n"); |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
fn test_from_si() { |
| 35 |
new_ucmd!() |
| 36 |
.args(&["--from=si"]) |
| 37 |
.pipe_in("1000\n1.1M\n0.1G") |
| 38 |
.succeeds() |
| 39 |
.stdout_is("1000\n1100000\n100000000"); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn test_from_iec() { |
| 44 |
new_ucmd!() |
| 45 |
.args(&["--from=iec"]) |
| 46 |
.pipe_in("1024\n1.1M\n0.1G") |
| 47 |
.succeeds() |
| 48 |
.stdout_is("1024\n1153434\n107374183"); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn test_from_iec_i() { |
| 53 |
new_ucmd!() |
| 54 |
.args(&["--from=iec-i"]) |
| 55 |
.pipe_in("1.1Mi\n0.1Gi") |
| 56 |
.succeeds() |
| 57 |
.stdout_is("1153434\n107374183"); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn test_from_iec_i_requires_suffix() { |
| 62 |
new_ucmd!() |
| 63 |
.args(&["--from=iec-i", "10M"]) |
| 64 |
.fails_with_code(2) |
| 65 |
.stderr_is("numfmt: missing 'i' suffix in input: '10M' (e.g Ki/Mi/Gi)\n"); |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn test_from_iec_fails_if_i_suffix() { |
| 70 |
new_ucmd!() |
| 71 |
.args(&["--from=iec", "10Mi"]) |
| 72 |
.fails_with_code(2) |
| 73 |
.stderr_is("numfmt: invalid suffix in input '10Mi': 'i'\n"); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_from_iec_i_without_suffix_are_bytes() { |
| 78 |
new_ucmd!() |
| 79 |
.args(&["--from=iec-i", "1024"]) |
| 80 |
.succeeds() |
| 81 |
.stdout_is("1024\n"); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn test_from_auto() { |
| 86 |
new_ucmd!() |
| 87 |
.args(&["--from=auto"]) |
| 88 |
.pipe_in("1K\n1Ki") |
| 89 |
.succeeds() |
| 90 |
.stdout_is("1000\n1024"); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn test_to_si() { |
| 95 |
new_ucmd!() |
| 96 |
.args(&["--to=si"]) |
| 97 |
.pipe_in("1000\n1100000\n100000000") |
| 98 |
.succeeds() |
| 99 |
.stdout_is("1.0k\n1.1M\n100M"); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn test_to_iec() { |
| 104 |
new_ucmd!() |
| 105 |
.args(&["--to=iec"]) |
| 106 |
.pipe_in("1024\n1153434\n107374182") |
| 107 |
.succeeds() |
| 108 |
.stdout_is("1.0K\n1.2M\n103M"); |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn test_to_iec_i() { |
| 113 |
new_ucmd!() |
| 114 |
.args(&["--to=iec-i"]) |
| 115 |
.pipe_in("1024\n1153434\n107374182") |
| 116 |
.succeeds() |
| 117 |
.stdout_is("1.0Ki\n1.2Mi\n103Mi"); |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn test_input_from_free_arguments() { |
| 122 |
new_ucmd!() |
| 123 |
.args(&["--from=si", "1K", "1.1M", "0.1G"]) |
| 124 |
.succeeds() |
| 125 |
.stdout_is("1000\n1100000\n100000000\n"); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn test_padding() { |
| 130 |
new_ucmd!() |
| 131 |
.args(&["--from=si", "--padding=8"]) |
| 132 |
.pipe_in("1K\n1.1M\n0.1G") |
| 133 |
.succeeds() |
| 134 |
.stdout_is(" 1000\n 1100000\n100000000"); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn test_negative_padding() { |
| 139 |
new_ucmd!() |
| 140 |
.args(&["--from=si", "--padding=-8"]) |
| 141 |
.pipe_in("1K\n1.1M\n0.1G") |
| 142 |
.succeeds() |
| 143 |
.stdout_is("1000 \n1100000 \n100000000"); |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn test_negative_padding_as_separate_arg() { |
| 148 |
// A negative padding value passed as its own argument (not attached |
| 149 |
// with `=`) must not be mistaken for a new, unrecognized flag. |
| 150 |
new_ucmd!() |
| 151 |
.args(&["--from=si", "--padding", "-8"]) |
| 152 |
.pipe_in("1K\n1.1M\n0.1G") |
| 153 |
.succeeds() |
| 154 |
.stdout_is("1000 \n1100000 \n100000000"); |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn test_suffix_hyphen_leading_as_separate_arg() { |
| 159 |
// A hyphen-leading suffix value passed as its own argument (not |
| 160 |
// attached with `=`) must not be mistaken for a new, unrecognized |
| 161 |
// flag. |
| 162 |
new_ucmd!() |
| 163 |
.args(&["--suffix", "-x"]) |
| 164 |
.pipe_in("5\n") |
| 165 |
.succeeds() |
| 166 |
.stdout_is("5-x\n"); |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
fn test_delimiter_hyphen_leading_as_separate_arg() { |
| 171 |
// A hyphen-leading delimiter value passed as its own argument (not |
| 172 |
// attached with `-d-x`/`=`) must not be mistaken for a new, |
| 173 |
// unrecognized flag. |
| 174 |
new_ucmd!() |
| 175 |
.args(&["-d", "-x", "--field=1"]) |
| 176 |
.fails() |
| 177 |
.stderr_contains("the delimiter must be a single character"); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn test_unit_size_hyphen_leading_as_separate_arg() { |
| 182 |
// A hyphen-leading unit size passed as its own argument must reach |
| 183 |
// our own validation instead of being read as an unknown flag. |
| 184 |
for opt in ["--from-unit", "--to-unit"] { |
| 185 |
new_ucmd!() |
| 186 |
.args(&[opt, "-1"]) |
| 187 |
.pipe_in("") |
| 188 |
.fails() |
| 189 |
.stderr_contains("invalid unit size: '-1'"); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
#[test] |
| 194 |
fn test_unit_hyphen_leading_as_separate_arg() { |
| 195 |
// Same for the --from/--to units. |
| 196 |
for opt in ["--from", "--to"] { |
| 197 |
new_ucmd!() |
| 198 |
.args(&[opt, "-x"]) |
| 199 |
.pipe_in("") |
| 200 |
.fails() |
| 201 |
.stderr_contains("invalid argument '-x' for '--"); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
#[test] |
| 206 |
fn test_unit_separator_hyphen_leading_as_separate_arg() { |
| 207 |
new_ucmd!() |
| 208 |
.args(&["--to=si", "--unit-separator", "-"]) |
| 209 |
.pipe_in("1000\n") |
| 210 |
.succeeds() |
| 211 |
.stdout_is("1.0-k\n"); |
| 212 |
} |
| 213 |
|
| 214 |
#[test] |
| 215 |
fn test_header() { |
| 216 |
new_ucmd!() |
| 217 |
.args(&["--from=si", "--header=2"]) |
| 218 |
.pipe_in("header\nheader2\n1K\n1.1M\n0.1G") |
| 219 |
.succeeds() |
| 220 |
.stdout_is("header\nheader2\n1000\n1100000\n100000000"); |
| 221 |
} |
| 222 |
|
| 223 |
#[test] |
| 224 |
fn test_header_default() { |
| 225 |
new_ucmd!() |
| 226 |
.args(&["--from=si", "--header"]) |
| 227 |
.pipe_in("header\n1K\n1.1M\n0.1G") |
| 228 |
.succeeds() |
| 229 |
.stdout_is("header\n1000\n1100000\n100000000"); |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn test_header_error_if_non_numeric() { |
| 234 |
new_ucmd!() |
| 235 |
.args(&["--header=two"]) |
| 236 |
.fails() |
| 237 |
.stderr_is("numfmt: invalid header value 'two'\n"); |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn test_header_error_if_0() { |
| 242 |
new_ucmd!() |
| 243 |
.args(&["--header=0"]) |
| 244 |
.fails() |
| 245 |
.stderr_is("numfmt: invalid header value '0'\n"); |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn test_header_error_if_negative() { |
| 250 |
new_ucmd!() |
| 251 |
.args(&["--header=-3"]) |
| 252 |
.fails() |
| 253 |
.stderr_is("numfmt: invalid header value '-3'\n"); |
| 254 |
} |
| 255 |
|
| 256 |
#[test] |
| 257 |
fn test_reject_leading_plus_and_scientific_notation() { |
| 258 |
// GNU numfmt rejects a leading '+' as an invalid number and rejects scientific |
| 259 |
// notation as an invalid suffix; Rust's parsers accept both, so guard against it. |
| 260 |
for input in ["+5", "+5K", "+1e-3"] { |
| 261 |
new_ucmd!() |
| 262 |
.arg("--from=auto") |
| 263 |
.pipe_in(format!("{input}\n")) |
| 264 |
.fails_with_code(2) |
| 265 |
.stderr_is(format!("numfmt: invalid number: '{input}'\n")); |
| 266 |
} |
| 267 |
for input in ["1e-3", "1e+3", "5e-5", "1.0e-2", "-1e-5"] { |
| 268 |
new_ucmd!() |
| 269 |
.arg("--from=auto") |
| 270 |
.pipe_in(format!("{input}\n")) |
| 271 |
.fails_with_code(2) |
| 272 |
.stderr_is(format!("numfmt: invalid suffix in input: '{input}'\n")); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
#[test] |
| 277 |
fn test_negative() { |
| 278 |
new_ucmd!() |
| 279 |
.args(&["--from=si"]) |
| 280 |
.pipe_in("-1000\n-1.1M\n-0.1G") |
| 281 |
.succeeds() |
| 282 |
.stdout_is("-1000\n-1100000\n-100000000"); |
| 283 |
new_ucmd!() |
| 284 |
.args(&["--to=iec-i"]) |
| 285 |
.pipe_in("-1024\n-1153434\n-107374182") |
| 286 |
.succeeds() |
| 287 |
.stdout_is("-1.0Ki\n-1.2Mi\n-103Mi"); |
| 288 |
} |
| 289 |
|
| 290 |
#[test] |
| 291 |
fn test_negative_zero() { |
| 292 |
new_ucmd!() |
| 293 |
.pipe_in("-0\n-0.0") |
| 294 |
.succeeds() |
| 295 |
.stdout_is("0\n0.0"); |
| 296 |
} |
| 297 |
|
| 298 |
#[test] |
| 299 |
fn test_no_op() { |
| 300 |
new_ucmd!() |
| 301 |
.pipe_in("1024\n1234567") |
| 302 |
.succeeds() |
| 303 |
.stdout_is("1024\n1234567"); |
| 304 |
} |
| 305 |
|
| 306 |
#[test] |
| 307 |
fn test_normalize() { |
| 308 |
new_ucmd!() |
| 309 |
.args(&["--from=si", "--to=si"]) |
| 310 |
.pipe_in("10000000K\n0.001K") |
| 311 |
.succeeds() |
| 312 |
.stdout_is("10G\n1"); |
| 313 |
} |
| 314 |
|
| 315 |
#[test] |
| 316 |
fn test_si_to_iec() { |
| 317 |
new_ucmd!() |
| 318 |
.args(&["--from=si", "--to=iec", "15334263563K"]) |
| 319 |
.succeeds() |
| 320 |
.stdout_is("14T\n"); |
| 321 |
} |
| 322 |
|
| 323 |
#[test] |
| 324 |
fn test_should_report_invalid_empty_number_on_empty_stdin() { |
| 325 |
new_ucmd!() |
| 326 |
.args(&["--from=auto"]) |
| 327 |
.pipe_in("\n") |
| 328 |
.fails() |
| 329 |
.stderr_is("numfmt: invalid number: ''\n"); |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn test_should_report_invalid_empty_number_on_blank_stdin() { |
| 334 |
new_ucmd!() |
| 335 |
.args(&["--from=auto"]) |
| 336 |
.pipe_in(" \t \n") |
| 337 |
.fails() |
| 338 |
.stderr_is("numfmt: invalid number: ''\n"); |
| 339 |
} |
| 340 |
|
| 341 |
#[test] |
| 342 |
fn test_suffixes() { |
| 343 |
let valid_suffixes = ['K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q', 'k']; |
| 344 |
|
| 345 |
for c in ('A'..='Z').chain('a'..='z') { |
| 346 |
let args = ["--from=si", "--to=si", &format!("1{c}")]; |
| 347 |
|
| 348 |
if valid_suffixes.contains(&c) { |
| 349 |
let s = if c == 'K' { 'k' } else { c }; |
| 350 |
new_ucmd!() |
| 351 |
.args(&args) |
| 352 |
.succeeds() |
| 353 |
.stdout_only(format!("1.0{s}\n")); |
| 354 |
} else { |
| 355 |
new_ucmd!() |
| 356 |
.args(&args) |
| 357 |
.fails_with_code(2) |
| 358 |
.stderr_only(format!("numfmt: invalid suffix in input: '1{c}'\n")); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
#[test] |
| 364 |
fn test_invalid_following_valid_suffix() { |
| 365 |
let valid_suffixes = ['K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q', 'k']; |
| 366 |
|
| 367 |
for valid_suffix in valid_suffixes { |
| 368 |
for c in ('A'..='Z').chain('a'..='z') { |
| 369 |
let args = ["--from=si", "--to=si", &format!("1{valid_suffix}{c}")]; |
| 370 |
|
| 371 |
new_ucmd!() |
| 372 |
.args(&args) |
| 373 |
.fails_with_code(2) |
| 374 |
.stderr_only(format!( |
| 375 |
"numfmt: invalid suffix in input '1{valid_suffix}{c}': '{c}'\n" |
| 376 |
)); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn test_long_invalid_suffix() { |
| 383 |
let args = ["--from=si", "--to=si", "1500VVVVVVVV"]; |
| 384 |
|
| 385 |
new_ucmd!() |
| 386 |
.args(&args) |
| 387 |
.fails_with_code(2) |
| 388 |
.stderr_only("numfmt: invalid suffix in input: '1500VVVVVVVV'\n"); |
| 389 |
} |
| 390 |
|
| 391 |
#[test] |
| 392 |
fn test_should_report_invalid_suffix_on_nan() { |
| 393 |
// GNU numfmt reports this one as "invalid number" |
| 394 |
new_ucmd!() |
| 395 |
.args(&["--from=auto"]) |
| 396 |
.pipe_in("NaN") |
| 397 |
.fails() |
| 398 |
.stderr_is("numfmt: invalid number: 'NaN'\n"); |
| 399 |
} |
| 400 |
|
| 401 |
#[test] |
| 402 |
fn test_should_report_invalid_number_with_interior_junk() { |
| 403 |
new_ucmd!() |
| 404 |
.args(&["--from=auto"]) |
| 405 |
.pipe_in("1x0K") |
| 406 |
.fails() |
| 407 |
.stderr_is("numfmt: invalid suffix in input: '1x0K'\n"); |
| 408 |
} |
| 409 |
|
| 410 |
#[test] |
| 411 |
fn test_should_report_invalid_number_with_sign_after_decimal() { |
| 412 |
new_ucmd!() |
| 413 |
.args(&["--", "-0.-1"]) |
| 414 |
.fails_with_code(2) |
| 415 |
.stderr_is("numfmt: invalid number: '-0.-1'\n"); |
| 416 |
} |
| 417 |
|
| 418 |
#[test] |
| 419 |
fn test_should_skip_leading_space_from_stdin() { |
| 420 |
new_ucmd!() |
| 421 |
.args(&["--from=auto"]) |
| 422 |
.pipe_in(" 2Ki") |
| 423 |
.succeeds() |
| 424 |
.stdout_is("2048"); |
| 425 |
|
| 426 |
// multi-line |
| 427 |
new_ucmd!() |
| 428 |
.args(&["--from=auto"]) |
| 429 |
.pipe_in("\t1Ki\n 2K") |
| 430 |
.succeeds() |
| 431 |
.stdout_is("1024\n2000"); |
| 432 |
} |
| 433 |
|
| 434 |
#[test] |
| 435 |
fn test_should_convert_only_first_number_in_line() { |
| 436 |
new_ucmd!() |
| 437 |
.args(&["--from=auto"]) |
| 438 |
.pipe_in("1Ki 2M 3G") |
| 439 |
.succeeds() |
| 440 |
.stdout_is("1024 2M 3G"); |
| 441 |
} |
| 442 |
|
| 443 |
#[test] |
| 444 |
fn test_leading_whitespace_should_imply_padding() { |
| 445 |
new_ucmd!() |
| 446 |
.args(&["--from=auto"]) |
| 447 |
.pipe_in(" 1K") |
| 448 |
.succeeds() |
| 449 |
.stdout_is(" 1000"); |
| 450 |
|
| 451 |
new_ucmd!() |
| 452 |
.args(&["--from=auto"]) |
| 453 |
.pipe_in(" 202Ki") |
| 454 |
.succeeds() |
| 455 |
.stdout_is(" 206848"); |
| 456 |
} |
| 457 |
|
| 458 |
#[test] |
| 459 |
fn test_should_calculate_implicit_padding_per_line() { |
| 460 |
new_ucmd!() |
| 461 |
.args(&["--from=auto"]) |
| 462 |
.pipe_in(" 1Ki\n 2K") |
| 463 |
.succeeds() |
| 464 |
.stdout_is(" 1024\n 2000"); |
| 465 |
} |
| 466 |
|
| 467 |
#[test] |
| 468 |
fn test_leading_whitespace_in_free_argument_should_imply_padding() { |
| 469 |
new_ucmd!() |
| 470 |
.args(&["--from=auto", " 1Ki"]) |
| 471 |
.succeeds() |
| 472 |
.stdout_is(" 1024\n"); |
| 473 |
} |
| 474 |
|
| 475 |
#[test] |
| 476 |
fn test_should_calculate_implicit_padding_per_free_argument() { |
| 477 |
new_ucmd!() |
| 478 |
.args(&["--from=auto", " 1Ki", " 2K"]) |
| 479 |
.succeeds() |
| 480 |
.stdout_is(" 1024\n 2000\n"); |
| 481 |
} |
| 482 |
|
| 483 |
#[test] |
| 484 |
fn test_to_si_should_truncate_output() { |
| 485 |
new_ucmd!() |
| 486 |
.args(&["--to=si"]) |
| 487 |
.pipe_in_fixture("gnutest_si_input.txt") |
| 488 |
.succeeds() |
| 489 |
.stdout_is_fixture("gnutest_si_result.txt"); |
| 490 |
} |
| 491 |
|
| 492 |
#[test] |
| 493 |
fn test_to_iec_should_truncate_output() { |
| 494 |
new_ucmd!() |
| 495 |
.args(&["--to=iec"]) |
| 496 |
.pipe_in_fixture("gnutest_iec_input.txt") |
| 497 |
.succeeds() |
| 498 |
.stdout_is_fixture("gnutest_iec_result.txt"); |
| 499 |
} |
| 500 |
|
| 501 |
#[test] |
| 502 |
fn test_to_iec_i_should_truncate_output() { |
| 503 |
new_ucmd!() |
| 504 |
.args(&["--to=iec-i"]) |
| 505 |
.pipe_in_fixture("gnutest_iec_input.txt") |
| 506 |
.succeeds() |
| 507 |
.stdout_is_fixture("gnutest_iec-i_result.txt"); |
| 508 |
} |
| 509 |
|
| 510 |
#[test] |
| 511 |
fn test_format_selected_field() { |
| 512 |
new_ucmd!() |
| 513 |
.args(&["--from=auto", "--field", "3", "1K 2K 3K"]) |
| 514 |
.succeeds() |
| 515 |
.stdout_only("1K 2K 3000\n"); |
| 516 |
new_ucmd!() |
| 517 |
.args(&["--from=auto", "--field", "2", "1K 2K 3K"]) |
| 518 |
.succeeds() |
| 519 |
.stdout_only("1K 2000 3K\n"); |
| 520 |
} |
| 521 |
|
| 522 |
#[test] |
| 523 |
fn test_field_with_multibyte_whitespace_separator() { |
| 524 |
// A multibyte Unicode whitespace separator (U+3000) before a selected field |
| 525 |
// is normalized to a single space rather than sliced mid-character. |
| 526 |
new_ucmd!() |
| 527 |
.args(&["--field", "2", "1 2"]) |
| 528 |
.succeeds() |
| 529 |
.stdout_only("1 2\n"); |
| 530 |
|
| 531 |
new_ucmd!() |
| 532 |
.args(&["--from=auto", "--field", "2"]) |
| 533 |
.pipe_in("1K\u{3000}2K\n") |
| 534 |
.succeeds() |
| 535 |
.stdout_only("1K 2000\n"); |
| 536 |
} |
| 537 |
|
| 538 |
#[test] |
| 539 |
fn test_format_selected_fields() { |
| 540 |
new_ucmd!() |
| 541 |
.args(&["--from=auto", "--field", "1,4,3", "1K 2K 3K 4K 5K 6K"]) |
| 542 |
.succeeds() |
| 543 |
.stdout_only("1000 2K 3000 4000 5K 6K\n"); |
| 544 |
|
| 545 |
new_ucmd!() |
| 546 |
.args(&["--from=auto", "--field", "1,4 3", "1K 2K 3K 4K 5K 6K"]) |
| 547 |
.succeeds() |
| 548 |
.stdout_only("1000 2K 3000 4000 5K 6K\n"); |
| 549 |
} |
| 550 |
|
| 551 |
#[test] |
| 552 |
fn test_format_implied_range_and_field() { |
| 553 |
new_ucmd!() |
| 554 |
.args(&["--from=auto", "--field", "-2,4", "1K 2K 3K 4K 5K 6K"]) |
| 555 |
.succeeds() |
| 556 |
.stdout_only("1000 2000 3K 4000 5K 6K\n"); |
| 557 |
} |
| 558 |
|
| 559 |
#[test] |
| 560 |
fn test_should_succeed_if_selected_field_out_of_range() { |
| 561 |
new_ucmd!() |
| 562 |
.args(&["--from=auto", "--field", "9", "1K 2K 3K"]) |
| 563 |
.succeeds() |
| 564 |
.stdout_only("1K 2K 3K\n"); |
| 565 |
} |
| 566 |
|
| 567 |
#[test] |
| 568 |
fn test_format_selected_field_range() { |
| 569 |
new_ucmd!() |
| 570 |
.args(&["--from=auto", "--field", "2-5", "1K 2K 3K 4K 5K 6K"]) |
| 571 |
.succeeds() |
| 572 |
.stdout_only("1K 2000 3000 4000 5000 6K\n"); |
| 573 |
} |
| 574 |
|
| 575 |
#[test] |
| 576 |
fn test_format_all_fields() { |
| 577 |
let all_fields_patterns = vec!["-", "-,3", "3,-", "1,-,3", "- 3"]; |
| 578 |
|
| 579 |
for pattern in all_fields_patterns { |
| 580 |
new_ucmd!() |
| 581 |
.args(&["--from=auto", "--field", pattern, "1K 2K 3K 4K 5K 6K"]) |
| 582 |
.succeeds() |
| 583 |
.stdout_only("1000 2000 3000 4000 5000 6000\n"); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
#[test] |
| 588 |
fn test_should_succeed_if_range_out_of_bounds() { |
| 589 |
new_ucmd!() |
| 590 |
.args(&["--from=auto", "--field", "5-10", "1K 2K 3K 4K 5K 6K"]) |
| 591 |
.succeeds() |
| 592 |
.stdout_only("1K 2K 3K 4K 5000 6000\n"); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn test_implied_initial_field_value() { |
| 597 |
new_ucmd!() |
| 598 |
.args(&["--from=auto", "--field", "-2", "1K 2K 3K"]) |
| 599 |
.succeeds() |
| 600 |
.stdout_only("1000 2000 3K\n"); |
| 601 |
|
| 602 |
// same as above but with the equal sign |
| 603 |
new_ucmd!() |
| 604 |
.args(&["--from=auto", "--field=-2", "1K 2K 3K"]) |
| 605 |
.succeeds() |
| 606 |
.stdout_only("1000 2000 3K\n"); |
| 607 |
} |
| 608 |
|
| 609 |
#[test] |
| 610 |
fn test_field_df_example() { |
| 611 |
// df -B1 | numfmt --header --field 2-4 --to=si |
| 612 |
new_ucmd!() |
| 613 |
.args(&["--header", "--field", "2-4", "--to=si"]) |
| 614 |
.pipe_in_fixture("df_input.txt") |
| 615 |
.succeeds() |
| 616 |
.stdout_is_fixture("df_expected.txt"); |
| 617 |
} |
| 618 |
|
| 619 |
#[test] |
| 620 |
fn test_delimiter_must_not_be_empty() { |
| 621 |
new_ucmd!().args(&["-d"]).fails(); |
| 622 |
} |
| 623 |
|
| 624 |
#[test] |
| 625 |
fn test_delimiter_must_not_be_more_than_one_character() { |
| 626 |
new_ucmd!() |
| 627 |
.args(&["--delimiter", "sad"]) |
| 628 |
.fails() |
| 629 |
.stderr_is("numfmt: the delimiter must be a single character\n"); |
| 630 |
} |
| 631 |
|
| 632 |
#[test] |
| 633 |
fn test_delimiter_only() { |
| 634 |
new_ucmd!() |
| 635 |
.args(&["-d", ","]) |
| 636 |
.pipe_in("1234,56") |
| 637 |
.succeeds() |
| 638 |
.stdout_only("1234,56"); |
| 639 |
} |
| 640 |
|
| 641 |
#[test] |
| 642 |
fn test_line_is_field_with_no_delimiter() { |
| 643 |
new_ucmd!() |
| 644 |
.args(&["-d,", "--to=iec"]) |
| 645 |
.pipe_in("123456") |
| 646 |
.succeeds() |
| 647 |
.stdout_only("121K"); |
| 648 |
} |
| 649 |
|
| 650 |
#[test] |
| 651 |
fn test_delimiter_to_si() { |
| 652 |
new_ucmd!() |
| 653 |
.args(&["-d=,", "--to=si"]) |
| 654 |
.pipe_in("1234,56") |
| 655 |
.succeeds() |
| 656 |
.stdout_only("1.3k,56"); |
| 657 |
} |
| 658 |
|
| 659 |
#[test] |
| 660 |
fn test_delimiter_skips_leading_whitespace() { |
| 661 |
new_ucmd!() |
| 662 |
.args(&["-d=,", "--to=si"]) |
| 663 |
.pipe_in(" \t 1234,56") |
| 664 |
.succeeds() |
| 665 |
.stdout_only("1.3k,56"); |
| 666 |
} |
| 667 |
|
| 668 |
#[test] |
| 669 |
fn test_delimiter_preserves_leading_whitespace_in_unselected_fields() { |
| 670 |
new_ucmd!() |
| 671 |
.args(&["-d=|", "--to=si"]) |
| 672 |
.pipe_in(" 1000| 2000") |
| 673 |
.succeeds() |
| 674 |
.stdout_only("1.0k| 2000"); |
| 675 |
} |
| 676 |
|
| 677 |
#[test] |
| 678 |
fn test_delimiter_from_si() { |
| 679 |
new_ucmd!() |
| 680 |
.args(&["-d=,", "--from=si"]) |
| 681 |
.pipe_in("1.2K,56") |
| 682 |
.succeeds() |
| 683 |
.stdout_only("1200,56"); |
| 684 |
} |
| 685 |
|
| 686 |
#[test] |
| 687 |
fn test_delimiter_overrides_whitespace_separator() { |
| 688 |
new_ucmd!() |
| 689 |
.args(&["-d,"]) |
| 690 |
.pipe_in("1 234,56") |
| 691 |
.fails() |
| 692 |
.stderr_is("numfmt: invalid suffix in input: '1 234'\n"); |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
fn test_delimiter_with_padding() { |
| 697 |
new_ucmd!() |
| 698 |
.args(&["-d=|", "--to=si", "--padding=5"]) |
| 699 |
.pipe_in("1000|2000") |
| 700 |
.succeeds() |
| 701 |
.stdout_only(" 1.0k|2000"); |
| 702 |
} |
| 703 |
|
| 704 |
#[test] |
| 705 |
fn test_delimiter_with_padding_and_fields() { |
| 706 |
new_ucmd!() |
| 707 |
.args(&["-d=|", "--to=si", "--padding=5", "--field=-"]) |
| 708 |
.pipe_in("1000|2000") |
| 709 |
.succeeds() |
| 710 |
.stdout_only(" 1.0k| 2.0k"); |
| 711 |
} |
| 712 |
|
| 713 |
#[test] |
| 714 |
fn test_round() { |
| 715 |
for (method, exp) in [ |
| 716 |
("from-zero", ["9.1k", "-9.1k", "9.1k", "-9.1k"]), |
| 717 |
("from-zer", ["9.1k", "-9.1k", "9.1k", "-9.1k"]), |
| 718 |
("f", ["9.1k", "-9.1k", "9.1k", "-9.1k"]), |
| 719 |
("towards-zero", ["9.0k", "-9.0k", "9.0k", "-9.0k"]), |
| 720 |
("up", ["9.1k", "-9.0k", "9.1k", "-9.0k"]), |
| 721 |
("down", ["9.0k", "-9.1k", "9.0k", "-9.1k"]), |
| 722 |
("nearest", ["9.0k", "-9.0k", "9.1k", "-9.1k"]), |
| 723 |
("near", ["9.0k", "-9.0k", "9.1k", "-9.1k"]), |
| 724 |
("n", ["9.0k", "-9.0k", "9.1k", "-9.1k"]), |
| 725 |
] { |
| 726 |
new_ucmd!() |
| 727 |
.args(&[ |
| 728 |
"--to=si", |
| 729 |
&format!("--round={method}"), |
| 730 |
"--", |
| 731 |
"9001", |
| 732 |
"-9001", |
| 733 |
"9099", |
| 734 |
"-9099", |
| 735 |
]) |
| 736 |
.succeeds() |
| 737 |
.stdout_only(exp.join("\n") + "\n"); |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
#[test] |
| 742 |
fn test_to_unitless_small_values_use_display_rounding() { |
| 743 |
new_ucmd!() |
| 744 |
.args(&[ |
| 745 |
"--to=si", "--", "0.4", "0.5", "0.6", "1.4", "3.14", "-0.4", "-0.5", "-0.6", "-1.4", |
| 746 |
]) |
| 747 |
.succeeds() |
| 748 |
.stdout_only("0\n0\n1\n1\n3\n-0\n-0\n-1\n-1\n"); |
| 749 |
} |
| 750 |
|
| 751 |
#[test] |
| 752 |
fn test_round_with_to_unit() { |
| 753 |
for (method, exp) in [ |
| 754 |
("from-zero", ["6", "-6", "5.9", "-5.9", "5.86", "-5.86"]), |
| 755 |
("towards-zero", ["5", "-5", "5.8", "-5.8", "5.85", "-5.85"]), |
| 756 |
("up", ["6", "-5", "5.9", "-5.8", "5.86", "-5.85"]), |
| 757 |
("down", ["5", "-6", "5.8", "-5.9", "5.85", "-5.86"]), |
| 758 |
("nearest", ["6", "-6", "5.9", "-5.9", "5.86", "-5.86"]), |
| 759 |
] { |
| 760 |
new_ucmd!() |
| 761 |
.args(&[ |
| 762 |
"--to-unit=1024", |
| 763 |
&format!("--round={method}"), |
| 764 |
"--", |
| 765 |
"6000", |
| 766 |
"-6000", |
| 767 |
"6000.0", |
| 768 |
"-6000.0", |
| 769 |
"6000.00", |
| 770 |
"-6000.00", |
| 771 |
]) |
| 772 |
.succeeds() |
| 773 |
.stdout_only(exp.join("\n") + "\n"); |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
#[test] |
| 778 |
fn test_to_unit_with_unitless_small_value_uses_display_rounding() { |
| 779 |
new_ucmd!() |
| 780 |
.args(&["--to=iec", "--to-unit=689", "701"]) |
| 781 |
.succeeds() |
| 782 |
.stdout_only("1\n"); |
| 783 |
|
| 784 |
new_ucmd!() |
| 785 |
.args(&["--to=si", "--to-unit=689", "701"]) |
| 786 |
.succeeds() |
| 787 |
.stdout_only("1\n"); |
| 788 |
|
| 789 |
new_ucmd!() |
| 790 |
.args(&["--to=none", "--to-unit=689", "701"]) |
| 791 |
.succeeds() |
| 792 |
.stdout_only("2\n"); |
| 793 |
} |
| 794 |
|
| 795 |
#[test] |
| 796 |
fn test_suffix_is_added_if_not_supplied() { |
| 797 |
new_ucmd!() |
| 798 |
.args(&["--suffix=TEST"]) |
| 799 |
.pipe_in("1000") |
| 800 |
.succeeds() |
| 801 |
.stdout_only("1000TEST"); |
| 802 |
} |
| 803 |
|
| 804 |
#[test] |
| 805 |
fn test_suffix_is_preserved() { |
| 806 |
new_ucmd!() |
| 807 |
.args(&["--suffix=TEST"]) |
| 808 |
.pipe_in("1000TEST") |
| 809 |
.succeeds() |
| 810 |
.stdout_only("1000TEST"); |
| 811 |
} |
| 812 |
|
| 813 |
#[test] |
| 814 |
fn test_suffix_is_only_applied_to_selected_field() { |
| 815 |
new_ucmd!() |
| 816 |
.args(&["--suffix=TEST", "--field=2"]) |
| 817 |
.pipe_in("1000 2000 3000") |
| 818 |
.succeeds() |
| 819 |
.stdout_only("1000 2000TEST 3000"); |
| 820 |
} |
| 821 |
|
| 822 |
#[test] |
| 823 |
fn test_transform_with_suffix_on_input() { |
| 824 |
new_ucmd!() |
| 825 |
.args(&["--suffix=b", "--to=si"]) |
| 826 |
.pipe_in("2000b") |
| 827 |
.succeeds() |
| 828 |
.stdout_only("2.0kb"); |
| 829 |
} |
| 830 |
|
| 831 |
#[test] |
| 832 |
fn test_transform_without_suffix_on_input() { |
| 833 |
new_ucmd!() |
| 834 |
.args(&["--suffix=b", "--to=si"]) |
| 835 |
.pipe_in("2000") |
| 836 |
.succeeds() |
| 837 |
.stdout_only("2.0kb"); |
| 838 |
} |
| 839 |
|
| 840 |
#[test] |
| 841 |
fn test_transform_with_suffix_and_delimiter() { |
| 842 |
new_ucmd!() |
| 843 |
.args(&["--suffix=b", "--to=si", "-d=|"]) |
| 844 |
.pipe_in("1000b|2000|3000") |
| 845 |
.succeeds() |
| 846 |
.stdout_only("1.0kb|2000|3000"); |
| 847 |
} |
| 848 |
|
| 849 |
#[test] |
| 850 |
fn test_suffix_with_padding() { |
| 851 |
new_ucmd!() |
| 852 |
.args(&["--suffix=pad", "--padding=12"]) |
| 853 |
.pipe_in("1000 2000 3000") |
| 854 |
.succeeds() |
| 855 |
.stdout_only(" 1000pad 2000 3000"); |
| 856 |
} |
| 857 |
|
| 858 |
#[test] |
| 859 |
fn test_invalid_stdin_number_returns_status_2() { |
| 860 |
new_ucmd!().pipe_in("hello").fails_with_code(2); |
| 861 |
} |
| 862 |
|
| 863 |
#[test] |
| 864 |
fn test_invalid_stdin_number_in_middle_of_input() { |
| 865 |
new_ucmd!() |
| 866 |
.pipe_in("100\nhello\n200") |
| 867 |
.ignore_stdin_write_error() |
| 868 |
.fails_with_code(2) |
| 869 |
.stdout_is("100\n"); |
| 870 |
} |
| 871 |
|
| 872 |
#[test] |
| 873 |
fn test_invalid_stdin_number_with_warn_returns_status_0() { |
| 874 |
new_ucmd!() |
| 875 |
.args(&["--invalid=warn"]) |
| 876 |
.pipe_in("4Q") |
| 877 |
.succeeds() |
| 878 |
.stdout_is("4Q") |
| 879 |
.stderr_is("numfmt: rejecting suffix in input: '4Q' (consider using --from)\n"); |
| 880 |
} |
| 881 |
|
| 882 |
#[test] |
| 883 |
fn test_invalid_stdin_number_with_ignore_returns_status_0() { |
| 884 |
new_ucmd!() |
| 885 |
.args(&["--invalid=ignore"]) |
| 886 |
.pipe_in("4Q") |
| 887 |
.succeeds() |
| 888 |
.stdout_only("4Q"); |
| 889 |
} |
| 890 |
|
| 891 |
#[test] |
| 892 |
fn test_invalid_stdin_number_with_abort_returns_status_2() { |
| 893 |
new_ucmd!() |
| 894 |
.args(&["--invalid=abort"]) |
| 895 |
.pipe_in("4Q") |
| 896 |
.fails_with_code(2) |
| 897 |
.stderr_only("numfmt: rejecting suffix in input: '4Q' (consider using --from)\n"); |
| 898 |
} |
| 899 |
|
| 900 |
#[test] |
| 901 |
fn test_invalid_stdin_number_with_fail_returns_status_2() { |
| 902 |
new_ucmd!() |
| 903 |
.args(&["--invalid=fail"]) |
| 904 |
.pipe_in("4Q") |
| 905 |
.fails_with_code(2) |
| 906 |
.stdout_is("4Q") |
| 907 |
.stderr_is("numfmt: rejecting suffix in input: '4Q' (consider using --from)\n"); |
| 908 |
} |
| 909 |
|
| 910 |
#[test] |
| 911 |
fn test_invalid_arg_number_with_warn_returns_status_0() { |
| 912 |
new_ucmd!() |
| 913 |
.args(&["--invalid=warn", "4Q"]) |
| 914 |
.succeeds() |
| 915 |
.stdout_is("4Q\n") |
| 916 |
.stderr_is("numfmt: rejecting suffix in input: '4Q' (consider using --from)\n"); |
| 917 |
} |
| 918 |
|
| 919 |
#[test] |
| 920 |
fn test_invalid_arg_number_with_ignore_returns_status_0() { |
| 921 |
new_ucmd!() |
| 922 |
.args(&["--invalid=ignore", "4Q"]) |
| 923 |
.succeeds() |
| 924 |
.stdout_only("4Q\n"); |
| 925 |
} |
| 926 |
|
| 927 |
#[test] |
| 928 |
fn test_invalid_arg_number_with_abort_returns_status_2() { |
| 929 |
new_ucmd!() |
| 930 |
.args(&["--invalid=abort", "4Q"]) |
| 931 |
.fails_with_code(2) |
| 932 |
.stderr_only("numfmt: rejecting suffix in input: '4Q' (consider using --from)\n"); |
| 933 |
} |
| 934 |
|
| 935 |
#[test] |
| 936 |
fn test_invalid_arg_number_with_fail_returns_status_2() { |
| 937 |
new_ucmd!() |
| 938 |
.args(&["--invalid=fail", "4Q"]) |
| 939 |
.fails_with_code(2) |
| 940 |
.stdout_is("4Q\n") |
| 941 |
.stderr_is("numfmt: rejecting suffix in input: '4Q' (consider using --from)\n"); |
| 942 |
} |
| 943 |
|
| 944 |
#[test] |
| 945 |
fn test_invalid_argument_returns_status_1() { |
| 946 |
new_ucmd!() |
| 947 |
.args(&["--header=hello"]) |
| 948 |
.pipe_in("53478") |
| 949 |
.ignore_stdin_write_error() |
| 950 |
.fails_with_code(1); |
| 951 |
} |
| 952 |
|
| 953 |
#[test] |
| 954 |
fn test_invalid_padding_value() { |
| 955 |
let padding_values = vec!["A", "0"]; |
| 956 |
|
| 957 |
for padding_value in padding_values { |
| 958 |
new_ucmd!() |
| 959 |
.arg(format!("--padding={padding_value}")) |
| 960 |
.arg("5") |
| 961 |
.fails_with_code(1) |
| 962 |
.stderr_contains(format!("invalid padding value '{padding_value}'")); |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
#[test] |
| 967 |
fn test_from_unit() { |
| 968 |
new_ucmd!() |
| 969 |
.args(&["--from-unit=512", "4"]) |
| 970 |
.succeeds() |
| 971 |
.stdout_is("2048\n"); |
| 972 |
} |
| 973 |
|
| 974 |
#[test] |
| 975 |
fn test_to_unit() { |
| 976 |
new_ucmd!() |
| 977 |
.args(&["--to-unit=512", "2048"]) |
| 978 |
.succeeds() |
| 979 |
.stdout_is("4\n"); |
| 980 |
} |
| 981 |
|
| 982 |
#[test] |
| 983 |
fn test_invalid_unit_size() { |
| 984 |
let commands = vec!["from", "to"]; |
| 985 |
let invalid_sizes = vec![ |
| 986 |
"A", |
| 987 |
"0", |
| 988 |
"18446744073709551616", |
| 989 |
"18446744073709551615K", |
| 990 |
"18014398509481984Ki", |
| 991 |
]; |
| 992 |
|
| 993 |
for command in commands { |
| 994 |
for invalid_size in &invalid_sizes { |
| 995 |
new_ucmd!() |
| 996 |
.arg(format!("--{command}-unit={invalid_size}")) |
| 997 |
.fails_with_code(1) |
| 998 |
.stderr_contains(format!("invalid unit size: '{invalid_size}'")); |
| 999 |
} |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
#[test] |
| 1004 |
fn test_valid_but_forbidden_suffix() { |
| 1005 |
let numbers = vec!["12K", "12Ki"]; |
| 1006 |
|
| 1007 |
for number in numbers { |
| 1008 |
new_ucmd!() |
| 1009 |
.arg(number) |
| 1010 |
.fails_with_code(2) |
| 1011 |
.stderr_contains(format!( |
| 1012 |
"rejecting suffix in input: '{number}' (consider using --from)" |
| 1013 |
)); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
#[test] |
| 1018 |
fn test_format() { |
| 1019 |
new_ucmd!() |
| 1020 |
.args(&["--format=--%f--", "50"]) |
| 1021 |
.succeeds() |
| 1022 |
.stdout_is("--50--\n"); |
| 1023 |
} |
| 1024 |
|
| 1025 |
#[test] |
| 1026 |
fn test_format_with_separate_value() { |
| 1027 |
new_ucmd!() |
| 1028 |
.args(&["--format", "--%f--", "50"]) |
| 1029 |
.succeeds() |
| 1030 |
.stdout_is("--50--\n"); |
| 1031 |
} |
| 1032 |
|
| 1033 |
#[test] |
| 1034 |
fn test_format_padding_with_prefix_and_suffix() { |
| 1035 |
new_ucmd!() |
| 1036 |
.args(&["--format=--%6f--", "50"]) |
| 1037 |
.succeeds() |
| 1038 |
.stdout_is("-- 50--\n"); |
| 1039 |
} |
| 1040 |
|
| 1041 |
#[test] |
| 1042 |
fn test_format_negative_padding_with_prefix_and_suffix() { |
| 1043 |
new_ucmd!() |
| 1044 |
.args(&["--format=--%-6f--", "50"]) |
| 1045 |
.succeeds() |
| 1046 |
.stdout_is("--50 --\n"); |
| 1047 |
} |
| 1048 |
|
| 1049 |
#[test] |
| 1050 |
fn test_format_with_format_padding_overriding_padding_option() { |
| 1051 |
new_ucmd!() |
| 1052 |
.args(&["--format=%6f", "--padding=10", "1234"]) |
| 1053 |
.succeeds() |
| 1054 |
.stdout_is(" 1234\n"); |
| 1055 |
} |
| 1056 |
|
| 1057 |
#[test] |
| 1058 |
fn test_format_with_format_padding_overriding_implicit_padding() { |
| 1059 |
new_ucmd!() |
| 1060 |
.args(&["--format=%6f", " 1234"]) |
| 1061 |
.succeeds() |
| 1062 |
.stdout_is(" 1234\n"); |
| 1063 |
} |
| 1064 |
|
| 1065 |
#[test] |
| 1066 |
fn test_format_with_negative_format_padding_and_suffix() { |
| 1067 |
new_ucmd!() |
| 1068 |
.args(&["--format=%-6f", "1234 ?"]) |
| 1069 |
.succeeds() |
| 1070 |
.stdout_is("1234 ?\n"); |
| 1071 |
} |
| 1072 |
|
| 1073 |
#[test] |
| 1074 |
fn test_format_with_zero_padding() { |
| 1075 |
let formats = vec!["%06f", "%0 6f"]; |
| 1076 |
|
| 1077 |
for format in formats { |
| 1078 |
new_ucmd!() |
| 1079 |
.args(&[format!("--format={format}"), String::from("1234")]) |
| 1080 |
.succeeds() |
| 1081 |
.stdout_is("001234\n"); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
#[test] |
| 1086 |
fn test_format_with_zero_padding_and_padding_option() { |
| 1087 |
new_ucmd!() |
| 1088 |
.args(&["--format=%06f", "--padding=8", "1234"]) |
| 1089 |
.succeeds() |
| 1090 |
.stdout_is(" 001234\n"); |
| 1091 |
} |
| 1092 |
|
| 1093 |
#[test] |
| 1094 |
fn test_format_with_zero_padding_and_negative_padding_option() { |
| 1095 |
new_ucmd!() |
| 1096 |
.args(&["--format=%06f", "--padding=-8", "1234"]) |
| 1097 |
.succeeds() |
| 1098 |
.stdout_is("001234 \n"); |
| 1099 |
} |
| 1100 |
|
| 1101 |
#[test] |
| 1102 |
fn test_format_with_zero_padding_and_implicit_padding() { |
| 1103 |
new_ucmd!() |
| 1104 |
.args(&["--format=%06f", " 1234"]) |
| 1105 |
.succeeds() |
| 1106 |
.stdout_is(" 001234\n"); |
| 1107 |
} |
| 1108 |
|
| 1109 |
#[test] |
| 1110 |
fn test_format_with_zero_padding_and_suffix() { |
| 1111 |
new_ucmd!() |
| 1112 |
.args(&["--format=%06f", "1234 ?"]) |
| 1113 |
.succeeds() |
| 1114 |
.stdout_is("001234 ?\n"); |
| 1115 |
} |
| 1116 |
|
| 1117 |
// GNU zero-pads the number only: "Optional zero (%010f) width will zero pad |
| 1118 |
// the number". The unit from --to and the --suffix text stay outside the width. |
| 1119 |
#[test] |
| 1120 |
fn test_format_with_zero_padding_excludes_unit_suffix() { |
| 1121 |
let cases = [ |
| 1122 |
(vec!["--format=%05.1f", "--to=si", "1234567"], "001.3M"), |
| 1123 |
(vec!["--format=%06.1f", "--to=si", "1234"], "0001.3k"), |
| 1124 |
( |
| 1125 |
vec!["--format=%08.1f", "--to=iec-i", "1234567"], |
| 1126 |
"000001.2Mi", |
| 1127 |
), |
| 1128 |
( |
| 1129 |
vec!["--format=%08.1f", "--to=si", "--", "-1234567"], |
| 1130 |
"-00001.3M", |
| 1131 |
), |
| 1132 |
(vec!["--format=%03.1f", "--to=si", "1234567"], "1.3M"), |
| 1133 |
( |
| 1134 |
vec!["--format=%05.1f", "--to=si", "--suffix=B", "1234567"], |
| 1135 |
"001.3MB", |
| 1136 |
), |
| 1137 |
(vec!["--format=%05.1f", "--suffix=B", "1.5"], "001.5B"), |
| 1138 |
( |
| 1139 |
vec!["--format=%08.1f", "--to=si", "--padding=12", "1234567"], |
| 1140 |
" 000001.3M", |
| 1141 |
), |
| 1142 |
]; |
| 1143 |
|
| 1144 |
for (args, expected) in cases { |
| 1145 |
new_ucmd!() |
| 1146 |
.args(&args) |
| 1147 |
.succeeds() |
| 1148 |
.stdout_only(format!("{expected}\n")); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
#[test] |
| 1153 |
fn test_format_with_precision() { |
| 1154 |
let values = vec![("0.99", "1.0"), ("1", "1.0"), ("1.01", "1.1")]; |
| 1155 |
|
| 1156 |
for (input, expected) in values { |
| 1157 |
new_ucmd!() |
| 1158 |
.args(&["--format=%.1f", input]) |
| 1159 |
.succeeds() |
| 1160 |
.stdout_is(format!("{expected}\n")); |
| 1161 |
} |
| 1162 |
|
| 1163 |
let values = vec![("0.99", "0.99"), ("1", "1.00"), ("1.01", "1.01")]; |
| 1164 |
|
| 1165 |
for (input, expected) in values { |
| 1166 |
new_ucmd!() |
| 1167 |
.args(&["--format=%.2f", input]) |
| 1168 |
.succeeds() |
| 1169 |
.stdout_is(format!("{expected}\n")); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
#[test] |
| 1174 |
fn test_format_with_precision_and_down_rounding() { |
| 1175 |
let values = vec![("0.99", "0.9"), ("1", "1.0"), ("1.01", "1.0")]; |
| 1176 |
|
| 1177 |
for (input, expected) in values { |
| 1178 |
new_ucmd!() |
| 1179 |
.args(&["--format=%.1f", input, "--round=down"]) |
| 1180 |
.succeeds() |
| 1181 |
.stdout_is(format!("{expected}\n")); |
| 1182 |
} |
| 1183 |
} |
| 1184 |
|
| 1185 |
#[test] |
| 1186 |
fn test_format_with_precision_and_to_arg() { |
| 1187 |
let values = vec![("%.1f", "10.0G"), ("%.4f", "9.9913G")]; |
| 1188 |
|
| 1189 |
for (format, expected) in values { |
| 1190 |
new_ucmd!() |
| 1191 |
.args(&[ |
| 1192 |
format!("--format={format}"), |
| 1193 |
"9991239123".to_string(), |
| 1194 |
"--to=si".to_string(), |
| 1195 |
]) |
| 1196 |
.succeeds() |
| 1197 |
.stdout_is(format!("{expected}\n")); |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
#[test] |
| 1202 |
fn test_format_with_precision_and_unitless_to_arg() { |
| 1203 |
new_ucmd!() |
| 1204 |
.args(&["--to=si", "--format=%.1f", "3.14"]) |
| 1205 |
.succeeds() |
| 1206 |
.stdout_is("4.0\n"); |
| 1207 |
|
| 1208 |
new_ucmd!() |
| 1209 |
.args(&["--to=si", "--format=%.1f", "--round=down", "3.14"]) |
| 1210 |
.succeeds() |
| 1211 |
.stdout_is("3.0\n"); |
| 1212 |
} |
| 1213 |
|
| 1214 |
#[test] |
| 1215 |
fn test_format_preserve_trailing_zeros_if_no_precision_is_specified() { |
| 1216 |
let values = vec!["10.0", "0.0100"]; |
| 1217 |
|
| 1218 |
for value in values { |
| 1219 |
new_ucmd!() |
| 1220 |
.args(&["--format=%f", value]) |
| 1221 |
.succeeds() |
| 1222 |
.stdout_is(format!("{value}\n")); |
| 1223 |
} |
| 1224 |
} |
| 1225 |
|
| 1226 |
#[test] |
| 1227 |
fn test_format_without_percentage_directive() { |
| 1228 |
let invalid_formats = vec!["", "hello"]; |
| 1229 |
|
| 1230 |
for invalid_format in invalid_formats { |
| 1231 |
new_ucmd!() |
| 1232 |
.arg(format!("--format={invalid_format}")) |
| 1233 |
.fails_with_code(1) |
| 1234 |
.stderr_contains(format!("format '{invalid_format}' has no % directive")); |
| 1235 |
} |
| 1236 |
} |
| 1237 |
|
| 1238 |
#[test] |
| 1239 |
fn test_format_with_percentage_directive_at_end() { |
| 1240 |
let invalid_format = "hello%"; |
| 1241 |
|
| 1242 |
new_ucmd!() |
| 1243 |
.arg(format!("--format={invalid_format}")) |
| 1244 |
.fails_with_code(1) |
| 1245 |
.stderr_contains(format!("format '{invalid_format}' ends in %")); |
| 1246 |
} |
| 1247 |
|
| 1248 |
#[test] |
| 1249 |
fn test_format_with_too_many_percentage_directives() { |
| 1250 |
let invalid_format = "%f %f"; |
| 1251 |
|
| 1252 |
new_ucmd!() |
| 1253 |
.arg(format!("--format={invalid_format}")) |
| 1254 |
.fails_with_code(1) |
| 1255 |
.stderr_contains(format!( |
| 1256 |
"format '{invalid_format}' has too many % directives" |
| 1257 |
)); |
| 1258 |
} |
| 1259 |
|
| 1260 |
#[test] |
| 1261 |
fn test_format_with_invalid_format() { |
| 1262 |
let invalid_formats = vec!["%d", "% -43 f"]; |
| 1263 |
|
| 1264 |
for invalid_format in invalid_formats { |
| 1265 |
new_ucmd!() |
| 1266 |
.arg(format!("--format={invalid_format}")) |
| 1267 |
.fails_with_code(1) |
| 1268 |
.stderr_contains(format!( |
| 1269 |
"invalid format '{invalid_format}', directive must be %[0]['][-][N][.][N]f" |
| 1270 |
)); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
#[test] |
| 1275 |
fn test_format_with_width_overflow() { |
| 1276 |
let invalid_format = "%18446744073709551616f"; |
| 1277 |
new_ucmd!() |
| 1278 |
.arg(format!("--format={invalid_format}")) |
| 1279 |
.fails_with_code(1) |
| 1280 |
.stderr_contains(format!( |
| 1281 |
"invalid format '{invalid_format}' (width overflow)" |
| 1282 |
)); |
| 1283 |
} |
| 1284 |
|
| 1285 |
#[test] |
| 1286 |
fn test_format_with_invalid_precision() { |
| 1287 |
let invalid_formats = vec!["%.-1f", "%.+1f", "%. 1f", "%.18446744073709551616f"]; |
| 1288 |
|
| 1289 |
for invalid_format in invalid_formats { |
| 1290 |
new_ucmd!() |
| 1291 |
.arg(format!("--format={invalid_format}")) |
| 1292 |
.fails_with_code(1) |
| 1293 |
.stderr_contains(format!("invalid precision in format '{invalid_format}'")); |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
#[test] |
| 1298 |
fn test_format_grouping_conflicts_with_to_option() { |
| 1299 |
new_ucmd!() |
| 1300 |
.args(&["--format=%'f", "--to=si"]) |
| 1301 |
.fails_with_code(1) |
| 1302 |
.stderr_contains("grouping cannot be combined with --to"); |
| 1303 |
} |
| 1304 |
|
| 1305 |
#[test] |
| 1306 |
fn test_grouping_conflicts_with_format_option() { |
| 1307 |
new_ucmd!() |
| 1308 |
.args(&["--format=%f", "--grouping"]) |
| 1309 |
.fails_with_code(1) |
| 1310 |
.stderr_contains("--grouping cannot be combined with --format"); |
| 1311 |
} |
| 1312 |
|
| 1313 |
#[test] |
| 1314 |
fn test_zero_terminated_command_line_args() { |
| 1315 |
new_ucmd!() |
| 1316 |
.args(&["--zero-terminated", "--to=si", "1000"]) |
| 1317 |
.succeeds() |
| 1318 |
.stdout_is("1.0k\x00"); |
| 1319 |
|
| 1320 |
new_ucmd!() |
| 1321 |
.args(&["-z", "--to=si", "1000"]) |
| 1322 |
.succeeds() |
| 1323 |
.stdout_is("1.0k\x00"); |
| 1324 |
|
| 1325 |
new_ucmd!() |
| 1326 |
.args(&["-z", "--to=si", "1000", "2000"]) |
| 1327 |
.succeeds() |
| 1328 |
.stdout_is("1.0k\x002.0k\x00"); |
| 1329 |
} |
| 1330 |
|
| 1331 |
#[test] |
| 1332 |
fn test_zero_terminated_input() { |
| 1333 |
let values = vec![ |
| 1334 |
("1000", "1.0k"), |
| 1335 |
("1000\x00", "1.0k\x00"), |
| 1336 |
("1000\x002000\x00", "1.0k\x002.0k\x00"), |
| 1337 |
]; |
| 1338 |
|
| 1339 |
for (input, expected) in values { |
| 1340 |
new_ucmd!() |
| 1341 |
.args(&["-z", "--to=si"]) |
| 1342 |
.pipe_in(input) |
| 1343 |
.succeeds() |
| 1344 |
.stdout_is(expected); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
#[test] |
| 1349 |
fn test_zero_terminated_embedded_newline() { |
| 1350 |
new_ucmd!() |
| 1351 |
.args(&["-z", "--from=si", "--field=-"]) |
| 1352 |
.pipe_in("1K\n2K\x003K\n4K\x00") |
| 1353 |
.succeeds() |
| 1354 |
// Newlines get replaced by a single space |
| 1355 |
.stdout_is("1000 2000\x003000 4000\x00"); |
| 1356 |
} |
| 1357 |
|
| 1358 |
#[cfg(unix)] |
| 1359 |
#[test] |
| 1360 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 1361 |
fn test_non_utf8_delimiter() { |
| 1362 |
use std::ffi::OsStr; |
| 1363 |
use std::os::unix::ffi::OsStrExt; |
| 1364 |
|
| 1365 |
// Single-byte non-UTF8 (0xFF) and multi-byte (0xA2E3, e.g. GB18030) |
| 1366 |
for delim in [&[0xFFu8][..], &[0xA2, 0xE3]] { |
| 1367 |
let input: Vec<u8> = [b"1", delim, b"2K"].concat(); |
| 1368 |
let expected: Vec<u8> = [b"1", delim, b"2000\n"].concat(); |
| 1369 |
new_ucmd!() |
| 1370 |
.args(&["--from=si", "--field=2", "-d"]) |
| 1371 |
.arg(OsStr::from_bytes(delim)) |
| 1372 |
.arg(OsStr::from_bytes(&input)) |
| 1373 |
.succeeds() |
| 1374 |
.stdout_is_bytes(expected); |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
#[test] |
| 1379 |
fn test_unit_separator() { |
| 1380 |
for (args, expected) in [ |
| 1381 |
(&["--to=si", "--unit-separator= ", "1000"][..], "1.0 k\n"), |
| 1382 |
(&["--to=iec", "--unit-separator= ", "1024"], "1.0 K\n"), |
| 1383 |
(&["--to=iec-i", "--unit-separator= ", "2048"], "2.0 Ki\n"), |
| 1384 |
(&["--to=si", "--unit-separator=__", "1000"], "1.0__k\n"), |
| 1385 |
(&["--to=si", "--unit-separator= ", "500"], "500\n"), // no unit = no separator |
| 1386 |
] { |
| 1387 |
new_ucmd!().args(args).succeeds().stdout_only(expected); |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
#[test] |
| 1392 |
fn test_debug_warnings() { |
| 1393 |
new_ucmd!() |
| 1394 |
.args(&["--debug", "4096"]) |
| 1395 |
.succeeds() |
| 1396 |
.stdout_is("4096\n") |
| 1397 |
.stderr_is("numfmt: no conversion option specified\n"); |
| 1398 |
|
| 1399 |
new_ucmd!() |
| 1400 |
.args(&["--debug", "--padding=10", "4096"]) |
| 1401 |
.succeeds() |
| 1402 |
.stdout_only(" 4096\n"); |
| 1403 |
|
| 1404 |
new_ucmd!() |
| 1405 |
.args(&["--debug", "--header", "--to=iec", "4096"]) |
| 1406 |
.succeeds() |
| 1407 |
.stdout_is("4.0K\n") |
| 1408 |
.stderr_is("numfmt: --header ignored with command-line input\n"); |
| 1409 |
|
| 1410 |
new_ucmd!() |
| 1411 |
.env("LC_ALL", "C") |
| 1412 |
.args(&["--debug", "--grouping", "--from=si", "4.0K"]) |
| 1413 |
.succeeds() |
| 1414 |
.stdout_is("4000\n") |
| 1415 |
.stderr_is("numfmt: grouping has no effect in this locale\n"); |
| 1416 |
} |
| 1417 |
|
| 1418 |
#[test] |
| 1419 |
fn test_debug_reports_failed_conversions_summary() { |
| 1420 |
new_ucmd!() |
| 1421 |
.args(&[ |
| 1422 |
"--invalid=fail", |
| 1423 |
"--debug", |
| 1424 |
"--to=si", |
| 1425 |
"1000", |
| 1426 |
"Foo", |
| 1427 |
"3000", |
| 1428 |
]) |
| 1429 |
.fails_with_code(2) |
| 1430 |
.stdout_is("1.0k\nFoo\n3.0k\n") |
| 1431 |
.stderr_is( |
| 1432 |
"numfmt: invalid number: 'Foo'\nnumfmt: failed to convert some of the input numbers\n", |
| 1433 |
); |
| 1434 |
} |
| 1435 |
|
| 1436 |
#[test] |
| 1437 |
fn test_invalid_fail_with_fields_does_not_duplicate_output() { |
| 1438 |
new_ucmd!() |
| 1439 |
.args(&["--invalid=fail", "--field=2", "--from=si", "--to=iec"]) |
| 1440 |
.pipe_in("A 1K x\nB Foo y\nC 3G z\n") |
| 1441 |
.fails_with_code(2) |
| 1442 |
.stdout_is("A 1000 x\nB Foo y\nC 2.8G z\n") |
| 1443 |
.stderr_is("numfmt: invalid number: 'Foo'\n"); |
| 1444 |
} |
| 1445 |
|
| 1446 |
#[test] |
| 1447 |
fn test_abort_with_fields_preserves_partial_output() { |
| 1448 |
new_ucmd!() |
| 1449 |
.args(&["--field=3", "--from=auto", "Hello 40M World 90G"]) |
| 1450 |
.fails_with_code(2) |
| 1451 |
.stdout_is("Hello 40M ") |
| 1452 |
.stderr_is("numfmt: invalid number: 'World'\n"); |
| 1453 |
} |
| 1454 |
|
| 1455 |
#[test] |
| 1456 |
fn test_rejects_malformed_number_forms() { |
| 1457 |
new_ucmd!() |
| 1458 |
.args(&["--from=si", "12.K"]) |
| 1459 |
.fails_with_code(2) |
| 1460 |
.stderr_contains("invalid number: '12.K'"); |
| 1461 |
|
| 1462 |
new_ucmd!() |
| 1463 |
.args(&["--from=si", "--delimiter=,", "12. 2"]) |
| 1464 |
.fails_with_code(2) |
| 1465 |
.stderr_contains("invalid number: '12. 2'"); |
| 1466 |
|
| 1467 |
new_ucmd!() |
| 1468 |
.arg("..1") |
| 1469 |
.fails_with_code(2) |
| 1470 |
.stderr_contains("invalid suffix in input: '..1'"); |
| 1471 |
} |
| 1472 |
|
| 1473 |
#[test] |
| 1474 |
fn test_empty_delimiter_success() { |
| 1475 |
for (args, expected) in [ |
| 1476 |
// Single space between number and suffix is allowed by default |
| 1477 |
(&["-d", "", "--from=si", "4.0 K"][..], "4000\n"), |
| 1478 |
// Trailing spaces without suffix are allowed |
| 1479 |
(&["-d", "", "--from=si", "4 "], "4\n"), |
| 1480 |
(&["-d", "", "--from=auto", "2 "], "2\n"), |
| 1481 |
(&["-d", "", "--from=auto", "2 "], "2\n"), |
| 1482 |
// Trailing space after suffix is allowed |
| 1483 |
(&["-d", "", "--from=auto", "2K "], "2000\n"), |
| 1484 |
// Explicit --unit-separator=" " allows single space |
| 1485 |
( |
| 1486 |
&["-d", "", "--from=si", "--unit-separator= ", "1 K"], |
| 1487 |
"1000\n", |
| 1488 |
), |
| 1489 |
( |
| 1490 |
&["-d", "", "--from=iec", "--unit-separator= ", "2 M"], |
| 1491 |
"2097152\n", |
| 1492 |
), |
| 1493 |
] { |
| 1494 |
new_ucmd!().args(args).succeeds().stdout_only(expected); |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 1498 |
#[test] |
| 1499 |
fn test_empty_delimiter_multi_char_unit_separator() { |
| 1500 |
// Two-space unit separator allows two spaces between number and suffix |
| 1501 |
new_ucmd!() |
| 1502 |
.args(&["-d", "", "--from=si", "--unit-separator= "]) |
| 1503 |
.pipe_in("1 K\n2 M\n3 G\n") |
| 1504 |
.succeeds() |
| 1505 |
.stdout_only("1000\n2000000\n3000000000\n"); |
| 1506 |
} |
| 1507 |
|
| 1508 |
#[test] |
| 1509 |
fn test_whitespace_mode_parses_custom_unit_separator_inputs() { |
| 1510 |
new_ucmd!() |
| 1511 |
.args(&["--from=iec", "--unit-separator=::"]) |
| 1512 |
.pipe_in("4::K\n") |
| 1513 |
.succeeds() |
| 1514 |
.stdout_only("4096\n"); |
| 1515 |
|
| 1516 |
new_ucmd!() |
| 1517 |
.args(&["--from=iec", "--unit-separator=\u{a0}"]) |
| 1518 |
.pipe_in("4\u{a0}K\n") |
| 1519 |
.succeeds() |
| 1520 |
.stdout_only("4096\n"); |
| 1521 |
} |
| 1522 |
|
| 1523 |
#[test] |
| 1524 |
fn test_empty_delimiter_whitespace_rejection() { |
| 1525 |
new_ucmd!() |
| 1526 |
.args(&["-d", "", "--from=auto", "2 K"]) |
| 1527 |
.fails_with_code(2) |
| 1528 |
.stderr_contains("invalid suffix in input"); |
| 1529 |
|
| 1530 |
new_ucmd!() |
| 1531 |
.args(&["-d", "", "--from=si", "--unit-separator=", "1 K"]) |
| 1532 |
.fails_with_code(2) |
| 1533 |
.stderr_contains("invalid suffix in input"); |
| 1534 |
} |
| 1535 |
|
| 1536 |
#[test] |
| 1537 |
fn test_null_byte_input() { |
| 1538 |
new_ucmd!() |
| 1539 |
.pipe_in("1000\x00\n") |
| 1540 |
.succeeds() |
| 1541 |
.stdout_is("1000\n"); |
| 1542 |
|
| 1543 |
new_ucmd!().pipe_in("1000\x00").succeeds().stdout_is("1000"); |
| 1544 |
} |
| 1545 |
|
| 1546 |
#[test] |
| 1547 |
fn test_null_byte_input_multiline() { |
| 1548 |
new_ucmd!() |
| 1549 |
.pipe_in("1000\x00\n2000\x00") |
| 1550 |
.succeeds() |
| 1551 |
.stdout_is("1000\n2000"); |
| 1552 |
|
| 1553 |
new_ucmd!() |
| 1554 |
.pipe_in("1000\x002000\n3000") |
| 1555 |
.succeeds() |
| 1556 |
.stdout_is("1000\n3000"); |
| 1557 |
} |
| 1558 |
|
| 1559 |
// https://github.com/uutils/coreutils/issues/11653 |
| 1560 |
// GNU rejects `-9923868` as an invalid short option (leading `-9`) and |
| 1561 |
// requires `--` separator; uutils accepts it as a negative positional number. |
| 1562 |
#[test] |
| 1563 |
fn test_numfmt_negative_treated_as_option() { |
| 1564 |
new_ucmd!() |
| 1565 |
.args(&["--to=iec", "-8765432"]) |
| 1566 |
.fails_with_code(1) |
| 1567 |
.stderr_contains("unexpected argument"); |
| 1568 |
} |
| 1569 |
|
| 1570 |
// https://github.com/uutils/coreutils/issues/11653 |
| 1571 |
// GNU rejects `-9923868` as an invalid short option (leading `-9`) and |
| 1572 |
// requires `--` separator; uutils accepts it as a negative positional number. |
| 1573 |
#[test] |
| 1574 |
fn test_numfmt_negative_after_double_dash_ok() { |
| 1575 |
new_ucmd!() |
| 1576 |
.args(&["--to=iec", "--", "-8765432"]) |
| 1577 |
.succeeds() |
| 1578 |
.stdout_is("-8.4M\n"); |
| 1579 |
} |
| 1580 |
|
| 1581 |
// https://github.com/uutils/coreutils/issues/11654 |
| 1582 |
// uutils parses large integers through f64, losing precision past 2^53. |
| 1583 |
#[test] |
| 1584 |
fn test_large_integer_precision_loss_issue_11654() { |
| 1585 |
new_ucmd!() |
| 1586 |
.args(&["--from=iec", "9153396227555392131"]) |
| 1587 |
.succeeds() |
| 1588 |
.stdout_is("9153396227555392131\n"); |
| 1589 |
} |
| 1590 |
|
| 1591 |
// https://github.com/uutils/coreutils/issues/11655 |
| 1592 |
// uutils accepts scientific notation (`1e9`, `5e-3`, ...); GNU rejects it |
| 1593 |
// as "invalid suffix in input". |
| 1594 |
#[test] |
| 1595 |
fn test_numfmt_scientific_notation_rejected() { |
| 1596 |
new_ucmd!() |
| 1597 |
.arg("2e8") |
| 1598 |
.fails_with_code(2) |
| 1599 |
.stderr_contains("invalid suffix in input"); |
| 1600 |
} |
| 1601 |
|
| 1602 |
#[test] |
| 1603 |
fn test_to_auto_rejected_at_parse_time() { |
| 1604 |
new_ucmd!() |
| 1605 |
.args(&["--to=auto", "100"]) |
| 1606 |
.fails_with_code(1) |
| 1607 |
.stderr_contains("invalid argument 'auto' for '--to'"); |
| 1608 |
} |
| 1609 |
|
| 1610 |
// https://github.com/uutils/coreutils/issues/11663 |
| 1611 |
// `--from-unit` multiplication with fractional input rounds to an integer; |
| 1612 |
// GNU preserves the fractional digits. |
| 1613 |
#[test] |
| 1614 |
fn test_from_unit_fractional_precision_issue_11663() { |
| 1615 |
new_ucmd!() |
| 1616 |
.args(&["--from=iec", "--from-unit=959", "--", "-615484.454"]) |
| 1617 |
.succeeds() |
| 1618 |
.stdout_is("-590249591.386\n"); |
| 1619 |
} |
| 1620 |
|
| 1621 |
// https://github.com/uutils/coreutils/issues/11664 |
| 1622 |
// Zero-padded `--format` places padding zeros before the sign for negative |
| 1623 |
// numbers; GNU (and C printf) puts the sign first. |
| 1624 |
#[test] |
| 1625 |
fn test_zero_pad_sign_order_issue_11664() { |
| 1626 |
new_ucmd!() |
| 1627 |
.args(&["--from=none", "--format=%018.2f", "--", "-9869647"]) |
| 1628 |
.succeeds() |
| 1629 |
.stdout_is("-00000009869647.00\n"); |
| 1630 |
} |
| 1631 |
|
| 1632 |
#[test] |
| 1633 |
fn test_to_unit_prefix_selection() { |
| 1634 |
new_ucmd!() |
| 1635 |
.args(&["--to=iec-i", "--to-unit=885", "100000"]) |
| 1636 |
.succeeds() |
| 1637 |
.stdout_is("113\n"); |
| 1638 |
} |
| 1639 |
|
| 1640 |
// https://github.com/uutils/coreutils/issues/11667 |
| 1641 |
// `--format='%.0f'` with `--to=<scale>` still prints one fractional digit; |
| 1642 |
// the precision specifier `.0` is ignored. |
| 1643 |
#[test] |
| 1644 |
fn test_format_precision_zero_with_to_scale_issue_11667() { |
| 1645 |
new_ucmd!() |
| 1646 |
.args(&["--to=iec", "--format=%.0f", "5183776"]) |
| 1647 |
.succeeds() |
| 1648 |
.stdout_is("5M\n"); |
| 1649 |
} |
| 1650 |
|
| 1651 |
#[test] |
| 1652 |
fn test_invalid_utf8_input() { |
| 1653 |
// 0xFF is invalid UTF-8 |
| 1654 |
new_ucmd!() |
| 1655 |
.pipe_in(*b"10\n\xFF") |
| 1656 |
.fails_with_code(2) |
| 1657 |
.stdout_is("10\n") |
| 1658 |
.stderr_is("numfmt: invalid number: '\\377'\n"); |
| 1659 |
} |
| 1660 |
|
| 1661 |
#[test] |
| 1662 |
fn test_format_value_too_large_issue_11936() { |
| 1663 |
// value * 10^precision needing 20+ digits should be rejected |
| 1664 |
let cases = [ |
| 1665 |
(vec!["--format=%5.1f", "1000000000000000000"], "1e+18/1"), |
| 1666 |
(vec!["--format=%.2f", "100000000000000000"], "1e+17/2"), |
| 1667 |
(vec!["--format=%.3f", "10000000000000000"], "1e+16/3"), |
| 1668 |
]; |
| 1669 |
for (args, hint) in cases { |
| 1670 |
new_ucmd!() |
| 1671 |
.args(&args) |
| 1672 |
.fails_with_code(2) |
| 1673 |
.stderr_contains("value/precision too large") |
| 1674 |
.stderr_contains(hint); |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
#[test] |
| 1679 |
fn test_format_value_below_large_threshold_ok() { |
| 1680 |
// one below the cutoff still formats |
| 1681 |
new_ucmd!() |
| 1682 |
.args(&["--format=%5.1f", "999999999999999999"]) |
| 1683 |
.succeeds() |
| 1684 |
.stdout_is("999999999999999999.0\n"); |
| 1685 |
} |
| 1686 |
|
| 1687 |
#[test] |
| 1688 |
#[cfg_attr(wasi_runner, ignore = "WASI: locale env vars not propagated")] |
| 1689 |
fn test_locale_fr_output() { |
| 1690 |
// Output uses the locale separator |
| 1691 |
new_ucmd!() |
| 1692 |
.env("LC_ALL", "fr_FR.UTF-8") |
| 1693 |
.args(&["--to=iec", "1500"]) |
| 1694 |
.succeeds() |
| 1695 |
.stdout_is("1,5K\n"); |
| 1696 |
} |
| 1697 |
|
| 1698 |
#[test] |
| 1699 |
#[cfg_attr(wasi_runner, ignore = "WASI: locale env vars not propagated")] |
| 1700 |
fn test_locale_fr_input_comma() { |
| 1701 |
// fr_FR should take '1,5' as a number |
| 1702 |
new_ucmd!() |
| 1703 |
.env("LC_ALL", "fr_FR.UTF-8") |
| 1704 |
.args(&["--format=%.3f", "1,5"]) |
| 1705 |
.succeeds() |
| 1706 |
.stdout_is("1,500\n"); |
| 1707 |
} |
| 1708 |
|
| 1709 |
#[test] |
| 1710 |
#[cfg_attr(wasi_runner, ignore = "WASI: locale env vars not propagated")] |
| 1711 |
fn test_locale_fr_rejects_period() { |
| 1712 |
// '.' isn't valid in fr_FR, should bail |
| 1713 |
new_ucmd!() |
| 1714 |
.env("LC_ALL", "fr_FR.UTF-8") |
| 1715 |
.args(&["--format=%.3f", "1.5"]) |
| 1716 |
.fails() |
| 1717 |
.stderr_contains("invalid"); |
| 1718 |
} |
| 1719 |
|
| 1720 |
#[test] |
| 1721 |
fn test_locale_c_uses_period() { |
| 1722 |
// C locale should still use '.' as usual |
| 1723 |
new_ucmd!() |
| 1724 |
.env("LC_ALL", "C") |
| 1725 |
.args(&["--to=iec", "1500"]) |
| 1726 |
.succeeds() |
| 1727 |
.stdout_is("1.5K\n"); |
| 1728 |
} |
| 1729 |
|
| 1730 |
// https://github.com/uutils/coreutils/issues/11935 |
| 1731 |
// the rejection path bypasses --invalid=warn/ignore/fail handling |
| 1732 |
#[test] |
| 1733 |
fn test_ignores_invalid_mode_issue11935() { |
| 1734 |
new_ucmd!() |
| 1735 |
.args(&["--invalid=warn", "100", "1e5", "200"]) |
| 1736 |
.succeeds() |
| 1737 |
.stderr_is("numfmt: invalid suffix in input: '1e5'\n") |
| 1738 |
.stdout_is("100\n1e5\n200\n"); |
| 1739 |
} |
| 1740 |
|
| 1741 |
#[test] |
| 1742 |
fn test_iec_format_precision_cap() { |
| 1743 |
// gnu zero pads after 3 decimals on iec |
| 1744 |
let cases = [ |
| 1745 |
("1500", "1.46500K"), |
| 1746 |
("999999", "976.56200K"), |
| 1747 |
("310174", "302.90500K"), |
| 1748 |
]; |
| 1749 |
for (input, expected) in cases { |
| 1750 |
new_ucmd!() |
| 1751 |
.args(&["--to=iec", "--format=%.5f", input]) |
| 1752 |
.succeeds() |
| 1753 |
.stdout_is(format!("{expected}\n")); |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
#[test] |
| 1758 |
fn test_si_format_precision_no_cap() { |
| 1759 |
// si shouldn't get the cap, full precision |
| 1760 |
new_ucmd!() |
| 1761 |
.args(&["--to=si", "--format=%.5f", "1234567"]) |
| 1762 |
.succeeds() |
| 1763 |
.stdout_is("1.23457M\n"); |
| 1764 |
} |
| 1765 |
|
| 1766 |
// https://github.com/uutils/coreutils/issues/11937 |
| 1767 |
// numfmt: --format width accounting diverges from GNU for multi-byte --suffix |
| 1768 |
#[test] |
| 1769 |
fn test_multibyte_suffix_issue11937() { |
| 1770 |
new_ucmd!() |
| 1771 |
.args(&["--suffix=€", "--format=%10.2f", "692"]) |
| 1772 |
.succeeds() |
| 1773 |
.stdout_is(" 692.00€\n"); |
| 1774 |
} |
| 1775 |
|
| 1776 |
#[test] |
| 1777 |
fn test_float_precision_greater_than_16bits() { |
| 1778 |
new_ucmd!() |
| 1779 |
.args(&["--to=iec", "--format=%.65536f", "1"]) |
| 1780 |
.succeeds() |
| 1781 |
.stdout_is("1\n"); |
| 1782 |
} |
| 1783 |
|
| 1784 |
#[test] |
| 1785 |
fn test_float_precision_greater_than_16bits_without_scaling() { |
| 1786 |
new_ucmd!() |
| 1787 |
.args(&["--format=%.65536f", "1.5"]) |
| 1788 |
.succeeds() |
| 1789 |
.stdout_is("2\n"); |
| 1790 |
} |
| 1791 |
|
| 1792 |
#[test] |
| 1793 |
fn test_float_precision_greater_than_16bits_with_suffix() { |
| 1794 |
new_ucmd!() |
| 1795 |
.args(&["--to=si", "--format=%.65536f", "1000000"]) |
| 1796 |
.succeeds() |
| 1797 |
.stdout_is("1M\n"); |
| 1798 |
} |
| 1799 |
|
| 1800 |
#[test] |
| 1801 |
fn test_format_precision_too_large_on_zero() { |
| 1802 |
new_ucmd!() |
| 1803 |
.args(&["--format=%.40f", "0"]) |
| 1804 |
.fails_with_code(2) |
| 1805 |
.stderr_only( |
| 1806 |
"numfmt: value/precision too large to be printed: '0e+0/40' (consider using --to)\n", |
| 1807 |
); |
| 1808 |
new_ucmd!() |
| 1809 |
.args(&["--format=%.18f", "0"]) |
| 1810 |
.succeeds() |
| 1811 |
.stdout_only("0.000000000000000000\n"); |
| 1812 |
} |
| 1813 |
|
| 1814 |
// https://github.com/uutils/coreutils/issues/13272 |
| 1815 |
// argument following `--header` must not be interpreted as the option's value |
| 1816 |
#[test] |
| 1817 |
fn test_header_detached() { |
| 1818 |
new_ucmd!() |
| 1819 |
.args(&["--header", "1", "2"]) |
| 1820 |
.succeeds() |
| 1821 |
.stdout_is("1\n2\n"); |
| 1822 |
} |
| 1823 |
|
| 1824 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 1825 |
mod diagnostics { |
| 1826 |
use super::*; |
| 1827 |
|
| 1828 |
#[cfg(unix)] |
| 1829 |
#[test] |
| 1830 |
fn test_snippet_points_at_the_bad_directive() { |
| 1831 |
let result = new_ucmd!() |
| 1832 |
.terminal_sim_stderr() |
| 1833 |
.args(&["--format=%q", "1000"]) |
| 1834 |
.fails_with_code(1); |
| 1835 |
|
| 1836 |
// The whole report: the `numfmt: ` prefix of the plain form, the |
| 1837 |
// argument list echoed back, a caret on the `q` inside `--format=%q`, |
| 1838 |
// and the format syntax advice. |
| 1839 |
assert_eq!( |
| 1840 |
result.stderr_as_displayed(), |
| 1841 |
"\ |
| 1842 |
numfmt: invalid format '%q', directive must be %[0]['][-][N][.][N]f |
| 1843 |
╭─[ numfmt:1:18 ] |
| 1844 |
│ |
| 1845 |
1 │ numfmt --format=%q 1000 |
| 1846 |
│ ┬ |
| 1847 |
│ ╰── f is the only conversion numfmt has; %d, %e, %g and the other C conversions are not accepted |
| 1848 |
│ |
| 1849 |
│ Help: a format is [PREFIX]%[0]['][-][WIDTH][.PRECISION]f[SUFFIX], as in \"%'-10.2f\" |
| 1850 |
───╯" |
| 1851 |
); |
| 1852 |
} |
| 1853 |
|
| 1854 |
#[cfg(unix)] |
| 1855 |
#[test] |
| 1856 |
fn test_snippet_points_at_the_stray_percent() { |
| 1857 |
let result = new_ucmd!() |
| 1858 |
.terminal_sim_stderr() |
| 1859 |
.args(&["--format=%f%", "1000"]) |
| 1860 |
.fails_with_code(1); |
| 1861 |
|
| 1862 |
assert_eq!( |
| 1863 |
result.stderr_as_displayed(), |
| 1864 |
"\ |
| 1865 |
numfmt: format '%f%' has too many % directives |
| 1866 |
╭─[ numfmt:1:19 ] |
| 1867 |
│ |
| 1868 |
1 │ numfmt --format=%f% 1000 |
| 1869 |
│ ┬ |
| 1870 |
│ ╰── a literal % must be written %% |
| 1871 |
│ |
| 1872 |
│ Help: a format is [PREFIX]%[0]['][-][WIDTH][.PRECISION]f[SUFFIX], as in \"%'-10.2f\" |
| 1873 |
───╯" |
| 1874 |
); |
| 1875 |
} |
| 1876 |
|
| 1877 |
#[cfg(unix)] |
| 1878 |
#[test] |
| 1879 |
fn test_snippet_underlines_the_oversized_precision() { |
| 1880 |
let result = new_ucmd!() |
| 1881 |
.terminal_sim_stderr() |
| 1882 |
.args(&["--format=%.88888888888888888888f", "1000"]) |
| 1883 |
.fails_with_code(1); |
| 1884 |
|
| 1885 |
// The underline covers the whole run of digits, not one character. |
| 1886 |
assert_eq!( |
| 1887 |
result.stderr_as_displayed(), |
| 1888 |
"\ |
| 1889 |
numfmt: invalid precision in format '%.88888888888888888888f' |
| 1890 |
╭─[ numfmt:1:19 ] |
| 1891 |
│ |
| 1892 |
1 │ numfmt --format=%.88888888888888888888f 1000 |
| 1893 |
│ ──────────┬───────── |
| 1894 |
│ ╰─────────── this number is too large |
| 1895 |
│ |
| 1896 |
│ Help: a format is [PREFIX]%[0]['][-][WIDTH][.PRECISION]f[SUFFIX], as in \"%'-10.2f\" |
| 1897 |
───╯" |
| 1898 |
); |
| 1899 |
} |
| 1900 |
|
| 1901 |
#[cfg(unix)] |
| 1902 |
#[test] |
| 1903 |
fn test_snippet_marks_a_format_without_a_directive() { |
| 1904 |
let result = new_ucmd!() |
| 1905 |
.terminal_sim_stderr() |
| 1906 |
.args(&["--format=qwe", "1000"]) |
| 1907 |
.fails_with_code(1); |
| 1908 |
|
| 1909 |
// Nothing inside is wrong on its own, so the whole format is marked. |
| 1910 |
assert_eq!( |
| 1911 |
result.stderr_as_displayed(), |
| 1912 |
"\ |
| 1913 |
numfmt: format 'qwe' has no % directive |
| 1914 |
╭─[ numfmt:1:17 ] |
| 1915 |
│ |
| 1916 |
1 │ numfmt --format=qwe 1000 |
| 1917 |
│ ─── |
| 1918 |
│ |
| 1919 |
│ Help: a format is [PREFIX]%[0]['][-][WIDTH][.PRECISION]f[SUFFIX], as in \"%'-10.2f\" |
| 1920 |
───╯" |
| 1921 |
); |
| 1922 |
} |
| 1923 |
|
| 1924 |
#[cfg(unix)] |
| 1925 |
#[test] |
| 1926 |
fn test_snippet_underlines_an_unknown_unit() { |
| 1927 |
let result = new_ucmd!() |
| 1928 |
.terminal_sim_stderr() |
| 1929 |
.args(&["--from=xyz", "1000"]) |
| 1930 |
.fails_with_code(1); |
| 1931 |
|
| 1932 |
// The value is wrong as a whole, so it is underlined as a whole and |
| 1933 |
// the help lists what --from would have taken instead. |
| 1934 |
assert_eq!( |
| 1935 |
result.stderr_as_displayed(), |
| 1936 |
"\ |
| 1937 |
numfmt: invalid argument 'xyz' for '--from' |
| 1938 |
╭─[ numfmt:1:15 ] |
| 1939 |
│ |
| 1940 |
1 │ numfmt --from=xyz 1000 |
| 1941 |
│ ─── |
| 1942 |
│ |
| 1943 |
│ Help: --from and --to take none, si, iec or iec-i, and --from also takes auto |
| 1944 |
───╯" |
| 1945 |
); |
| 1946 |
} |
| 1947 |
|
| 1948 |
#[cfg(unix)] |
| 1949 |
#[test] |
| 1950 |
fn test_snippet_says_auto_belongs_to_from() { |
| 1951 |
let result = new_ucmd!() |
| 1952 |
.terminal_sim_stderr() |
| 1953 |
.args(&["--to", "auto", "1000"]) |
| 1954 |
.fails_with_code(1); |
| 1955 |
|
| 1956 |
// 'auto' exists, it is just not a unit --to can scale to; the label |
| 1957 |
// says so rather than leaving the help to imply it is unknown. |
| 1958 |
assert_eq!( |
| 1959 |
result.stderr_as_displayed(), |
| 1960 |
"\ |
| 1961 |
numfmt: invalid argument 'auto' for '--to' |
| 1962 |
╭─[ numfmt:1:13 ] |
| 1963 |
│ |
| 1964 |
1 │ numfmt --to auto 1000 |
| 1965 |
│ ──┬─ |
| 1966 |
│ ╰─── auto guesses the unit of the input, so only --from takes it |
| 1967 |
│ |
| 1968 |
│ Help: --from and --to take none, si, iec or iec-i, and --from also takes auto |
| 1969 |
───╯" |
| 1970 |
); |
| 1971 |
} |
| 1972 |
|
| 1973 |
#[cfg(unix)] |
| 1974 |
#[test] |
| 1975 |
fn test_snippet_underlines_an_unknown_unit_size() { |
| 1976 |
let result = new_ucmd!() |
| 1977 |
.terminal_sim_stderr() |
| 1978 |
.args(&["--from-unit=Q", "1000"]) |
| 1979 |
.fails_with_code(1); |
| 1980 |
|
| 1981 |
assert_eq!( |
| 1982 |
result.stderr_as_displayed(), |
| 1983 |
"\ |
| 1984 |
numfmt: invalid unit size: 'Q' |
| 1985 |
╭─[ numfmt:1:20 ] |
| 1986 |
│ |
| 1987 |
1 │ numfmt --from-unit=Q 1000 |
| 1988 |
│ ─ |
| 1989 |
│ |
| 1990 |
│ Help: a unit size is a number, a K, M, G, T, P or E multiplier, or both, as in 512, K or 2Ki |
| 1991 |
───╯" |
| 1992 |
); |
| 1993 |
} |
| 1994 |
|
| 1995 |
#[cfg(unix)] |
| 1996 |
#[test] |
| 1997 |
fn test_snippet_says_a_unit_size_cannot_be_zero() { |
| 1998 |
let result = new_ucmd!() |
| 1999 |
.terminal_sim_stderr() |
| 2000 |
.args(&["--to-unit", "0", "1000"]) |
| 2001 |
.fails_with_code(1); |
| 2002 |
|
| 2003 |
// A zero is spelled like a valid size, so the label says what the |
| 2004 |
// syntax in the help cannot. |
| 2005 |
assert_eq!( |
| 2006 |
result.stderr_as_displayed(), |
| 2007 |
"\ |
| 2008 |
numfmt: invalid unit size: '0' |
| 2009 |
╭─[ numfmt:1:18 ] |
| 2010 |
│ |
| 2011 |
1 │ numfmt --to-unit 0 1000 |
| 2012 |
│ ┬ |
| 2013 |
│ ╰── a unit size must be at least 1 |
| 2014 |
│ |
| 2015 |
│ Help: a unit size is a number, a K, M, G, T, P or E multiplier, or both, as in 512, K or 2Ki |
| 2016 |
───╯" |
| 2017 |
); |
| 2018 |
} |
| 2019 |
|
| 2020 |
#[cfg(unix)] |
| 2021 |
#[test] |
| 2022 |
fn test_snippet_says_why_a_zero_padding_is_refused() { |
| 2023 |
let result = new_ucmd!() |
| 2024 |
.terminal_sim_stderr() |
| 2025 |
.args(&["--padding=0", "1000"]) |
| 2026 |
.fails_with_code(1); |
| 2027 |
|
| 2028 |
assert_eq!( |
| 2029 |
result.stderr_as_displayed(), |
| 2030 |
"\ |
| 2031 |
numfmt: invalid padding value '0' |
| 2032 |
╭─[ numfmt:1:18 ] |
| 2033 |
│ |
| 2034 |
1 │ numfmt --padding=0 1000 |
| 2035 |
│ ┬ |
| 2036 |
│ ╰── a padding is a width in characters, so 0 asks for nothing |
| 2037 |
│ |
| 2038 |
│ Help: --padding takes a non-zero whole number; a negative one left-aligns, as in --padding=-10 |
| 2039 |
───╯" |
| 2040 |
); |
| 2041 |
} |
| 2042 |
|
| 2043 |
#[cfg(unix)] |
| 2044 |
#[test] |
| 2045 |
fn test_snippet_underlines_a_header_count() { |
| 2046 |
let result = new_ucmd!() |
| 2047 |
.terminal_sim_stderr() |
| 2048 |
.args(&["--header=x", "1000"]) |
| 2049 |
.fails_with_code(1); |
| 2050 |
|
| 2051 |
// Nothing about 'x' is worth a label the message does not already |
| 2052 |
// carry, so the caret is bare and the help says what is taken. |
| 2053 |
assert_eq!( |
| 2054 |
result.stderr_as_displayed(), |
| 2055 |
"\ |
| 2056 |
numfmt: invalid header value 'x' |
| 2057 |
╭─[ numfmt:1:17 ] |
| 2058 |
│ |
| 2059 |
1 │ numfmt --header=x 1000 |
| 2060 |
│ ─ |
| 2061 |
│ |
| 2062 |
│ Help: --header takes the number of leading lines to pass through unchanged, at least 1 |
| 2063 |
───╯" |
| 2064 |
); |
| 2065 |
} |
| 2066 |
|
| 2067 |
#[cfg(unix)] |
| 2068 |
#[test] |
| 2069 |
fn test_snippet_points_at_the_suffix_of_a_bad_number() { |
| 2070 |
let result = new_ucmd!() |
| 2071 |
.terminal_sim_stderr() |
| 2072 |
.args(&["--to=si", "12abc"]) |
| 2073 |
.fails_with_code(2); |
| 2074 |
|
| 2075 |
// The number numfmt could read is left alone; the caret starts where |
| 2076 |
// it stopped. |
| 2077 |
assert_eq!( |
| 2078 |
result.stderr_as_displayed(), |
| 2079 |
"\ |
| 2080 |
numfmt: invalid suffix in input: '12abc' |
| 2081 |
╭─[ numfmt:1:18 ] |
| 2082 |
│ |
| 2083 |
1 │ numfmt --to=si 12abc |
| 2084 |
│ ─── |
| 2085 |
│ |
| 2086 |
│ Help: without --from a number must be plain; --from=auto reads a K, M or Gi suffix |
| 2087 |
───╯" |
| 2088 |
); |
| 2089 |
} |
| 2090 |
|
| 2091 |
#[cfg(unix)] |
| 2092 |
#[test] |
| 2093 |
fn test_snippet_marks_an_input_that_is_not_a_number() { |
| 2094 |
let result = new_ucmd!() |
| 2095 |
.terminal_sim_stderr() |
| 2096 |
.args(&["abc"]) |
| 2097 |
.fails_with_code(2); |
| 2098 |
|
| 2099 |
// Nothing of it parsed, so all of it is marked, and advice about |
| 2100 |
// suffixes would be beside the point. |
| 2101 |
assert_eq!( |
| 2102 |
result.stderr_as_displayed(), |
| 2103 |
"\ |
| 2104 |
numfmt: invalid number: 'abc' |
| 2105 |
╭─[ numfmt:1:8 ] |
| 2106 |
│ |
| 2107 |
1 │ numfmt abc |
| 2108 |
│ ─── |
| 2109 |
───╯" |
| 2110 |
); |
| 2111 |
} |
| 2112 |
|
| 2113 |
#[cfg(unix)] |
| 2114 |
#[test] |
| 2115 |
fn test_snippet_points_at_the_i_a_unit_does_not_take() { |
| 2116 |
let result = new_ucmd!() |
| 2117 |
.terminal_sim_stderr() |
| 2118 |
.args(&["--from=si", "1Ki"]) |
| 2119 |
.fails_with_code(2); |
| 2120 |
|
| 2121 |
assert_eq!( |
| 2122 |
result.stderr_as_displayed(), |
| 2123 |
"\ |
| 2124 |
numfmt: invalid suffix in input '1Ki': 'i' |
| 2125 |
╭─[ numfmt:1:20 ] |
| 2126 |
│ |
| 2127 |
1 │ numfmt --from=si 1Ki |
| 2128 |
│ ─ |
| 2129 |
│ |
| 2130 |
│ Help: the suffixes are K, M, G, T, P, E, Z, Y, R and Q, with an optional i under --from=auto or iec-i |
| 2131 |
───╯" |
| 2132 |
); |
| 2133 |
} |
| 2134 |
|
| 2135 |
#[cfg(unix)] |
| 2136 |
#[test] |
| 2137 |
fn test_input_from_stdin_keeps_its_plain_message() { |
| 2138 |
// There is no command line to point at, so the message stays as it is. |
| 2139 |
let result = new_ucmd!() |
| 2140 |
.terminal_sim_stderr() |
| 2141 |
.pipe_in("12abc\n") |
| 2142 |
.fails_with_code(2); |
| 2143 |
|
| 2144 |
assert_eq!( |
| 2145 |
result.stderr_str().trim_end(), |
| 2146 |
"numfmt: invalid suffix in input: '12abc'" |
| 2147 |
); |
| 2148 |
} |
| 2149 |
|
| 2150 |
#[cfg(unix)] |
| 2151 |
#[test] |
| 2152 |
fn test_a_line_of_fields_keeps_its_plain_message() { |
| 2153 |
// The failure is inside one field of the argument; the offsets are of |
| 2154 |
// the whole line, so no caret is drawn. |
| 2155 |
let result = new_ucmd!() |
| 2156 |
.terminal_sim_stderr() |
| 2157 |
.args(&["--field=2", "x 12abc"]) |
| 2158 |
.fails_with_code(2); |
| 2159 |
|
| 2160 |
assert_eq!( |
| 2161 |
result.stderr_str().trim_end(), |
| 2162 |
"numfmt: invalid suffix in input: '12abc'" |
| 2163 |
); |
| 2164 |
} |
| 2165 |
|
| 2166 |
#[cfg(unix)] |
| 2167 |
#[test] |
| 2168 |
fn test_other_option_errors_keep_their_plain_message() { |
| 2169 |
// The format is fine; the failure is elsewhere and must not be |
| 2170 |
// decorated with a caret into --format. The pseudo-terminal turns the |
| 2171 |
// newline into CRLF, hence the trim. |
| 2172 |
let result = new_ucmd!() |
| 2173 |
.terminal_sim_stderr() |
| 2174 |
.args(&["--format=%f", "--delimiter=ab", "1000"]) |
| 2175 |
.fails_with_code(1); |
| 2176 |
|
| 2177 |
assert_eq!( |
| 2178 |
result.stderr_str().trim_end(), |
| 2179 |
"numfmt: the delimiter must be a single character" |
| 2180 |
); |
| 2181 |
} |
| 2182 |
|
| 2183 |
#[cfg(unix)] |
| 2184 |
#[test] |
| 2185 |
fn test_snippet_ignores_another_option_with_the_same_value() { |
| 2186 |
// --delimiter carries the same text as --format; the caret must land |
| 2187 |
// inside --format, not the option that merely shares its suffix. |
| 2188 |
let result = new_ucmd!() |
| 2189 |
.terminal_sim_stderr() |
| 2190 |
.args(&["--delimiter=%q", "--format=%q", "1000"]) |
| 2191 |
.fails_with_code(1); |
| 2192 |
|
| 2193 |
assert_eq!( |
| 2194 |
result.stderr_as_displayed(), |
| 2195 |
"\ |
| 2196 |
numfmt: invalid format '%q', directive must be %[0]['][-][N][.][N]f |
| 2197 |
╭─[ numfmt:1:33 ] |
| 2198 |
│ |
| 2199 |
1 │ numfmt --delimiter=%q --format=%q 1000 |
| 2200 |
│ ┬ |
| 2201 |
│ ╰── f is the only conversion numfmt has; %d, %e, %g and the other C conversions are not accepted |
| 2202 |
│ |
| 2203 |
│ Help: a format is [PREFIX]%[0]['][-][WIDTH][.PRECISION]f[SUFFIX], as in \"%'-10.2f\" |
| 2204 |
───╯" |
| 2205 |
); |
| 2206 |
} |
| 2207 |
|
| 2208 |
#[cfg(unix)] |
| 2209 |
#[test] |
| 2210 |
fn test_snippet_marks_a_whitespace_only_number() { |
| 2211 |
// There is no number to point past, so the whitespace is marked whole |
| 2212 |
// rather than the two ends of it crossing. |
| 2213 |
let result = new_ucmd!() |
| 2214 |
.terminal_sim_stderr() |
| 2215 |
.args(&[" "]) |
| 2216 |
.fails_with_code(2); |
| 2217 |
|
| 2218 |
assert_eq!( |
| 2219 |
result.stderr_as_displayed(), |
| 2220 |
"\ |
| 2221 |
numfmt: invalid number: '' |
| 2222 |
╭─[ numfmt:1:9 ] |
| 2223 |
│ |
| 2224 |
1 │ numfmt ' ' |
| 2225 |
│ ─ |
| 2226 |
───╯" |
| 2227 |
); |
| 2228 |
} |
| 2229 |
|
| 2230 |
#[cfg(unix)] |
| 2231 |
#[test] |
| 2232 |
fn test_snippet_ignores_an_option_value_with_the_number_text() { |
| 2233 |
// --suffix takes a detached `q`, the same text as the operand; the |
| 2234 |
// caret must land on the operand, not on the value that precedes it. |
| 2235 |
let result = new_ucmd!() |
| 2236 |
.terminal_sim_stderr() |
| 2237 |
.args(&["--suffix", "q", "--from", "si", "q"]) |
| 2238 |
.fails_with_code(2); |
| 2239 |
|
| 2240 |
assert_eq!( |
| 2241 |
result.stderr_as_displayed(), |
| 2242 |
"\ |
| 2243 |
numfmt: invalid number: '' |
| 2244 |
╭─[ numfmt:1:29 ] |
| 2245 |
│ |
| 2246 |
1 │ numfmt --suffix q --from si q |
| 2247 |
│ ─ |
| 2248 |
───╯" |
| 2249 |
); |
| 2250 |
} |
| 2251 |
|
| 2252 |
#[cfg(unix)] |
| 2253 |
#[test] |
| 2254 |
fn test_snippet_says_nothing_about_suffixes_without_a_number() { |
| 2255 |
// A lone sign is the start of a number but not one yet, so advice about |
| 2256 |
// what may follow a number does not apply. |
| 2257 |
let result = new_ucmd!() |
| 2258 |
.terminal_sim_stderr() |
| 2259 |
.args(&["--from=si", "--", "-"]) |
| 2260 |
.fails_with_code(2); |
| 2261 |
|
| 2262 |
assert_eq!( |
| 2263 |
result.stderr_as_displayed(), |
| 2264 |
"\ |
| 2265 |
numfmt: invalid number: '-' |
| 2266 |
╭─[ numfmt:1:21 ] |
| 2267 |
│ |
| 2268 |
1 │ numfmt --from=si -- - |
| 2269 |
│ ─ |
| 2270 |
───╯" |
| 2271 |
); |
| 2272 |
} |
| 2273 |
|
| 2274 |
#[cfg(unix)] |
| 2275 |
#[test] |
| 2276 |
fn test_snippet_names_no_conversion_when_the_format_has_none() { |
| 2277 |
// Nothing stands where the conversion belongs, so there is no |
| 2278 |
// conversion to name — only a directive that never ends. |
| 2279 |
let result = new_ucmd!() |
| 2280 |
.terminal_sim_stderr() |
| 2281 |
.args(&["--format=%10", "1000"]) |
| 2282 |
.fails_with_code(1); |
| 2283 |
|
| 2284 |
assert_eq!( |
| 2285 |
result.stderr_as_displayed(), |
| 2286 |
"\ |
| 2287 |
numfmt: invalid format '%10', directive must be %[0]['][-][N][.][N]f |
| 2288 |
╭─[ numfmt:1:17 ] |
| 2289 |
│ |
| 2290 |
1 │ numfmt --format=%10 1000 |
| 2291 |
│ ─── |
| 2292 |
│ |
| 2293 |
│ Help: a format is [PREFIX]%[0]['][-][WIDTH][.PRECISION]f[SUFFIX], as in \"%'-10.2f\" |
| 2294 |
───╯" |
| 2295 |
); |
| 2296 |
} |
| 2297 |
|
| 2298 |
#[cfg(unix)] |
| 2299 |
#[test] |
| 2300 |
fn test_snippet_leaves_the_suffix_out_of_the_bad_conversion() { |
| 2301 |
// What follows the conversion would have been a valid suffix, so only |
| 2302 |
// the character standing in for `f` is underlined. |
| 2303 |
let result = new_ucmd!() |
| 2304 |
.terminal_sim_stderr() |
| 2305 |
.args(&["--format=%d bytes", "1000"]) |
| 2306 |
.fails_with_code(1); |
| 2307 |
|
| 2308 |
assert_eq!( |
| 2309 |
result.stderr_as_displayed(), |
| 2310 |
"\ |
| 2311 |
numfmt: invalid format '%d bytes', directive must be %[0]['][-][N][.][N]f |
| 2312 |
╭─[ numfmt:1:19 ] |
| 2313 |
│ |
| 2314 |
1 │ numfmt '--format=%d bytes' 1000 |
| 2315 |
│ ┬ |
| 2316 |
│ ╰── f is the only conversion numfmt has; %d, %e, %g and the other C conversions are not accepted |
| 2317 |
│ |
| 2318 |
│ Help: a format is [PREFIX]%[0]['][-][WIDTH][.PRECISION]f[SUFFIX], as in \"%'-10.2f\" |
| 2319 |
───╯" |
| 2320 |
); |
| 2321 |
} |
| 2322 |
|
| 2323 |
#[cfg(unix)] |
| 2324 |
#[test] |
| 2325 |
fn test_snippet_blames_the_suffix_not_the_zero_in_a_unit_size() { |
| 2326 |
// `0x` fails on the unknown `x`, not on the zero, so the label about a |
| 2327 |
// size of at least 1 would be beside the point. |
| 2328 |
let result = new_ucmd!() |
| 2329 |
.terminal_sim_stderr() |
| 2330 |
.args(&["--from-unit=0x", "1"]) |
| 2331 |
.fails_with_code(1); |
| 2332 |
|
| 2333 |
assert_eq!( |
| 2334 |
result.stderr_as_displayed(), |
| 2335 |
"\ |
| 2336 |
numfmt: invalid unit size: '0x' |
| 2337 |
╭─[ numfmt:1:20 ] |
| 2338 |
│ |
| 2339 |
1 │ numfmt --from-unit=0x 1 |
| 2340 |
│ ── |
| 2341 |
│ |
| 2342 |
│ Help: a unit size is a number, a K, M, G, T, P or E multiplier, or both, as in 512, K or 2Ki |
| 2343 |
───╯" |
| 2344 |
); |
| 2345 |
} |
| 2346 |
|
| 2347 |
#[test] |
| 2348 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 2349 |
// The test harness pipes stderr, so the report must not appear. |
| 2350 |
new_ucmd!() |
| 2351 |
.args(&["--format=%q", "1000"]) |
| 2352 |
.fails_with_code(1) |
| 2353 |
.stderr_only("numfmt: invalid format '%q', directive must be %[0]['][-][N][.][N]f\n"); |
| 2354 |
} |
| 2355 |
} |
| 2356 |
|
| 2357 |
#[cfg(unix)] |
| 2358 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 2359 |
mod field_diagnostics { |
| 2360 |
use super::*; |
| 2361 |
|
| 2362 |
#[test] |
| 2363 |
fn test_snippet_points_at_the_inverted_field_range() { |
| 2364 |
let result = new_ucmd!() |
| 2365 |
.terminal_sim_stderr() |
| 2366 |
.args(&["--field", "1,3-2", "--to=si", "1"]) |
| 2367 |
.fails_with_code(1); |
| 2368 |
let stderr = result.stderr_as_displayed(); |
| 2369 |
|
| 2370 |
assert!(stderr.contains("numfmt:1:18"), "{stderr}"); |
| 2371 |
assert!( |
| 2372 |
stderr.contains("this range ends before it starts"), |
| 2373 |
"{stderr}" |
| 2374 |
); |
| 2375 |
} |
| 2376 |
|
| 2377 |
#[test] |
| 2378 |
fn test_snippet_points_at_the_zero_field() { |
| 2379 |
let result = new_ucmd!() |
| 2380 |
.terminal_sim_stderr() |
| 2381 |
.args(&["--field=0", "--to=si", "1"]) |
| 2382 |
.fails_with_code(1); |
| 2383 |
let stderr = result.stderr_as_displayed(); |
| 2384 |
|
| 2385 |
assert!(stderr.contains("numfmt:1:16"), "{stderr}"); |
| 2386 |
assert!(stderr.contains("fields are numbered from 1"), "{stderr}"); |
| 2387 |
} |
| 2388 |
|
| 2389 |
#[test] |
| 2390 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 2391 |
new_ucmd!() |
| 2392 |
.args(&["--field=0", "--to=si", "1"]) |
| 2393 |
.fails_with_code(1) |
| 2394 |
.stderr_contains("range '0' was invalid: fields and positions are numbered from 1"); |
| 2395 |
} |
| 2396 |
} |