nushell/crates/nu-command/tests/commands/slice.rs
Bahex b55ed69c92
fix range bugs in str substring, str index-of, slice, bytes at (#14863)
- fixes #14769

# Description

## Bugs

-   `str substring 0..<0`

When passed a range containing no elements, for non-zero cases `str
substring` behaves correctly:
 
    ```nushell
    ("hello world" | str substring 1..<1) == ""
    # => true
    ```

    but if the range is `0..<0`, it returns the whole string instead

    ```nushell
    "hello world" | str substring 0..<0
    # => hello world
    ```
-   `[0 1 2] | range 0..<0`
    Similar behavior to `str substring`
-   `str index-of`
    - off-by-one on end bounds
    - underflow on negative start bounds
- `bytes at` has inconsistent behavior, works correctly when the size is
known, returns one byte less when it's not known (streaming)
This can be demonstrated by comparing the outputs of following snippets
    ```nushell
    "hello world" | into binary | bytes at ..<5 | decode
    # => hello

"hello world" | into binary | chunks 1 | bytes collect | bytes at ..<5 |
decode
    # => hell
    ```
- `bytes at` panics on decreasing (`5..3`) ranges if the input size is
known. Does not panic with streaming input.

## Changes

- implement `FromValue` for `IntRange`, as it is very common to use
integer ranges as arguments
- `IntRange::absolute_start` can now point one-past-end
- `IntRange::absolute_end` converts relative `Included` bounds to
absolute `Excluded` bounds
- `IntRange::absolute_bounds` is a convenience method that calls the
other `absolute_*` methods and transforms reverse ranges to empty at
`start` (`5..3` => `5..<5`)
- refactored `str substring` tests to allow empty exclusive range tests
- fix the `0..<0` case for `str substring` and `str index-of`
- `IntRange::distance` never returns `Included(0)`

  As a general rule `Included(n) == Excluded(n + 1)`.
  
This makes returning `Included(0)` bug prone as users of the function
will likely rely on this general rule and cause bugs.
- `ByteStream::slice` no longer has an off-by-one on inputs without a
known size. This affected `bytes at`.
- `bytes at` no longer panics on reverse ranges
- `bytes at` is now consistent between streaming and non streaming
inputs.

# User-Facing Changes
There should be no noticeable changes other than the bugfix.

# Tests + Formatting

- 🟢 toolkit fmt
- 🟢 toolkit clippy
- 🟢 toolkit test
- 🟢 toolkit test stdlib

# After Submitting
N/A
2025-01-30 06:50:01 -06:00

83 lines
1.8 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("slice_test_1", |dirs, sandbox| {
sandbox.with_files(&[EmptyFile("notes.txt"), EmptyFile("tests.txt")]);
let actual = nu!(
cwd: dirs.test(), pipeline(
"
ls
| sort-by name
| slice 0..0
| get name.0
"
));
assert_eq!(actual.out, "notes.txt");
});
}
#[test]
fn selects_some_rows() {
Playground::setup("slice_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
| slice 1..2
| length
"
));
assert_eq!(actual.out, "2");
});
}
#[test]
fn negative_indices() {
Playground::setup("slice_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
| slice (-1..)
| length
"
));
assert_eq!(actual.out, "1");
});
}
#[test]
fn zero_to_zero_exclusive() {
let actual = nu!(r#"[0 1 2 3] | slice 0..<0 | to nuon"#);
assert_eq!(actual.out, "[]");
}
#[test]
fn to_negative_one_inclusive() {
let actual = nu!(r#"[0 1 2 3] | slice 2..-1 | to nuon"#);
assert_eq!(actual.out, "[2, 3]");
}