nushell/tests/plugins/core_str.rs

167 lines
4.1 KiB
Rust
Raw Normal View History

2019-12-17 19:54:39 +01: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 11:11:38 +02:00
#[test]
2019-08-10 11:12:48 +02:00
fn can_only_apply_one() {
2019-08-29 02:32:42 +02:00
let actual = nu_error!(
2019-08-29 08:31:56 +02:00
cwd: "tests/fixtures/formats",
2019-08-10 11:12:48 +02:00
"open caco3_plastics.csv | first 1 | str origin --downcase --upcase"
2019-08-10 11:11:38 +02:00
);
assert!(actual.contains(r#"--downcase|--upcase|--to-int|--substring "start,end"|--replace|--find-replace [pattern replacement]]"#));
2019-08-10 11:11:38 +02:00
}
#[test]
2019-08-10 11:12:48 +02:00
fn acts_without_passing_field() {
2019-08-29 02:32:42 +02:00
Playground::setup("plugin_str_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
2019-08-10 11:12:48 +02:00
"sample.yml",
r#"
environment:
global:
PROJECT_NAME: nushell
"#,
2019-08-29 02:32:42 +02:00
)]);
2019-08-10 11:12:48 +02:00
2019-08-29 02:32:42 +02:00
let actual = nu!(
2019-08-29 08:31:56 +02:00
cwd: dirs.test(),
2019-08-29 02:32:42 +02:00
"open sample.yml | get environment.global.PROJECT_NAME | str --upcase | echo $it"
);
2019-08-10 11:11:38 +02:00
2019-08-29 02:32:42 +02:00
assert_eq!(actual, "NUSHELL");
})
2019-08-10 11:11:38 +02:00
}
#[test]
fn downcases() {
2019-08-29 02:32:42 +02:00
Playground::setup("plugin_str_test_2", |dirs, sandbox| {
2019-09-11 16:36:50 +02:00
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
2019-08-29 02:32:42 +02:00
[dependency]
name = "LIGHT"
"#,
)]);
let actual = nu!(
2019-08-29 08:31:56 +02:00
cwd: dirs.test(),
2019-08-29 02:32:42 +02:00
"open sample.toml | str dependency.name --downcase | get dependency.name | echo $it"
);
assert_eq!(actual, "light");
})
2019-08-10 11:11:38 +02:00
}
#[test]
fn upcases() {
2019-08-29 02:32:42 +02:00
Playground::setup("plugin_str_test_3", |dirs, sandbox| {
2019-09-11 16:36:50 +02:00
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
2019-08-29 02:32:42 +02:00
[package]
name = "nushell"
"#,
)]);
let actual = nu!(
2019-08-29 08:31:56 +02:00
cwd: dirs.test(),
2019-08-29 02:32:42 +02:00
"open sample.toml | str package.name --upcase | get package.name | echo $it"
);
2019-08-29 02:32:42 +02:00
assert_eq!(actual, "NUSHELL");
})
2019-08-10 11:11:38 +02:00
}
#[test]
fn converts_to_int() {
2019-08-29 02:32:42 +02:00
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
2019-08-29 08:31:56 +02:00
r#"
open caco3_plastics.csv
| first 1
| str tariff_item --to-int
| where tariff_item == 2509000000
| get tariff_item
2019-08-29 08:31:56 +02:00
| echo $it
"#
2019-08-29 02:32:42 +02:00
));
assert_eq!(actual, "2509000000");
2019-09-12 05:34:47 +02: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!(
cwd: dirs.test(), pipeline(
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!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
2019-11-30 01:21:05 +01:00
| str fortune.teller.phone --find-replace [KATZ "5289"]
| 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!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| get fortune.teller.phone
2019-11-30 01:21:05 +01:00
| str --find-replace [KATZ "5289"]
| echo $it
"#
));
assert_eq!(actual, "1-800-5289");
})
}