" Tests for the List and Dict types
func TearDown()
" Run garbage collection after every test
call test_garbagecollect_now()
endfunc
" Tests for List type
" List creation
func Test_list_create()
" Creating List directly with different types
let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
call assert_equal("[1, 'as''d', [1, 2, function('strlen')], {'a': 1}]", string(l))
call assert_equal({'a' : 1}, l[-1])
call assert_equal(1, l[-4])
let x = 10
try
let x = l[-5]
catch
call assert_match('E684:', v:exception)
endtry
call assert_equal(10, x)
endfunc
" List slices
func Test_list_slice()
let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[:])
call assert_equal(['as''d', [1, 2, function('strlen')], {'a': 1}], l[1:])
call assert_equal([1, 'as''d', [1, 2, function('strlen')]], l[:-2])
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([], test_null_list()[:2])
endfunc
" List identity
func Test_list_identity()
let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
let ll = l
let lx = copy(l)
call assert_true(l == ll)
call assert_false(l isnot ll)
call assert_true(l is ll)
call assert_true(l == lx)
call assert_false(l is lx)
call assert_true(l isnot lx)
endfunc
" removing items with :unlet
func Test_list_unlet()
let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
unlet l[2]
call assert_equal([1, 'as''d', {'a': 1}], l)
let l = range(8)
unlet l[:3]
unlet l[1:]
call assert_equal([4], l)
" removing items out of range: silently skip items that don't exist
let l = [0, 1, 2, 3]
call assert_fails('unlet l[2:1]', 'E684')
let l = [0, 1, 2, 3]
unlet l[2:2]
call assert_equal([0, 1, 3], l)
let l = [0, 1, 2, 3]
unlet l[2:3]
call assert_equal([0, 1], l)
let l = [0, 1, 2, 3]
unlet l[2:4]
call assert_equal([0, 1], l)
let l = [0, 1, 2, 3]
unlet l[2:5]
call assert_equal([0, 1], l)
let l = [0, 1, 2, 3]
call assert_fails('unlet l[-1:2]', 'E684')
let l = [0, 1, 2, 3]
unlet l[-2:2]
call assert_equal([0, 1, 3], l)
let l = [0, 1, 2, 3]
unlet l[-3:2]
call assert_equal([0, 3], l)
let l = [0, 1, 2, 3]
unlet l[-4:2]
call assert_equal([3], l)
let l = [0, 1, 2, 3]
unlet l[-5:2]
call assert_equal([3], l)
let l = [0, 1, 2, 3]
unlet l[-6:2]
call assert_equal([3], l)
endfunc
" assignment to a list
func Test_list_assign()
let l = [0, 1, 2, 3]
let [va, vb] = l[2:3]
call assert_equal([2, 3], [va, vb])
call assert_fails('let [va, vb] = l', 'E687')
call assert_fails('let [va, vb] = l[1:1]', 'E688')
endfunc
" test for range assign
func Test_list_range_assign()
let l = [0]
let l[:] = [1, 2]
call assert_equal([1, 2], l)
let l[-4:-1] = [5, 6]
call assert_equal([5, 6], l)
endfunc
" Test removing items in list
func Test_list_func_remove()
" Test removing 1 element
let l = [1, 2, 3, 4]
call assert_equal(1, remove(l, 0))
call assert_equal([2, 3, 4], l)
let l = [1, 2, 3, 4]
call assert_equal(2, remove(l, 1))
call assert_equal([1, 3, 4], l)
let l = [1, 2, 3, 4]
call assert_equal(4, remove(l, -1))
call assert_equal([1, 2, 3], l)
" Test removing range of element(s)
let l = [1, 2, 3, 4]
call assert_equal([3], remove(l, 2, 2))
call assert_equal([1, 2, 4], l)
let l = [1, 2, 3, 4]
call assert_equal([2, 3], remove(l, 1, 2))
call assert_equal([1, 4], l)
let l = [1, 2, 3, 4]
call assert_equal([2, 3], remove(l, -3, -2))
call assert_equal([1, 4], l)
" Test invalid cases
let l = [1, 2, 3, 4]
call assert_fails("call remove(l, 5)", 'E684:')
call assert_fails("call remove(l, 1, 5)", 'E684:')
call assert_fails("call remove(l, 3, 2)", 'E16:')
call assert_fails("call remove(1, 0)", 'E896:')
call assert_fails("call remove(l, l)", 'E745:')
endfunc
" Tests for Dictionary type
func Test_dict()
" Creating Dictionary directly with different types
let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d))
call assert_equal('asd', d.1)
call assert_equal(['-1', '1', 'b'], sort(keys(d)))
call assert_equal(['asd', [1, 2, function('strlen')], {'a':