mirror of
https://github.com/nushell/nushell
synced 2024-12-28 14:03:09 +00:00
Make str more strict. (#2173)
This commit is contained in:
parent
f2c4d22739
commit
e9313a61af
7 changed files with 105 additions and 4 deletions
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,7 +101,13 @@ fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,7 +101,13 @@ fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
51
crates/nu-cli/tests/commands/skip_while.rs
Normal file
51
crates/nu-cli/tests/commands/skip_while.rs
Normal 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");
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue