glass-test
Version:
Simple module unit test frameworks
140 lines (133 loc) • 4.85 kB
text/coffeescript
runTest = (name, test, callback) ->
expectedCallbacks = []
if typeof test is 'object'
for key, value of test
expectedCallbacks = expectedCallbacks.concat runTest name + ' ' + key, value, callback
else if typeof test is 'function'
if /^\s*function\s*\(\s*(done)?\s*\)/.test test.toString()
expectedCallbacks.push name
try
if test.length is 1
test (error) ->
callback name, error, null
else
result = test()
callback name, null, result
catch e
callback name, e, null
return expectedCallbacks
exports.runTests = runTests = (moduleIds, callback) ->
throw new Error "moduleIds is required" unless moduleIds
callback ?= exports.createCallback()
expectedCallbacks = {}
handler = (name, error, warning) ->
expectedCallbacks[name] = true
callback name, error, warning
for key, moduleId of moduleIds
try
module = require moduleId
name = if Array.isArray moduleIds then moduleId else key
array = runTest name, module.test, handler
for name in array
expectedCallbacks[name] ?= false
catch e
handler moduleId, e, null
getIncompleteCallbacks = ->
return (name for name, value of expectedCallbacks when not value)
inc = getIncompleteCallbacks();
if inc.length is 0
callback()
else
duration = 1000
setTimeout (=>
inc = getIncompleteCallbacks()
for name in inc
callback name, "Timed out after #{duration} ms"
callback()
), duration
exports.createCallback = (options) ->
options ?=
if global.java?
red: '<span style="color:red">'
green: '<span style="color:green">'
blue: '<span style="color:blue">'
plain: '<span>'
endColor: '</span>'
log: write
beep: ''
endLine: '<br>'
else
{}
red = options.red ? '\u001b[31m'
green = options.green ? '\u001b[32m'
blue = options.blue ? '\u001b[36m'
endColor = options.endColor ? '\u001b[0m'
plain = options.plain ? ''
beep = options.beep ? '\u0007'
log = options.log ? console.log
endLine = options.endLine ? ''
tests = 0
fails = 0
start = null
(name, error, result) ->
start ?= new Date().getTime()
if name?
tests++
fails++ if error?
color =
if error?
red
else if result?
blue
else
plain
log name + " " + color + (error ? result ? "") + endColor + endLine
else
finish = new Date().getTime()
time = finish - start
passed = tests - fails
log(endLine)
color = if passed is tests then green else red + beep
log color + "#{passed}/#{tests} Passed (#{time} ms)." + endColor + endLine
log(endLine)
# unit test ourselves!
exports.test = ->
assert =
equal: (a, b) ->
throw new Error "#{a} != #{b}" unless `a == b`
tests =
alpha: -> throw "Failure"
beta: ->
charlie: -> "Return value"
runTest 'fail', (-> throw 'Failure'), (name, error, result) ->
assert.equal name, 'fail'
assert.equal error, 'Failure'
assert.equal result, null
runTest 'pass', (->), (name, error, result) ->
assert.equal name, 'pass'
assert.equal error, null
assert.equal result, null
runTest 'warn', (-> 'warning'), (name, error, result) ->
assert.equal name, 'warn'
assert.equal error, null
assert.equal result, 'warning'
return
if require.main is module
np = require 'path'
args = process.argv.slice(2).map (x) -> x.replace /\\/g, '\/'
if args.length < 1
console.log "Usage: glass-test moduleid1 moduleid2.."
return
modules = {}
for arg in args
fullpath = np.join __dirname, arg
relative = np.relative process.cwd(), fullpath
modules[relative] = arg
runTests modules