nushell/crates/nu-command/tests/commands/take/until.rs

60 lines
1.8 KiB
Rust
Raw Normal View History

use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn condition_is_met() {
2022-04-07 22:49:28 +02:00
Playground::setup("take_until_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,1,1,1
Jonathan,1,1,1
Jason,1,1,1
Yehuda,1,1,1
Blue Chickens,,,
Andrés,1,1,2
Jonathan,1,1,2
Jason,1,1,2
Yehuda,1,1,2
Red Chickens,,,
Andrés,1,1,3
Jonathan,1,1,3
Jason,1,1,3
Yehuda,1,1,3
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open --raw caballeros.txt
| lines
| skip 2
| str trim
| str join (char nl)
| from csv
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 18:24:06 +01:00
| skip while {|row| $row."Chicken Collection" != "Blue Chickens" }
| take until {|row| $row."Chicken Collection" == "Red Chickens" }
| skip 1
| into int "31/04/2020"
| get "31/04/2020"
| math sum
"#
));
assert_eq!(actual.out, "8");
})
}
#[test]
fn fail_on_non_iterator() {
let actual = nu!(cwd: ".", pipeline("1 | take until {|row| $row == 2}"));
assert!(actual.err.contains("only_supports_this_input_type"));
}