nushell/tests/repl/test_ranges.rs
Ian Manske 62e56d3581
Rework operator type errors (#14429)
# Description

This PR adds two new `ParseError` and `ShellError` cases for type errors
relating to operators.
- `OperatorUnsupportedType` is used when a type is not supported by an
operator in any way, shape, or form. E.g., `+` does not support `bool`.
- `OperatorIncompatibleTypes` is used when a operator is used with types
it supports, but the combination of types provided cannot be used
together. E.g., `filesize + duration` is not a valid combination.

The other preexisting error cases related to operators have been removed
and replaced with the new ones above. Namely:

- `ShellError::OperatorMismatch`
- `ShellError::UnsupportedOperator`
- `ParseError::UnsupportedOperationLHS`
- `ParseError::UnsupportedOperationRHS`
- `ParseError::UnsupportedOperationTernary`

# User-Facing Changes

- `help operators` now lists the precedence of `not` as 55 instead of 0
(above the other boolean operators). Fixes #13675.
- `math median` and `math mode` now ignore NaN values so that `[NaN NaN]
| math median` and `[NaN NaN] | math mode` no longer trigger a type
error. Instead, it's now an empty input error. Fixing this in earnest
can be left for a future PR.
- Comparisons with `nan` now return false instead of causing an error.
E.g., `1 == nan` is now `false`.
- All the operator type errors have been standardized and reworked. In
particular, they can now have a help message, which is currently used
for types errors relating to `++`.

```nu
[1] ++ 2
```
```
Error: nu::parser::operator_unsupported_type

  × The '++' operator does not work on values of type 'int'.
   ╭─[entry #1:1:5]
 1 │ [1] ++ 2
   ·     ─┬ ┬
   ·      │ ╰── int
   ·      ╰── does not support 'int'
   ╰────
  help: if you meant to append a value to a list or a record to a table, use the `append` command or wrap the value in a list. For example: `$list ++ $value` should be
        `$list ++ [$value]` or `$list | append $value`.
```
2025-02-12 20:03:40 -08:00

77 lines
1.5 KiB
Rust

use crate::repl::tests::{fail_test, run_test, TestResult};
#[test]
fn int_in_inc_range() -> TestResult {
run_test(r#"1 in -4..9.42"#, "true")
}
#[test]
fn int_in_dec_range() -> TestResult {
run_test(r#"1 in 9..-4.42"#, "true")
}
#[test]
fn int_in_exclusive_range() -> TestResult {
run_test(r#"3 in 0..<3"#, "false")
}
#[test]
fn float_in_inc_range() -> TestResult {
run_test(r#"1.58 in -4.42..9"#, "true")
}
#[test]
fn float_in_dec_range() -> TestResult {
run_test(r#"1.42 in 9.42..-4.42"#, "true")
}
#[test]
fn non_number_in_range() -> TestResult {
fail_test(r#"'a' in 1..3"#, "nu::parser::operator_incompatible_types")
}
#[test]
fn float_not_in_inc_range() -> TestResult {
run_test(r#"1.4 not-in 2..9.42"#, "true")
}
#[test]
fn range_and_reduction() -> TestResult {
run_test(r#"1..6..36 | math sum"#, "148")
}
#[test]
fn zip_ranges() -> TestResult {
run_test(r#"1..3 | zip 4..6 | get 2.1"#, "6")
}
#[test]
fn int_in_stepped_range() -> TestResult {
run_test(r#"7 in 1..3..15"#, "true")
}
#[test]
fn int_in_unbounded_stepped_range() -> TestResult {
run_test(r#"1000001 in 1..3.."#, "true")
}
#[test]
fn int_not_in_unbounded_stepped_range() -> TestResult {
run_test(r#"2 in 1..3.."#, "false")
}
#[test]
fn float_in_stepped_range() -> TestResult {
run_test(r#"5.5 in 1..1.5..10"#, "true")
}
#[test]
fn float_in_unbounded_stepped_range() -> TestResult {
run_test(r#"100.5 in 1..1.5.."#, "true")
}
#[test]
fn float_not_in_unbounded_stepped_range() -> TestResult {
run_test(r#"2.1 in 1.2..3.."#, "false")
}