nushell/crates/nu-command/tests/commands/range.rs
Stefan Holderbach 406df7f208
Avoid taking unnecessary ownership of intermediates (#12740)
# Description

Judiciously try to avoid allocations/clone by changing the signature of
functions

- **Don't pass str by value unnecessarily if only read**
- **Don't require a vec in `Sandbox::with_files`**
- **Remove unnecessary string clone**
- **Fixup unnecessary borrow**
- **Use `&str` in shape color instead**
- **Vec -> Slice**
- **Elide string clone**
- **Elide `Path` clone**
- **Take &str to elide clone in tests**

# User-Facing Changes
None

# Tests + Formatting
This touches many tests purely in changing from owned to borrowed/static
data
2024-05-04 00:53:15 +00:00

68 lines
1.5 KiB
Rust

use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn selects_a_row() {
Playground::setup("range_test_1", |dirs, sandbox| {
sandbox.with_files(&[EmptyFile("notes.txt"), EmptyFile("tests.txt")]);
let actual = nu!(
cwd: dirs.test(), pipeline(
"
ls
| sort-by name
| range 0..0
| get name.0
"
));
assert_eq!(actual.out, "notes.txt");
});
}
#[test]
fn selects_some_rows() {
Playground::setup("range_test_2", |dirs, sandbox| {
sandbox.with_files(&[
EmptyFile("notes.txt"),
EmptyFile("tests.txt"),
EmptyFile("persons.txt"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
"
ls
| get name
| range 1..2
| length
"
));
assert_eq!(actual.out, "2");
});
}
#[test]
fn negative_indices() {
Playground::setup("range_test_negative_indices", |dirs, sandbox| {
sandbox.with_files(&[
EmptyFile("notes.txt"),
EmptyFile("tests.txt"),
EmptyFile("persons.txt"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
"
ls
| get name
| range (-1..)
| length
"
));
assert_eq!(actual.out, "1");
});
}