2020-07-18 03:59:23 +02:00
|
|
|
use nu_test_support::nu;
|
2024-03-11 19:46:04 +01:00
|
|
|
#[cfg(feature = "sqlite")]
|
2020-07-18 03:59:23 +02:00
|
|
|
use nu_test_support::pipeline;
|
2019-11-21 18:18:00 +01:00
|
|
|
|
2019-12-15 17:15:06 +01:00
|
|
|
#[test]
|
|
|
|
fn filters_by_unit_size_comparison() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats",
|
2022-09-29 00:08:17 +02:00
|
|
|
"ls | where size > 1kib | sort-by size | get name | first | str trim"
|
2019-12-15 17:15:06 +01:00
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "cargo_sample.toml");
|
2019-12-15 17:15:06 +01:00
|
|
|
}
|
2019-11-21 18:18:00 +01:00
|
|
|
|
2020-03-20 23:02:49 +01:00
|
|
|
#[test]
|
|
|
|
fn filters_with_nothing_comparison() {
|
|
|
|
let actual = nu!(
|
2023-03-16 04:50:58 +01:00
|
|
|
r#"'[{"foo": 3}, {"foo": null}, {"foo": 4}]' | from json | get foo | compact | where $it > 1 | math sum"#
|
2020-03-20 23:02:49 +01:00
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "7");
|
2020-03-20 23:02:49 +01:00
|
|
|
}
|
|
|
|
|
2022-12-10 18:23:24 +01:00
|
|
|
#[test]
|
|
|
|
fn where_inside_block_works() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!("{|x| ls | where $it =~ 'foo' } | describe");
|
2022-12-10 18:23:24 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "closure");
|
|
|
|
}
|
|
|
|
|
2022-11-09 23:05:15 +01:00
|
|
|
#[test]
|
|
|
|
fn filters_with_0_arity_block() {
|
2023-04-28 13:25:44 +02:00
|
|
|
let actual = nu!("[1 2 3 4] | where {|| $in < 3 } | to nuon");
|
2022-11-09 23:05:15 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "[1, 2]");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filters_with_1_arity_block() {
|
2023-04-28 13:25:44 +02:00
|
|
|
let actual = nu!("[1 2 3 6 7 8] | where {|e| $e < 5 } | to nuon");
|
2022-11-09 23:05:15 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "[1, 2, 3]");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unique_env_each_iteration() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats",
|
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 13:36:28 +01:00
|
|
|
"[1 2] | where {|| print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
|
2022-11-09 23:05:15 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "truetrue[1, 2]");
|
|
|
|
}
|
|
|
|
|
2020-04-26 07:32:17 +02:00
|
|
|
#[test]
|
|
|
|
fn where_in_table() {
|
|
|
|
let actual = nu!(
|
2022-10-26 18:36:42 +02:00
|
|
|
r#"'[{"name": "foo", "size": 3}, {"name": "foo", "size": 2}, {"name": "bar", "size": 4}]' | from json | where name in ["foo"] | get size | math sum"#
|
2020-04-26 07:32:17 +02:00
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "5");
|
2020-04-26 07:32:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn where_not_in_table() {
|
|
|
|
let actual = nu!(
|
2022-10-26 18:36:42 +02:00
|
|
|
r#"'[{"name": "foo", "size": 3}, {"name": "foo", "size": 2}, {"name": "bar", "size": 4}]' | from json | where name not-in ["foo"] | get size | math sum"#
|
2020-04-26 07:32:17 +02:00
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "4");
|
2020-04-26 07:32:17 +02:00
|
|
|
}
|
|
|
|
|
2022-11-21 14:35:11 +01:00
|
|
|
#[test]
|
2023-02-02 23:59:58 +01:00
|
|
|
fn where_uses_enumerate_index() {
|
2023-04-28 13:25:44 +02:00
|
|
|
let actual = nu!("[7 8 9 10] | enumerate | where {|el| $el.index < 2 } | to nuon");
|
2022-11-21 14:35:11 +01:00
|
|
|
|
2023-02-02 23:59:58 +01:00
|
|
|
assert_eq!(actual.out, "[[index, item]; [0, 7], [1, 8]]");
|
2022-11-21 14:35:11 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 01:58:11 +01:00
|
|
|
#[cfg(feature = "sqlite")]
|
2019-11-21 18:18:00 +01:00
|
|
|
#[test]
|
2019-12-15 17:15:06 +01:00
|
|
|
fn binary_operator_comparisons() {
|
2019-11-21 18:18:00 +01:00
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
open sample.db
|
2022-04-14 05:15:02 +02:00
|
|
|
| get ints
|
2019-11-21 18:18:00 +01:00
|
|
|
| first 4
|
|
|
|
| where z > 4200
|
2022-04-14 05:15:02 +02:00
|
|
|
| get z.0
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "4253");
|
2019-11-21 18:18:00 +01:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
open sample.db
|
2022-04-14 05:15:02 +02:00
|
|
|
| get ints
|
2019-11-21 18:18:00 +01:00
|
|
|
| first 4
|
|
|
|
| where z >= 4253
|
2022-04-14 05:15:02 +02:00
|
|
|
| get z.0
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "4253");
|
2019-11-21 18:18:00 +01:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
open sample.db
|
2022-04-14 05:15:02 +02:00
|
|
|
| get ints
|
2019-11-21 18:18:00 +01:00
|
|
|
| first 4
|
|
|
|
| where z < 10
|
2022-04-14 05:15:02 +02:00
|
|
|
| get z.0
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "1");
|
2019-11-21 18:18:00 +01:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
open sample.db
|
2022-04-14 05:15:02 +02:00
|
|
|
| get ints
|
2019-11-21 18:18:00 +01:00
|
|
|
| first 4
|
|
|
|
| where z <= 1
|
2022-04-14 05:15:02 +02:00
|
|
|
| get z.0
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "1");
|
2019-11-21 18:18:00 +01:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
open sample.db
|
2022-04-14 05:15:02 +02:00
|
|
|
| get ints
|
2019-11-21 18:18:00 +01:00
|
|
|
| where z != 1
|
2022-09-29 00:08:17 +02:00
|
|
|
| first
|
2019-11-21 18:18:00 +01:00
|
|
|
| get z
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "42");
|
2019-11-21 18:18:00 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 01:58:11 +01:00
|
|
|
#[cfg(feature = "sqlite")]
|
2019-11-21 18:18:00 +01:00
|
|
|
#[test]
|
2019-12-15 17:15:06 +01:00
|
|
|
fn contains_operator() {
|
2019-11-21 18:18:00 +01:00
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
open sample.db
|
2022-04-14 05:15:02 +02:00
|
|
|
| get strings
|
2019-11-21 18:18:00 +01:00
|
|
|
| where x =~ ell
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "4");
|
2019-11-21 18:18:00 +01:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
open sample.db
|
2022-04-14 05:15:02 +02:00
|
|
|
| get strings
|
2019-11-21 18:18:00 +01:00
|
|
|
| where x !~ ell
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2023-07-21 17:32:37 +02:00
|
|
|
"
|
2019-11-21 18:18:00 +01:00
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "2");
|
2019-11-21 18:18:00 +01:00
|
|
|
}
|
last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by raise error when given non-lists (#7623)
Closes https://github.com/nushell/nushell/issues/6941
2022-12-31 12:35:12 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fail_on_non_iterator() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(r#"{"name": "foo", "size": 3} | where name == "foo""#);
|
last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by raise error when given non-lists (#7623)
Closes https://github.com/nushell/nushell/issues/6941
2022-12-31 12:35:12 +01:00
|
|
|
|
2023-07-14 05:20:35 +02:00
|
|
|
assert!(actual.err.contains("command doesn't support"));
|
last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by raise error when given non-lists (#7623)
Closes https://github.com/nushell/nushell/issues/6941
2022-12-31 12:35:12 +01:00
|
|
|
}
|
2023-03-26 00:10:09 +01:00
|
|
|
|
|
|
|
// Test that filtering on columns that might be missing/null works
|
|
|
|
#[test]
|
|
|
|
fn where_gt_null() {
|
|
|
|
let actual = nu!("[{foo: 123} {}] | where foo? > 10 | to nuon");
|
|
|
|
assert_eq!(actual.out, "[[foo]; [123]]");
|
|
|
|
}
|