Files
nushell/tests/repl/test_ranges.rs
Solomon 89c0e325fa fix panic when ..= syntax is used in stepped ranges (#16231)
Fixes #16185

# Description

Stepped range literals where `..=` precedes the second value no longer
cause a parser panic:

```diff
random int 1..=3..5
-Error:   x Main thread panicked.
-  |-> at crates/nu-protocol/src/engine/state_working_set.rs:400:48
-  `-> slice index starts at 8 but ends at 7
+Error: nu::parser::parse_mismatch
+
+  × Parse mismatch during operation.
+   ╭─[entry #1:1:15]
+ 1 │ random int 1..=3..5
+   ·               ─┬
+   ·                ╰── expected number
```
2025-07-30 23:38:59 +02:00

90 lines
2.0 KiB
Rust

use crate::repl::tests::{TestResult, fail_test, run_test};
use rstest::rstest;
#[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")?;
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")
}
#[rstest]
#[case("1..=3..", "expected number")]
#[case("..=3..=15", "expected number")]
#[case("..=(..", "expected closing )")]
#[case("..=()..", "expected at least one range bound")]
#[case("..=..", "expected at least one range bound")]
#[test]
fn bad_range_syntax(#[case] input: &str, #[case] expect: &str) -> TestResult {
fail_test(&format!("def foo [r: range] {{}}; foo {input}"), expect)
}