2020-08-02 09:29:29 +02:00
|
|
|
mod collect;
|
2022-05-11 11:26:43 +02:00
|
|
|
mod into_string;
|
2020-08-02 09:29:29 +02:00
|
|
|
|
2020-05-27 00:19:18 +02:00
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
use nu_test_support::{nu, pipeline};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trims() {
|
|
|
|
Playground::setup("str_test_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[dependency]
|
|
|
|
name = "nu "
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2020-10-26 07:55:52 +01:00
|
|
|
"open sample.toml | str trim dependency.name | get dependency.name"
|
2020-05-27 00:19:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "nu");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-13 07:45:34 +02:00
|
|
|
#[test]
|
|
|
|
fn error_trim_multiple_chars() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
2022-02-04 03:01:45 +01:00
|
|
|
echo "does it work now?!" | str trim -c "?!"
|
2020-07-13 07:45:34 +02:00
|
|
|
"#
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.err.contains("char"));
|
|
|
|
}
|
|
|
|
|
2020-05-27 00:19:18 +02:00
|
|
|
#[test]
|
|
|
|
fn capitalizes() {
|
|
|
|
Playground::setup("str_test_2", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[dependency]
|
|
|
|
name = "nu"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2020-10-26 07:55:52 +01:00
|
|
|
"open sample.toml | str capitalize dependency.name | get dependency.name"
|
2020-05-27 00:19:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "Nu");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn downcases() {
|
|
|
|
Playground::setup("str_test_3", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[dependency]
|
|
|
|
name = "LIGHT"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2020-10-26 07:55:52 +01:00
|
|
|
"open sample.toml | str downcase dependency.name | get dependency.name"
|
2020-05-27 00:19:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "light");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn upcases() {
|
|
|
|
Playground::setup("str_test_4", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "nushell"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2020-10-26 07:55:52 +01:00
|
|
|
"open sample.toml | str upcase package.name | get package.name"
|
2020-05-27 00:19:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "NUSHELL");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:18:23 +02:00
|
|
|
#[test]
|
|
|
|
fn camelcases() {
|
|
|
|
Playground::setup("str_test_3", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[dependency]
|
|
|
|
name = "THIS_IS_A_TEST"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2020-10-26 07:55:52 +01:00
|
|
|
"open sample.toml | str camel-case dependency.name | get dependency.name"
|
2020-08-17 22:18:23 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "thisIsATest");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-27 00:19:18 +02:00
|
|
|
#[test]
|
|
|
|
fn converts_to_int() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by raise error when given non-lists (#7623)
Closes https://github.com/nushell/nushell/issues/6941
2022-12-31 12:35:12 +01:00
|
|
|
echo '[{number_as_string: "1"}]'
|
2020-05-27 00:19:18 +02:00
|
|
|
| from json
|
2022-02-04 03:01:45 +01:00
|
|
|
| into int number_as_string
|
2020-05-27 00:19:18 +02:00
|
|
|
| rename number
|
|
|
|
| where number == 1
|
2022-02-18 23:11:27 +01:00
|
|
|
| get number.0
|
2021-01-29 14:43:35 +01:00
|
|
|
|
2020-05-27 00:19:18 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-06-01 23:02:57 +02:00
|
|
|
fn converts_to_decimal() {
|
2020-05-27 00:19:18 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
echo "3.1, 0.0415"
|
|
|
|
| split row ","
|
2022-02-04 03:01:45 +01:00
|
|
|
| into decimal
|
2020-06-19 04:02:01 +02:00
|
|
|
| math sum
|
2020-05-27 00:19:18 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "3.1415");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_and_replaces() {
|
|
|
|
Playground::setup("str_test_6", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[fortune.teller]
|
|
|
|
phone = "1-800-KATZ"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open sample.toml
|
2022-04-07 15:41:09 +02:00
|
|
|
| str replace KATZ "5289" fortune.teller.phone
|
2020-05-27 00:19:18 +02:00
|
|
|
| get fortune.teller.phone
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1-800-5289");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_and_replaces_without_passing_field() {
|
|
|
|
Playground::setup("str_test_7", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[fortune.teller]
|
|
|
|
phone = "1-800-KATZ"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open sample.toml
|
|
|
|
| get fortune.teller.phone
|
2022-04-07 15:41:09 +02:00
|
|
|
| str replace KATZ "5289"
|
2020-05-27 00:19:18 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1-800-5289");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-12 14:25:40 +01:00
|
|
|
#[test]
|
|
|
|
fn regex_error_in_pattern() {
|
|
|
|
Playground::setup("str_test_8", |dirs, _sandbox| {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
'source string'
|
|
|
|
| str replace 'source \Ufoo' "destination"
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let err = actual.err;
|
|
|
|
let expecting_str = "Incorrect value";
|
|
|
|
assert!(
|
|
|
|
err.contains(expecting_str),
|
|
|
|
"Error should contain '{expecting_str}', but was: {err}"
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-27 00:19:18 +02:00
|
|
|
#[test]
|
|
|
|
fn substrings_the_input() {
|
|
|
|
Playground::setup("str_test_8", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[fortune.teller]
|
|
|
|
phone = "1-800-ROBALINO"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open sample.toml
|
2023-03-29 09:01:42 +02:00
|
|
|
| str substring 6..14 fortune.teller.phone
|
2020-05-27 00:19:18 +02:00
|
|
|
| get fortune.teller.phone
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "ROBALINO");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn substring_errors_if_start_index_is_greater_than_end_index() {
|
|
|
|
Playground::setup("str_test_9", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[fortune.teller]
|
|
|
|
phone = "1-800-ROBALINO"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open sample.toml
|
2023-03-29 09:01:42 +02:00
|
|
|
| str substring 6..5 fortune.teller.phone
|
2020-05-27 00:19:18 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual
|
|
|
|
.err
|
|
|
|
.contains("End must be greater than or equal to Start"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn substrings_the_input_and_returns_the_string_if_end_index_exceeds_length() {
|
|
|
|
Playground::setup("str_test_10", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "nu-arepas"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open sample.toml
|
2023-03-29 09:01:42 +02:00
|
|
|
| str substring 0..999 package.name
|
2020-05-27 00:19:18 +02:00
|
|
|
| get package.name
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "nu-arepas");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn substrings_the_input_and_returns_blank_if_start_index_exceeds_length() {
|
|
|
|
Playground::setup("str_test_11", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "nu-arepas"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open sample.toml
|
2023-03-29 09:01:42 +02:00
|
|
|
| str substring 50..999 package.name
|
2020-05-27 00:19:18 +02:00
|
|
|
| get package.name
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn substrings_the_input_and_treats_start_index_as_zero_if_blank_start_index_given() {
|
|
|
|
Playground::setup("str_test_12", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "nu-arepas"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open sample.toml
|
2023-03-29 09:01:42 +02:00
|
|
|
| str substring ..2 package.name
|
2020-05-27 00:19:18 +02:00
|
|
|
| get package.name
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "nu");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given() {
|
|
|
|
Playground::setup("str_test_13", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "nu-arepas"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open sample.toml
|
2023-03-29 09:01:42 +02:00
|
|
|
| str substring 3.. package.name
|
2020-05-27 00:19:18 +02:00
|
|
|
| get package.name
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "arepas");
|
|
|
|
})
|
|
|
|
}
|
2020-07-12 05:57:39 +02:00
|
|
|
|
2020-07-14 22:47:04 +02:00
|
|
|
#[test]
|
|
|
|
fn str_reverse() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
echo "nushell" | str reverse
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("llehsun"));
|
|
|
|
}
|
2022-02-21 23:22:21 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_redirection_trim() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
let x = (nu --testbin cococo niceone); $x | str trim | str length
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "7");
|
|
|
|
}
|