nushell/crates/nu-command/tests/commands/skip/while_.rs
Jakub Žádník 2bacc29d30
Replace row conditions with closures in commands (#7428)
# Description

This PR changes some commands that previously accepted row conditions
(like `$it > 5`) as parameter to accept closures instead. The reasons
are:
a) The commands would need to move into parser keywords in the future
while they feel more like commands to be implemented in Nushell code as
a part of standard library.
b) In scripts, it is useful to store the predicate condition in a
variable which you can't do with row conditions.
c) These commands are not used that often to benefit enough from the
shorter row condition syntax

# User-Facing Changes

The following commands now accept **closure** instead of a **row
condition**:
- `take until`
- `take while`
- `skip until`
- `skip while`
- `any`
- `all`

This is a part of an effort to move away from shape-directed parsing.
Related PR: https://github.com/nushell/nushell/pull/7365

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-10 19:24:06 +02:00

52 lines
1.6 KiB
Rust

use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn condition_is_met() {
Playground::setup("skip_while_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"caballeros.txt",
r#"
CHICKEN SUMMARY report date: April 29th, 2020
--------------------------------------------------------------------
Chicken Collection,29/04/2020,30/04/2020,31/04/2020
Yellow Chickens,,,
Andrés,0,0,1
Jonathan,0,0,1
Jason,0,0,1
Yehuda,0,0,1
Blue Chickens,,,
Andrés,0,0,1
Jonathan,0,0,1
Jason,0,0,1
Yehuda,0,0,2
Red Chickens,,,
Andrés,0,0,1
Jonathan,0,0,1
Jason,0,0,1
Yehuda,0,0,3
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open --raw caballeros.txt
| lines
| skip 2
| str trim
| str join (char nl)
| from csv
| skip while {|row| $row."Chicken Collection" != "Red Chickens" }
| skip 1
| into int "31/04/2020"
| get "31/04/2020"
| math sum
"#
));
assert_eq!(actual.out, "6");
})
}