nushell/crates/nu-command/tests/commands/drop.rs
Andrés N. Robalino 19d5f782cc
Allow dropping columns. (#3107)
`drop` is used for removing the last row. Passing a number allows dropping N rows.
Here we introduce the same logic for dropping columns instead.

You can certainly remove columns by using `reject`, however, there could be cases
where we are interested in removing columns from tables that contain, say, a big
number of columns. Using `reject` becomes impractical, especially when you don't
care about the column names that could either be known or not known when exploring
tables.

```
> echo [[lib, extension]; [nu-core, rs] [rake, rb]]
─────────┬───────────
   lib   │ extension
─────────┼───────────
 nu-core │ rs
 rake    │ rb
─────────┴───────────
```

```
> echo [[lib, extension]; [nu-core, rs] [rake, rb]] | drop column
─────────
   lib
─────────
 nu-core
 rake
─────────
```
2021-02-25 15:37:21 -05:00

69 lines
1.2 KiB
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn columns() {
let actual = nu!(
cwd: ".", pipeline(r#"
echo [
[arepas, color];
[3, white]
[8, yellow]
[4, white]
]
| drop column
| get
| count
"#)
);
assert_eq!(actual.out, "1");
}
#[test]
fn more_columns_than_table_has() {
let actual = nu!(
cwd: ".", pipeline(r#"
echo [
[arepas, color];
[3, white]
[8, yellow]
[4, white]
]
| drop column 3
| get
| empty?
"#)
);
assert_eq!(actual.out, "true");
}
#[test]
fn rows() {
let actual = nu!(
cwd: ".", pipeline(r#"
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!(cwd: ".", "date | drop 50 | count");
assert_eq!(actual.out, "0");
}