add tests for range + run rustfmt

This commit is contained in:
Sebastian Jung 2019-12-03 08:24:49 +01:00
parent 8390cc97e1
commit 201506a5ad
2 changed files with 77 additions and 9 deletions

View file

@ -1,6 +1,6 @@
use crate::commands::WholeStreamCommand; use crate::commands::WholeStreamCommand;
use crate::errors::ShellError;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::errors::ShellError;
use crate::prelude::*; use crate::prelude::*;
use nu_source::Tagged; use nu_source::Tagged;
@ -40,7 +40,7 @@ impl WholeStreamCommand for Range {
fn range( fn range(
RangeArgs { area: rows }: RangeArgs, RangeArgs { area: rows }: RangeArgs,
RunnableContext { input, name, .. }: RunnableContext, RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
match rows.item.find(".") { match rows.item.find(".") {
Some(value) => { Some(value) => {
let (first, last) = rows.item.split_at(value); let (first, last) = rows.item.split_at(value);
@ -56,13 +56,13 @@ fn range(
name, name,
)); ));
} }
}, }
}; };
let last = match last.trim_start_matches(".").parse::<u64>() { let last = match last.trim_start_matches(".").parse::<u64>() {
Ok(postion) => postion, Ok(postion) => postion,
Err(_) => { Err(_) => {
if last == ".." { if last == ".." {
std::u64::MAX std::u64::MAX - 1
} else { } else {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"no correct end of range", "no correct end of range",
@ -70,18 +70,18 @@ fn range(
name, name,
)); ));
} }
}, }
}; };
return Ok(OutputStream::from_input( return Ok(OutputStream::from_input(
input.values.skip(first).take(last-first+1), input.values.skip(first).take(last - first + 1),
)); ));
}, }
None => { None => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"No correct formated range found", "No correct formatted range found",
"format: <from>..<to>", "format: <from>..<to>",
name, name,
)); ));
} }
} }
} }

View file

@ -204,6 +204,74 @@ fn group_by_errors_if_unknown_column_name() {
}) })
} }
#[test]
fn range_selects_a_row() {
Playground::setup("range_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("notes.txt"), EmptyFile("tests.txt")]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
ls
| sort-by name
| range 0..0
| get name
| echo $it
"#
));
assert_eq!(actual, "notes.txt");
});
}
#[test]
fn range_selects_some_rows() {
Playground::setup("range_test_2", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("notes.txt"),
EmptyFile("tests.txt"),
EmptyFile("persons.txt"),
]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
ls
| get name
| range 1..2
| count
| echo $it
"#
));
assert_eq!(actual, "2");
});
}
#[test]
fn range_selects_all_rows() {
Playground::setup("range_test_3", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("notes.txt"),
EmptyFile("tests.txt"),
EmptyFile("persons.txt"),
]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
ls
| get name
| range ..
| count
| echo $it
"#
));
assert_eq!(actual, "3");
});
}
#[test] #[test]
fn split_by() { fn split_by() {
Playground::setup("split_by_test_1", |dirs, sandbox| { Playground::setup("split_by_test_1", |dirs, sandbox| {