Permalink
Browse files

Update test runner to use normal nodejs assert module, by wrapping th…

…e assert functions with a safe try/catch block
  • Loading branch information...
1 parent a62fc79 commit 753c9e6017330ee1edae031e63657bc3a18fb0f1 @justindujardin justindujardin committed Oct 29, 2012
Showing with 41 additions and 64 deletions.
  1. +37 −60 Cakefile
  2. +4 −4 test/tests.coffee
View
@@ -33,22 +33,36 @@ task 'doc', 'rebuild the Docco documentation', ->
)
task 'test', 'run the Docco test suite', ->
- runTests Docco
+ runTests()
# Simple test runner, borrowed from [CoffeeScript](http://coffeescript.org/).
-runTests = (Docco) ->
- startTime = Date.now()
- currentFile = null
- currentTest = null
+runTests = () ->
+ startTime = Date.now()
+ currentFile = null
+ currentTest = null
currentSource = null
- passedTests = 0
- passedEqual = 0
- failedEqual = 0
- failures = []
+ passedTests = 0
+ passedAssert = 0
+ failedAssert = 0
+ failures = []
- global[name] = func for name, func of require 'assert'
+ # The assert module API for NodeJS is stable and locked. There
+ wrapAssert = (func,name) ->
+ return () ->
+ try
+ result = func.apply this, arguments
+ ++passedAssert
+ catch e
+ ++failedAssert
+ e.description = arguments[2] if arguments.length == 3
+ e.source = currentSource
+ e.testName = currentTest
+ failures.push
+ filename: currentFile
+ error: e
+ result
- # Convenience alias.
+ global[name] = wrapAssert(func,name) for name, func of require 'assert'
global.Docco = Docco
# Our test helper function for delimiting different test cases.
@@ -64,41 +78,6 @@ runTests = (Docco) ->
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
- result = a isnt 0 or 1/a is 1/b
- else
- 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
- ++passedEqual
- yes
- else if a instanceof Array and b instanceof Array
- 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) ->
- 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.
@@ -107,22 +86,20 @@ runTests = (Docco) ->
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(new RegExp(fail.filename+":(\\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
- console.log [
- ""
- "Testing completed in #{time} seconds"
- " #{passedTests} tests passed, #{failures.length} failed"
- " #{passedEqual} asserts passed, #{failedEqual} failed"
- ""
- ].join '\n'
+ console.log "\n--------------------------------------------------------"
+ console.log " FAILED: #{error.testName}\n" if error.testName
+ console.log " FILE :#{jsFilename}: line #{line ? 'unknown'}, column #{col ? 'unknown'}"
+ console.log " ERROR : #{error.description}" if error.description
+ console.log " STACK : #{error.stack}" if error.stack
+ console.log " SOURCE: #{error.source}" if error.source
+ console.log "--------------------------------------------------------"
+ console.log "Testing completed in #{time} seconds"
+ console.log " #{passedTests} tests passed, #{failures.length} failed"
+ console.log " #{passedAssert} asserts passed, #{failedAssert} failed"
+ console.log "--------------------------------------------------------"
return if failures.length > 0 then 1 else 0
# Run every test in the `test` folder, recording failures.
View
@@ -21,7 +21,7 @@ testDoccoRun = (testName,sources,options=null,callback=null) ->
files = files.concat(Docco.resolveSource(src)) for src in sources
expected = files.length + 1
found = fs.readdirSync(destPath).length
- eq found, expected, "find expected output (#{expected} files) - (#{found})"
+ equal found, expected, "find expected output (#{expected} files) - (#{found})"
callback() if callback?
# **Custom jst template files should be supported**
@@ -63,16 +63,16 @@ test "single line comment parsing", ->
return testNextLanguage(keys, callback) if not path.existsSync languageExample
testDoccoRun languageTest, [languageExample], options, ->
- eq true, path.existsSync(languageOutput), "#{languageOutput} -> output file created properly"
+ equal true, path.existsSync(languageOutput), "#{languageOutput} -> output file created properly"
content = fs.readFileSync(languageOutput).toString()
comments = (c.trim() for c in content.split(',') when c.trim() != '')
- eq true, comments.length >= 1, 'expect at least the descriptor comment'
+ equal true, comments.length >= 1, 'expect at least the descriptor comment'
expected = parseInt(comments[0])
- eq comments.length, expected, [
+ equal comments.length, expected, [
""
"#{path.basename(languageOutput)} comments"
"------------------------"

0 comments on commit 753c9e6

Please sign in to comment.