nushell/tests/filter_str_tests.rs

148 lines
3.7 KiB
Rust
Raw Normal View History

2019-08-10 11:11:38 +02:00
mod helpers;
2019-08-10 11:12:48 +02:00
use h::{in_directory as cwd, Playground, Stub::*};
use helpers as h;
2019-08-10 11:11:38 +02:00
#[test]
2019-08-10 11:12:48 +02:00
fn can_only_apply_one() {
nu_error!(
2019-08-10 11:11:38 +02:00
output,
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
);
2019-08-11 19:46:14 +02:00
assert!(
output.contains("Usage: str field [--downcase|--upcase|--to-int|--replace|--find-replace]")
);
2019-08-10 11:11:38 +02:00
}
#[test]
2019-08-10 11:12:48 +02:00
fn acts_without_passing_field() {
2019-08-11 19:46:14 +02:00
Playground::setup_for("plugin_str_acts_without_passing_field_test").with_files(vec![
FileWithContent(
2019-08-10 11:12:48 +02:00
"sample.yml",
r#"
environment:
global:
PROJECT_NAME: nushell
"#,
2019-08-11 19:46:14 +02:00
),
]);
2019-08-10 11:12:48 +02:00
nu!(
2019-08-10 11:11:38 +02:00
output,
2019-08-11 19:46:14 +02:00
cwd("tests/fixtures/nuplayground/plugin_str_acts_without_passing_field_test"),
2019-08-10 11:12:48 +02:00
"open sample.yml | get environment.global.PROJECT_NAME | str --upcase | echo $it"
2019-08-10 11:11:38 +02:00
);
2019-08-10 11:12:48 +02:00
assert_eq!(output, "NUSHELL");
2019-08-10 11:11:38 +02:00
}
#[test]
fn downcases() {
2019-08-11 19:46:14 +02:00
Playground::setup_for("plugin_str_downcases_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[dependency]
name = "LIGHT"
"#,
)]);
2019-08-10 11:12:48 +02:00
2019-08-10 11:11:38 +02:00
nu!(
output,
2019-08-11 19:46:14 +02:00
cwd("tests/fixtures/nuplayground/plugin_str_downcases_test"),
2019-08-10 11:12:48 +02:00
"open sample.toml | str dependency.name --downcase | get dependency.name | echo $it"
2019-08-10 11:11:38 +02:00
);
2019-08-10 11:12:48 +02:00
assert_eq!(output, "light");
2019-08-10 11:11:38 +02:00
}
#[test]
fn upcases() {
2019-08-11 19:46:14 +02:00
Playground::setup_for("plugin_str_upcases_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nushell"
"#,
)]);
2019-08-10 11:12:48 +02:00
2019-08-10 11:11:38 +02:00
nu!(
output,
2019-08-11 19:46:14 +02:00
cwd("tests/fixtures/nuplayground/plugin_str_upcases_test"),
2019-08-10 11:12:48 +02:00
"open sample.toml | str package.name --upcase | get package.name | echo $it"
2019-08-10 11:11:38 +02:00
);
assert_eq!(output, "NUSHELL");
}
#[test]
fn converts_to_int() {
nu!(
output,
cwd("tests/fixtures/formats"),
2019-08-10 11:12:48 +02:00
"open caco3_plastics.csv | first 1 | str tariff_item --to-int | where tariff_item == 2509000000 | get tariff_item | echo $it"
2019-08-10 11:11:38 +02:00
);
assert_eq!(output, "2509000000");
2019-08-11 19:46:14 +02:00
}
#[test]
fn replaces() {
Playground::setup_for("plugin_str_replaces_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nushell"
"#,
)]);
nu!(
output,
cwd("tests/fixtures/nuplayground/plugin_str_replaces_test"),
"open sample.toml | str package.name --replace wykittenshell | get package.name | echo $it"
);
assert_eq!(output, "wykittenshell");
}
#[test]
fn find_and_replaces() {
Playground::setup_for("plugin_str_find_and_replaces_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[fortune.teller]
phone = "1-800-KATZ"
2019-08-11 19:46:14 +02:00
"#,
)]);
nu!(
output,
cwd("tests/fixtures/nuplayground/plugin_str_find_and_replaces_test"),
2019-08-27 13:14:19 +02:00
r#"open sample.toml | str fortune.teller.phone --find-replace KATZ "5289" | get fortune.teller.phone | echo $it"#
2019-08-11 19:46:14 +02:00
);
assert_eq!(output, "1-800-5289");
2019-08-11 19:46:14 +02:00
}
#[test]
fn find_and_replaces_without_passing_field() {
Playground::setup_for("plugin_str_find_and_replaces_without_passing_field_test").with_files(
vec![FileWithContent(
"sample.toml",
r#"
[fortune.teller]
phone = "1-800-KATZ"
2019-08-11 19:46:14 +02:00
"#,
)],
);
nu!(
output,
cwd("tests/fixtures/nuplayground/plugin_str_find_and_replaces_without_passing_field_test"),
2019-08-27 13:14:19 +02:00
r#"open sample.toml | get fortune.teller.phone | str --find-replace KATZ "5289" | echo $it"#
2019-08-11 19:46:14 +02:00
);
assert_eq!(output, "1-800-5289");
2019-08-11 19:46:14 +02:00
}