mirror of
https://github.com/nushell/nushell.git
synced 2025-03-13 15:08:43 +01:00
bugfix: math commands now return error with infinite range [#15135]
This commit is contained in:
parent
d122bc3d89
commit
4785f5f252
@ -1,6 +1,8 @@
|
||||
use core::slice;
|
||||
use core::{ops::Bound, slice};
|
||||
use indexmap::IndexMap;
|
||||
use nu_protocol::{engine::Call, IntoPipelineData, PipelineData, ShellError, Signals, Span, Value};
|
||||
use nu_protocol::{
|
||||
engine::Call, IntoPipelineData, PipelineData, Range, ShellError, Signals, Span, Value,
|
||||
};
|
||||
|
||||
pub fn run_with_function(
|
||||
call: &Call,
|
||||
@ -91,6 +93,10 @@ pub fn calculate(
|
||||
Ok(Value::record(record, span))
|
||||
}
|
||||
PipelineData::Value(Value::Range { val, .. }, ..) => {
|
||||
match *val {
|
||||
Range::IntRange(range) => ensure_bounded(range.end(), span, name)?,
|
||||
Range::FloatRange(range) => ensure_bounded(range.end(), span, name)?,
|
||||
}
|
||||
let new_vals: Result<Vec<Value>, ShellError> = val
|
||||
.into_range_iter(span, Signals::empty())
|
||||
.map(|val| mf(&[val], span, name))
|
||||
@ -110,3 +116,18 @@ pub fn calculate(
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ensure_bounded<T>(
|
||||
bound: Bound<T>,
|
||||
val_span: Span,
|
||||
call_span: Span,
|
||||
) -> Result<(), ShellError> {
|
||||
match bound {
|
||||
Bound::<T>::Unbounded => Err(ShellError::IncorrectValue {
|
||||
msg: "Range must be bounded".to_string(),
|
||||
val_span,
|
||||
call_span,
|
||||
}),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
@ -5,3 +5,10 @@ fn const_abs() {
|
||||
let actual = nu!("const ABS = -5.5 | math abs; $ABS");
|
||||
assert_eq!(actual.out, "5.5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_abs_range() {
|
||||
let actual = nu!("0..5 | math abs");
|
||||
|
||||
assert!(actual.err.contains("nu::parser::input_type_mismatch"));
|
||||
}
|
||||
|
@ -21,6 +21,20 @@ fn can_average_bytes() {
|
||||
assert_eq!(actual.out, "34985870");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_average_range() {
|
||||
let actual = nu!("0..5 | math avg");
|
||||
|
||||
assert_eq!(actual.out, "2.5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_average_infinite_range() {
|
||||
let actual = nu!("0.. | math avg");
|
||||
|
||||
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_avg() {
|
||||
let actual = nu!("const AVG = [1 3 5] | math avg; $AVG");
|
||||
|
@ -5,3 +5,10 @@ fn const_ceil() {
|
||||
let actual = nu!("const CEIL = 1.5 | math ceil; $CEIL");
|
||||
assert_eq!(actual.out, "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_ceil_range() {
|
||||
let actual = nu!("0..5 | math ceil");
|
||||
|
||||
assert!(actual.err.contains("nu::parser::input_type_mismatch"));
|
||||
}
|
||||
|
@ -5,3 +5,10 @@ fn const_floor() {
|
||||
let actual = nu!("const FLOOR = 15.5 | math floor; $FLOOR");
|
||||
assert_eq!(actual.out, "15");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_floor_range() {
|
||||
let actual = nu!("0.. | math floor");
|
||||
|
||||
assert!(actual.err.contains("nu::parser::input_type_mismatch"));
|
||||
}
|
||||
|
@ -5,3 +5,10 @@ fn const_log() {
|
||||
let actual = nu!("const LOG = 16 | math log 2; $LOG");
|
||||
assert_eq!(actual.out, "4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_log_range() {
|
||||
let actual = nu!("0.. | math log 2");
|
||||
|
||||
assert!(actual.err.contains("nu::parser::input_type_mismatch"));
|
||||
}
|
||||
|
@ -5,3 +5,10 @@ fn const_max() {
|
||||
let actual = nu!("const MAX = [1 3 5] | math max; $MAX");
|
||||
assert_eq!(actual.out, "5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_max_infinite_range() {
|
||||
let actual = nu!("0.. | math max");
|
||||
|
||||
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
||||
}
|
||||
|
@ -41,3 +41,10 @@ fn const_median() {
|
||||
let actual = nu!("const MEDIAN = [1 3 5] | math median; $MEDIAN");
|
||||
assert_eq!(actual.out, "3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_median_infinite_range() {
|
||||
let actual = nu!("0.. | math median");
|
||||
|
||||
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
||||
}
|
||||
|
@ -5,3 +5,10 @@ fn const_min() {
|
||||
let actual = nu!("const MIN = [1 3 5] | math min; $MIN");
|
||||
assert_eq!(actual.out, "1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_min_infinite_range() {
|
||||
let actual = nu!("0.. | math min");
|
||||
|
||||
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
||||
}
|
||||
|
@ -5,3 +5,10 @@ fn const_avg() {
|
||||
let actual = nu!("const MODE = [1 3 3 5] | math mode; $MODE");
|
||||
assert_eq!(actual.out, "╭───┬───╮│ 0 │ 3 │╰───┴───╯");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_mode_range() {
|
||||
let actual = nu!("0..5 | math mode");
|
||||
|
||||
assert!(actual.err.contains("nu::parser::input_type_mismatch"));
|
||||
}
|
||||
|
@ -5,3 +5,10 @@ fn const_product() {
|
||||
let actual = nu!("const PROD = [1 3 5] | math product; $PROD");
|
||||
assert_eq!(actual.out, "15");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_product_infinite_range() {
|
||||
let actual = nu!("0.. | math product");
|
||||
|
||||
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
||||
}
|
||||
|
@ -40,3 +40,10 @@ fn const_round() {
|
||||
let actual = nu!("const ROUND = 18.345 | math round; $ROUND");
|
||||
assert_eq!(actual.out, "18");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_round_infinite_range() {
|
||||
let actual = nu!("0..5 | math round");
|
||||
|
||||
assert!(actual.err.contains("nu::parser::input_type_mismatch"));
|
||||
}
|
||||
|
@ -26,3 +26,10 @@ fn const_sqrt() {
|
||||
let actual = nu!("const SQRT = 4 | math sqrt; $SQRT");
|
||||
assert_eq!(actual.out, "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_sqrt_range() {
|
||||
let actual = nu!("0..5 | math sqrt");
|
||||
|
||||
assert!(actual.err.contains("nu::parser::input_type_mismatch"));
|
||||
}
|
||||
|
@ -5,3 +5,10 @@ fn const_avg() {
|
||||
let actual = nu!("const SDEV = [1 2] | math stddev; $SDEV");
|
||||
assert_eq!(actual.out, "0.5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_stddev_range() {
|
||||
let actual = nu!("0..5 | math stddev");
|
||||
|
||||
assert!(actual.err.contains("nu::parser::input_type_mismatch"));
|
||||
}
|
||||
|
@ -81,3 +81,10 @@ fn const_sum() {
|
||||
let actual = nu!("const SUM = [1 3] | math sum; $SUM");
|
||||
assert_eq!(actual.out, "4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_sum_infinite_range() {
|
||||
let actual = nu!("0.. | math sum");
|
||||
|
||||
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
||||
}
|
||||
|
@ -5,3 +5,10 @@ fn const_variance() {
|
||||
let actual = nu!("const VAR = [1 2 3 4 5] | math variance; $VAR");
|
||||
assert_eq!(actual.out, "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_variance_range() {
|
||||
let actual = nu!("0..5 | math variance");
|
||||
|
||||
assert!(actual.err.contains("nu::parser::input_type_mismatch"));
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
# nu-pretty-hex
|
||||
|
||||
An update of prett-hex to make it prettier
|
||||
An update of pretty-hex to make it prettier
|
||||
|
||||
[](https://crates.io/crates/pretty-hex)
|
||||
[](https://docs.rs/pretty-hex)
|
||||
|
Loading…
Reference in New Issue
Block a user