mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:25:38 +02:00
Make str more strict. (#2173)
This commit is contained in:
committed by
GitHub
parent
f2c4d22739
commit
e9313a61af
@ -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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user