Vim scriptトラックでのテスト

ExercismでVim scriptの演習をテストする方法を学びます。


このトラックでは、テストスイートを実行し、ベストプラクティスを守るために、2つのツールを使います:

  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ファイルのlint

  1. vintをインストールします。
  2. すべてのVimファイルを再帰的にlintします:
    $ vint .
    

Vim scriptトラックの詳細については、ヘルプページを参照してください。