├── CHANGELOG.md ├── LICENSE ├── README.md ├── bin ├── cortado ├── major_filetype └── run_in_os_x_terminal └── plugin └── mocha.vim /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 2015-06-09 2 | * Fix InSpecFile() [#2](https://github.com/geekjuice/vim-mocha/pull/2) 3 | * Update RunLastSpec for last line and last file 4 | [#3](https://github.com/geekjuice/vim-mocha/pull/3) 5 | 6 | 7 | ## 2014-02-04 8 | * Update cortado and give test dir precedence over spec dir 9 | 10 | 11 | ## 2014-02-03 12 | * Extract newer version from vim-spec 13 | 14 | 15 | ## 2013-12-28 16 | * Fix `RunNearestSpec` Regex 17 | * Fix Regex for `InSpecFile` 18 | 19 | 20 | ## 2013-10-22 21 | * Based off of [Attamusc/vim-mocha](https://github.com/Attamusc/vim-mocha) and 22 | subsequently [thoughtbot/vim-rspec](https://github.com/thoughtbot/vim-rspec). 23 | * Adds mocha version of `RunNearestSpec` using mocha's grep on `it` 24 | description. 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2013 Nicholas 'Geekjuice' Hwang, inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-mocha 2 | 3 | This is a lightweight Mocha runner for Vim. Extracted from 4 | [vim-spec](https://github.com/geekjuice/vim-spec) 5 | 6 | Based off of [Attamusc/vim-mocha](https://github.com/Attamusc/vim-mocha) and 7 | subsequently [thoughtbot/vim-rspec](https://github.com/thoughtbot/vim-rspec). 8 | 9 | 10 | __Use both RSpec and Mocha? Take a look at [vim-spec](https://github.com/geekjuice/vim-spec)!__ 11 | 12 | 13 | ## Installation 14 | 15 | Using [vundle](https://github.com/gmarik/vundle): 16 | 17 | ```vim 18 | Bundle 'geekjuice/vim-mocha' 19 | ``` 20 | 21 | If using zsh on OS X it may be necessary to run move `/etc/zshenv` to `/etc/zshrc`. 22 | 23 | 24 | Using [pathogen](https://github.com/tpope/vim-pathogen) 25 | 26 | ```sh 27 | cd ~/.vim/bundle 28 | git clone git://github.com/geekjuice/vim-spec.git 29 | ``` 30 | 31 | Using [vim-plug](https://github.com/junegunn/vim-plug) 32 | 33 | ```vim 34 | Plug 'geekjuice/vim-mocha' 35 | ``` 36 | 37 | 38 | ## Example of key mappings 39 | 40 | __NOTE__: The mappings have changed and use the same mappings as vim-rspec and 41 | vim-spec. If this is a big issue for you, open an issue and I'll re-add/add the 42 | namespace. 43 | 44 | ```vim 45 | map t :call RunCurrentSpecFile() 46 | map s :call RunNearestSpec() 47 | map l :call RunLastSpec() 48 | map a :call RunAllSpecs() 49 | ``` 50 | 51 | ## Configuration 52 | 53 | Like [thoughtbot/vim-rspec](https://github.com/thoughtbot/vim-rspec), the 54 | following variables can be overwritten for custom spec commands: 55 | 56 | * `g:mocha_js_command` 57 | * `g:mocha_coffee_command` 58 | 59 | Examples: 60 | 61 | ```vim 62 | let g:mocha_js_command = "!mocha --recursive --no-colors {spec}" 63 | let g:mocha_coffee_command = "!mocha -b --compilers coffee:coffee-script{spec}" 64 | 65 | " Using test runners 66 | let g:mocha_coffee_command = "!cortado {spec}" 67 | ``` 68 | 69 | 70 | Note: [cortado](bin/cortado) is a sugar wrapper for a more complex `mocha` call. 71 | 72 | 73 | ## Notes 74 | * Allow configuration for `mocha` options i.e. `--recursive`, `--reporter dot` 75 | 76 | * __BUG__: Last nearest test fails if below `it` call 77 | 78 | 79 | ## Credits 80 | 81 | [thoughtbot/vim-rspec](https://github.com/thoughtbot/vim-rspec) 82 | 83 | [Attamusc/vim-mocha](https://github.com/Attamusc/vim-mocha) 84 | 85 | ## License 86 | 87 | mocha.vim is released under the [MIT License](LICENSE). 88 | -------------------------------------------------------------------------------- /bin/cortado: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Mocha 4 | clear 5 | NODE_ENV=test mocha \ 6 | --bail \ 7 | --compilers coffee:coffee-script/register \ 8 | --recursive \ 9 | --reporter spec \ 10 | --slow 100 \ 11 | "$@" 12 | -------------------------------------------------------------------------------- /bin/major_filetype: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Find the majority filetype 3 | # OSX Version (use sed -rn for Linux) 4 | 5 | # Defaults: Ruby, Javascript, Coffeescript 6 | 7 | filetypes="coffee\|js\|ts" 8 | 9 | find . -type f | \ 10 | tr '[:upper:]' '[:lower:]' | \ 11 | sed -En "s|.*/[^/]+\.($filetypes)$|\1|p" | \ 12 | sort | \ 13 | uniq -c | \ 14 | sort -nr | \ 15 | head -n 1 | \ 16 | awk "{ print \$2}" 17 | -------------------------------------------------------------------------------- /bin/run_in_os_x_terminal: -------------------------------------------------------------------------------- 1 | #!/usr/bin/osascript 2 | 3 | on run argv 4 | tell application "Terminal" 5 | if (count of windows) is 0 then 6 | do script argv 7 | else 8 | do script argv in window 1 9 | end if 10 | 11 | activate 12 | end tell 13 | 14 | tell application "MacVim" 15 | activate 16 | end tell 17 | end run 18 | -------------------------------------------------------------------------------- /plugin/mocha.vim: -------------------------------------------------------------------------------- 1 | " Get file path 2 | let s:plugin_path = expand(":p:h:h") 3 | 4 | let s:mocha_path = "./node_modules/.bin/_mocha" 5 | if !empty(glob("s:mocha_path")) 6 | let s:mocha_path = "mocha" 7 | endif 8 | 9 | " Set Javascript 10 | function! s:SetJavascriptCommand() 11 | if !exists("g:mocha_js_command") 12 | let s:cmd = "mocha {spec}" 13 | call s:GUIRunning() 14 | else 15 | let g:spec_command = g:mocha_js_command 16 | endif 17 | endfunction 18 | 19 | " Set Typescript 20 | function! s:SetTypescriptCommand() 21 | if !exists("g:mocha_ts_command") 22 | let s:cmd = s:mocha_path . " --ui bdd --require ts-node/register {spec}" 23 | call s:GUIRunning() 24 | else 25 | let g:spec_command = g:mocha_ts_command 26 | endif 27 | endfunction 28 | 29 | " Set Coffeescript 30 | function! s:SetCoffeescriptCommand() 31 | if !exists("g:mocha_coffee_command") 32 | let s:cmd = "mocha --compilers 'coffee:coffee-script/register' {spec}" 33 | call s:GUIRunning() 34 | else 35 | let g:spec_command = g:mocha_coffee_command 36 | endif 37 | endfunction 38 | 39 | " Run GUI version or Terminal version 40 | function! s:GUIRunning() 41 | if has("gui_running") && has("gui_macvim") 42 | let g:spec_command = "silent !" . s:plugin_path . "/bin/run_in_os_x_terminal '" . s:cmd . "'" 43 | else 44 | let g:spec_command = "!echo " . s:cmd . " && " . s:cmd 45 | endif 46 | endfunction 47 | 48 | " Initial Spec Command 49 | function! s:SetInitialSpecCommand() 50 | let l:spec = s:plugin_path . "/bin/major_filetype" 51 | let l:filetype = system(l:spec) 52 | if l:filetype =~ 'js' 53 | call s:SetJavascriptCommand() 54 | elseif l:filetype =~ 'ts' 55 | call s:SetTypescriptCommand() 56 | elseif l:filetype =~ 'coffee' 57 | call s:SetCoffeescriptCommand() 58 | else 59 | let g:spec_command = "" 60 | endif 61 | endfunction 62 | 63 | " Determine which command based on filetype 64 | function! s:GetCorrectCommand() 65 | " Set default {mocha} command (javascript) 66 | if &filetype ==? 'javascript' 67 | call s:SetJavascriptCommand() 68 | " Set default {mocha} command (typescript) 69 | elseif &filetype ==? 'typescript' 70 | call s:SetTypescriptCommand() 71 | " Set default {mocha} command (coffeescript) 72 | elseif &filetype ==? 'coffee' 73 | call s:SetCoffeescriptCommand() 74 | " Fallthrough default 75 | else 76 | call s:SetInitialSpecCommand() 77 | endif 78 | endfunction 79 | 80 | " Mocha Nearest Test 81 | function! s:GetNearestTest() 82 | let callLine = line (".") "cursor line 83 | let file = readfile(expand("%:p")) "read current file 84 | let lineCount = 0 "file line counter 85 | let lineDiff = 999 "arbituary large number 86 | let descPattern = '\v<(it|describe|context)\s*\(?\s*[''"](.*)[''"]\s*,' 87 | for line in file 88 | let lineCount += 1 89 | let match = match(line,descPattern) 90 | if(match != -1) 91 | let currentDiff = callLine - lineCount 92 | " break if closest test is the next test 93 | if(currentDiff < 0 && lineDiff != 999) 94 | break 95 | endif 96 | " if closer test is found, cache new nearest test 97 | if(currentDiff <= lineDiff) 98 | let lineDiff = currentDiff 99 | let s:nearestTest = substitute(matchlist(line,descPattern)[2],'\v([''"()])','(.{1})','g') 100 | endif 101 | endif 102 | endfor 103 | endfunction 104 | 105 | " All Specs 106 | function! RunAllSpecs() 107 | if isdirectory('test') 108 | let l:spec = "test" 109 | elseif isdirectory('spec') 110 | let l:spec = "spec" 111 | else 112 | let l:spec = "" 113 | endif 114 | call SetLastSpecCommand(l:spec) 115 | call RunSpecs(l:spec) 116 | endfunction 117 | 118 | " Current File 119 | function! RunCurrentSpecFile() 120 | if InSpecFile() 121 | let l:spec = @% 122 | call SetLastSpecCommand(l:spec) 123 | call SetLastSpecFile(@%) 124 | call RunSpecs(l:spec) 125 | else 126 | call RunLastSpecFile() 127 | endif 128 | endfunction 129 | 130 | " Nearest Spec 131 | function! RunNearestSpec() 132 | if InSpecFile() 133 | call s:GetNearestTest() 134 | let l:spec = @% . " -g '" . s:nearestTest . "'" 135 | call SetLastSpecCommand(l:spec) 136 | call SetLastSpecFile(@%) 137 | call SetLastNearestSpec(l:spec) 138 | call RunSpecs(l:spec) 139 | else 140 | call RunLastNearestSpec() 141 | endif 142 | endfunction 143 | 144 | " Current Spec File Name 145 | function! InSpecFile() 146 | " Not a js or coffee file 147 | if match(expand('%'), '\v(.ts|.js|.coffee)$') == -1 148 | return 0 149 | endif 150 | " Check for describe block 151 | let l:contents = join(getline(1,'$'), "\n") 152 | let l:regex = '\v