nushell/tests/filter_str_tests.rs

175 lines
4.1 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 helpers as h;
2019-08-29 08:31:56 +02:00
use h::{Playground, Stub::*};
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
);
2019-08-11 19:46:14 +02:00
assert!(
2019-08-29 02:32:42 +02:00
actual.contains("Usage: str field [--downcase|--upcase|--to-int|--replace|--find-replace]")
2019-08-11 19:46:14 +02:00
);
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| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[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| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[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"
);
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!(
2019-08-29 08:31:56 +02:00
cwd: "tests/fixtures/formats", h::pipeline(
r#"
open caco3_plastics.csv
| first 1
| str tariff_item --to-int
| where tariff_item == 2509000000
| get tariff_item
| echo $it
"#
2019-08-29 02:32:42 +02:00
));
assert_eq!(actual, "2509000000");
2019-08-11 19:46:14 +02:00
}
#[test]
fn replaces() {
2019-08-29 02:32:42 +02:00
Playground::setup("plugin_str_test_4", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nushell"
"#,
)]);
let actual = nu!(
2019-08-29 08:31:56 +02:00
cwd: dirs.test(), h::pipeline(
2019-08-29 02:32:42 +02:00
r#"
open sample.toml
| str package.name --replace wykittenshell
| get package.name
| echo $it
"#
));
assert_eq!(actual, "wykittenshell");
})
2019-08-11 19:46:14 +02:00
}
#[test]
fn find_and_replaces() {
2019-08-29 02:32:42 +02:00
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-08-29 08:31:56 +02:00
cwd: dirs.test(), h::pipeline(
2019-08-29 02:32:42 +02:00
r#"
open sample.toml
| str fortune.teller.phone --find-replace KATZ "5289"
| get fortune.teller.phone
| echo $it
"#
));
assert_eq!(actual, "1-800-5289");
})
2019-08-11 19:46:14 +02:00
}
#[test]
fn find_and_replaces_without_passing_field() {
2019-08-29 02:32:42 +02:00
Playground::setup("plugin_str_test_6", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
2019-08-11 19:46:14 +02:00
"sample.toml",
2019-08-29 02:32:42 +02:00
r#"
[fortune.teller]
phone = "1-800-KATZ"
"#,
)]);
let actual = nu!(
2019-08-29 08:31:56 +02:00
cwd: dirs.test(), h::pipeline(
2019-08-11 19:46:14 +02:00
r#"
2019-08-29 02:32:42 +02:00
open sample.toml
| get fortune.teller.phone
| str --find-replace KATZ "5289"
| echo $it
"#
));
assert_eq!(actual, "1-800-5289");
})
2019-08-11 19:46:14 +02:00
}