nushell/crates/nu-command/tests/commands/break_.rs
Wind e8764de3c6
don't allow break/continue in each and items command (#13398)
# Description
Fixes: #11451

# User-Facing Changes
### Before
```nushell
❯ [1 2 3] | each {|e| break; print $e }
╭────────────╮
│ empty list │
╰────────────╯
```

### After
```
❯ [1 2 3] | each {|e| break; print $e }
Error: nu:🐚:eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[entry #9:1:2]
 1 │ [1 2 3] | each {|e| break; print $e }
   ·  ┬
   ·  ╰── source value
   ╰────

Error:   × Break used outside of loop
   ╭─[entry #9:1:21]
 1 │ [1 2 3] | each {|e| break; print $e }
   ·                     ──┬──
   ·                       ╰── used outside of loop
   ╰────
```

# Tests + Formatting
Removes some tests.
2024-07-19 00:21:02 -07:00

18 lines
324 B
Rust

use nu_test_support::nu;
#[test]
fn break_for_loop() {
let actual = nu!("
for i in 1..10 { if $i == 2 { break }; print $i }
");
assert_eq!(actual.out, "1");
}
#[test]
fn break_while_loop() {
let actual = nu!(r#" while true { break }; print "hello" "#);
assert_eq!(actual.out, "hello");
}