Skip to content

Commit c203fef

Browse files
committed
add test for formatting javascript
1 parent 8969c39 commit c203fef

File tree

1 file changed

+62
-35
lines changed

1 file changed

+62
-35
lines changed

t/clang_format_spec.vim

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ function! GetBuffer()
2929
return join(getline(1, '$'), "\n")
3030
endfunction
3131

32-
function! ClangFormat(line1, line2)
32+
function! ClangFormat(line1, line2, ...)
3333
let opt = printf(" -lines=%d:%d -style='{BasedOnStyle: Google, IndentWidth: %d, UseTab: %s}' ", a:line1, a:line2, &l:shiftwidth, &l:expandtab==1 ? "false" : "true")
34-
let cmd = g:clang_format#command.opt.'./'.s:root_dir.'t/test.cpp --'
34+
let file = a:0 == 0 ? 'test.cpp' : a:1
35+
let cmd = g:clang_format#command.opt.'./'.s:root_dir.'t/' . file . ' --'
3536
return Chomp(system(cmd))
3637
endfunction
3738
"}}}
@@ -77,13 +78,15 @@ describe 'default settings'
7778
Expect exists('g:clang_format#extra_args') to_be_true
7879
Expect exists('g:clang_format#code_style') to_be_true
7980
Expect exists('g:clang_format#style_options') to_be_true
81+
Expect exists('g:clang_format#filetype_style_options') to_be_true
8082
Expect exists('g:clang_format#command') to_be_true
8183
Expect exists('g:clang_format#detect_style_file') to_be_true
8284
Expect exists('g:clang_format#auto_format') to_be_true
8385
Expect exists('g:clang_format#auto_format_on_insert_leave') to_be_true
8486
Expect g:clang_format#extra_args to_be_empty
8587
Expect g:clang_format#code_style ==# 'google'
8688
Expect g:clang_format#style_options to_be_empty
89+
Expect g:clang_format#filetype_style_options to_be_empty
8790
Expect executable(g:clang_format#command) to_be_true
8891
Expect g:clang_format#detect_style_file to_be_true
8992
end
@@ -213,41 +216,65 @@ end
213216
" test for :ClangFormat {{{
214217
describe ':ClangFormat'
215218

216-
before
217-
let g:clang_format#detect_style_file = 0
218-
new
219-
execute 'silent' 'edit!' './'.s:root_dir.'t/test.cpp'
220-
end
221-
222-
after
223-
bdelete!
224-
end
225-
226-
it 'formats the whole code in normal mode'
227-
let by_clang_format_command = ClangFormat(1, line('$'))
228-
ClangFormat
229-
let buffer = GetBuffer()
230-
Expect by_clang_format_command ==# buffer
231-
end
219+
describe 'with filetype cpp'
220+
221+
before
222+
let g:clang_format#detect_style_file = 0
223+
new
224+
execute 'silent' 'edit!' './'.s:root_dir.'t/test.cpp'
225+
end
226+
227+
after
228+
bdelete!
229+
end
230+
231+
it 'formats the whole code in normal mode'
232+
let by_clang_format_command = ClangFormat(1, line('$'))
233+
ClangFormat
234+
let buffer = GetBuffer()
235+
Expect by_clang_format_command ==# buffer
236+
end
237+
238+
it 'formats selected code in visual mode'
239+
" format for statement
240+
let by_clang_format_command = ClangFormat(11, 13)
241+
" move to for statement block
242+
execute 11
243+
normal! VjjV
244+
'<,'>ClangFormat
245+
let buffer = GetBuffer()
246+
Expect by_clang_format_command ==# buffer
247+
end
248+
249+
it 'doesn''t move cursor'
250+
execute 'normal!' (1+line('$')).'gg'
251+
let pos = getpos('.')
252+
ClangFormat
253+
Expect pos == getpos('.')
254+
end
255+
256+
end
257+
258+
describe 'with filetype javascript'
259+
260+
before
261+
let g:clang_format#detect_style_file = 0
262+
new
263+
execute 'silent' 'edit!' './'.s:root_dir.'t/test.js'
264+
end
265+
266+
after
267+
bdelete!
268+
end
269+
270+
it 'formats the whole code in normal mode'
271+
let by_clang_format_command = ClangFormat(1, line('$'), 'test.js')
272+
ClangFormat
273+
let buffer = GetBuffer()
274+
Expect by_clang_format_command ==# buffer
275+
end
232276

233-
it 'formats selected code in visual mode'
234-
" format for statement
235-
let by_clang_format_command = ClangFormat(11, 13)
236-
" move to for statement block
237-
execute 11
238-
normal! VjjV
239-
'<,'>ClangFormat
240-
let buffer = GetBuffer()
241-
Expect by_clang_format_command ==# buffer
242277
end
243-
244-
it 'doesn''t move cursor'
245-
execute 'normal!' (1+line('$')).'gg'
246-
let pos = getpos('.')
247-
ClangFormat
248-
Expect pos == getpos('.')
249-
end
250-
251278
end
252279
" }}}
253280

0 commit comments

Comments
 (0)