From 201506a5ad400630a811f49643878710c164792e Mon Sep 17 00:00:00 2001 From: Sebastian Jung Date: Tue, 3 Dec 2019 08:24:49 +0100 Subject: [PATCH] add tests for range + run rustfmt --- src/commands/range.rs | 18 +++++------ tests/commands_test.rs | 68 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/src/commands/range.rs b/src/commands/range.rs index e71f731f78..8a31c1c0e2 100644 --- a/src/commands/range.rs +++ b/src/commands/range.rs @@ -1,6 +1,6 @@ use crate::commands::WholeStreamCommand; -use crate::errors::ShellError; use crate::context::CommandRegistry; +use crate::errors::ShellError; use crate::prelude::*; use nu_source::Tagged; @@ -40,7 +40,7 @@ impl WholeStreamCommand for Range { fn range( RangeArgs { area: rows }: RangeArgs, RunnableContext { input, name, .. }: RunnableContext, -) -> Result { +) -> Result { match rows.item.find(".") { Some(value) => { let (first, last) = rows.item.split_at(value); @@ -56,13 +56,13 @@ fn range( name, )); } - }, + } }; let last = match last.trim_start_matches(".").parse::() { Ok(postion) => postion, Err(_) => { if last == ".." { - std::u64::MAX + std::u64::MAX - 1 } else { return Err(ShellError::labeled_error( "no correct end of range", @@ -70,18 +70,18 @@ fn range( name, )); } - }, + } }; return Ok(OutputStream::from_input( - input.values.skip(first).take(last-first+1), + input.values.skip(first).take(last - first + 1), )); - }, + } None => { return Err(ShellError::labeled_error( - "No correct formated range found", + "No correct formatted range found", "format: ..", name, )); } } -} \ No newline at end of file +} diff --git a/tests/commands_test.rs b/tests/commands_test.rs index 89ab430b36..776998e633 100644 --- a/tests/commands_test.rs +++ b/tests/commands_test.rs @@ -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] fn split_by() { Playground::setup("split_by_test_1", |dirs, sandbox| {