mirror of
https://github.com/nushell/nushell.git
synced 2024-11-07 17:14:23 +01:00
# 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:
parent
fc5fe4b445
commit
b56ad92e25
@ -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(),
|
||||
|
108
crates/nu-command/tests/commands/assignment/append_assign.rs
Normal file
108
crates/nu-command/tests/commands/assignment/append_assign.rs
Normal 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>"));
|
||||
}
|
1
crates/nu-command/tests/commands/assignment/mod.rs
Normal file
1
crates/nu-command/tests/commands/assignment/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
mod append_assign;
|
@ -2,6 +2,7 @@ mod alias;
|
||||
mod all;
|
||||
mod any;
|
||||
mod append;
|
||||
mod assignment;
|
||||
mod break_;
|
||||
mod cal;
|
||||
mod cd;
|
||||
|
@ -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 {
|
||||
|
@ -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),
|
||||
|
@ -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, "/="),
|
||||
|
Loading…
Reference in New Issue
Block a user