UNPKG

glass-test

Version:

Simple module unit test frameworks

208 lines (200 loc) 6.29 kB
// Generated by CoffeeScript 1.6.3 (function() { var arg, args, fullpath, modules, np, relative, runTest, runTests, _i, _len; runTest = function(name, test, callback) { var e, expectedCallbacks, key, result, value; expectedCallbacks = []; if (typeof test === 'object') { for (key in test) { value = test[key]; expectedCallbacks = expectedCallbacks.concat(runTest(name + ' ' + key, value, callback)); } } else if (typeof test === 'function') { if (/^\s*function\s*\(\s*(done)?\s*\)/.test(test.toString())) { expectedCallbacks.push(name); try { if (test.length === 1) { test(function(error) { return callback(name, error, null); }); } else { result = test(); callback(name, null, result); } } catch (_error) { e = _error; callback(name, e, null); } } } return expectedCallbacks; }; exports.runTests = runTests = function(moduleIds, callback) { var array, duration, e, expectedCallbacks, getIncompleteCallbacks, handler, inc, key, module, moduleId, name, _i, _len, _this = this; if (!moduleIds) { throw new Error("moduleIds is required"); } if (callback == null) { callback = exports.createCallback(); } expectedCallbacks = {}; handler = function(name, error, warning) { expectedCallbacks[name] = true; return callback(name, error, warning); }; for (key in moduleIds) { moduleId = moduleIds[key]; try { module = require(moduleId); name = Array.isArray(moduleIds) ? moduleId : key; array = runTest(name, module.test, handler); for (_i = 0, _len = array.length; _i < _len; _i++) { name = array[_i]; if (expectedCallbacks[name] == null) { expectedCallbacks[name] = false; } } } catch (_error) { e = _error; handler(moduleId, e, null); } } getIncompleteCallbacks = function() { var value; return (function() { var _results; _results = []; for (name in expectedCallbacks) { value = expectedCallbacks[name]; if (!value) { _results.push(name); } } return _results; })(); }; inc = getIncompleteCallbacks(); if (inc.length === 0) { return callback(); } else { duration = 1000; return setTimeout((function() { var _j, _len1; inc = getIncompleteCallbacks(); for (_j = 0, _len1 = inc.length; _j < _len1; _j++) { name = inc[_j]; callback(name, "Timed out after " + duration + " ms"); } return callback(); }), duration); } }; exports.createCallback = function(options) { var beep, blue, endColor, endLine, fails, green, log, plain, red, start, tests, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7; if (options == null) { options = global.java != null ? { red: '<span style="color:red">', green: '<span style="color:green">', blue: '<span style="color:blue">', plain: '<span>', endColor: '</span>', log: write, beep: '', endLine: '<br>' } : {}; } red = (_ref = options.red) != null ? _ref : '\u001b[31m'; green = (_ref1 = options.green) != null ? _ref1 : '\u001b[32m'; blue = (_ref2 = options.blue) != null ? _ref2 : '\u001b[36m'; endColor = (_ref3 = options.endColor) != null ? _ref3 : '\u001b[0m'; plain = (_ref4 = options.plain) != null ? _ref4 : ''; beep = (_ref5 = options.beep) != null ? _ref5 : '\u0007'; log = (_ref6 = options.log) != null ? _ref6 : console.log; endLine = (_ref7 = options.endLine) != null ? _ref7 : ''; tests = 0; fails = 0; start = null; return function(name, error, result) { var color, finish, passed, time, _ref8; if (start == null) { start = new Date().getTime(); } if (name != null) { tests++; if (error != null) { fails++; } color = error != null ? red : result != null ? blue : plain; return log(name + " " + color + ((_ref8 = error != null ? error : result) != null ? _ref8 : "") + endColor + endLine); } else { finish = new Date().getTime(); time = finish - start; passed = tests - fails; log(endLine); color = passed === tests ? green : red + beep; log(color + ("" + passed + "/" + tests + " Passed (" + time + " ms).") + endColor + endLine); return log(endLine); } }; }; exports.test = function() { var assert, tests; assert = { equal: function(a, b) { if (!a == b) { throw new Error("" + a + " != " + b); } } }; tests = { alpha: function() { throw "Failure"; }, beta: function() {}, charlie: function() { return "Return value"; } }; runTest('fail', (function() { throw 'Failure'; }), function(name, error, result) { assert.equal(name, 'fail'); assert.equal(error, 'Failure'); return assert.equal(result, null); }); runTest('pass', (function() {}), function(name, error, result) { assert.equal(name, 'pass'); assert.equal(error, null); return assert.equal(result, null); }); runTest('warn', (function() { return 'warning'; }), function(name, error, result) { assert.equal(name, 'warn'); assert.equal(error, null); return assert.equal(result, 'warning'); }); }; if (require.main === module) { np = require('path'); args = process.argv.slice(2).map(function(x) { return x.replace(/\\/g, '\/'); }); if (args.length < 1) { console.log("Usage: glass-test moduleid1 moduleid2.."); return; } modules = {}; for (_i = 0, _len = args.length; _i < _len; _i++) { arg = args[_i]; fullpath = np.join(__dirname, arg); relative = np.relative(process.cwd(), fullpath); modules[relative] = arg; } runTests(modules); } }).call(this); /* //@ sourceMappingURL=index.map */