2021-12-25 19:39:42 +00:00
|
|
|
use crate::tests::{fail_test, run_test, TestResult};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn concrete_variable_assignment() -> TestResult {
|
|
|
|
run_test(
|
2021-12-26 20:21:24 +00:00
|
|
|
"let x = (1..100 | each { |y| $y + 100 }); let y = ($x | length); $x | length",
|
2021-12-25 19:39:42 +00:00
|
|
|
"100",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proper_shadow() -> TestResult {
|
|
|
|
run_test("let x = 10; let x = $x + 9; $x", "19")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn in_variable_1() -> TestResult {
|
|
|
|
run_test(r#"[3] | if $in.0 > 4 { "yay!" } else { "boo" }"#, "boo")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn in_variable_2() -> TestResult {
|
|
|
|
run_test(r#"3 | if $in > 2 { "yay!" } else { "boo" }"#, "yay!")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn in_variable_3() -> TestResult {
|
|
|
|
run_test(r#"3 | if $in > 4 { "yay!" } else { $in }"#, "3")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn in_variable_4() -> TestResult {
|
|
|
|
run_test(r#"3 | do { $in }"#, "3")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn in_variable_5() -> TestResult {
|
|
|
|
run_test(r#"3 | if $in > 2 { $in - 10 } else { $in * 10 }"#, "-7")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn in_variable_6() -> TestResult {
|
|
|
|
run_test(r#"3 | if $in > 6 { $in - 10 } else { $in * 10 }"#, "30")
|
|
|
|
}
|
|
|
|
|
2022-04-30 21:13:21 +00:00
|
|
|
#[test]
|
|
|
|
fn in_and_if_else() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"[1, 2, 3] | if false {} else if true { $in | length }"#,
|
|
|
|
"3",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-25 19:39:42 +00:00
|
|
|
#[test]
|
|
|
|
fn help_works_with_missing_requirements() -> TestResult {
|
FEATURE: print example command results in the `help` (#8189)
Should close #8035.
> **Note**
> this is my first technical PR for `nushell`
> - i might very well miss things
> - i tried to be as complete as possible about the changes
> - please require further changes if i did something wrong, i'm open to
any remark :relieved:
# Description
this PR adds, when it is defined in the `examples` method of the
`Command` implementations, the output of the examples to the output of
the `help` command.
this PR
- only modifies `crates/nu-engine/src/documentation.rs` and the
`get_documentation` function
- defines a new `WD` constant to print a **W**hite **D**immed `...`
- a `match` statement at the end of the example loop to
- print a white dimmed `...` when the example is not set, i.e. set to
`None` in the `examples` method of the `Command` implementation of a
command
- pretty print the output of the associated example `Value` when it has
been defined
> **Warning**
> LIMITATIONS:
> - i use snippets from `crates/nu-protocol/src/pipeline_data.rs`
> - the table creation from `pub PipelineData::print`, i.e. the `let
decl_id = ...;` and `let table = ...;` in the change
> - the table item printing from `PipelineData::write_all_and_flush`,
i.e. the `for item in table { ... }`
>
> ADDRESSED:
> - ~~the formatting of the output is not perfect and has to be fully
left aligned with the first column for now~~ (fixed with
[`5abeefd558c34ba9bae15e2f183ff4625442921e`..`a62be1b5a2c730959da5dbc028bb91ffe5093f63`](5abeefd558c34ba9bae15e2f183ff4625442921e..a62be1b5a2c730959da5dbc028bb91ffe5093f63))
> - ~~i'm using `.unwrap()` on both the changes above, not sure how to
handle this for now~~ (fixed for now thanks to 49f1dc080)
> - ~~the tests and `clippy` checks do not pass for now, see below~~
(`clippy` now is happy with 49f1dc080 and the tests pass with
11666bc71526b31a27105a362ce11cf94a55b8b6)
# User-Facing Changes
the output of the `help <command>` command is now augmented with the
outputs of the examples, when they are defined.
- `with-env`
```bash
> help with-env
...
Examples:
Set the MYENV environment variable
> with-env [MYENV "my env value"] { $env.MYENV }
my env value
Set by primitive value list
> with-env [X Y W Z] { $env.X }
Y
Set by single row table
> with-env [[X W]; [Y Z]] { $env.W }
Z
Set by key-value record
> with-env {X: "Y", W: "Z"} { [$env.X $env.W] }
╭───┬───╮
│ 0 │ Y │
│ 1 │ Z │
╰───┴───╯
```
instead of the previous
```bash
> help with-env
...
Examples:
Set the MYENV environment variable
> with-env [MYENV "my env value"] { $env.MYENV }
Set by primitive value list
> with-env [X Y W Z] { $env.X }
Set by single row table
> with-env [[X W]; [Y Z]] { $env.W }
Set by key-value record
> with-env {X: "Y", W: "Z"} { [$env.X $env.W] }
```
- `merge`
```bash
> help merge
...
Examples:
Add an 'index' column to the input table
> [a b c] | wrap name | merge ( [1 2 3] | wrap index )
╭───┬──────╮
│ # │ name │
├───┼──────┤
│ 1 │ a │
│ 2 │ b │
│ 3 │ c │
╰───┴──────╯
Merge two records
> {a: 1, b: 2} | merge {c: 3}
╭───┬───╮
│ a │ 1 │
│ b │ 2 │
│ c │ 3 │
╰───┴───╯
Merge two tables, overwriting overlapping columns
> [{columnA: A0 columnB: B0}] | merge [{columnA: 'A0*'}]
╭───┬─────────┬─────────╮
│ # │ columnA │ columnB │
├───┼─────────┼─────────┤
│ 0 │ A0* │ B0 │
╰───┴─────────┴─────────╯
```
instead of the previous
```bash
> help merge
...
Examples:
Add an 'index' column to the input table
> [a b c] | wrap name | merge ( [1 2 3] | wrap index )
Merge two records
> {a: 1, b: 2} | merge {c: 3}
Merge two tables, overwriting overlapping columns
> [{columnA: A0 columnB: B0}] | merge [{columnA: 'A0*'}]
```
2023-02-26 20:05:11 +00:00
|
|
|
run_test(r#"each --help | lines | length"#, "65")
|
2021-12-25 19:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn scope_variable() -> TestResult {
|
2022-01-24 14:19:38 +00:00
|
|
|
run_test(
|
2022-03-04 16:36:11 +00:00
|
|
|
r#"let x = 3; $nu.scope.vars | where name == "$x" | get type.0"#,
|
2022-01-24 14:19:38 +00:00
|
|
|
"int",
|
|
|
|
)
|
2021-12-25 19:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn earlier_errors() -> TestResult {
|
|
|
|
fail_test(
|
2022-02-17 11:40:24 +00:00
|
|
|
r#"[1, "bob"] | each { |it| $it + 3 } | each { |it| $it / $it } | table"#,
|
2021-12-25 19:39:42 +00:00
|
|
|
"int",
|
|
|
|
)
|
|
|
|
}
|
2022-01-06 20:32:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_flags_are_nothing() -> TestResult {
|
|
|
|
run_test(
|
2022-12-22 20:30:10 +00:00
|
|
|
r#"def foo [--aaa(-a): int, --bbb(-b): int] { (if $aaa == null { 10 } else { $aaa }) + (if $bbb == null { 100 } else { $bbb }) }; foo"#,
|
2022-01-06 20:32:47 +00:00
|
|
|
"110",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_flags_are_nothing2() -> TestResult {
|
|
|
|
run_test(
|
2022-12-22 20:30:10 +00:00
|
|
|
r#"def foo [--aaa(-a): int, --bbb(-b): int] { (if $aaa == null { 10 } else { $aaa }) + (if $bbb == null { 100 } else { $bbb }) }; foo -a 90"#,
|
2022-01-06 20:32:47 +00:00
|
|
|
"190",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_flags_are_nothing3() -> TestResult {
|
|
|
|
run_test(
|
2022-12-22 20:30:10 +00:00
|
|
|
r#"def foo [--aaa(-a): int, --bbb(-b): int] { (if $aaa == null { 10 } else { $aaa }) + (if $bbb == null { 100 } else { $bbb }) }; foo -b 45"#,
|
2022-01-06 20:32:47 +00:00
|
|
|
"55",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_flags_are_nothing4() -> TestResult {
|
|
|
|
run_test(
|
2022-12-22 20:30:10 +00:00
|
|
|
r#"def foo [--aaa(-a): int, --bbb(-b): int] { (if $aaa == null { 10 } else { $aaa }) + (if $bbb == null { 100 } else { $bbb }) }; foo -a 3 -b 10000"#,
|
2022-01-06 20:32:47 +00:00
|
|
|
"10003",
|
|
|
|
)
|
|
|
|
}
|
2022-01-12 04:06:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proper_variable_captures() -> TestResult {
|
|
|
|
run_test(
|
2022-11-10 08:21:49 +00:00
|
|
|
r#"def foo [x] { let y = 100; { || $y + $x } }; do (foo 23)"#,
|
2022-01-12 04:06:56 +00:00
|
|
|
"123",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proper_variable_captures_with_calls() -> TestResult {
|
|
|
|
run_test(
|
Restrict closure expression to be something like `{|| ...}` (#8290)
# Description
As title, closes: #7921 closes: #8273
# User-Facing Changes
when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #2:1:1]
1 │ let x = {ss ss}
· ───┬───
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #4:1:1]
1 │ {major:2, minor:1, patch:4} | values | each { into string }
· ───────┬───────
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```
Just realize that It's a big breaking change, we need to update config
and scripts...
# 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 -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# 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.
2023-03-17 12:36:28 +00:00
|
|
|
r#"def foo [] { let y = 60; def bar [] { $y }; {|| bar } }; do (foo)"#,
|
2022-01-12 04:06:56 +00:00
|
|
|
"60",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proper_variable_captures_with_nesting() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def foo [x] { let z = 100; def bar [y] { $y - $x + $z } ; { |z| bar $z } }; do (foo 11) 13"#,
|
|
|
|
"102",
|
|
|
|
)
|
|
|
|
}
|
2022-01-15 15:26:52 +00:00
|
|
|
|
2022-01-20 18:23:26 +00:00
|
|
|
#[test]
|
|
|
|
fn divide_duration() -> TestResult {
|
|
|
|
run_test(r#"4ms / 4ms"#, "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn divide_filesize() -> TestResult {
|
|
|
|
run_test(r#"4mb / 4mb"#, "1")
|
|
|
|
}
|
2022-01-24 21:55:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn date_comparison() -> TestResult {
|
|
|
|
run_test(r#"(date now) < ((date now) + 2min)"#, "true")
|
|
|
|
}
|
2022-01-26 19:00:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_sees_input() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def c [] { let x = str length; $x }; "hello world" | c"#,
|
|
|
|
"11",
|
|
|
|
)
|
|
|
|
}
|
2022-01-26 23:46:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_sees_in_variable() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def c [] { let x = $in.name; $x | str length }; {name: bob, size: 100 } | c"#,
|
|
|
|
"3",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_sees_in_variable2() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def c [] { let x = ($in | str length); $x }; 'bob' | c"#,
|
|
|
|
"3",
|
|
|
|
)
|
|
|
|
}
|
2022-01-29 20:45:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_env() -> TestResult {
|
|
|
|
run_test(
|
2022-02-09 18:41:41 +00:00
|
|
|
r#"def-env bob [] { let-env BAR = "BAZ" }; bob; $env.BAR"#,
|
2022-01-29 20:45:46 +00:00
|
|
|
"BAZ",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_def_env() -> TestResult {
|
2022-09-21 00:46:01 +00:00
|
|
|
fail_test(r#"def bob [] { let-env BAR = "BAZ" }; bob; $env.BAR"#, "")
|
2022-01-29 20:45:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 18:02:03 +00:00
|
|
|
#[test]
|
|
|
|
fn def_env_hiding_something() -> TestResult {
|
|
|
|
fail_test(
|
2022-08-13 09:55:06 +00:00
|
|
|
r#"let-env FOO = "foo"; def-env bob [] { hide-env FOO }; bob; $env.FOO"#,
|
2022-09-21 00:46:01 +00:00
|
|
|
"",
|
2022-02-04 18:02:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_env_then_hide() -> TestResult {
|
|
|
|
fail_test(
|
2022-08-13 09:55:06 +00:00
|
|
|
r#"def-env bob [] { let-env BOB = "bob" }; def-env un-bob [] { hide-env BOB }; bob; un-bob; $env.BOB"#,
|
2022-09-21 00:46:01 +00:00
|
|
|
"",
|
2022-02-04 18:02:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-29 20:45:46 +00:00
|
|
|
#[test]
|
|
|
|
fn export_def_env() -> TestResult {
|
|
|
|
run_test(
|
2022-02-09 18:41:41 +00:00
|
|
|
r#"module foo { export def-env bob [] { let-env BAR = "BAZ" } }; use foo bob; bob; $env.BAR"#,
|
2022-01-29 20:45:46 +00:00
|
|
|
"BAZ",
|
|
|
|
)
|
|
|
|
}
|
2022-02-04 21:19:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dynamic_let_env() -> TestResult {
|
|
|
|
run_test(r#"let x = "FOO"; let-env $x = "BAZ"; $env.FOO"#, "BAZ")
|
|
|
|
}
|
2022-02-15 12:59:51 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reduce_spans() -> TestResult {
|
|
|
|
fail_test(
|
|
|
|
r#"let x = ([1, 2, 3] | reduce -f 0 { $it.item + 2 * $it.acc }); error make {msg: "oh that hurts", label: {text: "right here", start: (metadata $x).span.start, end: (metadata $x).span.end } }"#,
|
|
|
|
"right here",
|
|
|
|
)
|
|
|
|
}
|
2022-02-16 09:59:44 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn with_env_shorthand_nested_quotes() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"FOO='-arg "hello world"' echo $env | get FOO"#,
|
|
|
|
"-arg \"hello world\"",
|
|
|
|
)
|
|
|
|
}
|
2022-02-21 22:22:21 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_redirection_stderr() -> TestResult {
|
|
|
|
// try a nonsense binary
|
|
|
|
run_test(r#"do -i { asdjw4j5cnaabw44rd }; echo done"#, "done")
|
|
|
|
}
|
2022-02-24 02:02:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn datetime_literal() -> TestResult {
|
|
|
|
run_test(r#"(date now) - 2019-08-23 > 1hr"#, "true")
|
|
|
|
}
|
2022-02-27 22:02:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shortcircuiting_and() -> TestResult {
|
2022-12-07 23:02:11 +00:00
|
|
|
run_test(r#"false and (5 / 0; false)"#, "false")
|
2022-02-27 22:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shortcircuiting_or() -> TestResult {
|
2022-12-07 23:02:11 +00:00
|
|
|
run_test(r#"true or (5 / 0; false)"#, "true")
|
2022-02-27 22:02:53 +00:00
|
|
|
}
|
2022-02-28 16:15:31 +00:00
|
|
|
|
2022-11-26 16:02:37 +00:00
|
|
|
#[test]
|
|
|
|
fn nonshortcircuiting_xor() -> TestResult {
|
|
|
|
run_test(r#"true xor (print "hello"; false) | ignore"#, "hello")
|
|
|
|
}
|
|
|
|
|
2022-02-28 16:15:31 +00:00
|
|
|
#[test]
|
|
|
|
fn open_ended_range() -> TestResult {
|
|
|
|
run_test(r#"1.. | first 100000 | length"#, "100000")
|
|
|
|
}
|
2022-03-03 00:55:03 +00:00
|
|
|
|
2022-03-07 20:08:56 +00:00
|
|
|
#[test]
|
|
|
|
fn default_value1() -> TestResult {
|
|
|
|
run_test(r#"def foo [x = 3] { $x }; foo"#, "3")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value2() -> TestResult {
|
|
|
|
run_test(r#"def foo [x: int = 3] { $x }; foo"#, "3")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value3() -> TestResult {
|
|
|
|
run_test(r#"def foo [--x = 3] { $x }; foo"#, "3")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value4() -> TestResult {
|
|
|
|
run_test(r#"def foo [--x: int = 3] { $x }; foo"#, "3")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value5() -> TestResult {
|
|
|
|
run_test(r#"def foo [x = 3] { $x }; foo 10"#, "10")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value6() -> TestResult {
|
|
|
|
run_test(r#"def foo [x: int = 3] { $x }; foo 10"#, "10")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value7() -> TestResult {
|
|
|
|
run_test(r#"def foo [--x = 3] { $x }; foo --x 10"#, "10")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value8() -> TestResult {
|
|
|
|
run_test(r#"def foo [--x: int = 3] { $x }; foo --x 10"#, "10")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value9() -> TestResult {
|
|
|
|
fail_test(r#"def foo [--x = 3] { $x }; foo --x a"#, "expected int")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value10() -> TestResult {
|
|
|
|
fail_test(r#"def foo [x = 3] { $x }; foo a"#, "expected int")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value11() -> TestResult {
|
|
|
|
fail_test(
|
|
|
|
r#"def foo [x = 3, y] { $x }; foo a"#,
|
|
|
|
"after optional parameter",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value12() -> TestResult {
|
2022-12-31 11:18:53 +00:00
|
|
|
fail_test(
|
|
|
|
r#"def foo [--x:int = "a"] { $x }"#,
|
|
|
|
"default value should be int",
|
|
|
|
)
|
2022-03-07 20:08:56 +00:00
|
|
|
}
|
2022-04-08 21:41:05 +00:00
|
|
|
|
2022-12-27 23:00:44 +00:00
|
|
|
#[test]
|
|
|
|
fn default_value_expression() -> TestResult {
|
|
|
|
run_test(r#"def foo [x = ("foo" | str length)] { $x }; foo"#, "3")
|
|
|
|
}
|
|
|
|
|
2022-04-08 21:41:05 +00:00
|
|
|
#[test]
|
|
|
|
fn loose_each() -> TestResult {
|
Restrict closure expression to be something like `{|| ...}` (#8290)
# Description
As title, closes: #7921 closes: #8273
# User-Facing Changes
when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #2:1:1]
1 │ let x = {ss ss}
· ───┬───
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #4:1:1]
1 │ {major:2, minor:1, patch:4} | values | each { into string }
· ───────┬───────
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```
Just realize that It's a big breaking change, we need to update config
and scripts...
# 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 -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# 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.
2023-03-17 12:36:28 +00:00
|
|
|
run_test(
|
|
|
|
r#"[[1, 2, 3], [4, 5, 6]] | each {|| $in.1 } | math sum"#,
|
|
|
|
"7",
|
|
|
|
)
|
2022-04-08 21:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn in_means_input() -> TestResult {
|
|
|
|
run_test(r#"def shl [] { $in * 2 }; 2 | shl"#, "4")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn in_iteration() -> TestResult {
|
|
|
|
run_test(
|
Restrict closure expression to be something like `{|| ...}` (#8290)
# Description
As title, closes: #7921 closes: #8273
# User-Facing Changes
when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #2:1:1]
1 │ let x = {ss ss}
· ───┬───
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #4:1:1]
1 │ {major:2, minor:1, patch:4} | values | each { into string }
· ───────┬───────
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```
Just realize that It's a big breaking change, we need to update config
and scripts...
# 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 -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# 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.
2023-03-17 12:36:28 +00:00
|
|
|
r#"[3, 4, 5] | each {|| echo $"hi ($in)" } | str join"#,
|
2022-04-08 21:41:05 +00:00
|
|
|
"hi 3hi 4hi 5",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reuseable_in() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"[1, 2, 3, 4] | take (($in | length) - 1) | math sum"#,
|
|
|
|
"6",
|
|
|
|
)
|
|
|
|
}
|
2022-04-09 05:17:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn better_operator_spans() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"metadata ({foo: 10} | (20 - $in.foo)) | get span | $in.start < $in.end"#,
|
|
|
|
"true",
|
|
|
|
)
|
|
|
|
}
|
2022-04-26 18:39:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_right_exclusive() -> TestResult {
|
|
|
|
run_test(r#"[1, 4, 5, 8, 9] | range 1..<3 | math sum"#, "9")
|
|
|
|
}
|
2023-01-28 17:55:29 +00:00
|
|
|
|
|
|
|
/// Issue #7872
|
|
|
|
#[test]
|
|
|
|
fn assignment_to_in_var_no_panic() -> TestResult {
|
|
|
|
fail_test(r#"$in = 3"#, "needs to be a mutable variable")
|
|
|
|
}
|
2023-01-28 19:17:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assignment_to_env_no_panic() -> TestResult {
|
|
|
|
fail_test(r#"$env = 3"#, "cannot_replace_env")
|
|
|
|
}
|