fix into filesize tests and filesize (#932)

* fix into filesize tests and filesize

* tweaks

* added span back for like the 10th time

* Update filesize.rs

Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
This commit is contained in:
Darren Schroeder 2022-02-04 13:26:08 -06:00 committed by GitHub
parent fefd5fef12
commit ece1e43238
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 18 deletions

View File

@ -111,27 +111,42 @@ fn into_filesize(
}
pub fn action(input: &Value, span: Span) -> Value {
match input {
Value::Filesize { .. } => input.clone(),
Value::Int { val, .. } => Value::Filesize { val: *val, span },
Value::Float { val, .. } => Value::Filesize {
val: *val as i64,
span,
},
Value::String { val, .. } => match int_from_string(val, span) {
Ok(val) => Value::Filesize { val, span },
Err(error) => Value::Error { error },
},
_ => Value::Error {
if let Ok(value_span) = input.span() {
match input {
Value::Filesize { .. } => input.clone(),
Value::Int { val, .. } => Value::Filesize {
val: *val,
span: value_span,
},
Value::Float { val, .. } => Value::Filesize {
val: *val as i64,
span: value_span,
},
Value::String { val, .. } => match int_from_string(val, value_span) {
Ok(val) => Value::Filesize {
val,
span: value_span,
},
Err(error) => Value::Error { error },
},
_ => Value::Error {
error: ShellError::UnsupportedInput(
"'into filesize' for unsupported type".into(),
value_span,
),
},
}
} else {
Value::Error {
error: ShellError::UnsupportedInput(
"'into filesize' for unsupported type".into(),
span,
),
},
}
}
}
fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
match a_string.parse::<bytesize::ByteSize>() {
match a_string.trim().parse::<bytesize::ByteSize>() {
Ok(n) => Ok(n.0 as i64),
Err(_) => Err(ShellError::CantConvert("int".into(), "string".into(), span)),
}

View File

@ -36,8 +36,6 @@ fn into_filesize_str() {
assert!(actual.out.contains("2.0 KiB"));
}
// FIXME: jt: needs more work
#[ignore]
#[test]
fn into_filesize_str_newline() {
let actual = nu!(
@ -51,8 +49,6 @@ fn into_filesize_str_newline() {
assert!(actual.out.contains("2.0 KiB"));
}
// FIXME: jt: needs more work
#[ignore]
#[test]
fn into_filesize_str_many_newlines() {
let actual = nu!(