Files
nushell/crates/nu-command/tests/commands/drop.rs
Kumar Ujjawal 2a8364d259 drop nth command supports spreadable arguments (#15897)
##  Improve `drop nth` command to support spreadable arguments

### Summary

This PR updates the `drop nth` command to support **spreadable
arguments** in a way consistent with other commands like `which`,
enabling:

```nu
[1 2 3 4 5] | drop nth 0 2 4
```

### What's Changed

* **Previously**: only a single index or a single range was accepted as
the first argument, with rest arguments ignored for ranges.

* **Now**: the command accepts any combination of:

  * Integers: to drop individual rows
  * Ranges: to drop slices of rows
  * Unbounded ranges: like `3..`, to drop from index onward

Example:

```nu
[one two three four five six] | drop nth 0 2 4..5
# drops "one", "three", "five", and "six"
```

### Test 

Manual Test:

![nu-dron_n](https://github.com/user-attachments/assets/02f3988c-ac02-4245-967c-16a9604be406)


### Notes

As per feedback:

* We **only collect the list of indices** to drop, not the input stream.
* Unbounded ranges are handled by terminating the stream early.

Let me know if you'd like further changes

---------

Co-authored-by: Kumar Ujjawal <kumar.ujjawal@greenpista.com>
Co-authored-by: Kumar Ujjawal <kumarujjawal@Kumars-MacBook-Air.local>
2025-06-21 15:57:14 -04:00

210 lines
4.2 KiB
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn columns() {
let actual = nu!(pipeline(
"
echo [
[arepas, color];
[3, white]
[8, yellow]
[4, white]
] | drop column | columns | length
"
));
assert_eq!(actual.out, "1");
}
#[test]
fn drop_columns_positive_value() {
let actual = nu!("echo [[a, b];[1,2]] | drop column -1");
assert!(actual.err.contains("use a positive value"));
}
#[test]
fn more_columns_than_table_has() {
let actual = nu!(pipeline(
"
echo [
[arepas, color];
[3, white]
[8, yellow]
[4, white]
] | drop column 3 | columns | is-empty
"
));
assert_eq!(actual.out, "true");
}
#[test]
fn rows() {
let actual = nu!(pipeline(
"
echo [
[arepas];
[3]
[8]
[4]
]
| drop 2
| get arepas
| math sum
"
));
assert_eq!(actual.out, "3");
}
#[test]
fn more_rows_than_table_has() {
let actual = nu!("[date] | drop 50 | length");
assert_eq!(actual.out, "0");
}
#[test]
fn nth_range_inclusive() {
let actual = nu!("echo 10..15 | drop nth (2..3) | to json --raw");
assert_eq!(actual.out, "[10,11,14,15]");
}
#[test]
fn nth_range_exclusive() {
let actual = nu!("echo 10..15 | drop nth (1..<3) | to json --raw");
assert_eq!(actual.out, "[10,13,14,15]");
}
#[test]
fn nth_missing_first_argument() {
let actual = nu!("echo 10..15 | drop nth \"\"");
assert!(actual.err.contains("int or range"));
}
#[test]
fn fail_on_non_iterator() {
let actual = nu!("1 | drop 50");
assert!(actual.err.contains("command doesn't support"));
}
#[test]
fn disjoint_columns_first_row_becomes_empty() {
let actual = nu!(pipeline(
"
[{a: 1}, {b: 2, c: 3}]
| drop column
| columns
| to nuon
"
));
assert_eq!(actual.out, "[b, c]");
}
#[test]
fn disjoint_columns() {
let actual = nu!(pipeline(
"
[{a: 1, b: 2}, {c: 3}]
| drop column
| columns
| to nuon
"
));
assert_eq!(actual.out, "[a, c]");
}
#[test]
fn record() {
let actual = nu!("{a: 1, b: 2} | drop column | to nuon");
assert_eq!(actual.out, "{a: 1}");
}
#[test]
fn more_columns_than_record_has() {
let actual = nu!("{a: 1, b: 2} | drop column 3 | to nuon");
assert_eq!(actual.out, "{}");
}
#[test]
fn drop_single_index() {
let actual = nu!("echo 10..15 | drop nth 2 | to json --raw");
assert_eq!(actual.out, "[10,11,13,14,15]");
}
#[test]
fn drop_multiple_indices() {
let actual = nu!("echo 0..10 | drop nth 1 3 | to json --raw");
assert_eq!(actual.out, "[0,2,4,5,6,7,8,9,10]");
}
#[test]
fn drop_inclusive_range() {
let actual = nu!("echo 10..15 | drop nth (2..4) | to json --raw");
assert_eq!(actual.out, "[10,11,15]");
}
#[test]
fn drop_exclusive_range() {
let actual = nu!("echo 10..15 | drop nth (2..<4) | to json --raw");
assert_eq!(actual.out, "[10,11,14,15]");
}
#[test]
fn drop_unbounded_range() {
let actual = nu!("echo 0..5 | drop nth 3.. | to json --raw");
assert_eq!(actual.out, "[0,1,2]");
}
#[test]
fn drop_multiple_ranges_including_unbounded() {
let actual = nu!(pipeline(
r#"
0..30
| drop nth 0..10 20..
| to json --raw
"#
));
assert_eq!(actual.out, "[11,12,13,14,15,16,17,18,19]");
}
#[test]
fn drop_combination_of_unbounded_range_and_single_index() {
let actual = nu!(pipeline(
r#"
echo 0..15
| drop nth 10.. 5
| to json --raw
"#
));
assert_eq!(actual.out, "[0,1,2,3,4,6,7,8,9]");
}
#[test]
fn drop_combination_of_two_unbounded_ranges() {
let actual = nu!(pipeline(
r#"
echo 0..150
| drop nth 0..100 999..
| to json --raw
"#
));
let expected: Vec<u32> = (101..=150).collect();
let expected_json = serde_json::to_string(&expected).unwrap();
assert_eq!(actual.out, expected_json);
}