++= appendAssign operator (#7346) (#7354)

# Description

Closes  https://github.com/nushell/nushell/issues/7346



# Tests + Formatting
```
> mut a = [1 2 3]
> $a ++= [4 5 6]
> $a
[1 2 3 4 5 6]
```
This commit is contained in:
raccmonteiro 2022-12-09 16:20:58 +00:00 committed by GitHub
parent fc5fe4b445
commit b56ad92e25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 124 additions and 0 deletions

View File

@ -81,6 +81,13 @@ fn generate_operator_info() -> Vec<OperatorInfo> {
description: "Adds a value to a variable.".into(),
precedence: 10,
},
OperatorInfo {
op_type: "Assignment".into(),
operator: "++=".into(),
name: "AppendAssign".into(),
description: "Appends a list or a value to a variable.".into(),
precedence: 10,
},
OperatorInfo {
op_type: "Assignment".into(),
operator: "-=".into(),

View File

@ -0,0 +1,108 @@
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];
"#
));
assert!(actual
.err
.contains("expected list<int>, found list<string>"));
}

View File

@ -0,0 +1 @@
mod append_assign;

View File

@ -2,6 +2,7 @@ mod alias;
mod all;
mod any;
mod append;
mod assignment;
mod break_;
mod cal;
mod cd;

View File

@ -474,6 +474,10 @@ pub fn eval_expression(
let lhs = eval_expression(engine_state, stack, lhs)?;
lhs.div(op_span, &rhs, op_span)?
}
Assignment::AppendAssign => {
let lhs = eval_expression(engine_state, stack, lhs)?;
lhs.append(op_span, &rhs, op_span)?
}
};
match &lhs.expr {

View File

@ -4429,6 +4429,7 @@ pub fn parse_operator(
let operator = match contents {
b"=" => Operator::Assignment(Assignment::Assign),
b"+=" => Operator::Assignment(Assignment::PlusAssign),
b"++=" => Operator::Assignment(Assignment::AppendAssign),
b"-=" => Operator::Assignment(Assignment::MinusAssign),
b"*=" => Operator::Assignment(Assignment::MultiplyAssign),
b"/=" => Operator::Assignment(Assignment::DivideAssign),

View File

@ -51,6 +51,7 @@ pub enum Bits {
pub enum Assignment {
Assign,
PlusAssign,
AppendAssign,
MinusAssign,
MultiplyAssign,
DivideAssign,
@ -70,6 +71,7 @@ impl Display for Operator {
match self {
Operator::Assignment(Assignment::Assign) => write!(f, "="),
Operator::Assignment(Assignment::PlusAssign) => write!(f, "+="),
Operator::Assignment(Assignment::AppendAssign) => write!(f, "++="),
Operator::Assignment(Assignment::MinusAssign) => write!(f, "-="),
Operator::Assignment(Assignment::MultiplyAssign) => write!(f, "*="),
Operator::Assignment(Assignment::DivideAssign) => write!(f, "/="),