nushell/crates/nu-command/tests/commands/str_/mod.rs
JT 97e7d550c8
move 'str substring' to only use ranges (#8660)
# Description

This removes all the old style of quasi-ranges before we had full range
support from `str substring`. Functionality should otherwise work, but
only with the official range syntax.

# User-Facing Changes

Removes the array and string forms of ranges from `str substring`.
Leaves only the official range support for range values.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-03-29 20:01:42 +13:00

389 lines
9.1 KiB
Rust

mod collect;
mod into_string;
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn trims() {
Playground::setup("str_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[dependency]
name = "nu "
"#,
)]);
let actual = nu!(
cwd: dirs.test(),
"open sample.toml | str trim dependency.name | get dependency.name"
);
assert_eq!(actual.out, "nu");
})
}
#[test]
fn error_trim_multiple_chars() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo "does it work now?!" | str trim -c "?!"
"#
)
);
assert!(actual.err.contains("char"));
}
#[test]
fn capitalizes() {
Playground::setup("str_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[dependency]
name = "nu"
"#,
)]);
let actual = nu!(
cwd: dirs.test(),
"open sample.toml | str capitalize dependency.name | get dependency.name"
);
assert_eq!(actual.out, "Nu");
})
}
#[test]
fn downcases() {
Playground::setup("str_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[dependency]
name = "LIGHT"
"#,
)]);
let actual = nu!(
cwd: dirs.test(),
"open sample.toml | str downcase dependency.name | get dependency.name"
);
assert_eq!(actual.out, "light");
})
}
#[test]
fn upcases() {
Playground::setup("str_test_4", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nushell"
"#,
)]);
let actual = nu!(
cwd: dirs.test(),
"open sample.toml | str upcase package.name | get package.name"
);
assert_eq!(actual.out, "NUSHELL");
})
}
#[test]
fn camelcases() {
Playground::setup("str_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[dependency]
name = "THIS_IS_A_TEST"
"#,
)]);
let actual = nu!(
cwd: dirs.test(),
"open sample.toml | str camel-case dependency.name | get dependency.name"
);
assert_eq!(actual.out, "thisIsATest");
})
}
#[test]
fn converts_to_int() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo '[{number_as_string: "1"}]'
| from json
| into int number_as_string
| rename number
| where number == 1
| get number.0
"#
));
assert_eq!(actual.out, "1");
}
#[test]
fn converts_to_decimal() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo "3.1, 0.0415"
| split row ","
| into decimal
| math sum
"#
));
assert_eq!(actual.out, "3.1415");
}
#[test]
fn find_and_replaces() {
Playground::setup("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
| str replace KATZ "5289" fortune.teller.phone
| get fortune.teller.phone
"#
));
assert_eq!(actual.out, "1-800-5289");
})
}
#[test]
fn find_and_replaces_without_passing_field() {
Playground::setup("str_test_7", |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
| str replace KATZ "5289"
"#
));
assert_eq!(actual.out, "1-800-5289");
})
}
#[test]
fn regex_error_in_pattern() {
Playground::setup("str_test_8", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
'source string'
| str replace 'source \Ufoo' "destination"
"#
));
let err = actual.err;
let expecting_str = "Incorrect value";
assert!(
err.contains(expecting_str),
"Error should contain '{expecting_str}', but was: {err}"
);
})
}
#[test]
fn substrings_the_input() {
Playground::setup("str_test_8", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[fortune.teller]
phone = "1-800-ROBALINO"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| str substring 6..14 fortune.teller.phone
| get fortune.teller.phone
"#
));
assert_eq!(actual.out, "ROBALINO");
})
}
#[test]
fn substring_errors_if_start_index_is_greater_than_end_index() {
Playground::setup("str_test_9", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[fortune.teller]
phone = "1-800-ROBALINO"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| str substring 6..5 fortune.teller.phone
"#
));
assert!(actual
.err
.contains("End must be greater than or equal to Start"))
})
}
#[test]
fn substrings_the_input_and_returns_the_string_if_end_index_exceeds_length() {
Playground::setup("str_test_10", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nu-arepas"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| str substring 0..999 package.name
| get package.name
"#
));
assert_eq!(actual.out, "nu-arepas");
})
}
#[test]
fn substrings_the_input_and_returns_blank_if_start_index_exceeds_length() {
Playground::setup("str_test_11", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nu-arepas"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| str substring 50..999 package.name
| get package.name
"#
));
assert_eq!(actual.out, "");
})
}
#[test]
fn substrings_the_input_and_treats_start_index_as_zero_if_blank_start_index_given() {
Playground::setup("str_test_12", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nu-arepas"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| str substring ..2 package.name
| get package.name
"#
));
assert_eq!(actual.out, "nu");
})
}
#[test]
fn substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given() {
Playground::setup("str_test_13", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nu-arepas"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| str substring 3.. package.name
| get package.name
"#
));
assert_eq!(actual.out, "arepas");
})
}
#[test]
fn str_reverse() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo "nushell" | str reverse
"#
));
assert!(actual.out.contains("llehsun"));
}
#[test]
fn test_redirection_trim() {
let actual = nu!(
cwd: ".", pipeline(
r#"
let x = (nu --testbin cococo niceone); $x | str trim | str length
"#
));
assert_eq!(actual.out, "7");
}