Vim script 赛道上的测试

了解如何在 Exercism 上测试你的 Vim script 练习


本 track 使用两个工具来运行测试套件,并确保符合最佳实践:

  1. vader.vim
  2. vint

运行测试

  1. 安装 vader.vim。
  2. 打开你提交的解答:
    $ vim exercise.vim
    
  3. source 当前文件,让它里面的全局函数可供 Vim 使用:
    :source %
    
  4. 运行测试:
    :Vader exercise.vader
    

路径可能有所不同。

如果你想为上面的步骤准备一个方便的快捷方式,可以把下面这段放进你的 vimrc:

function! s:exercism_tests()
  if expand('%:e') == 'vim'
    let testfile = printf('%s/%s.vader', expand('%:p:h'),
          \ tr(expand('%:p:h:t'), '-', '_'))
    if !filereadable(testfile)
      echoerr 'File does not exist: '. testfile
      return
    endif
    source %
    execute 'Vader' testfile
  else
    let sourcefile = printf('%s/%s.vim', expand('%:p:h'),
          \ tr(expand('%:p:h:t'), '-', '_'))
    if !filereadable(sourcefile)
      echoerr 'File does not exist: '. sourcefile
      return
    endif
    execute 'source' sourcefile
    Vader
  endif
endfunction

autocmd BufRead *.{vader,vim}
      \ command! -buffer Test call s:exercism_tests()

之后,从练习目录中打开任意.vim或.vader文件,然后运行:Test命令。

检查 Vim 文件的代码风格

  1. 安装 vint。
  2. 递归检查所有 Vim 文件:
    $ vint .
    

想了解 Vim script track 的更多详细信息,请查看帮助页面。