nushell/crates/nu-protocol/tests/pipeline/byte_stream.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

286 lines
8.8 KiB
Rust

use nu_protocol::{ast::RangeInclusion, ByteStream, IntRange, Signals, Span, Value};
#[test]
pub fn test_simple_positive_slice_exclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(0, 5, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "Hello");
}
#[test]
pub fn test_simple_positive_slice_exclusive_streaming() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.with_known_size(None)
.slice(
Span::test_data(),
Span::test_data(),
create_range(0, 5, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "Hello");
}
#[test]
pub fn test_negative_start_exclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(-5, 11, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "World");
}
#[test]
pub fn test_negative_end_exclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(0, -6, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "Hello");
}
#[test]
pub fn test_negative_start_and_end_exclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(-5, -2, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "Wor");
}
#[test]
pub fn test_empty_slice_exclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(5, 5, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "");
}
#[test]
pub fn test_out_of_bounds_exclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(0, 20, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "Hello World");
}
#[test]
pub fn test_invalid_range_exclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(11, 5, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "");
}
#[test]
pub fn test_max_end_exclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(6, i64::MAX, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "World");
}
#[test]
pub fn test_simple_positive_slice_inclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(0, 5, RangeInclusion::RightExclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "Hello");
}
#[test]
pub fn test_negative_start_inclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(-5, 11, RangeInclusion::Inclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "World");
}
#[test]
pub fn test_negative_end_inclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(0, -7, RangeInclusion::Inclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "Hello");
}
#[test]
pub fn test_negative_start_and_end_inclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(-5, -1, RangeInclusion::Inclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "World");
}
#[test]
pub fn test_empty_slice_inclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(5, 5, RangeInclusion::Inclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, " ");
}
#[test]
pub fn test_out_of_bounds_inclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(0, 20, RangeInclusion::Inclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "Hello World");
}
#[test]
pub fn test_invalid_range_inclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(11, 5, RangeInclusion::Inclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "");
}
#[test]
pub fn test_max_end_inclusive() {
let data = b"Hello World".to_vec();
let stream = ByteStream::read_binary(data, Span::test_data(), Signals::empty());
let sliced = stream
.slice(
Span::test_data(),
Span::test_data(),
create_range(6, i64::MAX, RangeInclusion::Inclusive),
)
.unwrap();
let result = sliced.into_bytes().unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "World");
}
fn create_range(start: i64, end: i64, inclusion: RangeInclusion) -> IntRange {
IntRange::new(
Value::int(start, Span::unknown()),
Value::nothing(Span::test_data()),
Value::int(end, Span::unknown()),
inclusion,
Span::unknown(),
)
.unwrap()
}