nushell/crates/nu-command/tests/commands/str_/into_string.rs

258 lines
4.8 KiB
Rust
Raw Normal View History

use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn from_range() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo 1..5 | into string | to json -r
"#
)
);
assert_eq!(actual.out, "[\"1\",\"2\",\"3\",\"4\",\"5\"]");
}
#[test]
fn from_number() {
let actual = nu!(
cwd: ".", pipeline(
r#"
2021-05-27 19:18:02 +02:00
echo 5 | into string
"#
)
);
assert_eq!(actual.out, "5");
}
#[test]
fn from_decimal() {
let actual = nu!(
cwd: ".", pipeline(
r#"
2021-05-27 19:18:02 +02:00
echo 1.5 | into string
"#
)
);
assert_eq!(actual.out, "1.5");
}
#[test]
fn from_boolean() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo true | into string
"#
)
);
assert_eq!(actual.out, "true");
}
#[test]
fn from_string() {
let actual = nu!(
cwd: ".", pipeline(
r#"
2021-05-27 19:18:02 +02:00
echo "one" | into string
"#
)
);
assert_eq!(actual.out, "one");
}
#[test]
fn from_filename() {
Playground::setup("from_filename", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"sample.toml",
r#"
[dependency]
name = "nu"
"#,
)]);
let actual = nu!(
cwd: dirs.test(),
"ls sample.toml | get name | into string | get 0"
);
assert_eq!(actual.out, "sample.toml");
})
}
#[test]
fn from_filesize() {
Playground::setup("from_filesize", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"sample.toml",
r#"
[dependency]
name = "nu"
"#,
)]);
let actual = nu!(
cwd: dirs.test(),
"ls sample.toml | get size | into string | get 0"
);
let expected = if cfg!(windows) { "27 B" } else { "25 B" };
assert_eq!(actual.out, expected);
})
}
#[test]
fn from_decimal_correct_trailing_zeros() {
let actual = nu!(
cwd: ".", pipeline(
r#"
1.23000 | into string -d 3
"#
));
assert!(actual.out.contains("1.230"));
}
#[test]
fn from_int_decimal_correct_trailing_zeros() {
let actual = nu!(
cwd: ".", pipeline(
r#"
1.00000 | into string -d 3
"#
));
assert!(actual.out.contains("1.000"));
}
#[test]
fn from_int_decimal_trim_trailing_zeros() {
let actual = nu!(
cwd: ".", pipeline(
r#"
1.00000 | into string | $"($in) flat"
"#
));
assert!(actual.out.contains("1 flat")); // "1" would match "1.0"
}
#[test]
fn from_table() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo '[{"name": "foo", "weight": 32.377}, {"name": "bar", "weight": 15.2}]'
| from json
2021-05-27 19:18:02 +02:00
| into string weight -d 2
"#
));
assert!(actual.out.contains("32.38"));
assert!(actual.out.contains("15.20"));
}
#[test]
fn from_nothing() {
let actual = nu!(
cwd: ".", pipeline(
r#"
null | into string
"#
));
assert_eq!(actual.out, "");
}
Add decimals to int when using `into string --decimals` (#6085) * Add decimals to int when using `into string --decimals` * Add tests for `into string` when converting int with `--decimals` * Apply formatting * Merge `into_str` test files * Comment out unused code and add TODOs * Use decimal separator depending on system locale * Add test helper to run closure in different locale * Add tests for int-to-string conversion using different locales * Add utils function to get system locale * Add panic message when locking mutex fails * Catch and resume panic later to prevent Mutex poisoning when test fails * Move test to `nu-test-support` to keep `nu-utils` free of `nu-*` dependencies See https://github.com/nushell/nushell/pull/6085#issuecomment-1193131694 * Rename test support fn `with_fake_locale` to `with_locale_override` * Move `get_system_locale()` to `locale` module * Allow overriding locale with special env variable (when not in release) * Use special env var to override locale during testing * Allow callback to return a value in `with_locale_override()` * Allow multiple options in `nu!` macro * Allow to set locale as `nu!` macro option * Use new `locale` option of `nu!` macro instead of `with_locale_override` Using the `locale` options does not lock the `LOCALE_OVERRIDE_MUTEX` mutex in `nu-test-support::locale_override` but instead calls the `nu` command directly with the `NU_LOCALE_OVERRIDE` environment variable. This allows for parallel test excecution. * Fix: Add option identifier for `cwd` in usage of `nu!` macro * Rely on `Display` trait for formatting `nu!` macro command - Removed the `DisplayPath` trait - Implement `Display` for `AbsolutePath`, `RelativePath` and `AbsoluteFile` * Default to locale `en_US.UTF-8` for tests when using `nu!` macro * Add doc comment to `nu!` macro * Format code using `cargo fmt --all` * Pass function directly instead of wrapping the call in a closure https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure * Pass function to `or_else()` instead of calling it inside `or()` https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call * Fix: Add option identifier for `cwd` in usage of `nu!` macro
2022-08-13 04:13:50 +02:00
#[test]
fn int_into_string() {
let actual = nu!(
cwd: ".", pipeline(
r#"
10 | into string
"#
));
assert_eq!(actual.out, "10");
}
#[test]
fn int_into_string_decimals_0() {
let actual = nu!(
locale: "en_US.UTF-8",
pipeline(
r#"
10 | into string --decimals 0
"#
)
);
assert_eq!(actual.out, "10");
}
#[test]
fn int_into_string_decimals_1() {
let actual = nu!(
locale: "en_US.UTF-8",
pipeline(
r#"
10 | into string --decimals 1
"#
)
);
assert_eq!(actual.out, "10.0");
}
#[test]
fn int_into_string_decimals_10() {
let actual = nu!(
locale: "en_US.UTF-8",
pipeline(
r#"
10 | into string --decimals 10
"#
)
);
assert_eq!(actual.out, "10.0000000000");
}
#[test]
fn int_into_string_decimals_respects_system_locale_de() {
// Set locale to `de_DE`, which uses `,` (comma) as decimal separator
let actual = nu!(
locale: "de_DE.UTF-8",
pipeline(
r#"
10 | into string --decimals 1
"#
)
);
assert_eq!(actual.out, "10,0");
}
#[test]
fn int_into_string_decimals_respects_system_locale_en() {
// Set locale to `en_US`, which uses `.` (period) as decimal separator
let actual = nu!(
locale: "en_US.UTF-8",
pipeline(
r#"
10 | into string --decimals 1
"#
)
);
assert_eq!(actual.out, "10.0");
}