2020-07-18 01:59:23 +00:00
|
|
|
use nu_test_support::nu;
|
|
|
|
use nu_test_support::pipeline;
|
2019-11-21 17:18:00 +00:00
|
|
|
|
2019-12-15 16:15:06 +00:00
|
|
|
#[test]
|
|
|
|
fn filters_by_unit_size_comparison() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats",
|
2022-09-28 22:08:17 +00:00
|
|
|
"ls | where size > 1kib | sort-by size | get name | first | str trim"
|
2019-12-15 16:15:06 +00:00
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "cargo_sample.toml");
|
2019-12-15 16:15:06 +00:00
|
|
|
}
|
2019-11-21 17:18:00 +00:00
|
|
|
|
2020-03-20 22:02:49 +00:00
|
|
|
#[test]
|
|
|
|
fn filters_with_nothing_comparison() {
|
|
|
|
let actual = nu!(
|
2023-03-16 03:50:58 +00:00
|
|
|
r#"'[{"foo": 3}, {"foo": null}, {"foo": 4}]' | from json | get foo | compact | where $it > 1 | math sum"#
|
2020-03-20 22:02:49 +00:00
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "7");
|
2020-03-20 22:02:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 17:23:24 +00:00
|
|
|
#[test]
|
|
|
|
fn where_inside_block_works() {
|
2023-07-17 16:43:51 +00:00
|
|
|
let actual = nu!("{|x| ls | where $it =~ 'foo' } | describe");
|
2022-12-10 17:23:24 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "closure");
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:05:15 +00:00
|
|
|
#[test]
|
|
|
|
fn filters_with_0_arity_block() {
|
2023-04-28 11:25:44 +00:00
|
|
|
let actual = nu!("[1 2 3 4] | where {|| $in < 3 } | to nuon");
|
2022-11-09 22:05:15 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "[1, 2]");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filters_with_1_arity_block() {
|
2023-04-28 11:25:44 +00:00
|
|
|
let actual = nu!("[1 2 3 6 7 8] | where {|e| $e < 5 } | to nuon");
|
2022-11-09 22:05:15 +00: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 12:36:28 +00:00
|
|
|
"[1 2] | where {|| print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
|
2022-11-09 22:05:15 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "truetrue[1, 2]");
|
|
|
|
}
|
|
|
|
|
2020-04-26 05:32:17 +00:00
|
|
|
#[test]
|
|
|
|
fn where_in_table() {
|
|
|
|
let actual = nu!(
|
2022-10-26 16:36:42 +00: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 05:32:17 +00:00
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "5");
|
2020-04-26 05:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn where_not_in_table() {
|
|
|
|
let actual = nu!(
|
2022-10-26 16:36:42 +00: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 05:32:17 +00:00
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "4");
|
2020-04-26 05:32:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 13:35:11 +00:00
|
|
|
#[test]
|
2023-02-02 22:59:58 +00:00
|
|
|
fn where_uses_enumerate_index() {
|
2023-04-28 11:25:44 +00:00
|
|
|
let actual = nu!("[7 8 9 10] | enumerate | where {|el| $el.index < 2 } | to nuon");
|
2022-11-21 13:35:11 +00:00
|
|
|
|
2023-02-02 22:59:58 +00:00
|
|
|
assert_eq!(actual.out, "[[index, item]; [0, 7], [1, 8]]");
|
2022-11-21 13:35:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 00:58:11 +00:00
|
|
|
#[cfg(feature = "sqlite")]
|
2019-11-21 17:18:00 +00:00
|
|
|
#[test]
|
2019-12-15 16:15:06 +00:00
|
|
|
fn binary_operator_comparisons() {
|
2019-11-21 17:18:00 +00:00
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2019-11-21 17:18:00 +00:00
|
|
|
r#"
|
|
|
|
open sample.db
|
2022-04-14 03:15:02 +00:00
|
|
|
| get ints
|
2019-11-21 17:18:00 +00:00
|
|
|
| first 4
|
|
|
|
| where z > 4200
|
2022-04-14 03:15:02 +00:00
|
|
|
| get z.0
|
2019-11-21 17:18:00 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "4253");
|
2019-11-21 17:18:00 +00:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2019-11-21 17:18:00 +00:00
|
|
|
r#"
|
|
|
|
open sample.db
|
2022-04-14 03:15:02 +00:00
|
|
|
| get ints
|
2019-11-21 17:18:00 +00:00
|
|
|
| first 4
|
|
|
|
| where z >= 4253
|
2022-04-14 03:15:02 +00:00
|
|
|
| get z.0
|
2019-11-21 17:18:00 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "4253");
|
2019-11-21 17:18:00 +00:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2019-11-21 17:18:00 +00:00
|
|
|
r#"
|
|
|
|
open sample.db
|
2022-04-14 03:15:02 +00:00
|
|
|
| get ints
|
2019-11-21 17:18:00 +00:00
|
|
|
| first 4
|
|
|
|
| where z < 10
|
2022-04-14 03:15:02 +00:00
|
|
|
| get z.0
|
2019-11-21 17:18:00 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "1");
|
2019-11-21 17:18:00 +00:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2019-11-21 17:18:00 +00:00
|
|
|
r#"
|
|
|
|
open sample.db
|
2022-04-14 03:15:02 +00:00
|
|
|
| get ints
|
2019-11-21 17:18:00 +00:00
|
|
|
| first 4
|
|
|
|
| where z <= 1
|
2022-04-14 03:15:02 +00:00
|
|
|
| get z.0
|
2019-11-21 17:18:00 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "1");
|
2019-11-21 17:18:00 +00:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2019-11-21 17:18:00 +00:00
|
|
|
r#"
|
|
|
|
open sample.db
|
2022-04-14 03:15:02 +00:00
|
|
|
| get ints
|
2019-11-21 17:18:00 +00:00
|
|
|
| where z != 1
|
2022-09-28 22:08:17 +00:00
|
|
|
| first
|
2019-11-21 17:18:00 +00:00
|
|
|
| get z
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "42");
|
2019-11-21 17:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 00:58:11 +00:00
|
|
|
#[cfg(feature = "sqlite")]
|
2019-11-21 17:18:00 +00:00
|
|
|
#[test]
|
2019-12-15 16:15:06 +00:00
|
|
|
fn contains_operator() {
|
2019-11-21 17:18:00 +00:00
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2019-11-21 17:18:00 +00:00
|
|
|
r#"
|
|
|
|
open sample.db
|
2022-04-14 03:15:02 +00:00
|
|
|
| get strings
|
2019-11-21 17:18:00 +00:00
|
|
|
| where x =~ ell
|
2021-03-13 21:46:40 +00:00
|
|
|
| length
|
2019-11-21 17:18:00 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "4");
|
2019-11-21 17:18:00 +00:00
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
2019-11-21 17:18:00 +00:00
|
|
|
r#"
|
|
|
|
open sample.db
|
2022-04-14 03:15:02 +00:00
|
|
|
| get strings
|
2019-11-21 17:18:00 +00:00
|
|
|
| where x !~ ell
|
2021-03-13 21:46:40 +00:00
|
|
|
| length
|
2019-11-21 17:18:00 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "2");
|
2019-11-21 17:18:00 +00: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 11:35:12 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fail_on_non_iterator() {
|
2023-07-17 16:43:51 +00: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 11:35:12 +00:00
|
|
|
|
2023-07-14 03:20:35 +00: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 11:35:12 +00:00
|
|
|
}
|
2023-03-25 23:10:09 +00: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]]");
|
|
|
|
}
|