]> Sergey Matveev's repositories - vim-lsp.git/blob - test/gopls_tests.vim
Rearrange workspaceConfiguration call
[vim-lsp.git] / test / gopls_tests.vim
1 vim9script
2 # Unit tests for Vim Language Server Protocol (LSP) golang client
3
4 source common.vim
5
6 var lspServers = [{
7       filetype: ['go'],
8       path: exepath('gopls'),
9       args: ['serve']
10   }]
11 call LspAddServer(lspServers)
12 echomsg systemlist($'{lspServers[0].path} version')
13
14 # Test for :LspGotoDefinition, :LspGotoDeclaration, etc.
15 # This test also tests that multiple locations will be
16 # shown in a list or popup
17 def g:Test_LspGoto()
18   :silent! edit Xtest.go
19   var bnr = bufnr()
20
21   sleep 200m
22
23   var lines =<< trim END
24     package main
25
26     type A/*goto implementation*/ interface {
27             Hello()
28     }
29
30     type B struct{}
31
32     func (b *B) Hello() {}
33
34     type C struct{}
35
36     func (c *C) Hello() {}
37
38     func main() {
39     }
40   END
41   
42   setline(1, lines)
43   :redraw!
44   g:WaitForServerFileLoad(0)
45
46   cursor(9, 10)
47   :LspGotoDefinition
48   assert_equal([7, 6], [line('.'), col('.')])
49   exe "normal! \<C-t>"
50   assert_equal([9, 10], [line('.'), col('.')])
51
52   cursor(9, 13)
53   :LspGotoImpl
54   assert_equal([4, 9], [line('.'), col('.')])
55
56   cursor(13, 13)
57   :LspGotoImpl
58   assert_equal([4, 9], [line('.'), col('.')])
59
60   # Two implementions needs to be shown in a location list
61   cursor(4, 9)
62   assert_equal('', execute('LspGotoImpl'))
63   sleep 200m
64   var loclist: list<dict<any>> = getloclist(0)
65   assert_equal('quickfix', getwinvar(winnr('$'), '&buftype'))
66   assert_equal(2, loclist->len())
67   assert_equal(bnr, loclist[0].bufnr)
68   assert_equal([9, 13, ''], [loclist[0].lnum, loclist[0].col, loclist[0].type])
69   assert_equal([13, 13, ''], [loclist[1].lnum, loclist[1].col, loclist[1].type])
70   lclose
71
72   # Two implementions needs to be shown in a quickfix list
73   g:LspOptionsSet({ useQuickfixForLocations: true })
74   cursor(4, 9)
75   assert_equal('', execute('LspGotoImpl'))
76   sleep 200m
77   var qfl: list<dict<any>> = getqflist()
78   assert_equal('quickfix', getwinvar(winnr('$'), '&buftype'))
79   assert_equal(2, qfl->len())
80   assert_equal(bnr, qfl[0].bufnr)
81   assert_equal([9, 13, ''], [qfl[0].lnum, qfl[0].col, qfl[0].type])
82   assert_equal([13, 13, ''], [qfl[1].lnum, qfl[1].col, qfl[1].type])
83   cclose
84   g:LspOptionsSet({ useQuickfixForLocations: false })
85
86   # Two implementions needs to be peeked in a popup
87   cursor(4, 9)
88   :LspPeekImpl
89   sleep 10m
90   var ids = popup_list()
91   assert_equal(2, ids->len())
92   var filePopupAttrs = ids[0]->popup_getoptions()
93   var refPopupAttrs = ids[1]->popup_getoptions()
94   assert_match('Xtest', filePopupAttrs.title)
95   assert_match('Implementation', refPopupAttrs.title)
96   assert_equal(9, line('.', ids[0])) # current line in left panel
97   assert_equal(2, line('$', ids[1])) # last line in right panel
98   feedkeys("j\<CR>", 'xt')
99   assert_equal(13, line('.'))
100   assert_equal([], popup_list())
101   popup_clear()
102
103   # Jump to the first implementation
104   cursor(4, 9)
105   assert_equal('', execute(':1LspGotoImpl'))
106   assert_equal([9, 13], [line('.'), col('.')])
107
108   # Jump to the second implementation
109   cursor(4, 9)
110   assert_equal('', execute(':2LspGotoImpl'))
111   assert_equal([13, 13], [line('.'), col('.')])
112   bw!
113 enddef
114
115 # Start the gopls language server.  Returns true on success and false on
116 # failure.
117 def g:StartLangServer(): bool
118   return g:StartLangServerWithFile('Xtest.go')
119 enddef
120
121 # vim: shiftwidth=2 softtabstop=2 noexpandtab