Skip to content

Instantly share code, notes, and snippets.

@junegunn
Created May 31, 2016 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junegunn/03e51de7b0de2efb33e19566da2864dd to your computer and use it in GitHub Desktop.
Save junegunn/03e51de7b0de2efb33e19566da2864dd to your computer and use it in GitHub Desktop.
diff --git a/README.md b/README.md
index 6abc213..c02896a 100644
--- a/README.md
+++ b/README.md
@@ -69,6 +69,12 @@ Given [filetype] [(comment)]:
[input text]
```
+Alternatively, you can pass a file path to `Given` label as follows:
+
+```
+Given [filetype] [(comment)]: [filepath]
+```
+
#### Do
The content of a Do block is a sequence of normal-mode keystrokes that can
diff --git a/autoload/vader/parser.vim b/autoload/vader/parser.vim
index dbf5da0..9f5587c 100644
--- a/autoload/vader/parser.vim
+++ b/autoload/vader/parser.vim
@@ -22,7 +22,7 @@
" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
function! vader#parser#parse(fn, line1, line2)
- return s:parse_vader(s:read_vader(a:fn, a:line1, a:line2), a:line1)
+ return s:parse_vader(s:read_vader(a:fn, a:line1, a:line2), a:fn, a:line1)
endfunction
function! s:flush_buffer(cases, case, fn, lnum, raw, label, newlabel, buffer, final)
@@ -58,6 +58,9 @@ function! s:flush_buffer(cases, case, fn, lnum, raw, label, newlabel, buffer, fi
if a:final ||
\ a:newlabel == 'given' ||
\ index(['before', 'after', 'do', 'execute'], a:newlabel) >= 0 && fulfilled
+ if !empty(get(a:case, 'file', ''))
+ let a:case.given = remove(a:case, 'file')
+ endif
call add(a:cases, deepcopy(a:case))
let new = { 'comment': {}, 'lnum': a:lnum, 'pending': 0 }
if !empty(get(a:case, 'type', ''))
@@ -111,7 +114,17 @@ function! s:read_vader(fn, line1, line2)
return lines
endfunction
-function! s:parse_vader(lines, line1)
+function! s:matchany(str, patterns)
+ for pattern in a:patterns
+ let m = matchlist(a:str, pattern)
+ if !empty(m)
+ return m
+ endif
+ endfor
+ return []
+endfunction
+
+function! s:parse_vader(lines, fn, line1)
let label = ''
let newlabel = ''
let buffer = []
@@ -125,35 +138,31 @@ function! s:parse_vader(lines, line1)
continue
endif
- let matched = 0
- for l in ['Before', 'After', 'Given', 'Execute', 'Expect', 'Do', 'Then']
- let m = matchlist(line, '^'.l.'\%(\s\+\([^:;(]\+\)\)\?\s*\%((\(.*\))\)\?\s*\([:;]\)\s*$')
- if !empty(m)
- let newlabel = tolower(l)
- call s:flush_buffer(cases, case, fn, lnum, m[3] == ';', label, newlabel, buffer, 0)
-
- let label = newlabel
- let arg = m[1]
- let comment = m[2]
- if !empty(arg)
- if l == 'Given' | let case.type = arg
- elseif l == 'Execute' | let case.lang_if = arg
- end
- elseif l == 'Given'
- let case.type = ''
- endif
- if !empty(comment)
- let case.comment[tolower(l)] = comment
- if index(['do', 'execute'], label) >= 0 &&
- \ comment =~# '\<TODO\>\|\<FIXME\>'
- let case.pending = 1
- endif
+ let m = s:matchany(line,
+ \ ['^\(Before\|After\|Given\|Execute\|Expect\|Do\|Then\)\%(\s\+\([^:;( ]\+\)\)\?\s*\%((\(.*\))\)\?\s*\([:;]\)\s*$',
+ \ '^\(Given\)\%(\s\+\([^:;(]\+\)\)\?\s*\%((\(.*\))\)\?\s*\([:;]\)\s*\(\S\+\)$'])
+ if !empty(m)
+ let newlabel = tolower(m[1])
+ call s:flush_buffer(cases, case, fn, lnum, m[4] == ';', label, newlabel, buffer, 0)
+
+ let label = newlabel
+ let arg = m[2]
+ let comment = m[3]
+ if label == 'given'
+ let case.type = arg
+ let case.file = findfile(m[5], fnamemodify(a:fn, ':h'))
+ elseif label == 'execute' && !empty(arg)
+ let case.lang_if = arg
+ endif
+ if !empty(comment)
+ let case.comment[label] = comment
+ if index(['do', 'execute'], label) >= 0 &&
+ \ comment =~# '\<TODO\>\|\<FIXME\>'
+ let case.pending = 1
endif
- let matched = 1
- break
endif
- endfor
- if matched | continue | endif
+ continue
+ endif
" Continuation
if !empty(line) && !case.raw && line !~ '^ '
diff --git a/autoload/vader/window.vim b/autoload/vader/window.vim
index 2820acb..855a5fb 100644
--- a/autoload/vader/window.vim
+++ b/autoload/vader/window.vim
@@ -101,18 +101,28 @@ endfunction
function! vader#window#prepare(lines, type)
call s:switch_to_workbench()
- execute 'setlocal modifiable filetype='.a:type
+ if type(a:lines) == type('')
+ " TBD: Do we have to 'bd' buffers on cleanup?
+ " TBD: What if the file doesn't exist?
+ execute 'e' a:lines
+ if len(a:type)
+ execute 'setlocal filetype='.a:type
+ endif
+ else
+ execute 'setlocal modifiable filetype='.a:type
- %d _
- for line in a:lines
- call append(line('$') - 1, line)
- endfor
- normal! "_ddgg
+ %d _
+ for line in a:lines
+ call append(line('$') - 1, line)
+ endfor
+ normal! "_ddgg
+ endif
let &undolevels = &undolevels " Break undo block
endfunction
function! vader#window#cleanup()
+ execute s:workbench_tab.'tabclose'
execute 'silent! bd' s:workbench_bfr
call s:switch_to_console()
setlocal nomodifiable
diff --git a/syntax/vader.vim b/syntax/vader.vim
index fc4165d..126e9aa 100644
--- a/syntax/vader.vim
+++ b/syntax/vader.vim
@@ -49,6 +49,7 @@ syn match vaderSepSingle /^-.*/ contains=Todo
syn match vaderSepAsterisk /^\*.*/ contains=Todo
syn cluster vaderIgnored contains=vaderComment,vaderSepCaret,vaderSepTilde,vaderSepDouble,vaderSepSingle,vaderSepAsterisk
+syn region vaderGivenFile start=/^Given\(\s*\S\+\s*\)\?\(\s*(.*)\)\?\s*\ze:\s*\S\+$/ end=/:/ oneline contains=vaderMessage,vaderGivenType
syn region vaderGiven start=/^Given\(\s*(.*)\)\?\s*:\s*$/ end=/\(^[^ "^#~=*-]\)\@=/ contains=vaderMessage,vaderText,vaderComment,@vaderIgnored nextgroup=@vaderTopLevel skipempty
syn region vaderExpect start=/^Expect\(\s*(.*)\)\?\s*:\s*$/ end=/\(^[^ "^#~=*-]\)\@=/ contains=vaderMessage,vaderText,vaderComment,@vaderIgnored nextgroup=@vaderTopLevel skipempty
syn region vaderDo start=/^Do\(\s*(.*)\)\?\s*:\s*$/ end=/\(^[^ "^#~=*-]\)\@=/ contains=vaderMessage,vaderCommand,vaderComment,@vaderIgnored nextgroup=@vaderTopLevel skipempty
@@ -72,6 +73,7 @@ syn keyword Todo TODO FIXME XXX TBD
hi def link vaderInclude Repeat
hi def link vaderGiven Include
+hi def link vaderGivenFile Include
hi def link vaderGivenRaw Include
hi def link vaderBefore Special
hi def link vaderBeforeRaw Special
@alerque
Copy link

alerque commented Mar 6, 2021

Ran across this patch searching for any way to use existing fixture files. I was trying to write tests to check some path handling and it's not really possible to catch the error case given how Vader fixes everything to do with file loading before the thing I'm testing would even have a change to error.

The patch is out of date now, I'm curious why it never got merged or if there is an alternative that did and is eluding me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment