2022-05-11 11:26:43 +02:00
|
|
|
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
|
|
|
use nu_test_support::playground::Playground;
|
2021-01-29 14:43:35 +01:00
|
|
|
use nu_test_support::{nu, pipeline};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_range() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2022-05-11 11:26:43 +02:00
|
|
|
echo 1..5 | into string | to json -r
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2021-01-29 14:43:35 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "[\"1\",\"2\",\"3\",\"4\",\"5\"]");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_number() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2021-05-27 19:18:02 +02:00
|
|
|
echo 5 | into string
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2021-01-29 14:43:35 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "5");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-09-13 23:53:55 +02:00
|
|
|
fn from_float() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2021-05-27 19:18:02 +02:00
|
|
|
echo 1.5 | into string
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2021-01-29 14:43:35 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1.5");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_boolean() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2022-03-03 01:55:03 +01:00
|
|
|
echo true | into string
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2021-01-29 14:43:35 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_string() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2021-05-27 19:18:02 +02:00
|
|
|
echo "one" | into string
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2021-01-29 14:43:35 +01:00
|
|
|
|
|
|
|
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(),
|
2022-05-11 11:26:43 +02:00
|
|
|
"ls sample.toml | get name | into string | get 0"
|
2021-01-29 14:43:35 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
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(),
|
2022-05-11 11:26:43 +02:00
|
|
|
"ls sample.toml | get size | into string | get 0"
|
2021-01-29 14:43:35 +01:00
|
|
|
);
|
|
|
|
|
2022-05-11 11:26:43 +02:00
|
|
|
let expected = if cfg!(windows) { "27 B" } else { "25 B" };
|
|
|
|
|
|
|
|
assert_eq!(actual.out, expected);
|
2021-01-29 14:43:35 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-09-13 23:53:55 +02:00
|
|
|
fn from_float_correct_trailing_zeros() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2022-05-11 11:26:43 +02:00
|
|
|
1.23000 | into string -d 3
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2021-01-29 14:43:35 +01:00
|
|
|
|
|
|
|
assert!(actual.out.contains("1.230"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-09-13 23:53:55 +02:00
|
|
|
fn from_int_float_correct_trailing_zeros() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2022-05-11 11:26:43 +02:00
|
|
|
1.00000 | into string -d 3
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2021-01-29 14:43:35 +01:00
|
|
|
|
|
|
|
assert!(actual.out.contains("1.000"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-09-13 23:53:55 +02:00
|
|
|
fn from_int_float_trim_trailing_zeros() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2022-05-11 11:26:43 +02:00
|
|
|
1.00000 | into string | $"($in) flat"
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2021-01-29 14:43:35 +01:00
|
|
|
|
|
|
|
assert!(actual.out.contains("1 flat")); // "1" would match "1.0"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_table() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(pipeline(
|
2021-01-29 14:43:35 +01:00
|
|
|
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
|
2021-01-29 14:43:35 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("32.38"));
|
|
|
|
assert!(actual.out.contains("15.20"));
|
|
|
|
}
|
2022-05-11 11:26:43 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_nothing() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2022-12-22 21:30:10 +01:00
|
|
|
null | into string
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2022-05-11 11:26:43 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
2022-06-30 03:01:34 +02:00
|
|
|
|
2022-08-13 04:13:50 +02:00
|
|
|
#[test]
|
|
|
|
fn int_into_string() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"
|
2022-08-13 04:13:50 +02:00
|
|
|
10 | into string
|
2023-07-17 18:43:51 +02:00
|
|
|
"#);
|
2022-08-13 04:13:50 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|