Name: js-handler/node_modules/nodeunit/node_modules/tap/lib/tap-test.js
| 1: | // This is a very simple test framework that leverages the tap framework |
| 2: | // to run tests and output tap-parseable results. |
| 3: | |
| 4: | module.exports = Test |
| 5: | |
| 6: | var assert = require("./tap-assert") |
| 7: | , inherits = require("inherits") |
| 8: | , Results = require("./tap-results") |
| 9: | , Harness = require("./tap-harness") |
| 10: | |
| 11: | // tests are also test harnesses |
| 12: | inherits(Test, Harness) |
| 13: | |
| 14: | function Test (harness, name, conf) { |
| 15: | //console.error("test ctor") |
| 16: | if (!(this instanceof Test)) return new Test(harness, name, conf) |
| 17: | |
| 18: | Harness.call(this, Test) |
| 19: | |
| 20: | conf.name = name || conf.name || "(anonymous)" |
| 21: | this.conf = conf |
| 22: | |
| 23: | this.harness = harness |
| 24: | this.harness.add(this) |
| 25: | } |
| 26: | |
| 27: | // it's taking too long! |
| 28: | Test.prototype.timeout = function () { |
| 29: | // detect false alarms |
| 30: | if (this._ended) return |
| 31: | this.fail("Timeout!") |
| 32: | this.end() |
| 33: | } |
| 34: | |
| 35: | Test.prototype.clear = function () { |
| 36: | this._started = false |
| 37: | this._ended = false |
| 38: | this._plan = null |
| 39: | this._bailedOut = false |
| 40: | this._testCount = 0 |
| 41: | this.results = new Results() |
| 42: | } |
| 43: | |
| 44: | // this gets called if a test throws ever |
| 45: | Test.prototype.threw = function (ex) { |
| 46: | //console.error("threw!", ex.stack) |
| 47: | this.fail(ex.name + ": " + ex.message, { error: ex, thrown: true }) |
| 48: | // may emit further failing tests if the plan is not completed |
| 49: | //console.error("end, because it threw") |
| 50: | if (!this._ended) this.end() |
| 51: | } |
| 52: | |
| 53: | Test.prototype.comment = function (m) { |
| 54: | if (typeof m !== "string") { |
| 55: | return this.fail("Test.comment argument must be a string") |
| 56: | } |
| 57: | this.result("\n" + m.trim()) |
| 58: | } |
| 59: | |
| 60: | Test.prototype.result = function (res) { |
| 61: | this.results.add(res) |
| 62: | this._testCount ++ |
| 63: | this.emit("result", res) |
| 64: | if (this._plan === this._testCount) { |
| 65: | process.nextTick(this._endNice.bind(this)) |
| 66: | } |
| 67: | } |
| 68: | |
| 69: | Test.prototype._endNice = function () { |
| 70: | if (!this._ended) this.end() |
| 71: | } |
| 72: | |
| 73: | // parasitic |
| 74: | // Who says you can't do multiple inheritance in js? |
| 75: | Object.getOwnPropertyNames(assert).forEach(function (k) { |
| 76: | if (k === "prototype" || k === "name") return |
| 77: | var d = Object.getOwnPropertyDescriptor(assert, k) |
| 78: | , v = d.value |
| 79: | if (!v) return |
| 80: | d.value = assertParasite(v) |
| 81: | Object.defineProperty(Test.prototype, k, d) |
| 82: | }) |
| 83: | |
| 84: | function assertParasite (fn) { return function _testAssert () { |
| 85: | //console.error("_testAssert", fn.name, arguments) |
| 86: | if (this._bailedOut) return |
| 87: | var res = fn.apply(assert, arguments) |
| 88: | this.result(res) |
| 89: | return res |
| 90: | }} |
| 91: | |
| 92: | // a few tweaks on the EE emit function, because |
| 93: | // we want to catch all thrown errors and bubble up "bailout" |
| 94: | Test.prototype.emit = (function (em) { return function (t) { |
| 95: | // bailouts bubble until handled |
| 96: | if (t === "bailout" && |
| 97: | this.listeners(t).length === 0 && |
| 98: | this.harness) { |
| 99: | return this.harness.bailout(arguments[1]) |
| 100: | } |
| 101: | |
| 102: | if (t === "error") return em.apply(this, arguments) |
| 103: | try { |
| 104: | em.apply(this, arguments) |
| 105: | } catch (ex) { |
| 106: | // any exceptions in a test are a failure |
| 107: | //console.error("caught!", ex.stack) |
| 108: | this.threw(ex) |
| 109: | } |
| 110: | }})(Harness.prototype.emit) |
