Vim script 軌道上的測試

了解如何在 Exercism 上測試你的 Vim script 練習


這個軌道會使用兩個工具來執行測試套件,並確保符合最佳實務:

  1. vader.vim
  2. vint

執行測試

  1. 安裝 vader.vim。
  2. 開啟你的解答:
    $ vim exercise.vim
    
  3. 載入目前檔案,讓其中的全域函式能在 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 軌道,請參閱說明頁面。