1 |
3a515b92
|
cagy
|
{ iferr, tiferr, throwerr } = require '../index.coffee'
|
2 |
|
|
{ equal: eq, throws } = require 'assert'
|
3 |
|
|
|
4 |
|
|
invoke_fail = (cb) -> cb new Error 'callback error'
|
5 |
|
|
invoke_succ = (cb) -> cb null
|
6 |
|
|
throw_error = -> throw new Error 'thrown'
|
7 |
|
|
|
8 |
|
|
describe 'iferr', ->
|
9 |
|
|
it 'calls the error callback on errors', (done) ->
|
10 |
|
|
invoke_fail iferr(
|
11 |
|
|
(err) ->
|
12 |
|
|
eq err.message, 'callback error'
|
13 |
|
|
do done
|
14 |
|
|
->
|
15 |
|
|
done new Error 'shouldn\'t call the success callback'
|
16 |
|
|
)
|
17 |
|
|
|
18 |
|
|
it 'calls the success callback on success', (done) ->
|
19 |
|
|
invoke_succ iferr(
|
20 |
|
|
-> done new Error 'shouldn\'t call the error callback'
|
21 |
|
|
done
|
22 |
|
|
)
|
23 |
|
|
|
24 |
|
|
describe 'tiferr', ->
|
25 |
|
|
it 'catches errors in the success callback', (done) ->
|
26 |
|
|
invoke_succ tiferr(
|
27 |
|
|
(err) ->
|
28 |
|
|
eq err.message, 'thrown'
|
29 |
|
|
do done
|
30 |
|
|
throw_error
|
31 |
|
|
)
|
32 |
|
|
|
33 |
|
|
describe 'throwerr', ->
|
34 |
|
|
it 'throws errors passed to the callback', (done)->
|
35 |
|
|
try invoke_fail throwerr ->
|
36 |
|
|
done 'shouldn\'t call the success callback'
|
37 |
|
|
catch err
|
38 |
|
|
eq err.message, 'callback error'
|
39 |
|
|
do done
|
40 |
|
|
|
41 |
|
|
it 'delegates to the success callback otherwise', (done) ->
|
42 |
|
|
invoke_succ throwerr done
|