nushell/tests/repl/test_spread.rs
Alex Ionescu 1c3ff179bc
Improve CellPath display output (#14197)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

Fixes: #13362

This PR fixes the `Display` impl for `CellPath`, as laid out in #13362
and #14090:

```nushell
> $.0."0"
$.0."0"

> $."foo.bar".baz
$."foo.bar".baz
```

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

Cell-paths are now printed using the same `$.` notation that is used to
create them, and ambiguous column names are properly quoted.

# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the
tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# 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.
-->
2024-11-02 10:28:10 -05:00

200 lines
5.0 KiB
Rust

use crate::repl::tests::{fail_test, run_test, TestResult};
use nu_test_support::nu;
#[test]
fn spread_in_list() -> TestResult {
run_test(r#"[...[]] | to nuon"#, "[]").unwrap();
run_test(
r#"[1 2 ...[[3] {x: 1}] 5] | to nuon"#,
"[1, 2, [3], {x: 1}, 5]",
)
.unwrap();
run_test(
r#"[...("foo" | split chars) 10] | to nuon"#,
"[f, o, o, 10]",
)
.unwrap();
run_test(
r#"let l = [1, 2, [3]]; [...$l $l] | to nuon"#,
"[1, 2, [3], [1, 2, [3]]]",
)
.unwrap();
run_test(
r#"[ ...[ ...[ ...[ a ] b ] c ] d ] | to nuon"#,
"[a, b, c, d]",
)
}
#[test]
fn not_spread() -> TestResult {
run_test(r#"def ... [x] { $x }; ... ..."#, "...").unwrap();
run_test(
r#"let a = 4; [... $a ... [1] ... (5) ...bare ...] | to nuon"#,
r#"["...", 4, "...", [1], "...", 5, "...bare", "..."]"#,
)
}
#[test]
fn bad_spread_on_non_list() -> TestResult {
fail_test(r#"let x = 5; [...$x]"#, "cannot spread").unwrap();
fail_test(r#"[...({ x: 1 })]"#, "cannot spread")
}
#[test]
fn spread_type_list() -> TestResult {
run_test(
r#"def f [a: list<int>] { $a | describe }; f [1 ...[]]"#,
"list<int>",
)
.unwrap();
run_test(
r#"def f [a: list<int>] { $a | describe }; f [1 ...[2]]"#,
"list<int>",
)
.unwrap();
fail_test(
r#"def f [a: list<int>] { }; f ["foo" ...[4 5 6]]"#,
"expected int",
)
.unwrap();
fail_test(
r#"def f [a: list<int>] { }; f [1 2 ...["misfit"] 4]"#,
"expected int",
)
}
#[test]
fn spread_in_record() -> TestResult {
run_test(r#"{...{...{...{}}}} | to nuon"#, "{}").unwrap();
run_test(
r#"{foo: bar ...{a: {x: 1}} b: 3} | to nuon"#,
"{foo: bar, a: {x: 1}, b: 3}",
)
}
#[test]
fn duplicate_cols() -> TestResult {
fail_test(r#"{a: 1, ...{a: 3}}"#, "column used twice").unwrap();
fail_test(r#"{...{a: 4, x: 3}, x: 1}"#, "column used twice").unwrap();
fail_test(r#"{...{a: 0, x: 2}, ...{x: 5}}"#, "column used twice")
}
#[test]
fn bad_spread_on_non_record() -> TestResult {
fail_test(r#"let x = 5; { ...$x }"#, "cannot spread").unwrap();
fail_test(r#"{...([1, 2])}"#, "cannot spread")
}
#[test]
fn spread_type_record() -> TestResult {
run_test(
r#"def f [a: record<x: int>] { $a.x }; f { ...{x: 0} }"#,
"0",
)
.unwrap();
fail_test(
r#"def f [a: record<x: int>] {}; f { ...{x: "not an int"} }"#,
"type_mismatch",
)
}
#[test]
fn spread_external_args() {
assert_eq!(
nu!(r#"nu --testbin cococo ...[1 "foo"] 2 ...[3 "bar"]"#).out,
"1 foo 2 3 bar",
);
// exec doesn't have rest parameters but allows unknown arguments
assert_eq!(
nu!(r#"exec nu --testbin cococo "foo" ...[5 6]"#).out,
"foo 5 6"
);
}
#[test]
fn spread_internal_args() -> TestResult {
run_test(
r#"
let list = ["foo" 4]
def f [a b c? d? ...x] { [$a $b $c $d $x] | to nuon }
f 1 2 ...[5 6] 7 ...$list"#,
"[1, 2, null, null, [5, 6, 7, foo, 4]]",
)
.unwrap();
run_test(
r#"
def f [a b c? d? ...x] { [$a $b $c $d $x] | to nuon }
f 1 2 3 ...[5 6]"#,
"[1, 2, 3, null, [5, 6]]",
)
.unwrap();
run_test(
r#"
def f [--flag: int ...x] { [$flag $x] | to nuon }
f 2 ...[foo] 4 --flag 5 6 ...[7 8]"#,
"[5, [2, foo, 4, 6, 7, 8]]",
)
.unwrap();
run_test(
r#"
def f [a b? --flag: int ...x] { [$a $b $flag $x] | to nuon }
f 1 ...[foo] 4 --flag 5 6 ...[7 8]"#,
"[1, null, 5, [foo, 4, 6, 7, 8]]",
)
}
#[test]
fn bad_spread_internal_args() -> TestResult {
fail_test(
r#"
def f [a b c? d? ...x] { echo $a $b $c $d $x }
f 1 ...[5 6]"#,
"Missing required positional argument",
)
.unwrap();
fail_test(
r#"
def f [a b?] { echo a b c d }
f ...[5 6]"#,
"unexpected spread argument",
)
}
#[test]
fn spread_non_list_args() {
fail_test(r#"echo ...(1)"#, "cannot spread value").unwrap();
assert!(nu!(r#"nu --testbin cococo ...(1)"#)
.err
.contains("cannot spread value"));
}
#[test]
fn spread_args_type() -> TestResult {
fail_test(r#"def f [...x: int] {}; f ...["abc"]"#, "expected int")
}
#[test]
fn explain_spread_args() -> TestResult {
run_test(
r#"(explain { || echo ...[1 2] }).cmd_args.0 | select arg_type name type | to nuon"#,
r#"[[arg_type, name, type]; [spread, "[1 2]", list<int>]]"#,
)
}
#[test]
fn disallow_implicit_spread_for_externals() -> TestResult {
fail_test(r#"^echo [1 2]"#, "Lists are not automatically spread")
}
#[test]
fn respect_shape() -> TestResult {
fail_test(
"def foo [...rest] { ...$rest }; foo bar baz",
"Command `...$rest` not found",
)
.unwrap();
fail_test("module foo { ...$bar }", "expected_keyword").unwrap();
run_test(r#"def "...$foo" [] {2}; do { ...$foo }"#, "2").unwrap();
run_test(r#"match "...$foo" { ...$foo => 5 }"#, "5")
}