Skip to content

Commit

Permalink
Use a results object in TapConsumer, and move trailer into the TapPro…
Browse files Browse the repository at this point in the history
…ducer class
  • Loading branch information
isaacs committed Apr 6, 2011
1 parent 7531233 commit 918cc50
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
22 changes: 3 additions & 19 deletions lib/global-harness.js
Expand Up @@ -18,6 +18,9 @@ function GlobalHarness () {
GlobalHarness.super.apply(this, arguments)

this.output.pipe(process.stdout)
//this.output.on("data", function () {
// process.nextTick(process.stdout.flush.bind(process.stdout))
//})

this.test = this.test.bind(this)

Expand Down Expand Up @@ -50,25 +53,6 @@ function GlobalHarness () {
output.write(res)
})
this.results.list.length = 0
// summary trailer.
var trailer = "tests "+this.results.testsTotal + "\n"
+ "pass "+this.results.pass + "\n"
+ "fail "+this.results.fail + "\n"
if (this.results.skip) {
trailer += "skip "+this.results.skip + "\n"
}
if (this.results.todo) {
trailer += "todo "+this.results.todo + "\n"
}
if (this.results.bailedOut) {
trailer += "bailed out" + "\n"
}

if (this.results.testsTotal === this.results.pass) {
trailer += "\nok\n"
}
//console.error("Set trailer: "+JSON.stringify(trailer))
output.trailer = trailer
output.end()
streamEnded = true
}
Expand Down
3 changes: 2 additions & 1 deletion lib/results.js
Expand Up @@ -33,7 +33,7 @@ Results.prototype.addSet = function (r) {
//console.error("after addSet", this)
}

Results.prototype.add = function (r) {
Results.prototype.add = function (r, addToList) {
//console.error("add result", r)
var pf = r.ok ? "pass" : "fail"
, PF = r.ok ? "Pass" : "Fail"
Expand All @@ -55,6 +55,7 @@ Results.prototype.add = function (r) {
if (r.bailout || typeof r.bailout === "string") this.bailedOut = true
this.ok = !!(this.ok && r.ok)

if (addToList === false) return
this.list = this.list || []
this.list.push(r)
}
60 changes: 41 additions & 19 deletions lib/tap-consumer.js
Expand Up @@ -4,14 +4,36 @@ module.exports = TapConsumer
// and it'll emit "data" events with test objects or comment strings
// and an "end" event with the final results.

var tap = require("./tap")
, yamlish = require("./yamlish")
var yamlish = require("./yamlish")
, Results = require("./results")
, inherits = require("./inherits")

TapConsumer.decode = TapConsumer.parse = function (str) {
var tc = new TapConsumer
, list = []
tc.on("data", function (res) {
list.push(res)
})
tc.end(str)
tc.results.list = list
return tc.results
}

var Stream = require("stream").Stream
inherits(TapConsumer, require("stream").Stream)
function TapConsumer () {
this._planEnd = null
Stream.call(this)
if (!(this instanceof TapConsumer)) {
return new TapConsumer
}

TapConsumer.super.call(this)
this.results = new Results
this.readable = this.writable = true

this.on("data", function (res) {
if (typeof res === "object") this.results.add(res)
})

this._plan = null
this._buffer = ""
this._indent = []
this._current = null
Expand All @@ -21,13 +43,11 @@ function TapConsumer () {
//console.error("TapConsumer ctor done")
}

TapConsumer.prototype = Object.create(Stream.prototype)

TapConsumer.prototype.bailed = false
TapConsumer.prototype.bailedOut = false

TapConsumer.prototype.write = function (chunk) {
if (!this.writable) this.emit("error", new Error("not writable"))
if (this.bailed) return true
if (this.bailedOut) return true

this._buffer = this._buffer + chunk
// split it up into lines.
Expand Down Expand Up @@ -63,10 +83,10 @@ TapConsumer.prototype.end = function () {
if (this._buffer.match(/^\s/)) this._indent.push(this.buffer)
else this._parseLine(this._buffer)

if (!this.bailed &&
this._planEnd !== null &&
this._actualCount !== this._planEnd) {
while (this._actualCount < this._planEnd) {
if (!this.bailedOut &&
this._plan !== null &&
this.results.testsTotal !== this._plan) {
while (this._actualCount < this._plan) {
this.emit("data", {ok: false, name:"MISSING TEST",
id:this._actualCount ++ })
}
Expand All @@ -79,6 +99,7 @@ TapConsumer.prototype.end = function () {
}

TapConsumer.prototype._parseLine = function (line) {
if (this.bailedOut) return
//console.error("_parseLine", [line])
// if there are any indented lines, and there is a
// current object already, then they belong to it.
Expand All @@ -98,8 +119,9 @@ TapConsumer.prototype._parseLine = function (line) {
// try to see what kind of line this is.

if (line.match(/^bail out!/i)) {
this.bailed = true
this.emit("error", new Error(line))
this.bailedOut = true
// this.emit("error", new Error(line))
this.emit("bailout", line)
}

if (line.match(/^#/)) { // just a comment
Expand All @@ -117,7 +139,7 @@ TapConsumer.prototype._parseLine = function (line) {

// TODO: maybe do something else with this?
// it might be something like: "1..0 #Skip because of reasons"
this._planEnd = end
this._plan = end
this.emit("plan", end, comment)
// plan must come before or after all tests.
if (this._actualCount !== 0) {
Expand All @@ -139,9 +161,9 @@ TapConsumer.prototype._parseLine = function (line) {
TapConsumer.prototype._parseDirective = function (line) {
line = line.trim()
if (line.match(/^TODO\b/i)) {
return { todo:true, explanation: line.replace(/^TODO\s*/, "") }
return { todo:true, explanation: line.replace(/^TODO\s*/i, "") }
} else if (line.match(/^SKIP\b/i)) {
return { skip:true, explanation: line.replace(/^SKIP\s*/, "") }
return { skip:true, explanation: line.replace(/^SKIP\s*/i, "") }
}
}

Expand Down Expand Up @@ -174,7 +196,7 @@ TapConsumer.prototype._parseResultLine = function (line) {
res.ok = true
if (dir.skip) res.skip = true
else if (dir.todo) res.todo = true
res.explanation = dir.explanation
if (dir.explanation) res.explanation = dir.explanation
}
res.name = name

Expand Down

0 comments on commit 918cc50

Please sign in to comment.