nushell/crates/nu-command/tests/commands/assignment/append_assign.rs
Darren Schroeder e761954cf0
allow different types of lists to be appended (#8157)
# Description

closes #8153

This PR allows different types of lists, like `list<string>` and
`list<any>` to be appended with the `++=` operator.

## Before

```
mut args = [ hello ("hello" | path join world)]
$args ++= [ foo bar ]
Error: nu::parser::type_mismatch (link)

  × Type mismatch during operation.
   ╭─[entry #2:1:1]
 1 │ $args ++= [ foo bar ]
   ·           ─────┬─────
   ·                ╰── expected list<any>, found list<string>
   ╰────
```

## After

```
mut args = [ hello ("hello" | path join world)]
$args ++= [ foo bar ]
$args
╭───┬─────────────╮
│ 0 │ hello       │
│ 1 │ hello\world │
│ 2 │ foo         │
│ 3 │ bar         │
╰───┴─────────────╯
```

# User-Facing Changes

_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_

# 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-02-22 12:53:36 +00:00

107 lines
1.8 KiB
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn append_assign_int() {
let actual = nu!(
cwd: ".", pipeline(
r#"
mut a = [1 2];
$a ++= [3 4];
$a
"#
));
let expected = nu!(
cwd: ".", pipeline(
r#"
[1 2 3 4]
"#
));
print!("{}", actual.out);
print!("{}", expected.out);
assert_eq!(actual.out, expected.out);
}
#[test]
fn append_assign_string() {
let actual = nu!(
cwd: ".", pipeline(
r#"
mut a = [a b];
$a ++= [c d];
$a
"#
));
let expected = nu!(
cwd: ".", pipeline(
r#"
[a b c d]
"#
));
print!("{}", actual.out);
print!("{}", expected.out);
assert_eq!(actual.out, expected.out);
}
#[test]
fn append_assign_any() {
let actual = nu!(
cwd: ".", pipeline(
r#"
mut a = [1 2 a];
$a ++= [b 3];
$a
"#
));
let expected = nu!(
cwd: ".", pipeline(
r#"
[1 2 a b 3]
"#
));
print!("{}", actual.out);
print!("{}", expected.out);
assert_eq!(actual.out, expected.out);
}
#[test]
fn append_assign_both_empty() {
let actual = nu!(
cwd: ".", pipeline(
r#"
mut a = [];
$a ++= [];
$a
"#
));
let expected = nu!(
cwd: ".", pipeline(
r#"
[]
"#
));
print!("{}", actual.out);
print!("{}", expected.out);
assert_eq!(actual.out, expected.out);
}
#[test]
fn append_assign_type_mismatch() {
let actual = nu!(
cwd: ".", pipeline(
r#"
mut a = [1 2];
$a ++= [a];
$a | to json -r;
"#
));
assert_eq!(actual.out, r#"[1,2,"a"]"#);
}