2019-12-17 18:54:39 +00:00
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
use nu_test_support::{nu, nu_error, pipeline};
|
2019-08-10 09:11:38 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-08-10 09:12:48 +00:00
|
|
|
fn can_only_apply_one() {
|
2019-08-29 00:32:42 +00:00
|
|
|
let actual = nu_error!(
|
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
|
|
|
);
|
|
|
|
|
2019-11-27 00:03:22 +00:00
|
|
|
assert!(actual.contains(r#"--downcase|--upcase|--to-int|--substring "start,end"|--replace|--find-replace [pattern replacement]]"#));
|
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
|
|
|
|
2019-08-29 00:32:42 +00:00
|
|
|
assert_eq!(actual, "NUSHELL");
|
|
|
|
})
|
2019-08-10 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn downcases() {
|
2019-08-29 00:32:42 +00:00
|
|
|
Playground::setup("plugin_str_test_2", |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(),
|
2019-08-29 00:32:42 +00:00
|
|
|
"open sample.toml | str dependency.name --downcase | get dependency.name | echo $it"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual, "light");
|
|
|
|
})
|
2019-08-10 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn upcases() {
|
2019-08-29 00:32:42 +00:00
|
|
|
Playground::setup("plugin_str_test_3", |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
|
|
|
|
2019-08-29 00:32:42 +00:00
|
|
|
assert_eq!(actual, "NUSHELL");
|
|
|
|
})
|
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#"
|
2019-09-03 00:49:51 +00:00
|
|
|
open caco3_plastics.csv
|
|
|
|
| first 1
|
|
|
|
| str tariff_item --to-int
|
|
|
|
| where tariff_item == 2509000000
|
|
|
|
| get tariff_item
|
2019-08-29 06:31:56 +00:00
|
|
|
| echo $it
|
|
|
|
"#
|
2019-08-29 00:32:42 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual, "2509000000");
|
2019-09-12 03:34:47 +00:00
|
|
|
}
|
2019-11-27 00:03:22 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces() {
|
|
|
|
Playground::setup("plugin_str_test_4", |dirs, sandbox| {
|
|
|
|
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
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual, "wykittenshell");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_and_replaces() {
|
|
|
|
Playground::setup("plugin_str_test_5", |dirs, sandbox| {
|
|
|
|
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
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual, "1-800-5289");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_and_replaces_without_passing_field() {
|
|
|
|
Playground::setup("plugin_str_test_6", |dirs, sandbox| {
|
|
|
|
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
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual, "1-800-5289");
|
|
|
|
})
|
|
|
|
}
|