diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 07:39:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 07:39:41 +0000 |
commit | 00c068502d170f9f9b59c4a68aa12e8835859f6c (patch) | |
tree | 2047fc01b8c70326d9b87b47a575e7e5f2141b62 /src/testdir/test_vim9_assign.vim | |
parent | Adding upstream version 2:9.1.0016. (diff) | |
download | vim-00c068502d170f9f9b59c4a68aa12e8835859f6c.tar.xz vim-00c068502d170f9f9b59c4a68aa12e8835859f6c.zip |
Adding upstream version 2:9.1.0199.upstream/2%9.1.0199
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/testdir/test_vim9_assign.vim')
-rw-r--r-- | src/testdir/test_vim9_assign.vim | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim index aef09c5..4414f55 100644 --- a/src/testdir/test_vim9_assign.vim +++ b/src/testdir/test_vim9_assign.vim @@ -3484,4 +3484,101 @@ def Test_assign_type_to_list_dict() v9.CheckScriptFailure(lines, 'E1407: Cannot use a Typealias as a variable or value') enddef +" Test for modifying a final variable using a compound operator +def Test_final_var_modification_with_compound_op() + var lines =<< trim END + vim9script + + final i: number = 1000 + assert_fails('i += 2', 'E46: Cannot change read-only variable "i"') + assert_fails('i -= 2', 'E46: Cannot change read-only variable "i"') + assert_fails('i *= 2', 'E46: Cannot change read-only variable "i"') + assert_fails('i /= 2', 'E46: Cannot change read-only variable "i"') + assert_fails('i %= 2', 'E46: Cannot change read-only variable "i"') + assert_equal(1000, i) + + final f: float = 1000.0 + assert_fails('f += 2', 'E46: Cannot change read-only variable "f"') + assert_fails('f -= 2', 'E46: Cannot change read-only variable "f"') + assert_fails('f *= 2', 'E46: Cannot change read-only variable "f"') + assert_fails('f /= 2', 'E46: Cannot change read-only variable "f"') + assert_equal(1000.0, f) + + final s: string = 'abc' + assert_fails('s ..= "y"', 'E46: Cannot change read-only variable "s"') + assert_equal('abc', s) + END + v9.CheckScriptSuccess(lines) +enddef + +" Test for modifying a final variable with a List value +def Test_final_var_with_list_value() + var lines =<< trim END + vim9script + + final listA: list<string> = [] + var listB = listA + + listB->add('a') + assert_true(listA is listB) + assert_equal(['a'], listA) + assert_equal(['a'], listB) + + listB += ['b'] + assert_true(listA is listB) + assert_equal(['a', 'b'], listA) + assert_equal(['a', 'b'], listB) + + listA->add('c') + assert_true(listA is listB) + assert_equal(['a', 'b', 'c'], listA) + assert_equal(['a', 'b', 'c'], listB) + + listA += ['d'] + assert_true(listA is listB) + assert_equal(['a', 'b', 'c', 'd'], listA) + assert_equal(['a', 'b', 'c', 'd'], listB) + END + v9.CheckScriptSuccess(lines) +enddef + +" Test for modifying a final variable with a List value using "+=" from a legacy +" function. +func Test_final_var_with_list_value_legacy() + vim9cmd final g:TestVar = ['a'] + vim9cmd g:TestVar += ['b'] + call assert_equal(['a', 'b'], g:TestVar) +endfunc + +" Test for modifying a final variable with a Blob value +def Test_final_var_with_blob_value() + var lines =<< trim END + vim9script + + final blobA: blob = 0z10 + var blobB = blobA + + blobB->add(32) + assert_true(blobA is blobB) + assert_equal(0z1020, blobA) + assert_equal(0z1020, blobB) + + blobB += 0z30 + assert_true(blobA is blobB) + assert_equal(0z102030, blobA) + assert_equal(0z102030, blobB) + + blobA->add(64) + assert_true(blobA is blobB) + assert_equal(0z10203040, blobA) + assert_equal(0z10203040, blobB) + + blobA += 0z50 + assert_true(blobA is blobB) + assert_equal(0z1020304050, blobA) + assert_equal(0z1020304050, blobB) + END + v9.CheckScriptSuccess(lines) +enddef + " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |