]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/test/test_filerename.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:

Merge commit '294584081929424aec883f90c7d6515b3743358d' as '.vim/bundle/vim-lsp-ale'
[etc/vim.git] / .vim / bundle / ale / test / test_filerename.vader
1 Before:
2   call ale#test#SetDirectory('/testplugin/test')
3   call ale#test#SetFilename('dummy.txt')
4
5   let g:old_filename = expand('%:p')
6   let g:Callback = ''
7   let g:expr_list = []
8   let g:message_list = []
9   let g:handle_code_action_called = 0
10   let g:code_actions = []
11   let g:options = {}
12   let g:capability_checked = ''
13   let g:conn_id = v:null
14   let g:InitCallback = v:null
15
16   runtime autoload/ale/lsp_linter.vim
17   runtime autoload/ale/lsp.vim
18   runtime autoload/ale/util.vim
19   runtime autoload/ale/filerename.vim
20   runtime autoload/ale/code_action.vim
21
22   function! ale#lsp_linter#StartLSP(buffer, linter, Callback) abort
23     let g:conn_id = ale#lsp#Register('executable', '/foo/bar', '', {})
24     call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer)
25
26     if a:linter.lsp is# 'tsserver'
27         call ale#lsp#MarkConnectionAsTsserver(g:conn_id)
28     endif
29
30     let l:details = {
31     \ 'command': 'foobar',
32     \ 'buffer': a:buffer,
33     \ 'connection_id': g:conn_id,
34     \ 'project_root': '/foo/bar',
35     \}
36
37     let g:InitCallback = {-> ale#lsp_linter#OnInit(a:linter, l:details, a:Callback)}
38   endfunction
39
40   function! ale#lsp#HasCapability(conn_id, capability) abort
41     let g:capability_checked = a:capability
42
43     return 1
44   endfunction
45
46   function! ale#lsp#RegisterCallback(conn_id, callback) abort
47     let g:Callback = a:callback
48   endfunction
49
50   function! ale#lsp#Send(conn_id, message) abort
51     call add(g:message_list, a:message)
52
53     return 42
54   endfunction
55
56   function! ale#util#Execute(expr) abort
57     call add(g:expr_list, a:expr)
58   endfunction
59
60   function! ale#code_action#HandleCodeAction(code_action, options) abort
61     let g:handle_code_action_called = 1
62     Assert get(a:options, 'should_save')
63     call add(g:code_actions, a:code_action)
64   endfunction
65
66   function! ale#util#Input(message, value, completion) abort
67     return 'a-new-name'
68   endfunction
69
70   call ale#filerename#SetMap({
71   \  3: {
72   \    'old_name': 'oldName',
73   \    'new_name': 'aNewName',
74   \  },
75   \})
76
77 After:
78   if g:conn_id isnot v:null
79     call ale#lsp#RemoveConnectionWithID(g:conn_id)
80   endif
81
82   call ale#filerename#SetMap({})
83   call ale#test#RestoreDirectory()
84   call ale#linter#Reset()
85
86   unlet! g:capability_checked
87   unlet! g:InitCallback
88   unlet! g:old_filename
89   unlet! g:conn_id
90   unlet! g:Callback
91   unlet! g:message_list
92   unlet! g:expr_list
93   unlet! b:ale_linters
94   unlet! g:options
95   unlet! g:code_actions
96   unlet! g:handle_code_action_called
97
98   runtime autoload/ale/lsp_linter.vim
99   runtime autoload/ale/lsp.vim
100   runtime autoload/ale/util.vim
101   runtime autoload/ale/filerename.vim
102   runtime autoload/ale/code_action.vim
103
104 Execute(Other messages for the tsserver handler should be ignored):
105   call ale#filerename#HandleTSServerResponse(1, {'command': 'foo'})
106   AssertEqual g:handle_code_action_called, 0
107
108 Execute(Failed file rename responses should be handled correctly):
109   call ale#filerename#SetMap({3: {'old_name': 'oldName', 'new_name': 'a-test'}})
110   call ale#filerename#HandleTSServerResponse(
111   \ 1,
112   \ {'command': 'getEditsForFileRename', 'request_seq': 3}
113   \)
114   AssertEqual g:handle_code_action_called, 0
115
116 Given typescript(Some typescript file):
117   foo
118   somelongerline
119   bazxyzxyzxyz
120
121 Execute(Code actions from tsserver should be handled):
122   call ale#filerename#HandleTSServerResponse(1, {
123   \ 'command': 'getEditsForFileRename',
124   \ 'seq': 0,
125   \ 'request_seq': 3,
126   \ 'type': 'response',
127   \ 'success': v:true,
128   \ 'body': [
129   \   {
130   \     'fileName': '/foo/bar/file1.tsx',
131   \     'textChanges': [
132   \       {
133   \         'end': {'offset': 55, 'line': 22},
134   \         'newText': './file2',
135   \         'start': {'offset': 34, 'line': 22},
136   \       }
137   \     ]
138   \   }
139   \ ],
140   \})
141
142   AssertEqual
143   \ [
144   \   {
145   \     'description': 'filerename',
146   \     'changes': [
147   \       {
148   \         'fileName': '/foo/bar/file1.tsx',
149   \         'textChanges': [
150   \           {
151   \             'end': {'offset': 55, 'line': 22},
152   \             'newText': './file2',
153   \             'start': {'offset': 34, 'line': 22},
154   \           }
155   \         ]
156   \       }
157   \     ],
158   \   }
159   \ ],
160   \ g:code_actions
161
162 Execute(HandleTSServerResponse does nothing when no data in filerename_map):
163   call ale#filerename#HandleTSServerResponse(1, {
164   \ 'command': 'getEditsForFileRename',
165   \ 'request_seq': -9,
166   \ 'success': v:true,
167   \ 'body': {}
168   \})
169
170   AssertEqual g:handle_code_action_called, 0
171
172 Execute(Prints a tsserver error message when unsuccessful):
173   call ale#filerename#HandleTSServerResponse(1, {
174   \ 'command': 'getEditsForFileRename',
175   \ 'request_seq': 3,
176   \ 'success': v:false,
177   \ 'message': 'This file cannot be renamed',
178   \})
179
180   AssertEqual g:handle_code_action_called, 0
181   AssertEqual ['echom ''Error renaming file "oldName" to "aNewName". ' .
182   \ 'Reason: This file cannot be renamed'''], g:expr_list
183
184 Execute(Does nothing when no changes):
185   call ale#filerename#HandleTSServerResponse(1, {
186   \ 'command': 'getEditsForFileRename',
187   \ 'request_seq': 3,
188   \ 'success': v:true,
189   \ 'body': [],
190   \})
191
192   AssertEqual g:handle_code_action_called, 0
193   AssertEqual ['echom ''No changes while renaming "oldName" to "aNewName"'''], g:expr_list
194
195 Execute(tsserver file rename requests should be sent):
196   call ale#filerename#SetMap({})
197   call ale#linter#Reset()
198
199   runtime ale_linters/typescript/tsserver.vim
200   call setpos('.', [bufnr(''), 2, 5, 0])
201
202   ALEFileRename
203
204   " We shouldn't register the callback yet.
205   AssertEqual '''''', string(g:Callback)
206
207   AssertEqual type(function('type')), type(g:InitCallback)
208   call g:InitCallback()
209
210   AssertEqual 'filerename', g:capability_checked
211   AssertEqual
212   \ 'function(''ale#filerename#HandleTSServerResponse'')',
213   \ string(g:Callback)
214   AssertEqual
215   \ [
216   \   ale#lsp#tsserver_message#Change(bufnr('')),
217   \   [0, 'ts@getEditsForFileRename', {
218   \     'oldFilePath': expand('%:p'),
219   \     'newFilePath': 'a-new-name',
220   \   }]
221   \ ],
222   \ g:message_list
223   AssertEqual {'42': {'old_name': expand('%:p'), 'new_name': 'a-new-name'}},
224   \ ale#filerename#GetMap()