Skip to content

Commit

Permalink
Support new style custom matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
kana committed Oct 29, 2012
1 parent 431f02d commit 61ee87d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
25 changes: 19 additions & 6 deletions autoload/vspec.vim
Expand Up @@ -49,7 +49,7 @@ let s:current_suites = [] "{{{2




let s:custom_matchers = {} "{{{2 let s:custom_matchers = {} "{{{2
" :: MatcherNameString -> Funcref " :: MatcherNameString -> Matcher






Expand Down Expand Up @@ -149,8 +149,13 @@ endfunction






function! vspec#customize_matcher(matcher_name, funcref) "{{{2 function! vspec#customize_matcher(matcher_name, maybe_matcher) "{{{2
let s:custom_matchers[a:matcher_name] = a:funcref if type(a:maybe_matcher) == type({})
let matcher = a:maybe_matcher
else
let matcher = {'match': a:maybe_matcher}
endif
let s:custom_matchers[a:matcher_name] = matcher
endfunction endfunction




Expand Down Expand Up @@ -591,14 +596,22 @@ let s:VALID_MATCHERS = (s:VALID_MATCHERS_EQUALITY
function! s:are_matched(value_actual, expr_matcher, value_expected) "{{{2 function! s:are_matched(value_actual, expr_matcher, value_expected) "{{{2
if s:is_custom_matcher(a:expr_matcher) if s:is_custom_matcher(a:expr_matcher)
let custom_matcher_name = a:expr_matcher let custom_matcher_name = a:expr_matcher
if !has_key(s:custom_matchers, custom_matcher_name) let matcher = get(s:custom_matchers, custom_matcher_name, 0)
if matcher is 0
throw throw
\ 'vspec:InvalidOperation:Unknown custom matcher - ' \ 'vspec:InvalidOperation:Unknown custom matcher - '
\ . string(custom_matcher_name) \ . string(custom_matcher_name)
endif endif
let Match = get(matcher, 'match', 0)
if Match is 0
throw
\ 'vspec:InvalidOperation:Custom matcher does not have match function - '
\ . string(custom_matcher_name)
endif
return !!call( return !!call(
\ s:custom_matchers[custom_matcher_name], \ Match,
\ [a:value_actual] + a:value_expected \ [a:value_actual] + a:value_expected,
\ matcher
\ ) \ )
elseif s:is_equality_matcher(a:expr_matcher) elseif s:is_equality_matcher(a:expr_matcher)
let type_equality = type(a:value_actual) == type(a:value_expected) let type_equality = type(a:value_actual) == type(a:value_expected)
Expand Down
32 changes: 32 additions & 0 deletions t/custom-matchers.vim
Expand Up @@ -23,4 +23,36 @@ describe 'vspec#customize_matcher'


Expect [] to_be_empty Expect [] to_be_empty
end end

it 'supports new style usage'
let caught = !!0
try
Expect [] to_be_empty
let caught = !!0
catch /^vspec:InvalidOperation:Unknown custom matcher - 'to_be_empty'$/
let caught = !0
endtry
Expect caught to_be_true

let to_be_empty = {}
function! to_be_empty.match(actual)
return empty(a:actual)
endfunction
call vspec#customize_matcher('to_be_empty', to_be_empty)

Expect [] to_be_empty
end

it 'rejects matchers without required members'
call vspec#customize_matcher('to_be_empty', {})

let caught = !!0
try
Expect [] to_be_empty
let caught = !!0
catch /^vspec:InvalidOperation:Custom matcher does not have match function - 'to_be_empty'$/
let caught = !0
endtry
Expect caught to_be_true
end
end end

0 comments on commit 61ee87d

Please sign in to comment.