2023-07-17 18:43:51 +02:00
|
|
|
use nu_test_support::nu;
|
2024-10-22 11:54:31 +02:00
|
|
|
use rstest::rstest;
|
2022-11-11 07:51:08 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_variable() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut x = 3; $x = $x + 1; $x");
|
2022-11-11 07:51:08 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "4");
|
|
|
|
}
|
|
|
|
|
2024-10-22 11:54:31 +02:00
|
|
|
#[rstest]
|
|
|
|
#[case("mut in = 3")]
|
|
|
|
#[case("mut in: int = 3")]
|
|
|
|
fn mut_name_builtin_var(#[case] assignment: &str) {
|
|
|
|
assert!(nu!(assignment)
|
2023-04-20 19:44:31 +02:00
|
|
|
.err
|
|
|
|
.contains("'in' is the name of a builtin Nushell variable"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_name_builtin_var_with_dollar() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut $env = 3");
|
2023-04-20 19:44:31 +02:00
|
|
|
|
|
|
|
assert!(actual
|
|
|
|
.err
|
|
|
|
.contains("'env' is the name of a builtin Nushell variable"))
|
|
|
|
}
|
|
|
|
|
2022-11-11 07:51:08 +01:00
|
|
|
#[test]
|
|
|
|
fn mut_variable_in_loop() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut x = 1; for i in 1..10 { $x = $x + $i}; $x");
|
2022-11-11 07:51:08 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "56");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn capture_of_mutable_var() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut x = 123; {|| $x }");
|
2022-11-11 07:51:08 +01:00
|
|
|
|
|
|
|
assert!(actual.err.contains("capture of mutable variable"));
|
|
|
|
}
|
|
|
|
|
2022-11-11 19:50:43 +01:00
|
|
|
#[test]
|
|
|
|
fn mut_add_assign() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut y = 3; $y += 2; $y");
|
2022-11-11 19:50:43 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "5");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_minus_assign() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut y = 3; $y -= 2; $y");
|
2022-11-11 19:50:43 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_multiply_assign() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut y = 3; $y *= 2; $y");
|
2022-11-11 19:50:43 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_divide_assign() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut y = 8; $y /= 2; $y");
|
2022-11-11 19:50:43 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "4");
|
|
|
|
}
|
2022-12-06 18:51:55 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_path_insert() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut y = {abc: 123}; $y.abc = 456; $y.abc");
|
2022-12-06 18:51:55 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "456");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_path_insert_list() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut a = [0 1 2]; $a.3 = 3; $a | to nuon");
|
2022-12-06 18:51:55 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "[0, 1, 2, 3]");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_path_upsert() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut a = {b:[{c:1}]}; $a.b.0.d = 11; $a.b.0.d");
|
2022-12-06 18:51:55 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "11");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_path_upsert_list() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut a = [[[3] 2] 1]; $a.0.0.1 = 0; $a.0.2 = 0; $a.2 = 0; $a | to nuon");
|
2022-12-06 18:51:55 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "[[[3, 0], 2, 0], 1, 0]");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_path_operator_assign() {
|
2023-07-21 17:32:37 +02:00
|
|
|
let actual = nu!("mut a = {b:1}; $a.b += 3; $a.b -= 2; $a.b *= 10; $a.b /= 4; $a.b");
|
2022-12-06 18:51:55 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "5");
|
|
|
|
}
|
2023-05-22 18:51:07 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_records_update_properly() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!("mut a = {}; $a.b.c = 100; $a.b.c");
|
2023-05-22 18:51:07 +02:00
|
|
|
assert_eq!(actual.out, "100");
|
|
|
|
}
|
2023-07-13 10:55:41 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_value_with_if() {
|
|
|
|
let actual = nu!("mut a = 3; $a = if 3 == 3 { 10 }; echo $a");
|
|
|
|
assert_eq!(actual.out, "10");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_value_with_match() {
|
|
|
|
let actual = nu!("mut a = 3; $a = match 3 { 1 => { 'yes!' }, _ => { 'no!' } }; echo $a");
|
|
|
|
assert_eq!(actual.out, "no!");
|
|
|
|
}
|
2024-02-28 16:05:35 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_glob_type() {
|
|
|
|
let actual = nu!("mut x: glob = 'aa'; $x | describe");
|
|
|
|
assert_eq!(actual.out, "glob");
|
|
|
|
}
|
2024-05-02 15:36:37 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_raw_string() {
|
|
|
|
let actual = nu!(r#"mut x = r#'abcde""fghi"''''jkl'#; $x"#);
|
|
|
|
assert_eq!(actual.out, r#"abcde""fghi"''''jkl"#);
|
|
|
|
|
|
|
|
let actual = nu!(r#"mut x = r##'abcde""fghi"''''#jkl'##; $x"#);
|
|
|
|
assert_eq!(actual.out, r#"abcde""fghi"''''#jkl"#);
|
|
|
|
|
|
|
|
let actual = nu!(r#"mut x = r###'abcde""fghi"'''##'#jkl'###; $x"#);
|
|
|
|
assert_eq!(actual.out, r#"abcde""fghi"'''##'#jkl"#);
|
|
|
|
|
|
|
|
let actual = nu!(r#"mut x = r#'abc'#; $x"#);
|
|
|
|
assert_eq!(actual.out, "abc");
|
|
|
|
}
|
2024-11-14 10:05:33 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_should_not_mutate_mut() {
|
|
|
|
let actual = nu!("mut a = 3; def foo [] { $a = 4}");
|
|
|
|
assert!(actual.err.contains("capture of mutable variable"));
|
|
|
|
assert!(!actual.status.success())
|
|
|
|
}
|