Skip to content

Commit

Permalink
Check types with AssertEqual/AssertNotEqual (#111)
Browse files Browse the repository at this point in the history
This is meant to make the error when comparing a list with something
else more meaningful, where you currently get the following only:

> Vim(if):E691: Can only compare List with List

Fixes #115.
Closes #116.
  • Loading branch information
blueyed authored and junegunn committed Jan 29, 2017
1 parent 48b36ca commit 427fe19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions autoload/vader/assert.vim
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@

let s:assertions = [0, 0]

let s:type_names = {
\ 0: 'Number',
\ 1: 'String',
\ 2: 'Funcref',
\ 3: 'List',
\ 4: 'Dictionary',
\ 5: 'Float',
\ 6: 'Boolean',
\ 7: 'Null' }

function! vader#assert#reset()
let s:assertions = [0, 0]
endfunction
Expand All @@ -50,10 +60,20 @@ function! vader#assert#true(...)
return 1
endfunction

function! s:check_types(...)
let [exp, got] = a:000[0:1]
if type(exp) !=# type(got)
throw get(a:000, 2, printf("type mismatch: %s (%s) should be equal to %s (%s)",
\ string(got), get(s:type_names, type(got), type(got)),
\ string(exp), get(s:type_names, type(exp), type(exp))))
endif
endfunction

function! vader#assert#equal(...)
let [exp, got] = a:000[0:1]
let s:assertions[1] += 1

call s:check_types(exp, got)
if exp !=# got
throw get(a:000, 2, printf("%s should be equal to %s", string(got), string(exp)))
endif
Expand All @@ -65,6 +85,7 @@ function! vader#assert#not_equal(...)
let [exp, got] = a:000[0:1]
let s:assertions[1] += 1

call s:check_types(exp, got)
if exp ==# got
throw get(a:000, 2, printf("%s should not be equal to %s", string(got), string(exp)))
endif
Expand Down
2 changes: 1 addition & 1 deletion test/feature/save-restore.vader
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Execute (Restore everything):
let number = &number
Restore
AssertEqual 3, g:xyz
AssertEqual 4, $ENVVAR
AssertEqual '4', $ENVVAR
AssertEqual !number, &number

Execute (g:undefined should not exist):
Expand Down

0 comments on commit 427fe19

Please sign in to comment.