Permalink
Browse files

Support :before blocks

  • Loading branch information...
1 parent ae3e289 commit e60bdaa508c64f96a83f7b3567cc5e77c0770695 @kana committed Jan 30, 2012
Showing with 221 additions and 122 deletions.
  1. +1 −1 after/indent/vim.vim
  2. +1 −0 after/syntax/vim/vspec.vim
  3. +21 −0 autoload/vspec.vim
  4. +14 −0 doc/vspec.txt
  5. +19 −0 t/before.vim
  6. +1 −1 t/error-in-it.t
  7. +1 −1 t/error-in-should-evaluating.t
  8. +1 −1 t/error-in-should-parsing.t
  9. +28 −0 t/indent.vim
  10. +134 −118 t/syntax.vim
View
@@ -37,7 +37,7 @@ function GetVimVspecIndent(base_indent)
let base_lnum = prevnonblank(v:lnum - 1)
let line = getline(base_lnum)
- if 0 <= match(line, '\(^\||\)\s*\(describe\|it\)\>')
+ if 0 <= match(line, '\(^\||\)\s*\(before\|describe\|it\)\>')
let indent += &l:shiftwidth
endif
@@ -22,6 +22,7 @@
" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}
+syntax keyword vimVspecCommand before
syntax keyword vimVspecCommand describe skipwhite nextgroup=vimString
syntax keyword vimVspecCommand end
syntax keyword vimVspecCommand Expect skipwhite
View
@@ -212,6 +212,7 @@ function! vspec#test(specfile_path) "{{{2
call s:push_current_suite(suite)
for example in suite.example_list
let example_count += 1
+ call suite.before_block()
try
call suite.example_dict[suite.generate_example_function_name(example)]()
echo printf(
@@ -329,6 +330,13 @@ endfunction
+function! s:suite.before_block() "{{{2
+ " No-op to avoid null checks.
+endfunction
+
+
+
+
function! s:suite.generate_example_function_name(example_description) "{{{2
return substitute(
\ a:example_description,
@@ -421,6 +429,15 @@ function! s:translate_script(slines) "{{{2
continue
endif
+ let tokens = matchlist(sline, '^\s*before\s*$')
+ if !empty(tokens)
+ call insert(stack, 'before', 0)
+ call extend(rlines, [
+ \ 'function! suite.before_block()',
+ \ ])
+ continue
+ endif
+
let tokens = matchlist(sline, '^\s*end\s*$')
if !empty(tokens)
let type = remove(stack, 0)
@@ -430,6 +447,10 @@ function! s:translate_script(slines) "{{{2
call extend(rlines, [
\ 'endfunction',
\ ])
+ elseif type ==# 'before'
+ call extend(rlines, [
+ \ 'endfunction',
+ \ ])
else
" Nothing to do.
endif
View
@@ -170,6 +170,20 @@ COMMANDS *vspec-commands*
NOT match with {expected} value, in a sense of
{matcher} or {custom-matcher}.
+:before *:before*
+:end
+ Define a code block which is run before each example
+ defined by |:it|.
+
+ Note that:
+ - Only one |:before| block can be written in each
+ |:describe| block.
+ - |:before| blocks cannot be defined for |:describe|
+ blocks.
+ - |:before| blocks cannot be defined for a specific
+ |:it| block.
+ These limitations might be resolved later.
+
:ResetContext *:ResetContext*
Reset the current context of a script with one saved
by |:SaveContext|.
View
@@ -0,0 +1,19 @@
+describe 'before'
+ let g:counter = 0
+
+ before
+ let g:counter += 1
+ end
+
+ it 'should call the before block for each example (the first time)'
+ Expect g:counter == 1
+ end
+
+ it 'should call the before block for each example (the second time)'
+ Expect g:counter == 2
+ end
+
+ it 'should call the before block for each example (the third time)'
+ Expect g:counter == 3
+ end
+end
View
@@ -29,7 +29,7 @@ END
) <(cat <<'END'
ok 1 - Suite 1 should be executed
not ok 2 - Suite 2 should be executed and fail
-# function <SNR>1_main..vspec#test..4..A..B..C, line 1
+# function <SNR>1_main..vspec#test..5..A..B..C, line 1
# Vim:E492: Not an editor command: ThisLineIsNotAValidVimScriptStatement
ok 3 - Suite 3 should be executed
1..3
@@ -20,7 +20,7 @@ END
) <(cat <<'END'
ok 1 - Suite 1 should be executed
not ok 2 - Suite 2 should be executed and fail
-# function <SNR>1_main..vspec#test..4, line 1
+# function <SNR>1_main..vspec#test..5, line 1
# Vim(call):E121: Undefined variable: foo
ok 3 - Suite 3 should be executed
1..3
@@ -20,7 +20,7 @@ END
) <(cat <<'END'
ok 1 - Suite 1 should be executed
not ok 2 - Suite 2 should be executed and fail
-# function <SNR>1_main..vspec#test..4..<SNR>2_parse_should_arguments, line 2
+# function <SNR>1_main..vspec#test..5..<SNR>2_parse_should_arguments, line 2
# Vim(let):E688: More targets than List items
ok 3 - Suite 3 should be executed
1..3
View
@@ -58,4 +58,32 @@ describe 'Automatic indentation'
call s:after()
end
+
+ it 'should indent lines after :before'
+ call s:before()
+
+ execute 'normal!' 'i' . join([
+ \ 'describe ''foo''',
+ \ 'before',
+ \ 'qux',
+ \ 'end',
+ \ 'it ''bar''',
+ \ 'baz',
+ \ 'end',
+ \ 'end',
+ \ ], "\<Return>")
+
+ Expect getline(1, '$') ==# [
+ \ 'describe ''foo''',
+ \ ' before',
+ \ ' qux',
+ \ ' end',
+ \ ' it ''bar''',
+ \ ' baz',
+ \ ' end',
+ \ 'end',
+ \ ]
+
+ call s:after()
+ end
end
Oops, something went wrong.

0 comments on commit e60bdaa

Please sign in to comment.