Permalink
Browse files

Update Cakefile test runner to properly catch equal assertions, and o…

…utput passed/failed tests and assertions.
  • Loading branch information...
1 parent e3c71ae commit a62fc79ae0c4b0c79b88a9bd57a8fc6c382ca3bd @justindujardin justindujardin committed Oct 29, 2012
Showing with 40 additions and 10 deletions.
  1. +40 −10 Cakefile
View
@@ -39,7 +39,11 @@ task 'test', 'run the Docco test suite', ->
runTests = (Docco) ->
startTime = Date.now()
currentFile = null
+ currentTest = null
+ currentSource = null
passedTests = 0
+ passedEqual = 0
+ failedEqual = 0
failures = []
global[name] = func for name, func of require 'assert'
@@ -51,50 +55,75 @@ runTests = (Docco) ->
global.test = (description, fn) ->
try
fn.test = {description, currentFile}
+ currentTest = description
+ currentSource = fn.toString() if fn.toString?
fn.call(fn)
++passedTests
catch e
+ e.testName = currentTest
e.description = description if description?
e.source = fn.toString() if fn.toString?
failures.push filename: currentFile, error: e
# See http://wiki.ecmascript.org/doku.php?id=harmony:egal
egal = (a, b) ->
if a is b
- a isnt 0 or 1/a is 1/b
+ result = a isnt 0 or 1/a is 1/b
else
- a isnt a and b isnt b
+ result = a isnt a and b isnt b
+ if result then ++passedEqual else ++failedEqual
+ result
# A recursive functional equivalence helper; uses egal for testing equivalence.
arrayEgal = (a, b) ->
- if egal a, b then yes
+ if egal a, b
+ ++passedEqual
+ yes
else if a instanceof Array and b instanceof Array
- return no unless a.length is b.length
- return no for el, idx in a when not arrayEgal el, b[idx]
+ if a.length isnt b.length
+ ++failedEqual
+ return no
+ for el, idx in a when not arrayEgal el, b[idx]
+ ++failedEqual
+ return no
+ ++passedEqual
yes
- global.eq = (a, b, msg) -> ok egal(a, b), msg
+ global.eq = (a, b, msg) ->
+ try
+ return ok egal(a, b), msg
+ catch e
+ e.description = msg if msg?
+ e.source = currentSource
+ e.testName = currentTest
+ failures.push filename: currentFile, error: e
+ false
global.arrayEq = (a, b, msg) -> ok arrayEgal(a,b), msg
# When all the tests have run, collect and print errors.
# If a stacktrace is available, output the compiled function source.
process.on 'exit', ->
time = ((Date.now() - startTime) / 1000).toFixed(2)
- message = "passed #{passedTests} tests in #{time} seconds"
- return console.log(message) unless failures.length
- console.log "failed #{failures.length} and #{message}"
for fail in failures
{error, filename} = fail
jsFilename = filename.replace(/\.coffee$/,'.js')
match = error.stack?.match(new RegExp(fail.file+":(\\d+):(\\d+)"))
match = error.stack?.match(/on line (\d+):/) unless match
[match, line, col] = match if match
console.log ''
+ console.log " #{error.testName}" if error.testName
console.log " #{error.description}"
console.log " #{error.stack}"
console.log " #{jsFilename}: line #{line ? 'unknown'}, column #{col ? 'unknown'}"
console.log " #{error.source}" if error.source
- return
+ console.log [
+ ""
+ "Testing completed in #{time} seconds"
+ " #{passedTests} tests passed, #{failures.length} failed"
+ " #{passedEqual} asserts passed, #{failedEqual} failed"
+ ""
+ ].join '\n'
+ return if failures.length > 0 then 1 else 0
# Run every test in the `test` folder, recording failures.
files = fs.readdirSync 'test'
@@ -104,5 +133,6 @@ runTests = (Docco) ->
try
CoffeeScript.run code.toString(), {filename}
catch error
+ error.description = currentTest
failures.push {filename, error}
return !failures.length

0 comments on commit a62fc79

Please sign in to comment.