simplify the nu! tests for last and first commands (#9608)

# Description
in most of the tests for `last` and `first`, we do not need to
- give `cwd` to `nu!`
- use pipeline as the tests are all short pipes
- use `r#" ... "#` as the pipes never contain quotes

this PR removes all these points from the tests for the `last` and
`first` commands.
This commit is contained in:
Antoine Stevan 2023-07-05 12:30:53 +02:00 committed by GitHub
parent 65a163357d
commit 53cd4df806
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 106 deletions

View file

@ -1,6 +1,6 @@
use nu_test_support::fs::Stub::EmptyFile; use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::nu;
use nu_test_support::playground::Playground; use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test] #[test]
fn gets_first_rows_by_amount() { fn gets_first_rows_by_amount() {
@ -12,14 +12,7 @@ fn gets_first_rows_by_amount() {
EmptyFile("arepas.clu"), EmptyFile("arepas.clu"),
]); ]);
let actual = nu!( let actual = nu!(cwd: dirs.test(), "ls | first 3 | length");
cwd: dirs.test(), pipeline(
r#"
ls
| first 3
| length
"#
));
assert_eq!(actual.out, "3"); assert_eq!(actual.out, "3");
}) })
@ -36,13 +29,7 @@ fn gets_all_rows_if_amount_higher_than_all_rows() {
]); ]);
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(), "ls | first 99 | length");
r#"
ls
| first 99
| length
"#
));
assert_eq!(actual.out, "4"); assert_eq!(actual.out, "4");
}) })
@ -53,14 +40,7 @@ fn gets_first_row_when_no_amount_given() {
Playground::setup("first_test_3", |dirs, sandbox| { Playground::setup("first_test_3", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("caballeros.txt"), EmptyFile("arepas.clu")]); sandbox.with_files(vec![EmptyFile("caballeros.txt"), EmptyFile("arepas.clu")]);
let actual = nu!( let actual = nu!(cwd: dirs.test(), "ls | first | length");
cwd: dirs.test(), pipeline(
r#"
ls
| first
| length
"#
));
assert_eq!(actual.out, "1"); assert_eq!(actual.out, "1");
}) })
@ -68,35 +48,21 @@ fn gets_first_row_when_no_amount_given() {
#[test] #[test]
fn gets_first_row_as_list_when_amount_given() { fn gets_first_row_as_list_when_amount_given() {
let actual = nu!(pipeline( let actual = nu!("[1, 2, 3] | first 1 | describe");
r#"
[1, 2, 3]
| first 1
| describe
"#
));
assert_eq!(actual.out, "list<int> (stream)"); assert_eq!(actual.out, "list<int> (stream)");
} }
#[test] #[test]
fn gets_first_bytes() { fn gets_first_bytes() {
let actual = nu!(pipeline( let actual = nu!("(0x[aa bb cc] | first 2) == 0x[aa bb]");
r#"
(0x[aa bb cc] | first 2) == 0x[aa bb]
"#
));
assert_eq!(actual.out, "true"); assert_eq!(actual.out, "true");
} }
#[test] #[test]
fn gets_first_byte() { fn gets_first_byte() {
let actual = nu!(pipeline( let actual = nu!("0x[aa bb cc] | first");
r#"
0x[aa bb cc] | first
"#
));
assert_eq!(actual.out, "170"); assert_eq!(actual.out, "170");
} }
@ -111,24 +77,14 @@ fn works_with_binary_list() {
#[test] #[test]
fn errors_on_negative_rows() { fn errors_on_negative_rows() {
let actual = nu!(pipeline( let actual = nu!("[1, 2, 3] | first -10");
r#"
[1, 2, 3]
| first -10
"#
));
assert!(actual.err.contains("use a positive value")); assert!(actual.err.contains("use a positive value"));
} }
#[test] #[test]
fn errors_on_empty_list_when_no_rows_given() { fn errors_on_empty_list_when_no_rows_given() {
let actual = nu!(pipeline( let actual = nu!("[] | first");
r#"
[]
| first
"#
));
assert!(actual.err.contains("index too large")); assert!(actual.err.contains("index too large"));
} }

View file

@ -1,6 +1,6 @@
use nu_test_support::fs::Stub::EmptyFile; use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::nu;
use nu_test_support::playground::Playground; use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test] #[test]
fn gets_the_last_row() { fn gets_the_last_row() {
@ -22,14 +22,7 @@ fn gets_last_rows_by_amount() {
EmptyFile("arepas.clu"), EmptyFile("arepas.clu"),
]); ]);
let actual = nu!( let actual = nu!(cwd: dirs.test(), "ls | last 3 | length");
cwd: dirs.test(), pipeline(
r#"
ls
| last 3
| length
"#
));
assert_eq!(actual.out, "3"); assert_eq!(actual.out, "3");
}) })
@ -40,14 +33,7 @@ fn gets_last_row_when_no_amount_given() {
Playground::setup("last_test_2", |dirs, sandbox| { Playground::setup("last_test_2", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("caballeros.txt"), EmptyFile("arepas.clu")]); sandbox.with_files(vec![EmptyFile("caballeros.txt"), EmptyFile("arepas.clu")]);
let actual = nu!( let actual = nu!(cwd: dirs.test(), "ls | last | length");
cwd: dirs.test(), pipeline(
r#"
ls
| last
| length
"#
));
assert_eq!(actual.out, "1"); assert_eq!(actual.out, "1");
}) })
@ -55,60 +41,35 @@ fn gets_last_row_when_no_amount_given() {
#[test] #[test]
fn requests_more_rows_than_table_has() { fn requests_more_rows_than_table_has() {
let actual = nu!( let actual = nu!("[date] | last 50 | length");
cwd: ".", pipeline(
r#"
[date] | last 50 | length
"#
));
assert_eq!(actual.out, "1"); assert_eq!(actual.out, "1");
} }
#[test] #[test]
fn gets_last_row_as_list_when_amount_given() { fn gets_last_row_as_list_when_amount_given() {
let actual = nu!( let actual = nu!("[1, 2, 3] | last 1 | describe");
cwd: ".", pipeline(
r#"
[1, 2, 3]
| last 1
| describe
"#
));
assert_eq!(actual.out, "list<int> (stream)"); assert_eq!(actual.out, "list<int> (stream)");
} }
#[test] #[test]
fn gets_last_bytes() { fn gets_last_bytes() {
let actual = nu!(pipeline( let actual = nu!("(0x[aa bb cc] | last 2) == 0x[bb cc]");
r#"
(0x[aa bb cc] | last 2) == 0x[bb cc]
"#
));
assert_eq!(actual.out, "true"); assert_eq!(actual.out, "true");
} }
#[test] #[test]
fn gets_last_byte() { fn gets_last_byte() {
let actual = nu!(pipeline( let actual = nu!("0x[aa bb cc] | last");
r#"
0x[aa bb cc] | last
"#
));
assert_eq!(actual.out, "204"); assert_eq!(actual.out, "204");
} }
#[test] #[test]
fn last_errors_on_negative_index() { fn last_errors_on_negative_index() {
let actual = nu!(pipeline( let actual = nu!("[1, 2, 3] | last -2");
"
[1, 2, 3]
| last -2
"
));
assert!(actual.err.contains("use a positive value")); assert!(actual.err.contains("use a positive value"));
} }
@ -122,12 +83,7 @@ fn fail_on_non_iterator() {
#[test] #[test]
fn errors_on_empty_list_when_no_rows_given() { fn errors_on_empty_list_when_no_rows_given() {
let actual = nu!(pipeline( let actual = nu!("[] | last");
r#"
[]
| last
"#
));
assert!(actual.err.contains("index too large")); assert!(actual.err.contains("index too large"));
} }