make first behave same way as last: always return list when with number argument (#6616)

* make `first` behave same way as `last`

* better behaviour

* fix tests

* add tests
This commit is contained in:
pwygab 2022-09-29 06:08:17 +08:00 committed by GitHub
parent dd578926c3
commit 32fbcf39cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 44 additions and 16 deletions

View File

@ -146,7 +146,7 @@ fn first_helper(
}
}
_ => {
if rows_desired == 1 {
if rows_desired == 1 && rows.is_none() {
match input_peek.next() {
Some(val) => Ok(val.into_pipeline_data()),
None => Err(ShellError::AccessBeyondEndOfStream(head)),

View File

@ -65,3 +65,17 @@ fn gets_first_row_when_no_amount_given() {
assert_eq!(actual.out, "1");
})
}
#[test]
fn gets_first_row_as_list_when_amount_given() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[1, 2, 3]
| first 1
| describe
"#
));
assert_eq!(actual.out, "list<int>");
}

View File

@ -64,3 +64,17 @@ fn requests_more_rows_than_table_has() {
assert_eq!(actual.out, "1");
}
#[test]
fn gets_last_row_as_list_when_amount_given() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[1, 2, 3]
| last 1
| describe
"#
));
assert_eq!(actual.out, "list<int>");
}

View File

@ -9,7 +9,7 @@ fn lines() {
| lines
| skip while $it != "[dependencies]"
| skip 1
| first 1
| first
| split column "="
| get column1.0
| str trim

View File

@ -154,7 +154,7 @@ fn parses_tsv() {
cwd: "tests/fixtures/formats", pipeline(
r#"
open caco3_plastics.tsv
| first 1
| first
| get origin
"#
));
@ -216,7 +216,7 @@ fn parses_arrow_ipc() {
r#"
open-df caco3_plastics.arrow
| into nu
| first 1
| first
| get origin
"#
));

View File

@ -4,7 +4,7 @@ use nu_test_support::nu;
fn can_get_reverse_first() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"ls | sort-by name | reverse | first 1 | get name | str trim "
"ls | sort-by name | reverse | first | get name | str trim "
);
assert_eq!(actual.out, "utf16.ini");

View File

@ -12,7 +12,7 @@ fn by_column() {
| split column "="
| sort-by column1
| skip 1
| first 1
| first
| get column1
| str trim
"#
@ -33,7 +33,7 @@ fn by_invalid_column() {
| split column "="
| sort-by ColumnThatDoesNotExist
| skip 1
| first 1
| first
| get column1
| str trim
"#
@ -69,7 +69,7 @@ fn sort_primitive_values() {
| skip 1
| first 6
| sort-by
| first 1
| first
"#
));

View File

@ -6,7 +6,7 @@ use nu_test_support::pipeline;
fn filters_by_unit_size_comparison() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"ls | where size > 1kib | sort-by size | get name | first 1 | str trim"
"ls | where size > 1kib | sort-by size | get name | first | str trim"
);
assert_eq!(actual.out, "cargo_sample.toml");
@ -103,7 +103,7 @@ fn binary_operator_comparisons() {
open sample.db
| get ints
| where z != 1
| first 1
| first
| get z
"#
));

View File

@ -6,7 +6,7 @@ use nu_test_support::{nu, pipeline};
fn table_to_csv_text_and_from_csv_text_back_into_table() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"open caco3_plastics.csv | to csv | from csv | first 1 | get origin "
"open caco3_plastics.csv | to csv | from csv | first | get origin "
);
assert_eq!(actual.out, "SPAIN");

View File

@ -6,7 +6,7 @@ use nu_test_support::{nu, pipeline};
fn table_to_tsv_text_and_from_tsv_text_back_into_table() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"open caco3_plastics.tsv | to tsv | from tsv | first 1 | get origin"
"open caco3_plastics.tsv | to tsv | from tsv | first | get origin"
);
assert_eq!(actual.out, "SPAIN");
@ -16,7 +16,7 @@ fn table_to_tsv_text_and_from_tsv_text_back_into_table() {
fn table_to_tsv_text_and_from_tsv_text_back_into_table_using_csv_separator() {
let actual = nu!(
cwd: "tests/fixtures/formats",
r"open caco3_plastics.tsv | to tsv | from csv --separator '\t' | first 1 | get origin"
r"open caco3_plastics.tsv | to tsv | from csv --separator '\t' | first | get origin"
);
assert_eq!(actual.out, "SPAIN");

View File

@ -7,7 +7,7 @@ fn chooses_highest_increment_if_given_more_than_one() {
let actual = nu_with_plugins!(
cwd: "tests/fixtures/formats",
plugin: ("nu_plugin_inc"),
"open cargo_sample.toml | first 1 | inc package.version --major --minor | get package.version"
"open cargo_sample.toml | first | inc package.version --major --minor | get package.version"
);
assert_eq!(actual.out, "1.0.0");
@ -16,7 +16,7 @@ fn chooses_highest_increment_if_given_more_than_one() {
cwd: "tests/fixtures/formats",
plugin: ("nu_plugin_inc"),
// Regardless of order of arguments
"open cargo_sample.toml | first 1 | inc package.version --minor --major | get package.version"
"open cargo_sample.toml | first | inc package.version --minor --major | get package.version"
);
assert_eq!(actual.out, "1.0.0");

View File

@ -232,7 +232,7 @@ mod stdin_evaluation {
| nu --testbin chop
| nu --testbin chop
| lines
| first 1 )
| first )
"#
))
.out;