nushell/crates/nu-command/tests/commands/math/sum.rs
Darren Schroeder a0d4ae18ee
better error message for "sum", "product", and "sum_of_squares" (#14711)
# Description

This PR tries to improve a few error messages.

### Before

![image](https://github.com/user-attachments/assets/58ab3ff6-baab-4075-8746-e83cb3acab14)

### After

![image](https://github.com/user-attachments/assets/9653776a-371b-4454-b092-3cc49f1329cd)

 
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the
tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-12-31 16:04:23 -06:00

84 lines
2.3 KiB
Rust

use nu_test_support::{nu, pipeline};
use std::str::FromStr;
#[test]
fn all() {
let sample = r#"
{
meals: [
{description: "1 large egg", calories: 90},
{description: "1 cup white rice", calories: 250},
{description: "1 tablespoon fish oil", calories: 108}
]
}
"#;
let actual = nu!(pipeline(&format!(
r#"
{sample}
| get meals
| get calories
| math sum
"#
)));
assert_eq!(actual.out, "448");
}
#[test]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::float_cmp)]
fn compute_sum_of_individual_row() -> Result<(), String> {
let answers_for_columns = [
("cpu", 88.257434),
("mem", 3032375296.),
("virtual", 102579965952.),
];
for (column_name, expected_value) in answers_for_columns {
let actual = nu!(
cwd: "tests/fixtures/formats/",
format!("open sample-ps-output.json | select {column_name} | math sum | get {column_name}")
);
let result =
f64::from_str(&actual.out).map_err(|_| String::from("Failed to parse float."))?;
assert_eq!(result, expected_value);
}
Ok(())
}
#[test]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::float_cmp)]
fn compute_sum_of_table() -> Result<(), String> {
let answers_for_columns = [
("cpu", 88.257434),
("mem", 3032375296.),
("virtual", 102579965952.),
];
for (column_name, expected_value) in answers_for_columns {
let actual = nu!(
cwd: "tests/fixtures/formats/",
format!("open sample-ps-output.json | select cpu mem virtual | math sum | get {column_name}")
);
let result =
f64::from_str(&actual.out).map_err(|_| String::from("Failed to parse float."))?;
assert_eq!(result, expected_value);
}
Ok(())
}
#[test]
fn sum_of_a_row_containing_a_table_is_an_error() {
let actual = nu!(
cwd: "tests/fixtures/formats/",
"open sample-sys-output.json | math sum"
);
assert!(actual.err.contains("can't convert record"));
}
#[test]
fn const_sum() {
let actual = nu!("const SUM = [1 3] | math sum; $SUM");
assert_eq!(actual.out, "4");
}