mirror of
https://github.com/nushell/nushell.git
synced 2024-12-23 23:49:44 +01:00
remove comment and add test for averaging integers
This commit is contained in:
parent
8262c2dd33
commit
43fbf4345d
@ -88,27 +88,24 @@ impl Plugin for Average {
|
||||
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, 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![]),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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!(
|
||||
|
Loading…
Reference in New Issue
Block a user