Add named positionals to all (#3863)

This commit is contained in:
JT 2021-07-30 09:12:24 +12:00 committed by GitHub
parent 9c016ad479
commit f3e487e829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -96,7 +96,11 @@ fn all(args: CommandArgs) -> Result<OutputStream, ShellError> {
let result = args.input.fold(init, move |acc, row| {
let condition = condition.clone();
let ctx = ctx.clone();
ctx.scope.add_var("$it", row);
if let Some(positional) = all_args.predicate.block.params.positional.get(0) {
ctx.scope.add_var(positional.0.name(), row);
} else {
ctx.scope.add_var("$it", row);
}
let condition = evaluate_baseline_expr(&condition, &ctx);

View File

@ -19,6 +19,34 @@ fn checks_all_rows_are_true() {
})
}
#[test]
fn checks_all_rows_are_false_with_param() {
Playground::setup("all_test_1", |_, nu| {
assert_that!(
nu.pipeline(&input(
r#"
[1, 2, 3, 4] | all? { |a| $a >= 5 }
"#
)),
says().stdout("false")
);
})
}
#[test]
fn checks_all_rows_are_true_with_param() {
Playground::setup("all_test_1", |_, nu| {
assert_that!(
nu.pipeline(&input(
r#"
[1, 2, 3, 4] | all? { |a| $a < 5 }
"#
)),
says().stdout("true")
);
})
}
#[test]
fn checks_all_columns_of_a_table_is_true() {
Playground::setup("any_test_1", |_, nu| {