Skip to content

Instantly share code, notes, and snippets.

@timbertson
Created March 26, 2010 13:43
Show Gist options
  • Save timbertson/344886 to your computer and use it in GitHub Desktop.
Save timbertson/344886 to your computer and use it in GitHub Desktop.
# basically, you just have nested "describe" blocks, with one "it" block per test.
describe 'deferred', ->
it 'should deal with a single defer', ->
single_def: (x) -> defer return_arg(x)
equal single_def.length, 2
callback_called: false
single_def "a", (result) ->
callback_called: true
ok(result is "a")
check_later -> ok callback_called is true
it 'should allow for multiple defers in a single expression', ->
multi_def: (x) -> 2 + defer return_arg(x) * defer return_arg(3)
equal multi_def.length, 2
callback_called: 0
multi_def 2, (result) ->
ok(result is 8)
callback_called += 1
check_later -> equal(callback_called, 1)
it 'should *always* return', ->
callback_called: false
implicit_return_from_skipped_branch: ->
x: defer return_arg(2)
while x > 3
# force a pure_expression, so we get no funny business...
break
implicit_return_from_skipped_branch(-> callback_called: true)
check_later -> ok callback_called
it 'should maintain the original `this` in a continuation', ->
end_this: null
messing_with_this: ->
initial_this: this
x: defer return_arg(1)
ok(this is initial_this)
messing_with_this.call("this", ->)
it 'should not interfere with splats', ->
splat_def: (a, rest...) -> defer return_arg(rest)
callback_called: false
splat_def 1, 2, 3, 4, (rest) ->
# callback_called: true
expected_rest: [2,3,4]
equal(rest, expected_rest)
check_later -> ok callback_called, true
splat_in_defer: () ->
rest: defer splat_def(1, 2, 3, 4)
ok(rest is [2, 3, 4])
splat_in_defer(->)
ok(1 is 1)
it 'should continue code after a defer', ->
deferred_body: (x) ->
x: x + 1
y: defer return_arg(x + 1)
z: y + 1
return z
ok deferred_body.length is 2
deferred_body(1, (result) -> ok result is 4)
it 'should allow a deferred lambda', ->
deferred_lambda: ->
x: defer ((val) -> defer return_arg(val))(1)
ok deferred_lambda.length is 1
deferred_lambda((_) -> ok _ is 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment