From e744237451b1d6185e451cf46674cb9a8f0de39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Sat, 10 Aug 2019 04:12:48 -0500 Subject: [PATCH] Clean up. --- src/plugins/str.rs | 27 ++-------- tests/filter_inc_tests.rs | 110 +++++++++++++++++++++++--------------- tests/filter_str_tests.rs | 69 +++++++++++++++++------- 3 files changed, 122 insertions(+), 84 deletions(-) diff --git a/src/plugins/str.rs b/src/plugins/str.rs index 817c8137d8..14bc30e4df 100644 --- a/src/plugins/str.rs +++ b/src/plugins/str.rs @@ -79,10 +79,7 @@ impl Str { } impl Str { - fn strutils( - &self, - value: Tagged, - ) -> Result, ShellError> { + fn strutils(&self, value: Tagged) -> Result, ShellError> { match value.item { Value::Primitive(Primitive::String(ref s)) => { Ok(Tagged::from_item(self.apply(&s), value.tag())) @@ -176,9 +173,7 @@ impl Plugin for Str { } fn filter(&mut self, input: Tagged) -> Result, ShellError> { - Ok(vec![ReturnSuccess::value( - self.strutils(input)?, - )]) + Ok(vec![ReturnSuccess::value(self.strutils(input)?)]) } } @@ -254,11 +249,7 @@ mod tests { let mut plugin = Str::new(); assert!(plugin - .begin_filter( - CallStub::new() - .with_long_flag("downcase") - .create() - ) + .begin_filter(CallStub::new().with_long_flag("downcase").create()) .is_ok()); assert!(plugin.action.is_some()); } @@ -268,11 +259,7 @@ mod tests { let mut plugin = Str::new(); assert!(plugin - .begin_filter( - CallStub::new() - .with_long_flag("upcase") - .create() - ) + .begin_filter(CallStub::new().with_long_flag("upcase").create()) .is_ok()); assert!(plugin.action.is_some()); } @@ -282,11 +269,7 @@ mod tests { let mut plugin = Str::new(); assert!(plugin - .begin_filter( - CallStub::new() - .with_long_flag("to-int") - .create() - ) + .begin_filter(CallStub::new().with_long_flag("to-int").create()) .is_ok()); assert!(plugin.action.is_some()); } diff --git a/tests/filter_inc_tests.rs b/tests/filter_inc_tests.rs index 6f7341845e..bc038f5cf3 100644 --- a/tests/filter_inc_tests.rs +++ b/tests/filter_inc_tests.rs @@ -1,29 +1,7 @@ mod helpers; -use helpers::in_directory as cwd; - -#[test] -fn regular_field_by_one() { - nu!( - output, - cwd("tests/fixtures/formats"), - "open cargo_sample.toml | inc package.edition | get package.edition | echo $it" - ); - - assert_eq!(output, "2019"); -} - - -#[test] -fn by_one_without_passing_field() { - nu!( - output, - cwd("tests/fixtures/formats"), - "open cargo_sample.toml | get package.edition | inc | echo $it" - ); - - assert_eq!(output, "2019"); -} +use h::{in_directory as cwd, Playground, Stub::*}; +use helpers as h; #[test] fn can_only_apply_one() { @@ -37,34 +15,82 @@ fn can_only_apply_one() { } #[test] -fn semversion_major() { +fn regular_field_by_one() { + Playground::setup_for("plugin_inc_test_1") + .with_files(vec![FileWithContent( + "sample.toml", + r#" + [package] + edition = "2018" + "#, + )]); + nu!( output, - cwd("tests/fixtures/formats"), - "open cargo_sample.toml | inc package.version --major | get package.version | echo $it" + cwd("tests/fixtures/nuplayground/plugin_inc_test_1"), + "open sample.toml | inc package.edition | get package.edition | echo $it" + ); + + assert_eq!(output, "2019"); +} + + +#[test] +fn by_one_without_passing_field() { + Playground::setup_for("plugin_inc_test_2") + .with_files(vec![FileWithContent( + "sample.toml", + r#" + [package] + contributors = "2" + "#, + )]); + + nu!( + output, + cwd("tests/fixtures/nuplayground/plugin_inc_test_2"), + "open sample.toml | get package.contributors | inc | echo $it" + ); + + assert_eq!(output, "3"); +} + +#[test] +fn semversion() { + Playground::setup_for("plugin_inc_test_3") + .with_files(vec![FileWithContent( + "sample.toml", + r#" + [package] + version = "0.1.3" + "#, + )]); + + nu!( + output, + cwd("tests/fixtures/nuplayground/plugin_inc_test_3"), + "open sample.toml | inc package.version --major | get package.version | echo $it" ); assert_eq!(output, "1.0.0"); } #[test] -fn semversion_minor() { +fn semversion_without_passing_field() { + Playground::setup_for("plugin_inc_test_4") + .with_files(vec![FileWithContent( + "sample.toml", + r#" + [package] + version = "0.1.3" + "#, + )]); + nu!( output, - cwd("tests/fixtures/formats"), - "open cargo_sample.toml | inc package.version --minor | get package.version | echo $it" + cwd("tests/fixtures/nuplayground/plugin_inc_test_4"), + "open sample.toml | get package.version | inc --patch | echo $it" ); - assert_eq!(output, "0.2.0"); -} - -#[test] -fn semversion_patch() { - nu!( - output, - cwd("tests/fixtures/formats"), - "open cargo_sample.toml | inc package.version --patch | get package.version | echo $it" - ); - - assert_eq!(output, "0.1.2"); + assert_eq!(output, "0.1.4"); } \ No newline at end of file diff --git a/tests/filter_str_tests.rs b/tests/filter_str_tests.rs index fd85652bd1..9a4ab553ae 100644 --- a/tests/filter_str_tests.rs +++ b/tests/filter_str_tests.rs @@ -1,20 +1,10 @@ mod helpers; -use helpers::in_directory as cwd; +use h::{in_directory as cwd, Playground, Stub::*}; +use helpers as h; #[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() { +fn can_only_apply_one() { nu_error!( output, cwd("tests/fixtures/formats"), @@ -25,22 +15,61 @@ fn str_can_only_apply_one() { } #[test] -fn downcases() { +fn acts_without_passing_field() { + Playground::setup_for("plugin_str_test_without_passing_field") + .with_files(vec![FileWithContent( + "sample.yml", + r#" + environment: + global: + PROJECT_NAME: nushell + "#, + )]); + nu!( output, - cwd("tests/fixtures/formats"), - "open caco3_plastics.csv | first 1 | str origin --downcase | get origin | echo $it" + cwd("tests/fixtures/nuplayground/plugin_str_test_without_passing_field"), + "open sample.yml | get environment.global.PROJECT_NAME | str --upcase | echo $it" ); - assert_eq!(output, "spain"); + assert_eq!(output, "NUSHELL"); +} + +#[test] +fn downcases() { + Playground::setup_for("plugin_str_test_downcases") + .with_files(vec![FileWithContent( + "sample.toml", + r#" + [dependency] + name = "LIGHT" + "#, + )]); + + nu!( + output, + cwd("tests/fixtures/nuplayground/plugin_str_test_downcases"), + "open sample.toml | str dependency.name --downcase | get dependency.name | echo $it" + ); + + assert_eq!(output, "light"); } #[test] fn upcases() { + Playground::setup_for("plugin_str_test_upcases") + .with_files(vec![FileWithContent( + "sample.toml", + r#" + [package] + name = "nushell" + "#, + )]); + nu!( output, - cwd("tests/fixtures/formats"), - "open appveyor.yml | str environment.global.PROJECT_NAME --upcase | get environment.global.PROJECT_NAME | echo $it" + cwd("tests/fixtures/nuplayground/plugin_str_test_upcases"), + "open sample.toml | str package.name --upcase | get package.name | echo $it" ); assert_eq!(output, "NUSHELL"); @@ -51,7 +80,7 @@ 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" + "open caco3_plastics.csv | first 1 | str tariff_item --to-int | where tariff_item == 2509000000 | get tariff_item | echo $it" ); assert_eq!(output, "2509000000");