Make str more strict. (#2173)

This commit is contained in:
Andrés N. Robalino 2020-07-14 10:04:00 -05:00 committed by GitHub
parent f2c4d22739
commit e9313a61af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 105 additions and 4 deletions

View file

@ -123,7 +123,13 @@ fn action(
let out = match DateTime::parse_from_str(s, dt) { let out = match DateTime::parse_from_str(s, dt) {
Ok(d) => UntaggedValue::date(d), 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)) Ok(out.into_value(tag))
@ -166,4 +172,15 @@ mod tests {
_ => panic!("Didn't convert to date"), _ => 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());
}
} }

View file

@ -101,7 +101,13 @@ fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
let other = s.trim(); let other = s.trim();
let out = match BigDecimal::from_str(other) { let out = match BigDecimal::from_str(other) {
Ok(v) => UntaggedValue::decimal(v), 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)) Ok(out.into_value(tag))
} }
@ -138,4 +144,13 @@ mod tests {
let actual = action(&word, Tag::unknown()).unwrap(); let actual = action(&word, Tag::unknown()).unwrap();
assert_eq!(actual, expected); 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());
}
} }

View file

@ -101,7 +101,13 @@ fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
let other = s.trim(); let other = s.trim();
let out = match BigInt::from_str(other) { let out = match BigInt::from_str(other) {
Ok(v) => UntaggedValue::int(v), 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)) Ok(out.into_value(tag))
} }
@ -137,4 +143,13 @@ mod tests {
let actual = action(&word, Tag::unknown()).unwrap(); let actual = action(&word, Tag::unknown()).unwrap();
assert_eq!(actual, expected); 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());
}
} }

View file

@ -37,8 +37,9 @@ fn condition_is_met() {
| skip 2 | skip 2
| split column ',' | split column ','
| headers | headers
| skip-while "Chickens Collction" != "Blue Chickens" | skip-while "Chicken Collection" != "Blue Chickens"
| keep-until "Chicken Collection" == "Red Chickens" | keep-until "Chicken Collection" == "Red Chickens"
| skip 1
| str to-int "31/04/2020" | str to-int "31/04/2020"
| get "31/04/2020" | get "31/04/2020"
| math sum | math sum

View file

@ -44,6 +44,7 @@ mod save;
mod select; mod select;
mod semicolon; mod semicolon;
mod skip_until; mod skip_until;
mod skip_while;
mod sort_by; mod sort_by;
mod split_by; mod split_by;
mod split_column; mod split_column;

View file

@ -38,6 +38,7 @@ fn condition_is_met() {
| split column ',' | split column ','
| headers | headers
| skip-until "Chicken Collection" == "Red Chickens" | skip-until "Chicken Collection" == "Red Chickens"
| skip 1
| str to-int "31/04/2020" | str to-int "31/04/2020"
| get "31/04/2020" | get "31/04/2020"
| math sum | math sum

View file

@ -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");
})
}