nushell/crates/nu-command/tests/commands/default.rs
Ian Manske 56cdee1fd8
Refactor first and last (#12478)
# Description

- Refactors `first` and `last` using `Vec::truncate` and `Vec::drain`.
- `std::mem::take` was also used to eliminate a few `Value` clones.
- The `NeedsPositiveValue` error now uses the span of the `rows`
argument instead of the call head span.
- `last` now errors on an empty stream to match `first` which does
error.
-  Made metadata preservation more consistent.

# User-Facing Changes
Breaking change: `last` now errors on an empty stream to match `first`
which does error.
2024-04-13 14:58:54 +00:00

34 lines
845 B
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn adds_row_data_if_column_missing() {
let sample = r#"
{
"amigos": [
{"name": "Yehuda"},
{"name": "JT", "rusty_luck": 0},
{"name": "Andres", "rusty_luck": 0},
{"name":"GorbyPuff"}
]
}
"#;
let actual = nu!(pipeline(&format!(
"
{sample}
| get amigos
| default 1 rusty_luck
| where rusty_luck == 1
| length
"
)));
assert_eq!(actual.out, "2");
}
#[test]
fn default_after_empty_filter() {
let actual = nu!("[a b] | where $it == 'c' | get -i 0 | default 'd'");
assert_eq!(actual.out, "d");
}