Fix tests

This commit is contained in:
Ian Manske 2024-11-17 13:36:01 -08:00
parent 0e2922ad60
commit fa017d32a7
15 changed files with 91 additions and 121 deletions

View File

@ -127,8 +127,8 @@ impl Command for SubCommand {
},
Example {
description: "convert filesize to string",
example: "1KiB | into string",
result: Some(Value::test_string("1.0 KiB")),
example: "1kB | into string",
result: Some(Value::test_string("1 kB")),
},
Example {
description: "convert duration to string",

View File

@ -107,8 +107,8 @@ impl Command for FormatFilesize {
},
Example {
description: "Convert the size data to MB",
example: "4Gb | format filesize MB",
result: Some(Value::test_string("4000.0 MB")),
example: "4GB | format filesize MB",
result: Some(Value::test_string("4000 MB")),
},
]
}

View File

@ -76,13 +76,13 @@ fn format_filesize_works() {
cwd: dirs.test(), pipeline(
"
ls
| format filesize KB size
| format filesize kB size
| get size
| first
"
));
assert_eq!(actual.out, "0.0 KB");
assert_eq!(actual.out, "0 kB");
})
}
@ -105,10 +105,10 @@ fn format_filesize_works_with_nonempty_files() {
);
#[cfg(not(windows))]
assert_eq!(actual.out, "25");
assert_eq!(actual.out, "25 B");
#[cfg(windows)]
assert_eq!(actual.out, "27");
assert_eq!(actual.out, "27 B");
},
)
}

View File

@ -1,30 +1,28 @@
use nu_test_support::{nu, pipeline};
#[test]
fn into_filesize_int() {
fn int() {
let actual = nu!("1 | into filesize");
assert!(actual.out.contains("1 B"));
}
#[test]
fn into_filesize_float() {
fn 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
"#);
fn str() {
let actual = nu!("'2000' | into filesize");
assert!(actual.out.contains("2.0 KiB"));
assert!(actual.out.contains("2 kB"));
}
#[test]
fn into_filesize_str_newline() {
fn str_newline() {
let actual = nu!(pipeline(
r#"
"2000
@ -32,11 +30,11 @@ fn into_filesize_str_newline() {
"#
));
assert!(actual.out.contains("2.0 KiB"));
assert!(actual.out.contains("2 kB"));
}
#[test]
fn into_filesize_str_many_newlines() {
fn str_many_newlines() {
let actual = nu!(pipeline(
r#"
"2000
@ -45,88 +43,88 @@ fn into_filesize_str_many_newlines() {
"#
));
assert!(actual.out.contains("2.0 KiB"));
assert!(actual.out.contains("2 kB"));
}
#[test]
fn into_filesize_filesize() {
let actual = nu!("3kib | into filesize");
fn filesize() {
let actual = nu!("3kB | into filesize");
assert!(actual.out.contains("3.0 KiB"));
assert!(actual.out.contains("3 kB"));
}
#[test]
fn into_filesize_negative_filesize() {
let actual = nu!("-3kib | into filesize");
fn negative_filesize() {
let actual = nu!("-3kB | into filesize");
assert!(actual.out.contains("-3.0 KiB"));
assert!(actual.out.contains("-3 kB"));
}
#[test]
fn into_filesize_negative_str_filesize() {
let actual = nu!("'-3kib' | into filesize");
fn negative_str_filesize() {
let actual = nu!("'-3kB' | into filesize");
assert!(actual.out.contains("-3.0 KiB"));
assert!(actual.out.contains("-3 kB"));
}
#[test]
fn into_filesize_wrong_negative_str_filesize() {
let actual = nu!("'--3kib' | into filesize");
fn wrong_negative_str_filesize() {
let actual = nu!("'--3kB' | 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");
fn large_negative_str_filesize() {
let actual = nu!("'-10000PB' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn into_filesize_negative_str() {
fn negative_str() {
let actual = nu!("'-1' | into filesize");
assert!(actual.out.contains("-1 B"));
}
#[test]
fn into_filesize_wrong_negative_str() {
fn 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");
fn positive_str_filesize() {
let actual = nu!("'+1kB' | into filesize");
assert!(actual.out.contains("1.0 KiB"));
assert!(actual.out.contains("1 kB"));
}
#[test]
fn into_filesize_wrong_positive_str_filesize() {
let actual = nu!("'++1Kib' | into filesize");
fn wrong_positive_str_filesize() {
let actual = nu!("'++1kB' | 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");
fn large_positive_str_filesize() {
let actual = nu!("'+10000PB' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));
}
#[test]
fn into_filesize_positive_str() {
fn positive_str() {
let actual = nu!("'+1' | into filesize");
assert!(actual.out.contains("1 B"));
}
#[test]
fn into_filesize_wrong_positive_str() {
fn wrong_positive_str() {
let actual = nu!("'++1' | into filesize");
assert!(actual.err.contains("can't convert string to filesize"));

View File

@ -308,55 +308,55 @@ fn floor_div_mod_large_num() {
fn unit_multiplication_math() {
let actual = nu!(pipeline(
r#"
1mb * 2
1MB * 2
"#
));
assert_eq!(actual.out, "1.9 MiB");
assert_eq!(actual.out, "2 MB");
}
#[test]
fn unit_multiplication_float_math() {
let actual = nu!(pipeline(
r#"
1mb * 1.2
1MB * 1.2
"#
));
assert_eq!(actual.out, "1.1 MiB");
assert_eq!(actual.out, "1.2 MB");
}
#[test]
fn unit_float_floor_division_math() {
let actual = nu!(pipeline(
r#"
1mb // 3.0
1MB // 3.0
"#
));
assert_eq!(actual.out, "325.5 KiB");
assert_eq!(actual.out, "333.333 kB");
}
#[test]
fn unit_division_math() {
let actual = nu!(pipeline(
r#"
1mb / 4
1MB / 4
"#
));
assert_eq!(actual.out, "244.1 KiB");
assert_eq!(actual.out, "250 kB");
}
#[test]
fn unit_float_division_math() {
let actual = nu!(pipeline(
r#"
1mb / 3.1
1MB / 3.2
"#
));
assert_eq!(actual.out, "315.0 KiB");
assert_eq!(actual.out, "312.5 kB");
}
#[test]

View File

@ -857,7 +857,7 @@ fn test_cp_arg_no_clobber() {
fn test_cp_arg_no_clobber_twice() {
Playground::setup("ucp_test_29", |dirs, sandbox| {
sandbox.with_files(&[
EmptyFile("source.txt"),
FileWithContent("source.txt", "fake data"),
FileWithContent("source_with_body.txt", "some-body"),
]);
nu!(

View File

@ -616,7 +616,7 @@ mod tests {
#[rstest]
#[case(999, "999 B")]
#[case(1000, "1 kB")]
#[case(1024, "1.024 KB")]
#[case(1024, "1.024 kB")]
fn display_auto_decimal(#[case] val: i64, #[case] exp: &str) {
let filesize = Filesize::new(val);
assert_eq!(

View File

@ -25,22 +25,22 @@ fn config_preserved_after_do() {
#[test]
fn config_affected_when_mutated() {
let actual = nu!(nu_repl_code(&[
r#"$env.config = { filesize: { metric: false, format:"auto" } }"#,
r#"$env.config = { filesize: { metric: true, format:"auto" } }"#,
"20mib | into string"
r#"$env.config = { filesize: { unit: binary } }"#,
r#"$env.config = { filesize: { unit: decimal } }"#,
"20MB | into string"
]));
assert_eq!(actual.out, "21.0 MB");
assert_eq!(actual.out, "20 MB");
}
#[test]
fn config_affected_when_deep_mutated() {
let actual = nu!(cwd: "crates/nu-utils/src/sample_config", nu_repl_code(&[
r#"source default_config.nu"#,
r#"$env.config.filesize.metric = true"#,
r#"20mib | into string"#]));
r#"$env.config.filesize.unit = 'binary'"#,
r#"20MiB | into string"#]));
assert_eq!(actual.out, "21.0 MB");
assert_eq!(actual.out, "20 MiB");
}
#[test]

View File

@ -1,53 +1,43 @@
use nu_test_support::{nu, nu_repl_code};
#[test]
fn filesize_metric_true() {
fn filesize_mb() {
let code = &[
r#"$env.config = { filesize: { metric: true, format:"mb" } }"#,
r#"20mib | into string"#,
r#"$env.config = { filesize: { unit: MB } }"#,
r#"20MB | into string"#,
];
let actual = nu!(nu_repl_code(code));
assert_eq!(actual.out, "21.0 MB");
assert_eq!(actual.out, "20 MB");
}
#[test]
fn filesize_metric_false() {
fn filesize_mib() {
let code = &[
r#"$env.config = { filesize: { metric: false, format:"mib" } }"#,
r#"20mib | into string"#,
r#"$env.config = { filesize: { unit: MiB } }"#,
r#"20MiB | into string"#,
];
let actual = nu!(nu_repl_code(code));
assert_eq!(actual.out, "20.0 MiB");
assert_eq!(actual.out, "20 MiB");
}
#[test]
fn filesize_metric_overrides_format() {
fn filesize_format_decimal() {
let code = &[
r#"$env.config = { filesize: { metric: false, format:"mb" } }"#,
r#"20mib | into string"#,
r#"$env.config = { filesize: { unit: decimal } }"#,
r#"[2MB 2GB 2TB] | into string | to nuon"#,
];
let actual = nu!(nu_repl_code(code));
assert_eq!(actual.out, "20.0 MiB");
assert_eq!(actual.out, r#"["2 MB", "2 GB", "2 TB"]"#);
}
#[test]
fn filesize_format_auto_metric_true() {
fn filesize_format_binary() {
let code = &[
r#"$env.config = { filesize: { metric: true, format:"auto" } }"#,
r#"[2mb 2gb 2tb] | into string | to nuon"#,
r#"$env.config = { filesize: { unit: binary } }"#,
r#"[2MiB 2GiB 2TiB] | into string | to nuon"#,
];
let actual = nu!(nu_repl_code(code));
assert_eq!(actual.out, r#"["2.0 MB", "2.0 GB", "2.0 TB"]"#);
}
#[test]
fn filesize_format_auto_metric_false() {
let code = &[
r#"$env.config = { filesize: { metric: false, format:"auto" } }"#,
r#"[2mb 2gb 2tb] | into string | to nuon"#,
];
let actual = nu!(nu_repl_code(code));
assert_eq!(actual.out, r#"["1.9 MiB", "1.9 GiB", "1.8 TiB"]"#);
assert_eq!(actual.out, r#"["2 MiB", "2 GiB", "2 TiB"]"#);
}
#[test]

View File

@ -187,7 +187,7 @@ impl<'a> Playground<'a> {
let mut permission_set = false;
let mut write_able = true;
let (file_name, contents) = match *f {
Stub::EmptyFile(name) => (name, "fake data".to_string()),
Stub::EmptyFile(name) => (name, String::new()),
Stub::FileWithContent(name, content) => (name, content.to_string()),
Stub::FileWithContentToBeTrimmed(name, content) => (
name,

View File

@ -122,8 +122,8 @@ fn const_string_interpolation_date() {
#[test]
fn const_string_interpolation_filesize() {
let actual = nu!(r#"const s = $"(2kb)"; $s"#);
assert_eq!(actual.out, "2.0 KiB");
let actual = nu!(r#"const s = $"(2kB)"; $s"#);
assert_eq!(actual.out, "2 kB");
}
#[test]

View File

@ -88,7 +88,7 @@ fn literal_float() {
#[test]
fn literal_filesize() {
test_eval("30MiB", Eq("30.0 MiB"))
test_eval("30MB", Eq("30 MB"))
}
#[test]

View File

@ -95,8 +95,8 @@ fn mutate_nu_config_nested_history() -> TestResult {
#[test]
fn mutate_nu_config_nested_filesize() -> TestResult {
run_test_std(
r#"$env.config.filesize.format = 'kb'; $env.config.filesize.format"#,
"kb",
r#"$env.config.filesize.unit = 'kB'; $env.config.filesize.unit"#,
"kB",
)
}

View File

@ -741,12 +741,12 @@ fn duration_with_faulty_number() -> TestResult {
#[test]
fn filesize_with_underscores_1() -> TestResult {
run_test("420_mb", "400.5 MiB")
run_test("420_mb", "420 MB")
}
#[test]
fn filesize_with_underscores_2() -> TestResult {
run_test("1_000_000B", "976.6 KiB")
run_test("1_000_000B", "1 MB")
}
#[test]

View File

@ -768,7 +768,7 @@ fn filesize_math() {
100 * 10kib
");
assert_eq!(actual.out, "1024.0 kB");
assert_eq!(actual.out, "1.024 MB");
}
#[test]
@ -783,45 +783,27 @@ fn filesize_math2() {
#[test]
fn filesize_math3() {
let actual = nu!("
100kib / 10
100kB / 10
");
assert_eq!(actual.out, "10.0 KiB");
assert_eq!(actual.out, "10 kB");
}
#[test]
fn filesize_math4() {
let actual = nu!("
100kib * 5
100kB * 5
");
assert_eq!(actual.out, "500.0 KiB");
assert_eq!(actual.out, "500 kB");
}
#[test]
fn filesize_math5() {
let actual = nu!("
1000 * 1kib
100 * 1kB
");
assert_eq!(actual.out, "1000.0 KiB");
}
#[test]
fn filesize_math6() {
let actual = nu!("
1000 * 1mib
");
assert_eq!(actual.out, "1000.0 MiB");
}
#[test]
fn filesize_math7() {
let actual = nu!("
1000 * 1gib
");
assert_eq!(actual.out, "1000.0 GiB");
assert_eq!(actual.out, "100 kB");
}
#[test]