nushell/crates/nu-command/tests/commands/into_filesize.rs
Stefan Holderbach 2a3d5a9d42
Bump bytesize to fix into filesize (#15088)
# Description
Closes https://github.com/nushell/nushell/issues/14866

Incorporates https://github.com/bytesize-rs/bytesize/pull/59 with
bytesize version 1.3.1

# User-Facing Changes
Now rejected strings
```
"1.3 1.3 kB" | into filesize
"1 420 kB" | into filesize
```
# Tests + Formatting
Added test with invalid input that was silently ignored before
2025-02-11 11:33:48 +01:00

138 lines
2.5 KiB
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn int() {
let actual = nu!("1 | into filesize");
assert!(actual.out.contains("1 B"));
}
#[test]
fn float() {
let actual = nu!("1.2 | into filesize");
assert!(actual.out.contains("1 B"));
}
#[test]
fn str() {
let actual = nu!("'2000' | into filesize");
assert!(actual.out.contains("2.0 kB"));
}
#[test]
fn str_newline() {
let actual = nu!(pipeline(
r#"
"2000
" | into filesize
"#
));
assert!(actual.out.contains("2.0 kB"));
}
#[test]
fn str_many_newlines() {
let actual = nu!(pipeline(
r#"
"2000
" | into filesize
"#
));
assert!(actual.out.contains("2.0 kB"));
}
#[test]
fn filesize() {
let actual = nu!("3kB | into filesize");
assert!(actual.out.contains("3.0 kB"));
}
#[test]
fn negative_filesize() {
let actual = nu!("-3kB | into filesize");
assert!(actual.out.contains("-3.0 kB"));
}
#[test]
fn negative_str_filesize() {
let actual = nu!("'-3kB' | into filesize");
assert!(actual.out.contains("-3.0 kB"));
}
#[test]
fn wrong_negative_str_filesize() {
let actual = nu!("'--3kB' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn large_negative_str_filesize() {
let actual = nu!("'-10000PB' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn negative_str() {
let actual = nu!("'-1' | into filesize");
assert!(actual.out.contains("-1 B"));
}
#[test]
fn wrong_negative_str() {
let actual = nu!("'--1' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn positive_str_filesize() {
let actual = nu!("'+1kB' | into filesize");
assert!(actual.out.contains("1.0 kB"));
}
#[test]
fn wrong_positive_str_filesize() {
let actual = nu!("'++1kB' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn large_positive_str_filesize() {
let actual = nu!("'+10000PB' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn positive_str() {
let actual = nu!("'+1' | into filesize");
assert!(actual.out.contains("1 B"));
}
#[test]
fn wrong_positive_str() {
let actual = nu!("'++1' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn invalid_str() {
let actual = nu!("'42.0 42.0 kB' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}