remove comment and add test for averaging integers

This commit is contained in:
notryanb@gmail.com 2019-10-14 17:55:42 -04:00
parent 8262c2dd33
commit 43fbf4345d
2 changed files with 32 additions and 20 deletions

View file

@ -88,27 +88,24 @@ impl Plugin for Average {
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> { fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
match self.total { match self.total {
None => Ok(vec![]), None => Ok(vec![]),
Some(ref inner) => { Some(ref inner) => match inner.item() {
match inner.item() { Value::Primitive(Primitive::Int(i)) => {
Value::Primitive(Primitive::Int(i)) => { let total: u64 = i
let total: u64 = i .tagged(inner.tag.clone())
.tagged(inner.tag.clone()) .coerce_into("converting for average")?;
.coerce_into("converting for average")?; let avg = total as f64 / self.count as f64;
let avg = total as f64 / self.count as f64; let primitive_value: Value = Primitive::from(avg).into();
let primitive_value: Value = Primitive::from(avg).into(); let tagged_value = primitive_value.tagged(inner.tag.clone());
let tagged_value = primitive_value.tagged(inner.tag.clone()); Ok(vec![ReturnSuccess::value(tagged_value)])
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![]),
} }
} 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![]),
},
} }
} }
} }

View file

@ -579,6 +579,21 @@ fn can_sum() {
assert_eq!(actual, "203") 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] #[test]
fn can_filter_by_unit_size_comparison() { fn can_filter_by_unit_size_comparison() {
let actual = nu!( let actual = nu!(