mirror of
https://github.com/nushell/nushell.git
synced 2025-04-22 20:28:22 +02:00
# Description This PR tries to improve a few error messages. ### Before  ### After  # 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. -->
84 lines
2.3 KiB
Rust
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");
|
|
}
|