]> git.madduck.net Git - etc/vim.git/blob - test/lsp/test_lsp_custom_request.vader

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / test / lsp / test_lsp_custom_request.vader
1 Before:
2   runtime autoload/ale/linter.vim
3   runtime autoload/ale/lsp.vim
4   runtime autoload/ale/lsp_linter.vim
5
6   let g:address = 'ccls_address'
7   let g:conn_id = -1
8   let g:executable = 'ccls'
9   let g:executable_or_address = ''
10   let g:linter_name = 'ccls'
11   let g:magic_number = 42
12   let g:no_result = 0
13   let g:message_list = []
14   let g:message_id = 1
15   let g:method = '$ccls/call'
16   let g:parameters = {}
17   let g:project_root = '/project/root'
18   let g:response = ''
19   let g:return_value = -1
20
21   let g:linter_list = [{
22   \   'output_stream': 'stdout',
23   \   'lint_file': 0,
24   \   'language': 'cpp',
25   \   'name': g:linter_name,
26   \   'project_root': {b -> g:project_root},
27   \   'aliases': [],
28   \   'read_buffer': 1,
29   \   'command': '%e'
30   \ }]
31
32   let g:callback_result = g:no_result
33
34   " Encode dictionary to jsonrpc
35   function! Encode(obj) abort
36     let l:body = json_encode(a:obj)
37     return 'Content-Length: ' . strlen(l:body) . "\r\n\r\n" . l:body
38   endfunction
39
40   " Replace the StartLSP function to mock an LSP linter
41   function! ale#lsp_linter#StartLSP(buffer, linter, Callback) abort
42     let g:conn_id = ale#lsp#Register(g:executable_or_address, g:project_root, '', {})
43     call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer)
44     call ale#lsp#HandleMessage(g:conn_id, Encode({'method': 'initialize'}))
45
46     let l:details = {
47     \ 'command': g:executable,
48     \ 'buffer': a:buffer,
49     \ 'connection_id': g:conn_id,
50     \ 'project_root': g:project_root,
51     \}
52
53     call ale#lsp_linter#OnInit(a:linter, l:details, a:Callback)
54   endfunction
55
56   " Dummy callback
57   function! Callback(response) abort
58     let g:callback_result = a:response.result.value
59   endfunction
60
61   " Replace the GetAll function to mock an LSP linter
62   function! ale#linter#GetAll(filetype) abort
63     return g:linter_list
64   endfunction
65
66   " Replace the Send function to mock an LSP linter
67   function! ale#lsp#Send(conn_id, message) abort
68     call add(g:message_list, a:message)
69     return g:message_id
70   endfunction
71
72   " Code for a test case
73   function! TestCase(is_notification) abort
74     " Test sending a custom request
75     let g:return_value = ale#lsp_linter#SendRequest(
76     \  bufnr('%'),
77     \  g:linter_name,
78     \  [a:is_notification, g:method, g:parameters],
79     \  function('Callback'))
80
81     Assert index(g:message_list, [a:is_notification, g:method, g:parameters]) >= 0
82
83     " Mock an incoming response to the request
84     let g:response = Encode({
85     \  'id': g:message_id,
86     \  'jsonrpc': '2.0',
87     \  'result': {'value': g:magic_number}
88     \ })
89     call ale#lsp#HandleMessage(g:conn_id, g:response)
90
91     AssertEqual
92     \ a:is_notification ? g:no_result : g:magic_number,
93     \ g:callback_result
94   endfunction
95
96 After:
97   if g:conn_id isnot v:null
98     call ale#lsp#RemoveConnectionWithID(g:conn_id)
99   endif
100
101   unlet! g:callback_result
102   unlet! g:conn_id
103   unlet! g:executable
104   unlet! g:is_notification
105   unlet! g:linter_name
106   unlet! g:magic_number
107   unlet! g:message_list
108   unlet! g:message_id
109   unlet! g:method
110   unlet! g:no_result
111   unlet! g:parameters
112   unlet! g:project_root
113   unlet! g:response
114   unlet! g:return_value
115
116   delfunction Encode
117   delfunction Callback
118   delfunction TestCase
119
120   runtime autoload/ale/linter.vim
121   runtime autoload/ale/lsp.vim
122   runtime autoload/ale/lsp_linter.vim
123
124 Given cpp(Empty cpp file):
125 Execute(Test custom request to server identified by executable):
126   let g:executable_or_address = g:executable
127   let g:linter_list[0].executable = {b -> g:executable}
128   let g:linter_list[0].lsp = 'stdio'
129   let g:is_notification = 0
130
131   call TestCase(g:is_notification)
132
133 Given cpp(Empty cpp file):
134 Execute(Test custom notification to server identified by executable):
135   let g:executable_or_address = g:executable
136   let g:linter_list[0].executable = {b -> g:executable}
137   let g:linter_list[0].lsp = 'stdio'
138   let g:is_notification = 1
139
140   call TestCase(g:is_notification)
141
142 Given cpp(Empty cpp file):
143 Execute(Test custom request to server identified by address):
144   let g:executable_or_address = g:address
145   let g:linter_list[0].address = {b -> g:address}
146   let g:linter_list[0].lsp = 'socket'
147   let g:is_notification = 0
148
149   call TestCase(g:is_notification)
150
151 Given cpp(Empty cpp file):
152 Execute(Test custom notification to server identified by address):
153   let g:executable_or_address = g:address
154   let g:linter_list[0].address = {b -> g:address}
155   let g:linter_list[0].lsp = 'socket'
156   let g:is_notification = 1
157
158   call TestCase(g:is_notification)