diff src/testdir/test_const.vim @ 21847:fb74a3387694 v8.2.1473

patch 8.2.1473: items in a list given to :const can still be modified Commit: https://github.com/vim/vim/commit/021bda56710d98c09a6b35610a476ab2dd8c58ad Author: Bram Moolenaar <Bram@vim.org> Date: Mon Aug 17 21:07:22 2020 +0200 patch 8.2.1473: items in a list given to :const can still be modified Problem: Items in a list given to :const can still be modified. Solution: Work like ":lockvar! name" but don't lock referenced items. Make locking a blob work.
author Bram Moolenaar <Bram@vim.org>
date Mon, 17 Aug 2020 21:15:03 +0200
parents 4a4678d26822
children ff21e2962490
line wrap: on
line diff
--- a/src/testdir/test_const.vim
+++ b/src/testdir/test_const.vim
@@ -273,20 +273,35 @@ func Test_const_with_eval_name()
     call assert_fails('const {s2} = "bar"', 'E995:')
 endfunc
 
-func Test_lock_depth_is_1()
-    const l = [1, 2, 3]
-    const d = {'foo': 10}
-
-    " Modify list - setting item is OK, adding/removing items not
-    let l[0] = 42
+func Test_lock_depth_is_2()
+    " Modify list - error when changing item or adding/removing items
+    const l = [1, 2, [3, 4]]
+    call assert_fails('let l[0] = 42', 'E741:')
+    call assert_fails('let l[2][0] = 42', 'E741:')
     call assert_fails('call add(l, 4)', 'E741:')
     call assert_fails('unlet l[1]', 'E741:')
 
-    " Modify dict - changing item is OK, adding/removing items not
-    let d['foo'] = 'hello'
-    let d.foo = 44
+    " Modify blob - error when changing
+    const b = 0z001122
+    call assert_fails('let b[0] = 42', 'E741:')
+
+    " Modify dict - error when changing item or adding/removing items
+    const d = {'foo': 10}
+    call assert_fails("let d['foo'] = 'hello'", 'E741:')
+    call assert_fails("let d.foo = 'hello'", 'E741:')
     call assert_fails("let d['bar'] = 'hello'", 'E741:')
     call assert_fails("unlet d['foo']", 'E741:')
+
+    " Modifying list or dict item contents is OK.
+    let lvar = ['a', 'b']
+    let bvar = 0z1122
+    const l2 = [0, lvar, bvar]
+    let l2[1][0] = 'c'
+    let l2[2][1] = 0x33
+    call assert_equal([0, ['c', 'b'], 0z1133], l2)
+
+    const d2 = #{a: 0, b: lvar, c: 4}
+    let d2.b[1] = 'd'
 endfunc
 
 " vim: shiftwidth=2 sts=2 expandtab