nushell/crates/nu-command/tests/commands/str_/mod.rs

408 lines
9.6 KiB
Rust
Raw Normal View History

mod into_string;
Add string/binary type color to `ByteStream` (#12897) # Description This PR allows byte streams to optionally be colored as being specifically binary or string data, which guarantees that they'll be converted to `Binary` or `String` appropriately on `into_value()`, making them compatible with `Type` guarantees. This makes them significantly more broadly usable for command input and output. There is still an `Unknown` type for byte streams coming from external commands, which uses the same behavior as we previously did where it's a string if it's UTF-8. A small number of commands were updated to take advantage of this, just to prove the point. I will be adding more after this merges. # User-Facing Changes - New types in `describe`: `string (stream)`, `binary (stream)` - These commands now return a stream if their input was a stream: - `into binary` - `into string` - `bytes collect` - `str join` - `first` (binary) - `last` (binary) - `take` (binary) - `skip` (binary) - Streams that are explicitly binary colored will print as a streaming hexdump - example: ```nushell 1.. | each { into binary } | bytes collect ``` # Tests + Formatting I've added some tests to cover it at a basic level, and it doesn't break anything existing, but I do think more would be nice. Some of those will come when I modify more commands to stream. # After Submitting There are a few things I'm not quite satisfied with: - **String trimming behavior.** We automatically trim newlines from streams from external commands, but I don't think we should do this with internal commands. If I call a command that happens to turn my string into a stream, I don't want the newline to suddenly disappear. I changed this to specifically do it only on `Child` and `File`, but I don't know if this is quite right, and maybe we should bring back the old flag for `trim_end_newline` - **Known binary always resulting in a hexdump.** It would be nice to have a `print --raw`, so that we can put binary data on stdout explicitly if we want to. This PR doesn't change how external commands work though - they still dump straight to stdout. Otherwise, here's the normal checklist: - [ ] release notes - [ ] docs update for plugin protocol changes (added `type` field) --------- Co-authored-by: Ian Manske <ian.manske@pm.me>
2024-05-20 02:35:32 +02:00
mod join;
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(&[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");
})
}
2020-07-13 07:45:34 +02:00
#[test]
fn error_trim_multiple_chars() {
let actual = nu!(pipeline(
2020-07-13 07:45:34 +02:00
r#"
2023-10-19 22:08:09 +02:00
echo "does it work now?!" | str trim --char "?!"
2020-07-13 07:45:34 +02:00
"#
));
2020-07-13 07:45:34 +02:00
assert!(actual.err.contains("char"));
}
#[test]
fn capitalizes() {
Playground::setup("str_test_2", |dirs, sandbox| {
sandbox.with_files(&[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(&[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 non_ascii_downcase() {
let actual = nu!("'ὈΔΥΣΣΕΎΣ' | str downcase");
assert_eq!(actual.out, "ὀδυσσεύς");
}
#[test]
fn upcases() {
Playground::setup("str_test_4", |dirs, sandbox| {
sandbox.with_files(&[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 non_ascii_upcase() {
let actual = nu!("'ὀδυσσεύς' | str upcase");
assert_eq!(actual.out, "ὈΔΥΣΣΕΎΣ");
}
#[test]
#[ignore = "Playgrounds are not supported in nu-cmd-extra"]
fn camelcases() {
Playground::setup("str_test_3", |dirs, sandbox| {
sandbox.with_files(&[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!(pipeline(
r#"
echo '[{number_as_string: "1"}]'
| from json
| into int number_as_string
| rename number
| where number == 1
2022-02-18 23:11:27 +01:00
| get number.0
"#
));
assert_eq!(actual.out, "1");
}
#[test]
fn converts_to_float() {
let actual = nu!(pipeline(
r#"
echo "3.1, 0.0415"
| split row ","
| into float
| math sum
"#
));
assert_eq!(actual.out, "3.1415");
}
#[test]
fn find_and_replaces() {
Playground::setup("str_test_6", |dirs, sandbox| {
sandbox.with_files(&[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(&[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 -r '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(&[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_empty_if_start_index_is_greater_than_end_index() {
Playground::setup("str_test_9", |dirs, sandbox| {
sandbox.with_files(&[FileWithContent(
"sample.toml",
r#"
[fortune.teller]
phone = "1-800-ROBALINO"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| str substring 6..4 fortune.teller.phone
| get fortune.teller.phone
"#
));
assert_eq!(actual.out, "")
})
}
#[test]
fn substrings_the_input_and_returns_the_string_if_end_index_exceeds_length() {
Playground::setup("str_test_10", |dirs, sandbox| {
sandbox.with_files(&[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(&[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(&[FileWithContent(
"sample.toml",
r#"
[package]
name = "nu-arepas"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| str substring ..1 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(&[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 substring_by_negative_index() {
Playground::setup("str_test_13", |dirs, _| {
let actual = nu!(
cwd: dirs.test(), "'apples' | str substring 0..-1",
);
assert_eq!(actual.out, "apples");
let actual = nu!(
cwd: dirs.test(), "'apples' | str substring 0..<-1",
);
assert_eq!(actual.out, "apple");
})
}
#[test]
fn str_reverse() {
let actual = nu!(r#"
echo "nushell" | str reverse
"#);
assert!(actual.out.contains("llehsun"));
}
#[test]
fn test_redirection_trim() {
let actual = nu!(r#"
let x = (nu --testbin cococo niceone); $x | str trim | str length
"#);
assert_eq!(actual.out, "7");
}