Skip to content

Commit

Permalink
++= appendAssign operator (#7346)
Browse files Browse the repository at this point in the history
append_assign tests

test type mismatch
  • Loading branch information
raccmonteiro committed Dec 9, 2022
1 parent bc0c9ab commit 82589e1
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/nu-command/src/core_commands/help_operators.rs
Expand Up @@ -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(),
Expand Down
108 changes: 108 additions & 0 deletions crates/nu-command/tests/commands/assignment/append_assign.rs
@@ -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>"));
}
1 change: 1 addition & 0 deletions crates/nu-command/tests/commands/assignment/mod.rs
@@ -0,0 +1 @@
mod append_assign;
1 change: 1 addition & 0 deletions crates/nu-command/tests/commands/mod.rs
Expand Up @@ -2,6 +2,7 @@ mod alias;
mod all;
mod any;
mod append;
mod assignment;
mod break_;
mod cal;
mod cd;
Expand Down
4 changes: 4 additions & 0 deletions crates/nu-engine/src/eval.rs
Expand Up @@ -477,6 +477,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 {
Expand Down
1 change: 1 addition & 0 deletions crates/nu-parser/src/parser.rs
Expand Up @@ -4473,6 +4473,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),
Expand Down
2 changes: 2 additions & 0 deletions crates/nu-protocol/src/ast/operator.rs
Expand Up @@ -51,6 +51,7 @@ pub enum Bits {
pub enum Assignment {
Assign,
PlusAssign,
AppendAssign,
MinusAssign,
MultiplyAssign,
DivideAssign,
Expand All @@ -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, "/="),
Expand Down

0 comments on commit 82589e1

Please sign in to comment.