2019-12-17 18:54:39 +00:00
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
use nu_test_support::playground::Playground;
|
2020-05-07 11:03:43 +00:00
|
|
|
use nu_test_support::{nu, pipeline};
|
2019-08-10 09:11:38 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-08-10 09:12:48 +00:00
|
|
|
fn can_only_apply_one() {
|
2020-05-07 11:03:43 +00:00
|
|
|
let actual = nu!(
|
2019-08-29 06:31:56 +00:00
|
|
|
cwd: "tests/fixtures/formats",
|
2019-08-10 09:12:48 +00:00
|
|
|
"open caco3_plastics.csv | first 1 | str origin --downcase --upcase"
|
2019-08-10 09:11:38 +00:00
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert!(actual.err.contains(r#"--capitalize|--downcase|--upcase|--to-int|--substring "start,end"|--replace|--find-replace [pattern replacement]|to-date-time|--trim]"#));
|
2019-08-10 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-08-10 09:12:48 +00:00
|
|
|
fn acts_without_passing_field() {
|
2019-08-29 00:32:42 +00:00
|
|
|
Playground::setup("plugin_str_test_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
2019-08-10 09:12:48 +00:00
|
|
|
"sample.yml",
|
|
|
|
r#"
|
|
|
|
environment:
|
|
|
|
global:
|
|
|
|
PROJECT_NAME: nushell
|
|
|
|
"#,
|
2019-08-29 00:32:42 +00:00
|
|
|
)]);
|
2019-08-10 09:12:48 +00:00
|
|
|
|
2019-08-29 00:32:42 +00:00
|
|
|
let actual = nu!(
|
2019-08-29 06:31:56 +00:00
|
|
|
cwd: dirs.test(),
|
2019-08-29 00:32:42 +00:00
|
|
|
"open sample.yml | get environment.global.PROJECT_NAME | str --upcase | echo $it"
|
|
|
|
);
|
2019-08-10 09:11:38 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "NUSHELL");
|
2019-08-29 00:32:42 +00:00
|
|
|
})
|
2019-08-10 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-24 21:37:58 +00:00
|
|
|
fn trims() {
|
2019-08-29 00:32:42 +00:00
|
|
|
Playground::setup("plugin_str_test_2", |dirs, sandbox| {
|
2020-04-24 21:37:58 +00:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[dependency]
|
|
|
|
name = "nu "
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"open sample.toml | str dependency.name --trim | get dependency.name | echo $it"
|
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "nu");
|
2020-04-24 21:37:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn capitalizes() {
|
|
|
|
Playground::setup("plugin_str_test_3", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[dependency]
|
|
|
|
name = "nu"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"open sample.toml | str dependency.name --capitalize | get dependency.name | echo $it"
|
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "Nu");
|
2020-04-24 21:37:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn downcases() {
|
|
|
|
Playground::setup("plugin_str_test_4", |dirs, sandbox| {
|
2019-09-11 14:36:50 +00:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
2019-08-29 00:32:42 +00:00
|
|
|
[dependency]
|
|
|
|
name = "LIGHT"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
2019-08-29 06:31:56 +00:00
|
|
|
cwd: dirs.test(),
|
2020-02-12 02:24:31 +00:00
|
|
|
"open sample.toml | str dependency.name -d | get dependency.name | echo $it"
|
2019-08-29 00:32:42 +00:00
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "light");
|
2019-08-29 00:32:42 +00:00
|
|
|
})
|
2019-08-10 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn upcases() {
|
2020-04-24 21:37:58 +00:00
|
|
|
Playground::setup("plugin_str_test_5", |dirs, sandbox| {
|
2019-09-11 14:36:50 +00:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
2019-08-29 00:32:42 +00:00
|
|
|
[package]
|
|
|
|
name = "nushell"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
2019-08-29 06:31:56 +00:00
|
|
|
cwd: dirs.test(),
|
2019-08-29 00:32:42 +00:00
|
|
|
"open sample.toml | str package.name --upcase | get package.name | echo $it"
|
|
|
|
);
|
2019-09-03 00:49:51 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "NUSHELL");
|
2019-08-29 00:32:42 +00:00
|
|
|
})
|
2019-08-10 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn converts_to_int() {
|
2019-08-29 00:32:42 +00:00
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2019-08-29 06:31:56 +00:00
|
|
|
r#"
|
2020-03-16 20:50:45 +00:00
|
|
|
echo '{number_as_string: "1"}'
|
2020-05-04 08:44:33 +00:00
|
|
|
| from json
|
2020-03-16 20:50:45 +00:00
|
|
|
| str number_as_string --to-int
|
|
|
|
| rename number
|
|
|
|
| where number == 1
|
|
|
|
| get number
|
2019-08-29 06:31:56 +00:00
|
|
|
| echo $it
|
|
|
|
"#
|
2019-08-29 00:32:42 +00:00
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "1");
|
2019-09-12 03:34:47 +00:00
|
|
|
}
|
2019-11-27 00:03:22 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces() {
|
2020-04-24 21:37:58 +00:00
|
|
|
Playground::setup("plugin_str_test_5", |dirs, sandbox| {
|
2019-11-27 00:03:22 +00:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "nushell"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: dirs.test(), pipeline(
|
2019-11-27 00:03:22 +00:00
|
|
|
r#"
|
|
|
|
open sample.toml
|
|
|
|
| str package.name --replace wykittenshell
|
|
|
|
| get package.name
|
|
|
|
| echo $it
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "wykittenshell");
|
2019-11-27 00:03:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_and_replaces() {
|
2020-04-24 21:37:58 +00:00
|
|
|
Playground::setup("plugin_str_test_6", |dirs, sandbox| {
|
2019-11-27 00:03:22 +00:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[fortune.teller]
|
|
|
|
phone = "1-800-KATZ"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: dirs.test(), pipeline(
|
2019-11-27 00:03:22 +00:00
|
|
|
r#"
|
|
|
|
open sample.toml
|
2019-11-30 00:21:05 +00:00
|
|
|
| str fortune.teller.phone --find-replace [KATZ "5289"]
|
2019-11-27 00:03:22 +00:00
|
|
|
| get fortune.teller.phone
|
|
|
|
| echo $it
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "1-800-5289");
|
2019-11-27 00:03:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_and_replaces_without_passing_field() {
|
2020-04-24 21:37:58 +00:00
|
|
|
Playground::setup("plugin_str_test_7", |dirs, sandbox| {
|
2019-11-27 00:03:22 +00:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[fortune.teller]
|
|
|
|
phone = "1-800-KATZ"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: dirs.test(), pipeline(
|
2019-11-27 00:03:22 +00:00
|
|
|
r#"
|
|
|
|
open sample.toml
|
|
|
|
| get fortune.teller.phone
|
2019-11-30 00:21:05 +00:00
|
|
|
| str --find-replace [KATZ "5289"]
|
2019-11-27 00:03:22 +00:00
|
|
|
| echo $it
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "1-800-5289");
|
2019-11-27 00:03:22 +00:00
|
|
|
})
|
|
|
|
}
|