diff src/testdir/test_tagjump.vim @ 15016:c338c91086b9 v8.1.0519

patch 8.1.0519: cannot save and restore the tag stack commit https://github.com/vim/vim/commit/f49cc60aa802862c595ff619dccc11271633a94b Author: Bram Moolenaar <Bram@vim.org> Date: Sun Nov 11 15:21:05 2018 +0100 patch 8.1.0519: cannot save and restore the tag stack Problem: Cannot save and restore the tag stack. Solution: Add gettagstack() and settagstack(). (Yegappan Lakshmanan, closes #3604)
author Bram Moolenaar <Bram@vim.org>
date Sun, 11 Nov 2018 15:30:07 +0100
parents b88fa651c824
children 213b88f89597
line wrap: on
line diff
--- a/src/testdir/test_tagjump.vim
+++ b/src/testdir/test_tagjump.vim
@@ -257,4 +257,113 @@ func Test_tagjump_etags()
   bwipe!
 endfunc
 
+" Test for getting and modifying the tag stack
+func Test_getsettagstack()
+  call writefile(['line1', 'line2', 'line3'], 'Xfile1')
+  call writefile(['line1', 'line2', 'line3'], 'Xfile2')
+  call writefile(['line1', 'line2', 'line3'], 'Xfile3')
+
+  enew | only
+  call settagstack(1, {'items' : []})
+  call assert_equal(0, gettagstack(1).length)
+  call assert_equal([], gettagstack(1).items)
+  " Error cases
+  call assert_equal({}, gettagstack(100))
+  call assert_equal(-1, settagstack(100, {'items' : []}))
+  call assert_fails('call settagstack(1, [1, 10])', 'E715')
+  call assert_fails("call settagstack(1, {'items' : 10})", 'E714')
+  call assert_fails("call settagstack(1, {'items' : []}, 10)", 'E928')
+  call assert_fails("call settagstack(1, {'items' : []}, 'b')", 'E962')
+
+  set tags=Xtags
+  call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
+        \ "one\tXfile1\t1",
+        \ "three\tXfile3\t3",
+        \ "two\tXfile2\t2"],
+        \ 'Xtags')
+
+  let stk = []
+  call add(stk, {'bufnr' : bufnr('%'), 'tagname' : 'one',
+	\ 'from' : [bufnr('%'), line('.'), col('.'), 0], 'matchnr' : 1})
+  tag one
+  call add(stk, {'bufnr' : bufnr('%'), 'tagname' : 'two',
+	\ 'from' : [bufnr('%'), line('.'), col('.'), 0], 'matchnr' : 1})
+  tag two
+  call add(stk, {'bufnr' : bufnr('%'), 'tagname' : 'three',
+	\ 'from' : [bufnr('%'), line('.'), col('.'), 0], 'matchnr' : 1})
+  tag three
+  call assert_equal(3, gettagstack(1).length)
+  call assert_equal(stk, gettagstack(1).items)
+  " Check for default - current window
+  call assert_equal(3, gettagstack().length)
+  call assert_equal(stk, gettagstack().items)
+
+  " Try to set current index to invalid values
+  call settagstack(1, {'curidx' : -1})
+  call assert_equal(1, gettagstack().curidx)
+  call settagstack(1, {'curidx' : 50})
+  call assert_equal(4, gettagstack().curidx)
+
+  " Try pushing invalid items onto the stack
+  call settagstack(1, {'items' : []})
+  call settagstack(1, {'items' : ["plate"]}, 'a')
+  call assert_equal(0, gettagstack().length)
+  call assert_equal([], gettagstack().items)
+  call settagstack(1, {'items' : [{"tagname" : "abc"}]}, 'a')
+  call assert_equal(0, gettagstack().length)
+  call assert_equal([], gettagstack().items)
+  call settagstack(1, {'items' : [{"from" : 100}]}, 'a')
+  call assert_equal(0, gettagstack().length)
+  call assert_equal([], gettagstack().items)
+  call settagstack(1, {'items' : [{"from" : [2, 1, 0, 0]}]}, 'a')
+  call assert_equal(0, gettagstack().length)
+  call assert_equal([], gettagstack().items)
+
+  " Push one item at a time to the stack
+  call settagstack(1, {'items' : []})
+  call settagstack(1, {'items' : [stk[0]]}, 'a')
+  call settagstack(1, {'items' : [stk[1]]}, 'a')
+  call settagstack(1, {'items' : [stk[2]]}, 'a')
+  call settagstack(1, {'curidx' : 4})
+  call assert_equal({'length' : 3, 'curidx' : 4, 'items' : stk},
+        \ gettagstack(1))
+
+  " Try pushing items onto a full stack
+  for i in range(7)
+    call settagstack(1, {'items' : stk}, 'a')
+  endfor
+  call assert_equal(20, gettagstack().length)
+  call settagstack(1,
+        \ {'items' : [{'tagname' : 'abc', 'from' : [1, 10, 1, 0]}]}, 'a')
+  call assert_equal('abc', gettagstack().items[19].tagname)
+
+  " Tag with multiple matches
+  call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
+        \ "two\tXfile1\t1",
+        \ "two\tXfile2\t3",
+        \ "two\tXfile3\t2"],
+        \ 'Xtags')
+  call settagstack(1, {'items' : []})
+  tag two
+  tnext
+  tnext
+  call assert_equal(1, gettagstack().length)
+  call assert_equal(3, gettagstack().items[0].matchnr)
+
+  " Memory allocation failures
+  call test_alloc_fail(GetAllocId('tagstack_items'), 0, 0)
+  call assert_fails('call gettagstack()', 'E342:')
+  call test_alloc_fail(GetAllocId('tagstack_from'), 0, 0)
+  call assert_fails('call gettagstack()', 'E342:')
+  call test_alloc_fail(GetAllocId('tagstack_details'), 0, 0)
+  call assert_fails('call gettagstack()', 'E342:')
+
+  call settagstack(1, {'items' : []})
+  call delete('Xfile1')
+  call delete('Xfile2')
+  call delete('Xfile3')
+  call delete('Xtags')
+  set tags&
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab