nushell/crates/nu-command/tests/commands/into_filesize.rs
Sang-Heon Jeon dc76183cd5
fix wrong casting with into filesize (#13110)
# Description
Fix wrong casting which is related to
https://github.com/nushell/nushell/pull/12974#discussion_r1618598336

# User-Facing Changes
AS-IS (before fixing)
```
$ "-10000PiB" | into filesize
6.2 EiB                                                         <--- Wrong casted value
$ "10000PiB" | into filesize 
-6.2 EiB                                                        <--- Wrong casted value
```

TO-BE (after fixing)
```
$ "-10000PiB" | into filesize
Error: nu:🐚:cant_convert

  × Can't convert to filesize.
   ╭─[entry #6:1:1]
 1 │ "-10000PiB" | into filesize
   · ─────┬─────
   ·      ╰── can't convert string to filesize
   ╰────

$ "10000PiB" | into filesize
Error: nu:🐚:cant_convert

  × Can't convert to filesize.
   ╭─[entry #7:1:1]
 1 │ "10000PiB" | into filesize
   · ─────┬────
   ·      ╰── can't convert string to filesize
   ╰────
```
2024-06-10 10:43:17 +08:00

134 lines
2.7 KiB
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn into_filesize_int() {
let actual = nu!("1 | into filesize");
assert!(actual.out.contains("1 B"));
}
#[test]
fn into_filesize_float() {
let actual = nu!("1.2 | into filesize");
assert!(actual.out.contains("1 B"));
}
#[test]
fn into_filesize_str() {
let actual = nu!(r#"
'2000' | into filesize
"#);
assert!(actual.out.contains("2.0 KiB"));
}
#[test]
fn into_filesize_str_newline() {
let actual = nu!(pipeline(
r#"
"2000
" | into filesize
"#
));
assert!(actual.out.contains("2.0 KiB"));
}
#[test]
fn into_filesize_str_many_newlines() {
let actual = nu!(pipeline(
r#"
"2000
" | into filesize
"#
));
assert!(actual.out.contains("2.0 KiB"));
}
#[test]
fn into_filesize_filesize() {
let actual = nu!("3kib | into filesize");
assert!(actual.out.contains("3.0 KiB"));
}
#[test]
fn into_filesize_negative_filesize() {
let actual = nu!("-3kib | into filesize");
assert!(actual.out.contains("-3.0 KiB"));
}
#[test]
fn into_filesize_negative_str_filesize() {
let actual = nu!("'-3kib' | into filesize");
assert!(actual.out.contains("-3.0 KiB"));
}
#[test]
fn into_filesize_wrong_negative_str_filesize() {
let actual = nu!("'--3kib' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn into_filesize_large_negative_str_filesize() {
let actual = nu!("'-10000PiB' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn into_filesize_negative_str() {
let actual = nu!("'-1' | into filesize");
assert!(actual.out.contains("-1 B"));
}
#[test]
fn into_filesize_wrong_negative_str() {
let actual = nu!("'--1' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn into_filesize_positive_str_filesize() {
let actual = nu!("'+1Kib' | into filesize");
assert!(actual.out.contains("1.0 KiB"));
}
#[test]
fn into_filesize_wrong_positive_str_filesize() {
let actual = nu!("'++1Kib' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn into_filesize_large_positive_str_filesize() {
let actual = nu!("'+10000PiB' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn into_filesize_positive_str() {
let actual = nu!("'+1' | into filesize");
assert!(actual.out.contains("1 B"));
}
#[test]
fn into_filesize_wrong_positive_str() {
let actual = nu!("'++1' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}