vim-patch:8.2.0619: null dict is not handled like an empty dict

Problem:    Null dict is not handled like an empty dict.
Solution:   Fix the code and add tests. (Yegappan Lakshmanan, closes vim/vim#5968)

ea04a6e8ba

Nvim doesn't support modifying NULL list, so comment out a line.
This commit is contained in:
zeertzjq
2022-10-26 19:53:54 +08:00
parent cfccae9584
commit ef363ed37c
10 changed files with 72 additions and 24 deletions

View File

@@ -1449,6 +1449,7 @@ char *get_lval(char *const name, typval_T *const rettv, lval_T *const lp, const
key[len] = prevval;
}
if (wrong) {
tv_clear(&var1);
return NULL;
}
}

View File

@@ -2490,10 +2490,14 @@ bool tv_dict_equal(dict_T *const d1, dict_T *const d2, const bool ic, const bool
if (d1 == d2) {
return true;
}
if (d1 == NULL || d2 == NULL) {
if (tv_dict_len(d1) != tv_dict_len(d2)) {
return false;
}
if (tv_dict_len(d1) != tv_dict_len(d2)) {
if (tv_dict_len(d1) == 0) {
// empty and NULL dicts are considered equal
return true;
}
if (d1 == NULL || d2 == NULL) {
return false;
}

View File

@@ -209,6 +209,7 @@ func Test_blob_add()
call assert_equal(0z001122, b)
call add(b, '51')
call assert_equal(0z00112233, b)
call assert_equal(1, add(v:_null_blob, 0x22))
call assert_fails('call add(b, [9])', 'E745:')
call assert_fails('call add("", 0x01)', 'E897:')

View File

@@ -99,13 +99,6 @@ func Test_loop_over_null_list()
endfor
endfunc
func Test_compare_null_dict()
call assert_fails('let x = v:_null_dict[10]')
call assert_equal({}, {})
call assert_equal(v:_null_dict, v:_null_dict)
call assert_notequal({}, v:_null_dict)
endfunc
func Test_set_reg_null_list()
call setreg('x', v:_null_list)
endfunc

View File

@@ -88,6 +88,7 @@ func Test_map_filter_fails()
call assert_fails("let l = filter('abc', '\"> \" . v:val')", 'E896:')
call assert_fails("let l = filter([1, 2, 3], '{}')", 'E728:')
call assert_fails("let l = filter({'k' : 10}, '{}')", 'E728:')
call assert_fails("let l = filter([1, 2], {})", 'E731:')
call assert_equal(0, map(v:_null_list, '"> " .. v:val'))
call assert_equal(0, map(v:_null_dict, '"> " .. v:val'))
endfunc

View File

@@ -275,6 +275,7 @@ func Test_let_errors()
call assert_fails('let &buftype[1] = "nofile"', 'E18:')
let s = "var"
let var = 1
call assert_fails('let var += [1,2]', 'E734:')
call assert_fails('let {s}.1 = 2', 'E18:')
call assert_fails('let a[1] = 5', 'E121:')
let l = [[1,2]]
@@ -287,6 +288,8 @@ func Test_let_errors()
call assert_fails('let l[0:1] = [1, 2, 3]', 'E710:')
call assert_fails('let l[-2:-3] = [3, 4]', 'E684:')
call assert_fails('let l[0:4] = [5, 6]', 'E711:')
call assert_fails('let g:["a;b"] = 10', 'E461:')
call assert_fails('let g:.min = function("max")', 'E704:')
" This test works only when the language is English
if v:lang == "C" || v:lang =~ '^[Ee]n'

View File

@@ -32,7 +32,11 @@ func Test_list_slice()
call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[0:8])
call assert_equal([], l[8:-1])
call assert_equal([], l[0:-10])
call assert_equal([], v:_null_list[:2])
" perform an operation on a list slice
let l = [1, 2, 3]
let l[:1] += [1, 2]
let l[2:] -= [1]
call assert_equal([2, 4, 2], l)
endfunc
" List identity
@@ -147,6 +151,20 @@ func Test_list_func_remove()
call assert_fails("call remove(l, l)", 'E745:')
endfunc
" List add() function
func Test_list_add()
let l = []
call add(l, 1)
call add(l, [2, 3])
call add(l, [])
call add(l, v:_null_list)
call add(l, {'k' : 3})
call add(l, {})
call add(l, v:_null_dict)
call assert_equal([1, [2, 3], [], [], {'k' : 3}, {}, {}], l)
" call assert_equal(1, add(v:_null_list, 4))
endfunc
" Tests for Dictionary type
func Test_dict()
@@ -660,8 +678,6 @@ func Test_reverse_sort_uniq()
call assert_fails("call sort([1, 2], function('min'), 1)", "E715:")
call assert_fails("call sort([1, 2], function('invalid_func'))", "E700:")
call assert_fails("call sort([1, 2], function('min'))", "E702:")
call assert_equal(0, sort(v:_null_list))
call assert_equal(0, uniq(v:_null_list))
endfunc
" reduce a list or a blob
@@ -709,7 +725,7 @@ func Test_reduce()
call assert_equal(42, reduce(v:_null_blob, function('add'), 42))
endfunc
" splitting a string to a List
" splitting a string to a List using split()
func Test_str_split()
call assert_equal(['aa', 'bb'], split(' aa bb '))
call assert_equal(['aa', 'bb'], split(' aa bb ', '\W\+', 0))
@@ -964,6 +980,11 @@ func Test_listdict_index()
call assert_fails("let l = insert([1,2,3], 4, 10)", 'E684:')
call assert_fails("let l = insert([1,2,3], 4, -10)", 'E684:')
call assert_fails("let l = insert([1,2,3], 4, [])", 'E745:')
let l = [1, 2, 3]
call assert_fails("let l[i] = 3", 'E121:')
call assert_fails("let l[1.1] = 4", 'E806:')
call assert_fails("let l[:i] = [4, 5]", 'E121:')
call assert_fails("let l[:3.2] = [4, 5]", 'E806:')
endfunc
" Test for a null list
@@ -1005,12 +1026,18 @@ endfunc
" Test for a null dict
func Test_null_dict()
call assert_equal(0, items(v:_null_dict))
call assert_equal(0, keys(v:_null_dict))
call assert_equal(0, values(v:_null_dict))
call assert_false(has_key(v:_null_dict, 'k'))
call assert_fails("let l = [] + v:_null_list", 'E15:')
call assert_fails("let l = v:_null_list + []", 'E15:')
call assert_equal(v:_null_dict, v:_null_dict)
let d = v:_null_dict
call assert_equal({}, d)
call assert_equal(0, len(d))
call assert_equal(1, empty(d))
call assert_equal(0, items(d))
call assert_equal(0, keys(d))
call assert_equal(0, values(d))
call assert_false(has_key(d, 'k'))
call assert_equal('{}', string(d))
call assert_fails('let x = v:_null_dict[10]')
call assert_equal({}, {})
endfunc
" vim: shiftwidth=2 sts=2 expandtab

View File

@@ -27,6 +27,7 @@ func Test_unlet_fails()
call assert_fails("unlet l['k'", 'E111:')
let d = {'k' : 1}
call assert_fails("unlet d.k2", 'E716:')
call assert_fails("unlet {a};", 'E488:')
endfunc
func Test_unlet_env()

View File

@@ -623,17 +623,17 @@ func Test_usercmd_custom()
return "a\nb\n"
endfunc
command -nargs=* -complete=customlist,T1 TCmd1
call feedkeys(":T1 \<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"T1 ', @:)
call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"TCmd1 ', @:)
delcommand TCmd1
delfunc T1
func T2(a, c, p)
return ['a', 'b', 'c']
return {}
endfunc
command -nargs=* -complete=customlist,T2 TCmd2
call feedkeys(":T2 \<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"T2 ', @:)
call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"TCmd2 ', @:)
delcommand TCmd2
delfunc T2
endfunc

View File

@@ -1658,6 +1658,20 @@ func Test_compound_assignment_operators()
call assert_fails('let x .= "f"', 'E734')
let x = !3.14
call assert_equal(0.0, x)
" integer and float operations
let x = 1
let x *= 2.1
call assert_equal(2.1, x)
let x = 1
let x /= 0.25
call assert_equal(4.0, x)
let x = 1
call assert_fails('let x %= 0.25', 'E734:')
let x = 1
call assert_fails('let x .= 0.25', 'E734:')
let x = 1.0
call assert_fails('let x += [1.1]', 'E734:')
endif
" Test for environment variable
@@ -1839,6 +1853,9 @@ func Test_missing_end()
" Missing 'in' in a :for statement
call assert_fails('for i range(1) | endfor', 'E690:')
" Incorrect number of variables in for
call assert_fails('for [i,] in range(3) | endfor', 'E475:')
endfunc
" Test for deep nesting of if/for/while/try statements {{{1