Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tiny CoffeeScript testing
#!/usr/bin/env coffee
class TestCase
@run: (print, callback) ->
tests = (new this testName for testName in @getTestNames())
results = []
passed = true
print "1..#{tests.length}"
do next = ->
if test = tests.shift()
test.run (success, result) ->
if success
print "ok #{test.name}"
else
print "not ok #{test.name}"
passed = false
results[test.name] = result
next()
else
callback passed, results
@getTestNames: ->
for name of @prototype when name.match /^test /
name.slice 5
@test: (name, callback) ->
@::["test #{name}"] = callback
constructor: (@name) ->
@callbacks = []
@passed = []
@failed = []
run: (callback) ->
@perform "test #{@name}"
@perform "finish", callback
@proceed()
perform: (method, args...) ->
@callbacks.push =>
@[method].apply this, args
@proceed()
waitFor: (callback) ->
@callbacks.unshift =>
callback.call this, @proceed
proceed: =>
setTimeout =>
callback = @callbacks.shift()
callback?.call this
, 10
assert: (expression, message = "should be true") ->
if expression
@pass message
else
@fail message
pass: (message) ->
@passed.push message
fail: (message) ->
@failed.push message
finish: (callback) ->
callback @failed.length is 0, {@passed, @failed}
class TestOne extends TestCase
@test "a successful assertion", ->
@assert true
@test "a failed assertion", ->
@assert false
@test "a successful delayed assertion", ->
@waitFor (done) ->
setTimeout =>
@assert true
done()
, 100
@test "a failed delayed assertion", ->
@waitFor (done) ->
setTimeout =>
@assert false
done()
, 100
@test "one more successful assertion", ->
@assert true
TestOne.run console.log, (success, results) ->
process.exit if success then 0 else 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment