diff --git a/crates/nu-cli/src/commands/str_/to_datetime.rs b/crates/nu-cli/src/commands/str_/to_datetime.rs index 7814d83ed..8909ddccb 100644 --- a/crates/nu-cli/src/commands/str_/to_datetime.rs +++ b/crates/nu-cli/src/commands/str_/to_datetime.rs @@ -123,7 +123,13 @@ fn action( let out = match DateTime::parse_from_str(s, dt) { Ok(d) => UntaggedValue::date(d), - Err(_) => UntaggedValue::string(s), + Err(reason) => { + return Err(ShellError::labeled_error( + "could not parse as datetime", + reason.to_string(), + tag.into().span, + )) + } }; Ok(out.into_value(tag)) @@ -166,4 +172,15 @@ mod tests { _ => panic!("Didn't convert to date"), } } + + #[test] + fn communicates_parsing_error_given_an_invalid_datetimelike_string() { + let date_str = string("16.11.1984 8:00 am Oops0000"); + + let fmt_options = DatetimeFormat("%d.%m.%Y %H:%M %P %z".to_string()); + + let actual = action(&date_str, &fmt_options, Tag::unknown()); + + assert!(actual.is_err()); + } } diff --git a/crates/nu-cli/src/commands/str_/to_decimal.rs b/crates/nu-cli/src/commands/str_/to_decimal.rs index 3e908d22c..e07d0e08d 100644 --- a/crates/nu-cli/src/commands/str_/to_decimal.rs +++ b/crates/nu-cli/src/commands/str_/to_decimal.rs @@ -101,7 +101,13 @@ fn action(input: &Value, tag: impl Into) -> Result { let other = s.trim(); let out = match BigDecimal::from_str(other) { Ok(v) => UntaggedValue::decimal(v), - Err(_) => UntaggedValue::string(s), + Err(reason) => { + return Err(ShellError::labeled_error( + "could not parse as decimal", + reason.to_string(), + tag.into().span, + )) + } }; Ok(out.into_value(tag)) } @@ -138,4 +144,13 @@ mod tests { let actual = action(&word, Tag::unknown()).unwrap(); assert_eq!(actual, expected); } + + #[test] + fn communicates_parsing_error_given_an_invalid_decimallike_string() { + let decimal_str = string("11.6anra"); + + let actual = action(&decimal_str, Tag::unknown()); + + assert!(actual.is_err()); + } } diff --git a/crates/nu-cli/src/commands/str_/to_integer.rs b/crates/nu-cli/src/commands/str_/to_integer.rs index 41277401a..2e2a92a48 100644 --- a/crates/nu-cli/src/commands/str_/to_integer.rs +++ b/crates/nu-cli/src/commands/str_/to_integer.rs @@ -101,7 +101,13 @@ fn action(input: &Value, tag: impl Into) -> Result { let other = s.trim(); let out = match BigInt::from_str(other) { Ok(v) => UntaggedValue::int(v), - Err(_) => UntaggedValue::string(s), + Err(reason) => { + return Err(ShellError::labeled_error( + "could not parse as an integer", + reason.to_string(), + tag.into().span, + )) + } }; Ok(out.into_value(tag)) } @@ -137,4 +143,13 @@ mod tests { let actual = action(&word, Tag::unknown()).unwrap(); assert_eq!(actual, expected); } + + #[test] + fn communicates_parsing_error_given_an_invalid_integerlike_string() { + let integer_str = string("36anra"); + + let actual = action(&integer_str, Tag::unknown()); + + assert!(actual.is_err()); + } } diff --git a/crates/nu-cli/tests/commands/keep_until.rs b/crates/nu-cli/tests/commands/keep_until.rs index 6dab4273d..bc09099ec 100644 --- a/crates/nu-cli/tests/commands/keep_until.rs +++ b/crates/nu-cli/tests/commands/keep_until.rs @@ -37,8 +37,9 @@ fn condition_is_met() { | skip 2 | split column ',' | headers - | skip-while "Chickens Collction" != "Blue Chickens" + | skip-while "Chicken Collection" != "Blue Chickens" | keep-until "Chicken Collection" == "Red Chickens" + | skip 1 | str to-int "31/04/2020" | get "31/04/2020" | math sum diff --git a/crates/nu-cli/tests/commands/mod.rs b/crates/nu-cli/tests/commands/mod.rs index 1cfc9ca42..4996f054f 100644 --- a/crates/nu-cli/tests/commands/mod.rs +++ b/crates/nu-cli/tests/commands/mod.rs @@ -44,6 +44,7 @@ mod save; mod select; mod semicolon; mod skip_until; +mod skip_while; mod sort_by; mod split_by; mod split_column; diff --git a/crates/nu-cli/tests/commands/skip_until.rs b/crates/nu-cli/tests/commands/skip_until.rs index 1344f49d9..bd3da95a3 100644 --- a/crates/nu-cli/tests/commands/skip_until.rs +++ b/crates/nu-cli/tests/commands/skip_until.rs @@ -38,6 +38,7 @@ fn condition_is_met() { | split column ',' | headers | skip-until "Chicken Collection" == "Red Chickens" + | skip 1 | str to-int "31/04/2020" | get "31/04/2020" | math sum diff --git a/crates/nu-cli/tests/commands/skip_while.rs b/crates/nu-cli/tests/commands/skip_while.rs new file mode 100644 index 000000000..4e5d17f9c --- /dev/null +++ b/crates/nu-cli/tests/commands/skip_while.rs @@ -0,0 +1,51 @@ +use nu_test_support::fs::Stub::FileWithContentToBeTrimmed; +use nu_test_support::playground::Playground; +use nu_test_support::{nu, pipeline}; + +#[test] +fn condition_is_met() { + Playground::setup("skip-while_test_1", |dirs, sandbox| { + sandbox.with_files(vec![FileWithContentToBeTrimmed( + "caballeros.txt", + r#" + CHICKEN SUMMARY report date: April 29th, 2020 + -------------------------------------------------------------------- + Chicken Collection,29/04/2020,30/04/2020,31/04/2020, + Yellow Chickens,,, + Andrés,0,0,1 + Jonathan,0,0,1 + Jason,0,0,1 + Yehuda,0,0,1 + Blue Chickens,,, + Andrés,0,0,1 + Jonathan,0,0,1 + Jason,0,0,1 + Yehuda,0,0,2 + Red Chickens,,, + Andrés,0,0,1 + Jonathan,0,0,1 + Jason,0,0,1 + Yehuda,0,0,3 + "#, + )]); + + let actual = nu!( + cwd: dirs.test(), pipeline( + r#" + open --raw caballeros.txt + | lines + | skip 2 + | split column ',' + | headers + | skip-while "Chicken Collection" != "Red Chickens" + | skip 1 + | str to-int "31/04/2020" + | get "31/04/2020" + | math sum + | echo $it + "# + )); + + assert_eq!(actual.out, "6"); + }) +}