nushell/crates/nu-command/tests/commands/bytes/at.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

78 lines
2.4 KiB
Rust

use nu_test_support::nu;
#[test]
pub fn returns_error_for_relative_range_on_infinite_stream() {
let actual = nu!("nu --testbin iecho 3 | bytes at ..-3");
assert!(
actual.err.contains(
"Relative range values cannot be used with streams that don't have a known length"
),
"Expected error message for negative range with infinite stream"
);
}
#[test]
pub fn returns_bytes_for_fixed_range_on_infinite_stream_including_end() {
let actual = nu!("nu --testbin iecho 3 | bytes at ..10 | decode");
assert_eq!(
actual.out, "333333",
"Expected bytes from index 0 to 10, but got different output"
);
let actual = nu!("nu --testbin iecho 3 | bytes at ..10 | decode");
assert_eq!(
actual.out, "333333",
"Expected bytes from index 0 to 10, but got different output"
);
}
#[test]
pub fn returns_bytes_for_fixed_range_on_infinite_stream_excluding_end() {
let actual = nu!("nu --testbin iecho 3 | bytes at ..<9 | decode");
assert_eq!(
actual.out, "33333",
"Expected bytes from index 0 to 8, but got different output"
);
}
#[test]
pub fn test_string_returns_correct_slice_for_simple_positive_slice() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at ..4 | decode");
assert_eq!(actual.out, "Hello");
}
#[test]
pub fn test_string_returns_correct_slice_for_negative_start() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at (-5)..10 | decode");
assert_eq!(actual.out, "World");
}
#[test]
pub fn test_string_returns_correct_slice_for_negative_end() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at ..-7 | decode");
assert_eq!(actual.out, "Hello");
}
#[test]
pub fn test_string_returns_correct_slice_for_empty_slice() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at 5..<5 | decode");
assert_eq!(actual.out, "");
}
#[test]
pub fn test_string_returns_correct_slice_for_out_of_bounds() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at ..20 | decode");
assert_eq!(actual.out, "Hello World");
}
#[test]
pub fn test_string_returns_correct_slice_for_invalid_range() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at 11..5 | decode");
assert_eq!(actual.out, "");
}
#[test]
pub fn test_string_returns_correct_slice_for_max_end() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at 6..<11 | decode");
assert_eq!(actual.out, "World");
}