Name: js-handler/node_modules/nodeunit/node_modules/tap/lib/tap-producer.js
| 1: | module.exports = TapProducer |
| 2: | |
| 3: | var Results = require("./tap-results") |
| 4: | , inherits = require("inherits") |
| 5: | , yamlish = require("yamlish") |
| 6: | |
| 7: | TapProducer.encode = function (result, diag) { |
| 8: | var tp = new TapProducer(diag) |
| 9: | , out = "" |
| 10: | tp.on("data", function (c) { out += c }) |
| 11: | if (Array.isArray(result)) { |
| 12: | result.forEach(tp.write, tp) |
| 13: | } else tp.write(result) |
| 14: | tp.end() |
| 15: | return out |
| 16: | } |
| 17: | |
| 18: | var Stream = require("stream").Stream |
| 19: | inherits(TapProducer, Stream) |
| 20: | function TapProducer (diag) { |
| 21: | Stream.call(this) |
| 22: | this.diag = diag |
| 23: | this.count = 0 |
| 24: | this.readable = this.writable = true |
| 25: | this.results = new Results |
| 26: | } |
| 27: | |
| 28: | TapProducer.prototype.trailer = true |
| 29: | |
| 30: | TapProducer.prototype.write = function (res) { |
| 31: | // console.error("TapProducer.write", res) |
| 32: | if (typeof res === "function") throw new Error("wtf?") |
| 33: | if (!this.writable) this.emit("error", new Error("not writable")) |
| 34: | |
| 35: | if (!this._didHead) { |
| 36: | this.emit("data", "TAP version 13\n") |
| 37: | this._didHead = true |
| 38: | } |
| 39: | |
| 40: | var diag = res.diag |
| 41: | if (diag === undefined) diag = this.diag |
| 42: | |
| 43: | this.emit("data", encodeResult(res, this.count + 1, diag)) |
| 44: | |
| 45: | if (typeof res === "string") return true |
| 46: | |
| 47: | if (res.bailout) { |
| 48: | var bo = "bail out!" |
| 49: | if (typeof res.bailout === "string") bo += " " + res.bailout |
| 50: | this.emit("data", bo) |
| 51: | return |
| 52: | } |
| 53: | this.results.add(res, false) |
| 54: | |
| 55: | this.count ++ |
| 56: | } |
| 57: | |
| 58: | TapProducer.prototype.end = function (res) { |
| 59: | if (res) this.write(res) |
| 60: | // console.error("TapProducer end", res, this.results) |
| 61: | this.emit("data", "\n1.."+this.results.testsTotal+"\n") |
| 62: | if (this.trailer && typeof this.trailer !== "string") { |
| 63: | // summary trailer. |
| 64: | var trailer = "tests "+this.results.testsTotal + "\n" |
| 65: | if (this.results.pass) { |
| 66: | trailer += "pass " + this.results.pass + "\n" |
| 67: | } |
| 68: | if (this.results.fail) { |
| 69: | trailer += "fail " + this.results.fail + "\n" |
| 70: | } |
| 71: | if (this.results.skip) { |
| 72: | trailer += "skip "+this.results.skip + "\n" |
| 73: | } |
| 74: | if (this.results.todo) { |
| 75: | trailer += "todo "+this.results.todo + "\n" |
| 76: | } |
| 77: | if (this.results.bailedOut) { |
| 78: | trailer += "bailed out" + "\n" |
| 79: | } |
| 80: | |
| 81: | if (this.results.testsTotal === this.results.pass) { |
| 82: | trailer += "\nok\n" |
| 83: | } |
| 84: | this.trailer = trailer |
| 85: | } |
| 86: | if (this.trailer) this.write(this.trailer) |
| 87: | this.writable = false |
| 88: | this.emit("end", null, this.count, this.ok) |
| 89: | } |
| 90: | |
| 91: | function encodeResult (res, count, diag) { |
| 92: | // console.error(res, count, diag) |
| 93: | if (typeof res === "string") { |
| 94: | res = res.split(/\r?\n/).map(function (l) { |
| 95: | if (!l.trim()) return l.trim() |
| 96: | return "# " + l |
| 97: | }).join("\n") |
| 98: | if (res.substr(-1) !== "\n") res += "\n" |
| 99: | return res |
| 100: | } |
| 101: | |
| 102: | if (res.bailout) return "" |
| 103: | |
| 104: | |
| 105: | if (!!process.env.TAP_NODIAG) diag = false |
| 106: | else if (!!process.env.TAP_DIAG) diag = true |
| 107: | else if (diag === undefined) diag = !res.ok |
| 108: | |
| 109: | var output = "" |
| 110: | res.name = res.name && ("" + res.name).trim() |
| 111: | output += ( !res.ok ? "not " : "") + "ok " + count |
| 112: | + ( !res.name ? "" |
| 113: | : " " + res.name.replace(/[\r\n]/g, " ") ) |
| 114: | + ( res.skip ? " # SKIP" |
| 115: | : res.todo ? " # TODO" |
| 116: | : "" ) |
| 117: | + "\n" |
| 118: | |
| 119: | if (!diag) return output |
| 120: | var d = {} |
| 121: | , dc = 0 |
| 122: | Object.keys(res).filter(function (k) { |
| 123: | return k !== "ok" && k !== "name" && k !== "id" |
| 124: | }).forEach(function (k) { |
| 125: | dc ++ |
| 126: | d[k] = res[k] |
| 127: | }) |
| 128: | //console.error(d, "about to encode") |
| 129: | if (dc > 0) output += " ---"+yamlish.encode(d)+"\n ...\n" |
| 130: | return output |
| 131: | } |
