From 43fbf4345d626bc1d0d6fbeb0c03c8ffc9c538fa Mon Sep 17 00:00:00 2001 From: "notryanb@gmail.com" Date: Mon, 14 Oct 2019 17:55:42 -0400 Subject: [PATCH] remove comment and add test for averaging integers --- src/plugins/average.rs | 37 +++++++++++++++++-------------------- tests/filters_test.rs | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/plugins/average.rs b/src/plugins/average.rs index 8f82a23d10..f78078450a 100644 --- a/src/plugins/average.rs +++ b/src/plugins/average.rs @@ -88,27 +88,24 @@ impl Plugin for Average { fn end_filter(&mut self) -> Result, ShellError> { match self.total { None => Ok(vec![]), - Some(ref inner) => { - match inner.item() { - Value::Primitive(Primitive::Int(i)) => { - let total: u64 = i - .tagged(inner.tag.clone()) - .coerce_into("converting for average")?; - let avg = total as f64 / self.count as f64; - let primitive_value: Value = Primitive::from(avg).into(); - let tagged_value = primitive_value.tagged(inner.tag.clone()); - Ok(vec![ReturnSuccess::value(tagged_value)]) - } - Value::Primitive(Primitive::Bytes(bytes)) => { - // let total: u64 = b.tagged(inner.tag.clone()).coerce_into("converting for average")?; - let avg = *bytes as f64 / self.count as f64; - let primitive_value: Value = Primitive::from(avg).into(); - let tagged_value = primitive_value.tagged(inner.tag.clone()); - Ok(vec![ReturnSuccess::value(tagged_value)]) - } - _ => Ok(vec![]), + Some(ref inner) => match inner.item() { + Value::Primitive(Primitive::Int(i)) => { + let total: u64 = i + .tagged(inner.tag.clone()) + .coerce_into("converting for average")?; + let avg = total as f64 / self.count as f64; + let primitive_value: Value = Primitive::from(avg).into(); + let tagged_value = primitive_value.tagged(inner.tag.clone()); + Ok(vec![ReturnSuccess::value(tagged_value)]) } - } + Value::Primitive(Primitive::Bytes(bytes)) => { + let avg = *bytes as f64 / self.count as f64; + let primitive_value: Value = Primitive::from(avg).into(); + let tagged_value = primitive_value.tagged(inner.tag.clone()); + Ok(vec![ReturnSuccess::value(tagged_value)]) + } + _ => Ok(vec![]), + }, } } } diff --git a/tests/filters_test.rs b/tests/filters_test.rs index f0d5dead61..7696b2f80b 100644 --- a/tests/filters_test.rs +++ b/tests/filters_test.rs @@ -579,6 +579,21 @@ fn can_sum() { assert_eq!(actual, "203") } +#[test] +fn can_average() { + let actual = nu!( + cwd: "tests/fixtures/formats", h::pipeline( + r#" + open sgml_description.json + | get glossary.GlossDiv.GlossList.GlossEntry.Sections + | average + | echo $it + "# + )); + + assert_eq!(actual, "101.5000000000000") +} + #[test] fn can_filter_by_unit_size_comparison() { let actual = nu!(