-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathclang_format_spec.vim
513 lines (430 loc) · 13.7 KB
/
clang_format_spec.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
" test with vim-vspec
" https://github.com/kana/vim-vspec
" helpers "{{{
" clang-format detection
function! s:detect_clang_format() abort
if $CLANG_FORMAT !=# '' && executable($CLANG_FORMAT)
return $CLANG_FORMAT
endif
for suffix in ['-HEAD', '-5.0', '-4.0', '-3.8', '-3.7', '-3.6', '-3.5', '-3.4', '']
let c = 'clang-format' . suffix
if executable(c)
return c
endif
endfor
throw 'not ok because detect clang-format could not be found in $PATH'
endfunction
let g:clang_format#command = s:detect_clang_format()
function! Chomp(s) abort
return a:s =~# '\n$'
\ ? a:s[0:len(a:s)-2]
\ : a:s
endfunction
function! ChompHead(s) abort
return a:s =~# '^\n'
\ ? a:s[1:len(a:s)-1]
\ : a:s
endfunction
function! GetBuffer() abort
return join(getline(1, '$'), "\n")
endfunction
function! ClangFormat(line1, line2, ...) abort
let opt = printf("-lines=%d:%d '-style={BasedOnStyle: Google, IndentWidth: %d, UseTab: %s", a:line1, a:line2, &l:shiftwidth, &l:expandtab==1 ? "false" : "true")
let file = 'test.cpp'
for a in a:000
if a =~# '^test\.\w\+$'
let file = a
else
let opt .= a
endif
endfor
let opt .= "}'"
let cmd = printf('%s %s ./t/%s --', g:clang_format#command, opt, file)
let result = Chomp(system(cmd))
if v:shell_error
throw "Error on system(): clang-format exited with non-zero.\nCommand: " . cmd . "\nOutput: " . result
endif
return Chomp(system(cmd))
endfunction
"}}}
" setup {{{
let s:root_dir = resolve(getcwd() . '/..')
execute 'set' 'rtp +=' . s:root_dir
runtime! plugin/clang_format.vim
call vspec#customize_matcher('to_be_empty', function('empty'))
function! RaisesException(cmd) abort
try
execute a:cmd
return 0
catch
return 1
endtry
endfunction
call vspec#customize_matcher('to_throw_exception', function('RaisesException'))
"}}}
" test for default settings {{{
describe 'default settings'
it 'provide a default <Plug> mapping'
Expect maparg('<Plug>(operator-clang-format)') not to_be_empty
end
it 'provide autoload functions'
" load autload script
silent! call clang_format#get_version()
silent! call operator#clang_format#do()
Expect exists('*operator#clang_format#do') to_be_true
Expect exists('*clang_format#format') to_be_true
Expect exists('*clang_format#get_version') to_be_true
end
it 'provide variables to customize this plugin'
Expect exists('g:clang_format#extra_args') to_be_true
Expect exists('g:clang_format#code_style') to_be_true
Expect exists('g:clang_format#style_options') to_be_true
Expect exists('g:clang_format#filetype_style_options') to_be_true
Expect exists('g:clang_format#command') to_be_true
Expect exists('g:clang_format#detect_style_file') to_be_true
Expect exists('g:clang_format#praise') to_be_true
Expect exists('g:clang_format#auto_format') to_be_true
Expect exists('g:clang_format#auto_format_on_insert_leave') to_be_true
Expect g:clang_format#extra_args to_be_empty
Expect g:clang_format#code_style ==# 'google'
Expect g:clang_format#style_options to_be_empty
Expect g:clang_format#filetype_style_options to_be_empty
Expect executable(g:clang_format#command) to_be_true
Expect g:clang_format#detect_style_file to_be_true
Expect g:clang_format#praise to_be_true
end
it 'provide commands'
Expect exists(':ClangFormat') to_be_true
Expect exists(':ClangFormatEchoFormattedCode') to_be_true
end
end
"}}}
" test for clang_format#format() {{{
function! s:expect_the_same_output(line1, line2) abort
Expect clang_format#format(a:line1, a:line2) ==# ClangFormat(a:line1, a:line2)
endfunction
describe 'clang_format#format()'
before
let g:clang_format#detect_style_file = 0
new
execute 'silent' 'edit!' './t/test.cpp'
end
after
bdelete!
end
it 'formats whole t/test.cpp'
call s:expect_the_same_output(1, line('$'))
end
it 'formats too long macro definitions'
call s:expect_the_same_output(3, 3)
end
it 'formats one line functions'
call s:expect_the_same_output(5, 5)
end
it 'formats initilizer list definition'
call s:expect_the_same_output(9, 9)
end
it 'formats for statement'
call s:expect_the_same_output(11, 13)
end
it 'formats too long string to multiple lines'
call s:expect_the_same_output(17, 17)
end
it 'doesn''t move cursor'
execute 'normal!' (1+line('$')).'gg'
let pos = getpos('.')
call s:expect_the_same_output(1, line('$'))
Expect pos == getpos('.')
end
describe 'g:clang_format#style_options'
before
let g:clang_format#detect_style_file = 0
new
execute 'silent' 'edit!' './t/test.cpp'
let s:saved_styles = [g:clang_format#style_options, &expandtab, &shiftwidth]
set expandtab
set shiftwidth=4
end
after
close!
let [g:clang_format#style_options, &expandtab, &shiftwidth] = s:saved_styles
end
it 'customizes code styles'
let g:clang_format#style_options = {'UseTab' : 'false', 'IndentWidth' : 4}
call s:expect_the_same_output(1, line('$'))
end
it 'can contain v:true/v:false'
if exists('v:false')
let g:clang_format#style_options = {'UseTab' : v:false, 'IndentWidth' : 4}
call s:expect_the_same_output(1, line('$'))
endif
end
end
it 'ensures to fix issue #38'
let saved = g:clang_format#style_options
try
let g:clang_format#style_options = {
\ "BraceWrapping" : {
\ "AfterControlStatement" : "true" ,
\ "AfterClass " : "true",
\ },
\ }
try
call clang_format#format(1, line('$'))
catch /^YAML:\d\+:\d\+/
" OK
endtry
finally
let g:clang_format#style_options = saved
endtry
end
end
describe 'clang_format#replace()'
before
let g:clang_format#detect_style_file = 0
new
execute 'silent' 'edit!' './t/test.cpp'
let s:cmd_tmp = g:clang_format#command
end
after
bdelete!
let g:clang_format#command = s:cmd_tmp
end
it 'throws an error when command is not found'
let g:clang_format#command = "clang_format_not_exist"
Expect "call clang_format#replace(1, line('$'))" to_throw_exception
end
it 'throws an error when command is not found'
let g:clang_format#command = './t/clang-format-dummy.sh'
Expect "call clang_format#replace(1, line('$'))" to_throw_exception
end
end
" }}}
" test for <Plug>(operator-clang-format) {{{
describe '<Plug>(operator-clang-format)'
before
let g:clang_format#detect_style_file = 0
new
execute 'silent' 'edit!' './t/test.cpp'
map x <Plug>(operator-clang-format)
end
after
bdelete!
end
it 'formats in visual mode'
let by_clang_format_command = ClangFormat(1, line('$'))
normal ggVGx
let buffer = GetBuffer()
Expect by_clang_format_command ==# buffer
end
it 'formats a text object'
" format for statement
let by_clang_format_command = ClangFormat(11, 13)
" move to for statement block
execute 12
" do format a text object {}
normal xa{
let buffer = GetBuffer()
Expect by_clang_format_command ==# buffer
end
it 'doesn''t move cursor'
execute 12
let pos = getpos('.')
normal xa{
Expect pos == getpos('.')
end
end
" }}}
" test for :ClangFormat {{{
describe ':ClangFormat'
describe 'with filetype cpp'
before
let g:clang_format#detect_style_file = 0
new
execute 'silent' 'edit!' './t/test.cpp'
end
after
bdelete!
end
it 'formats the whole code in normal mode'
let by_clang_format_command = ClangFormat(1, line('$'))
ClangFormat
let buffer = GetBuffer()
Expect by_clang_format_command ==# buffer
end
it 'formats selected code in visual mode'
" format for statement
let by_clang_format_command = ClangFormat(11, 13)
" move to for statement block
execute 11
normal! VjjV
'<,'>ClangFormat
let buffer = GetBuffer()
Expect by_clang_format_command ==# buffer
end
it 'doesn''t move cursor'
execute 'normal!' (1+line('$')).'gg'
let pos = getpos('.')
ClangFormat
Expect pos == getpos('.')
end
it 'maintains screen position while formatting'
execute 'normal!' "3\<C-e>"
let prev = line('w0')
ClangFormat
let current = line('w0')
Expect prev ==# current
end
end
describe 'with filetype javascript'
before
let g:clang_format#detect_style_file = 0
new
execute 'silent' 'edit!' './t/test.js'
end
after
bdelete!
end
it 'formats the whole code in normal mode'
let by_clang_format_command = ClangFormat(1, line('$'), 'test.js')
ClangFormat
let buffer = GetBuffer()
Expect by_clang_format_command ==# buffer
end
end
describe 'hard tab'
before
let g:clang_format#detect_style_file = 0
let s:saved_expandtab = &expandtab
let s:saved_shiftwidth = &shiftwidth
set noexpandtab shiftwidth=8
new
execute 'silent' 'edit!' './t/test.cpp'
end
after
let &expandtab = s:saved_expandtab
let &shiftwidth = s:saved_shiftwidth
end
it 'does not break formatting'
let by_clang_format_command = ClangFormat(1, line('$'), 'test.cpp')
ClangFormat
let buffer = GetBuffer()
Expect by_clang_format_command ==# buffer
end
end
end
" }}}
" test for auto formatting {{{
describe 'g:clang_format#auto_format'
before
let g:clang_format#auto_format = 1
new
execute 'silent' 'edit!' './t/test.cpp'
end
after
bdelete!
end
it 'formats a current buffer on BufWritePre if the value is 1'
SKIP 'because somehow BufWritePre event isn''t fired'
let formatted = clang_format#format(1, line('$'))
doautocmd BufWritePre
let auto_formatted = join(getline(1, line('$')), "\n")
Expect auto_formatted ==# formatted
end
end
" }}}
" test for auto formatting on insert leave {{{
describe 'g:clang_format#auto_format_on_insert_leave'
before
let g:clang_format#auto_format_on_insert_leave = 1
new
execute 'silent' 'edit!' './t/test.cpp'
end
after
bdelete!
end
it 'formats a inserted area on InsertLeave if the value is 1'
SKIP 'because somehow InsertEnter and InsertLeave events aren''t fired'
execute 10
execute 'normal' "iif(1+2)return 4;\<Esc>"
Expect getline('.') ==# ' if (1 + 2) return 4;'
end
end
" }}}
" test for auto 'formatexpr' setting feature {{{
describe 'g:clang_format#auto_formatexpr'
before
let g:clang_format#auto_formatexpr = 1
new
execute 'silent' 'edit!' './t/test.cpp'
end
after
bdelete!
end
it 'formats the text object using gq operator'
SKIP 'because of unknown backslash on formatting too long macros'
doautocmd Filetype cpp
let expected = ClangFormat(1, line('$'))
normal ggVGgq
let actual = GetBuffer()
Expect expected ==# actual
end
end
" }}}
" test for undo {{{
describe 'undo formatting text'
before
let g:clang_format#detect_style_file = 0
new
execute 'silent' 'edit!' './t/test.cpp'
end
after
bdelete!
end
it 'can make a change, format (constitutes as a second change), then undo and keep the first change'
normal! ggOmega
ClangFormat
" When running :ClangFormat from a normal vim instance, setline() does add an entry in the undolist
" For some reason it does not do so when running in this test framework
" Also, seems like it is hard to even add another item to the change list too, always rendering:
" # number changes when saved
" # 9 1 0 seconds ago
normal! ggOadditional
:Debug GetBuffer()
" TODO: should enable this
" normal! u
Expect 0 != search('mega', 'cw')
undo
Expect 0 == search('mega', 'cw')
end
it 'restores previous text as editing buffer normally'
let prev = GetBuffer()
ClangFormat
let buf = GetBuffer()
Expect prev != buf
undo
let buf = GetBuffer()
Expect prev ==# buf
end
end
" }}}
" test for empty buffer {{{
describe 'empty buffer'
before
let g:clang_format#detect_style_file = 0
new
end
after
bdelete!
end
it 'can format empty buffer'
ClangFormat
end
it 'can format a buffer containing only new lines'
call setline('.', ['', ''])
ClangFormat
call setline('.', ['', '', ''])
ClangFormat
end
end
" }}}"