mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Allow iteration blocks to have an optional extra index parameter (alternative to -n
flags) (#6994)
Alters `all`, `any`, `each while`, `each`, `insert`, `par-each`, `reduce`, `update`, `upsert` and `where`, so that their blocks take an optional parameter containing the index.
This commit is contained in:
@ -108,6 +108,16 @@ fn early_exits_with_0_param_blocks() {
|
||||
assert_eq!(actual.out, "1false");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_optional_index_argument() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[7 8 9] | all {|el ind| print $ind | true }"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "012true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unique_env_each_iteration() {
|
||||
let actual = nu!(
|
||||
|
@ -84,6 +84,16 @@ fn early_exits_with_0_param_blocks() {
|
||||
assert_eq!(actual.out, "1true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_optional_index_argument() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[7 8 9] | any {|el ind| print $ind | false }"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "012false");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unique_env_each_iteration() {
|
||||
let actual = nu!(
|
||||
|
@ -71,3 +71,33 @@ fn each_implicit_it_in_block() {
|
||||
|
||||
assert_eq!(actual.out, "ace");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_optional_index_argument() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[7 8 9 10] | each {|el ind| $ind } | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "[0, 1, 2, 3]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_while_uses_optional_index_argument() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[7 8 9 10] | each while {|el ind| $ind } | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "[0, 1, 2, 3]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn par_each_uses_optional_index_argument() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[7 8 9 10] | par-each {|el ind| $ind } | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "[0, 1, 2, 3]");
|
||||
}
|
||||
|
@ -14,6 +14,15 @@ fn insert_the_column() {
|
||||
assert_eq!(actual.out, "0.7.0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doesnt_convert_record_to_table() {
|
||||
let actual = nu!(
|
||||
cwd: ".", r#"{a:1} | insert b 2 | to nuon"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "{a: 1, b: 2}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_the_column_conflict() {
|
||||
let actual = nu!(
|
||||
@ -76,3 +85,13 @@ fn insert_past_end_list() {
|
||||
|
||||
assert_eq!(actual.out, r#"[1,2,3,null,null,"abc"]"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_optional_index_argument() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[[a]; [7] [6]] | insert b {|el ind| $ind + 1 + $el.a } | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "[[a, b]; [7, 8], [6, 8]]");
|
||||
}
|
||||
|
@ -119,3 +119,13 @@ fn error_reduce_empty() {
|
||||
|
||||
assert!(actual.err.contains("needs input"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_optional_index_argument() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[18 19 20] | reduce -f 0 {|elem accum index| $accum + $index } | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "3");
|
||||
}
|
||||
|
@ -14,6 +14,15 @@ fn sets_the_column() {
|
||||
assert_eq!(actual.out, "0.7.0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doesnt_convert_record_to_table() {
|
||||
let actual = nu!(
|
||||
cwd: ".", r#"{a:1} | update a 2 | to nuon"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "{a: 2}");
|
||||
}
|
||||
|
||||
#[cfg(features = "inc")]
|
||||
#[test]
|
||||
fn sets_the_column_from_a_block_run_output() {
|
||||
@ -105,3 +114,13 @@ fn update_nonexistent_column() {
|
||||
|
||||
assert!(actual.err.contains("cannot find column 'b'"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_optional_index_argument() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[[a]; [7] [6]] | update a {|el ind| $ind + 1 + $el.a } | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "[[a]; [8], [8]]");
|
||||
}
|
||||
|
@ -14,6 +14,15 @@ fn sets_the_column() {
|
||||
assert_eq!(actual.out, "0.7.0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doesnt_convert_record_to_table() {
|
||||
let actual = nu!(
|
||||
cwd: ".", r#"{a:1} | upsert a 2 | to nuon"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "{a: 2}");
|
||||
}
|
||||
|
||||
#[cfg(features = "inc")]
|
||||
#[test]
|
||||
fn sets_the_column_from_a_block_run_output() {
|
||||
@ -58,3 +67,23 @@ fn sets_the_column_from_a_subexpression() {
|
||||
|
||||
assert_eq!(actual.out, "true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_optional_index_argument_inserting() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[[a]; [7] [6]] | upsert b {|el ind| $ind + 1 + $el.a } | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "[[a, b]; [7, 8], [6, 8]]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_optional_index_argument_updating() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[[a]; [7] [6]] | upsert a {|el ind| $ind + 1 + $el.a } | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "[[a]; [8], [8]]");
|
||||
}
|
||||
|
@ -72,6 +72,16 @@ fn where_not_in_table() {
|
||||
assert_eq!(actual.out, "4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_optional_index_argument() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
r#"[7 8 9 10] | where {|el ind| $ind < 2 } | to nuon"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "[7, 8]");
|
||||
}
|
||||
|
||||
#[cfg(feature = "database")]
|
||||
#[test]
|
||||
fn binary_operator_comparisons() {
|
||||
|
Reference in New Issue
Block a user