Add tests

This commit is contained in:
Shougo Matsushita 2015-12-26 12:12:08 +09:00
parent 402f77feb3
commit 795a13701f
3 changed files with 92 additions and 53 deletions

View File

@ -73,32 +73,6 @@ function! neosnippet#handlers#_all_clear_markers() "{{{
endtry endtry
endfunction"}}} endfunction"}}}
function! neosnippet#handlers#_get_in_paren(str) abort "{{{
let s = ''
let level = 0
for c in split(a:str, '\zs')
if c == '('
let level += 1
if level == 1
continue
endif
elseif c == ')'
if level == 1 && s != ''
return s
else
let level -= 1
endif
endif
if level > 0
let s .= c
endif
endfor
return ''
endfunction"}}}
function! s:is_auto_pairs() abort "{{{ function! s:is_auto_pairs() abort "{{{
return get(g:, 'neopairs#enable', 0) return get(g:, 'neopairs#enable', 0)
endfunction"}}} endfunction"}}}

View File

@ -315,34 +315,55 @@ function! neosnippet#parser#_get_completed_snippet(completed_item) "{{{
" Make snippet arguments " Make snippet arguments
let cnt = 1 let cnt = 1
let snippet = '' let snippet = ''
if key == '(' for arg in split(substitute(
for arg in split(substitute(neosnippet#handlers#_get_in_paren(abbr), \ neosnippet#parser#_get_in_paren(key, pair, abbr),
\ '(\zs.\{-}\ze)', '', 'g'), '[^[]\zs\s*,\s*') \ key.'\zs.\{-}\ze'.pair, '', 'g'), '[^[]\zs\s*,\s*')
if arg ==# 'self' && &filetype ==# 'python' if key ==# '(' && arg ==# 'self' && &filetype ==# 'python'
" Ignore self argument " Ignore self argument
continue continue
endif endif
if cnt != 1 if cnt != 1
let snippet .= ', ' let snippet .= ', '
endif endif
let snippet .= printf('${%d:#:%s}', cnt, escape(arg, '{}')) let snippet .= printf('${%d:#:%s}', cnt, escape(arg, '{}'))
let cnt += 1
endfor
endif
if key != '(' && snippet[-1:] ==# key
let snippet .= '${' . cnt . '}' . pair
let cnt += 1 let cnt += 1
elseif snippet[-1:] !=# pair endfor
let snippet .= pair
endif let snippet .= pair
let snippet .= '${' . cnt . '}' let snippet .= '${' . cnt . '}'
return snippet return snippet
endfunction"}}} endfunction"}}}
function! neosnippet#parser#_get_in_paren(key, pair, str) abort "{{{
let s = ''
let level = 0
for c in split(a:str, '\zs')
if c ==# a:key
let level += 1
if level == 1
continue
endif
elseif c ==# a:pair
if level == 1 && s != ''
return s
else
let level -= 1
endif
endif
if level > 0
let s .= c
endif
endfor
return ''
endfunction"}}}
let &cpo = s:save_cpo let &cpo = s:save_cpo
unlet s:save_cpo unlet s:save_cpo

View File

@ -2,26 +2,70 @@ let s:suite = themis#suite('toml')
let s:assert = themis#helper('assert') let s:assert = themis#helper('assert')
function! s:suite.get_in_paren() function! s:suite.get_in_paren()
call s:assert.equals(neosnippet#handlers#_get_in_paren( call s:assert.equals(neosnippet#parser#_get_in_paren(
\ '(', ')',
\ '(foobar)'), \ '(foobar)'),
\ 'foobar') \ 'foobar')
call s:assert.equals(neosnippet#handlers#_get_in_paren( call s:assert.equals(neosnippet#parser#_get_in_paren(
\ '(', ')',
\ '(foobar, baz)'), \ '(foobar, baz)'),
\ 'foobar, baz') \ 'foobar, baz')
call s:assert.equals(neosnippet#handlers#_get_in_paren( call s:assert.equals(neosnippet#parser#_get_in_paren(
\ '(', ')',
\ '(foobar, (baz))'), \ '(foobar, (baz))'),
\ 'foobar, (baz)') \ 'foobar, (baz)')
call s:assert.equals(neosnippet#handlers#_get_in_paren( call s:assert.equals(neosnippet#parser#_get_in_paren(
\ '(', ')',
\ 'foobar('), \ 'foobar('),
\ '') \ '')
call s:assert.equals(neosnippet#handlers#_get_in_paren( call s:assert.equals(neosnippet#parser#_get_in_paren(
\ '(', ')',
\ 'foobar()'), \ 'foobar()'),
\ '') \ '')
call s:assert.equals(neosnippet#handlers#_get_in_paren( call s:assert.equals(neosnippet#parser#_get_in_paren(
\ '(', ')',
\ 'foobar(fname)'), \ 'foobar(fname)'),
\ 'fname') \ 'fname')
call s:assert.equals(neosnippet#handlers#_get_in_paren( call s:assert.equals(neosnippet#parser#_get_in_paren(
\ '(', ')',
\ 'wait() wait(long, int)'), \ 'wait() wait(long, int)'),
\ 'long, int') \ 'long, int')
endfunction endfunction
function! s:suite.get_completed_snippet()
call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'foo(', 'abbr' : 'foo()',
\ 'menu' : '', 'info' : ''
\ }), ')${1}')
call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'foo(', 'abbr' : 'foo(hoge)',
\ 'menu' : '', 'info' : ''
\ }), '${1:#:hoge})${2}')
call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'foo', 'abbr' : 'foo(hoge)',
\ 'menu' : '', 'info' : ''
\ }), '')
call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'foo(', 'abbr' : 'foo(hoge, piyo)',
\ 'menu' : '', 'info' : ''
\ }), '${1:#:hoge}, ${2:#:piyo})${3}')
call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'foo(', 'abbr' : 'foo(hoge, piyo(foobar))',
\ 'menu' : '', 'info' : ''
\ }), '${1:#:hoge}, ${2:#:piyo()})${3}')
call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'foo{', 'abbr' : 'foo{}',
\ 'menu' : '', 'info' : ''
\ }), '}${1}')
call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'foo{', 'abbr' : 'foo{piyo}',
\ 'menu' : '', 'info' : ''
\ }), '${1:#:piyo}}${2}')
endfunction