From 5372463b37aa531c41948f3e8db1904946b99b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Sat, 10 Aug 2019 04:11:38 -0500 Subject: [PATCH] Extract tests. --- src/plugins/str.rs | 33 +++++++++++----------- tests/filter_str_tests.rs | 58 +++++++++++++++++++++++++++++++++++++++ tests/filters_test.rs | 45 +----------------------------- 3 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 tests/filter_str_tests.rs diff --git a/src/plugins/str.rs b/src/plugins/str.rs index 4587baad74..817c8137d8 100644 --- a/src/plugins/str.rs +++ b/src/plugins/str.rs @@ -1,6 +1,6 @@ use indexmap::IndexMap; use nu::{ - serve_plugin, CallInfo, NamedType, Plugin, PositionalType, Primitive, ReturnSuccess, + serve_plugin, CallInfo, NamedType, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError, Signature, Tagged, Value, }; @@ -82,16 +82,15 @@ impl Str { fn strutils( &self, value: Tagged, - field: &Option, ) -> Result, ShellError> { match value.item { Value::Primitive(Primitive::String(ref s)) => { Ok(Tagged::from_item(self.apply(&s), value.tag())) } - Value::Object(_) => match field { - Some(f) => { + Value::Object(_) => match self.field { + Some(ref f) => { let replacement = match value.item.get_data_by_path(value.tag(), f) { - Some(result) => self.strutils(result.map(|x| x.clone()), &None)?, + Some(result) => self.strutils(result.map(|x| x.clone()))?, None => { return Err(ShellError::string("str could not find field to replace")) } @@ -129,7 +128,7 @@ impl Plugin for Str { Ok(Signature { name: "str".to_string(), - positional: vec![PositionalType::optional_any("Field")], + positional: vec![], is_filter: true, named, rest_positional: true, @@ -178,7 +177,7 @@ impl Plugin for Str { fn filter(&mut self, input: Tagged) -> Result, ShellError> { Ok(vec![ReturnSuccess::value( - self.strutils(input, &self.field)?, + self.strutils(input)?, )]) } } @@ -224,11 +223,11 @@ mod tests { self } - fn create(&self, name_span: Span) -> CallInfo { + fn create(&self) -> CallInfo { CallInfo { args: EvaluatedArgs::new(Some(self.positionals.clone()), Some(self.flags.clone())), source_map: SourceMap::new(), - name_span, + name_span: Span::unknown(), } } } @@ -258,7 +257,7 @@ mod tests { .begin_filter( CallStub::new() .with_long_flag("downcase") - .create(Span::unknown()) + .create() ) .is_ok()); assert!(plugin.action.is_some()); @@ -272,7 +271,7 @@ mod tests { .begin_filter( CallStub::new() .with_long_flag("upcase") - .create(Span::unknown()) + .create() ) .is_ok()); assert!(plugin.action.is_some()); @@ -286,7 +285,7 @@ mod tests { .begin_filter( CallStub::new() .with_long_flag("to-int") - .create(Span::unknown()) + .create() ) .is_ok()); assert!(plugin.action.is_some()); @@ -300,7 +299,7 @@ mod tests { .begin_filter( CallStub::new() .with_parameter("package.description") - .create(Span::unknown()) + .create() ) .is_ok()); @@ -317,7 +316,7 @@ mod tests { .with_long_flag("upcase") .with_long_flag("downcase") .with_long_flag("to-int") - .create(Span::unknown()), + .create(), ) .is_err()); assert_eq!(plugin.error, Some("can only apply one".to_string())); @@ -353,7 +352,7 @@ mod tests { CallStub::new() .with_long_flag("upcase") .with_parameter("name") - .create(Span::unknown()) + .create() ) .is_ok()); @@ -381,7 +380,7 @@ mod tests { CallStub::new() .with_long_flag("downcase") .with_parameter("name") - .create(Span::unknown()) + .create() ) .is_ok()); @@ -409,7 +408,7 @@ mod tests { CallStub::new() .with_long_flag("to-int") .with_parameter("Nu_birthday") - .create(Span::unknown()) + .create() ) .is_ok()); diff --git a/tests/filter_str_tests.rs b/tests/filter_str_tests.rs new file mode 100644 index 0000000000..fd85652bd1 --- /dev/null +++ b/tests/filter_str_tests.rs @@ -0,0 +1,58 @@ +mod helpers; + +use helpers::in_directory as cwd; + +#[test] +fn acts_without_passing_field() { + nu!( + output, + cwd("tests/fixtures/formats"), + "open caco3_plastics.csv | first 1 | get origin | str --downcase | echo $it" + ); + + assert_eq!(output, "spain"); +} + +#[test] +fn str_can_only_apply_one() { + nu_error!( + output, + cwd("tests/fixtures/formats"), + "open caco3_plastics.csv | first 1 | str origin --downcase --upcase" + ); + + assert!(output.contains("Usage: str field [--downcase|--upcase|--to-int]")); +} + +#[test] +fn downcases() { + nu!( + output, + cwd("tests/fixtures/formats"), + "open caco3_plastics.csv | first 1 | str origin --downcase | get origin | echo $it" + ); + + assert_eq!(output, "spain"); +} + +#[test] +fn upcases() { + nu!( + output, + cwd("tests/fixtures/formats"), + "open appveyor.yml | str environment.global.PROJECT_NAME --upcase | get environment.global.PROJECT_NAME | echo $it" + ); + + assert_eq!(output, "NUSHELL"); +} + +#[test] +fn converts_to_int() { + nu!( + output, + cwd("tests/fixtures/formats"), + "open caco3_plastics.csv | get 0 | str tariff_item --to-int | where tariff_item == 2509000000 | get tariff_item | echo $it" + ); + + assert_eq!(output, "2509000000"); +} \ No newline at end of file diff --git a/tests/filters_test.rs b/tests/filters_test.rs index 0dcdd07789..8b0e40bef3 100644 --- a/tests/filters_test.rs +++ b/tests/filters_test.rs @@ -68,50 +68,6 @@ fn can_split_by_column() { assert_eq!(output, "name"); } -#[test] -fn str_can_only_apply_one() { - nu_error!( - output, - cwd("tests/fixtures/formats"), - "open caco3_plastics.csv | first 1 | str origin --downcase --upcase" - ); - - assert!(output.contains("Usage: str field [--downcase|--upcase|--to-int]")); -} - -#[test] -fn str_downcases() { - nu!( - output, - cwd("tests/fixtures/formats"), - "open caco3_plastics.csv | first 1 | str origin --downcase | get origin | echo $it" - ); - - assert_eq!(output, "spain"); -} - -#[test] -fn str_upcases() { - nu!( - output, - cwd("tests/fixtures/formats"), - "open appveyor.yml | str environment.global.PROJECT_NAME --upcase | get environment.global.PROJECT_NAME | echo $it" - ); - - assert_eq!(output, "NUSHELL"); -} - -#[test] -fn str_converts_to_int() { - nu!( - output, - cwd("tests/fixtures/formats"), - "open caco3_plastics.csv | first 1 | str tariff_item --to-int | where tariff_item == 2509000000 | get tariff_item | echo $it" - ); - - assert_eq!(output, "2509000000"); -} - #[test] fn can_sum() { nu!( @@ -122,6 +78,7 @@ fn can_sum() { assert_eq!(output, "203") } + #[test] fn can_filter_by_unit_size_comparison() { nu!(