From 28c21121cfbf1f2ca8b2d1b48547e9ff4f212471 Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Sat, 18 Jun 2022 02:01:37 +0800 Subject: [PATCH] fixes `to nuon` for inf, -inf, and NaN (#5818) --- crates/nu-command/src/formats/to/nuon.rs | 6 +++- .../tests/format_conversions/nuon.rs | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index aae3007b9..771773217 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -87,7 +87,11 @@ fn value_to_string(v: &Value, span: Span) -> Result { )), Value::Filesize { val, .. } => Ok(format!("{}b", *val)), Value::Float { val, .. } => { - if &val.round() == val { + if &val.round() == val + && val != &f64::NAN + && val != &f64::INFINITY + && val != &f64::NEG_INFINITY + { Ok(format!("{}.0", *val)) } else { Ok(format!("{}", *val)) diff --git a/crates/nu-command/tests/format_conversions/nuon.rs b/crates/nu-command/tests/format_conversions/nuon.rs index 7f4c0e136..eac8d3d04 100644 --- a/crates/nu-command/tests/format_conversions/nuon.rs +++ b/crates/nu-command/tests/format_conversions/nuon.rs @@ -202,3 +202,39 @@ fn float_doesnt_become_int() { assert_eq!(actual.out, "1.0") } + +#[test] +fn float_inf_parsed_properly() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + inf | to nuon + "# + )); + + assert_eq!(actual.out, "inf") +} + +#[test] +fn float_neg_inf_parsed_properly() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + -inf | to nuon + "# + )); + + assert_eq!(actual.out, "-inf") +} + +#[test] +fn float_nan_parsed_properly() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + NaN | to nuon + "# + )); + + assert_eq!(actual.out, "NaN") +}