2019-12-17 18:54:39 +00:00
|
|
|
use nu_test_support::fs::Stub::EmptyFile;
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
use nu_test_support::{nu, pipeline};
|
2019-12-15 16:15:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn gets_first_rows_by_amount() {
|
|
|
|
Playground::setup("first_test_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
EmptyFile("los.txt"),
|
|
|
|
EmptyFile("tres.txt"),
|
|
|
|
EmptyFile("amigos.txt"),
|
|
|
|
EmptyFile("arepas.clu"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls
|
|
|
|
| first 3
|
2021-03-13 21:46:40 +00:00
|
|
|
| length
|
2019-12-15 16:15:06 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "3");
|
2019-12-15 16:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn gets_all_rows_if_amount_higher_than_all_rows() {
|
|
|
|
Playground::setup("first_test_2", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
EmptyFile("los.txt"),
|
|
|
|
EmptyFile("tres.txt"),
|
|
|
|
EmptyFile("amigos.txt"),
|
|
|
|
EmptyFile("arepas.clu"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls
|
|
|
|
| first 99
|
2021-03-13 21:46:40 +00:00
|
|
|
| length
|
2019-12-15 16:15:06 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "4");
|
2019-12-15 16:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn gets_first_row_when_no_amount_given() {
|
|
|
|
Playground::setup("first_test_3", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![EmptyFile("caballeros.txt"), EmptyFile("arepas.clu")]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls
|
|
|
|
| first
|
2021-03-13 21:46:40 +00:00
|
|
|
| length
|
2019-12-15 16:15:06 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "1");
|
2019-12-15 16:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2022-09-28 22:08:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn gets_first_row_as_list_when_amount_given() {
|
2023-05-17 23:55:26 +00:00
|
|
|
let actual = nu!(pipeline(
|
2022-09-28 22:08:17 +00:00
|
|
|
r#"
|
2023-05-17 23:55:26 +00:00
|
|
|
[1, 2, 3]
|
|
|
|
| first 1
|
|
|
|
| describe
|
|
|
|
"#
|
2022-09-28 22:08:17 +00:00
|
|
|
));
|
|
|
|
|
2023-01-04 05:08:05 +00:00
|
|
|
assert_eq!(actual.out, "list<int> (stream)");
|
2022-09-28 22:08:17 +00:00
|
|
|
}
|
2022-11-13 23:15:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
// covers a situation where `first` used to behave strangely on list<binary> input
|
|
|
|
fn works_with_binary_list() {
|
2023-05-17 23:55:26 +00:00
|
|
|
let actual = nu!("([0x[01 11]] | first) == 0x[01 11]");
|
2022-11-13 23:15:27 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
}
|
2022-11-23 04:04:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_on_negative_rows() {
|
2023-05-17 23:55:26 +00:00
|
|
|
let actual = nu!(pipeline(
|
2022-11-23 04:04:28 +00:00
|
|
|
r#"
|
2023-05-17 23:55:26 +00:00
|
|
|
[1, 2, 3]
|
|
|
|
| first -10
|
|
|
|
"#
|
2022-11-23 04:04:28 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("use a positive value"));
|
|
|
|
}
|