Skip to content

Commit

Permalink
tapjsGH-18: Merged tap-test, git history included.
Browse files Browse the repository at this point in the history
  • Loading branch information
kusor committed Nov 17, 2011
2 parents 1512c10 + 9ad5f65 commit 332ef85
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ this is very useful for CI systems and such.
support for running TAP tests.

More docs coming soon, hopefully.

12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
, "dependencies" :
{ "tap-global-harness" : "0.x"
, "tap-runner" : "0.x >=0.0.6"
, "tap-test" : "0.x"
, "tap-harness" : "0.x"
, "tap-results" : "0.x"
, "tap-consumer" : "0.x"
Expand All @@ -22,14 +21,12 @@
, "tap-harness"
, "tap-producer"
, "tap-results"
, "tap-test"
, "yamlish"
]
, "keywords" :
[
"assert",
"test",
"tap"
[ "assert"
, "test"
, "tap"
]
, "contributors" :
[
Expand All @@ -38,7 +35,8 @@
]
, "license": {
"type": "MIT",
"url": "https://github.com/isaacs/tap-assert/raw/master/LICENSE"
"url": "https://github.com/isaacs/node-tap/raw/master/LICENSE"
}
, "repository" : "git://github.com/isaacs/node-tap.git"
, "scripts" : { "test" : "bin/tap.js test" } }

108 changes: 108 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// This is a very simple test framework that leverages the tap framework
// to run tests and output tap-parseable results.

module.exports = Test

var assert = require("tap-assert")
, inherits = require("inherits")
, Results = require("tap-results")

// tests are also test harnesses
inherits(Test, require("tap-harness"))

function Test (harness, name, conf) {
//console.error("test ctor")
if (!(this instanceof Test)) return new Test(harness, name, conf)

Test.super.call(this, Test)

conf.name = name || conf.name || "(anonymous)"
this.conf = conf

this.harness = harness
this.harness.add(this)
}

// it's taking too long!
Test.prototype.timeout = function () {
// detect false alarms
if (this._ended) return
this.fail("Timeout!")
this.end()
}

Test.prototype.clear = function () {
this._started = false
this._ended = false
this._plan = null
this._bailedOut = false
this._testCount = 0
this.results = new Results()
}

// this gets called if a test throws ever
Test.prototype.threw = function (ex) {
//console.error("threw!", ex.stack)
this.fail(ex.name + ": " + ex.message, { error: ex, thrown: true })
// may emit further failing tests if the plan is not completed
//console.error("end, because it threw")
this.end()
}

Test.prototype.comment = function (m) {
if (typeof m !== "string") {
return this.fail("Test.comment argument must be a string")
}
this.result("\n" + m.trim())
}

Test.prototype.result = function (res) {
this.results.add(res)
this._testCount ++
this.emit("result", res)
if (this._plan === this._testCount) {
process.nextTick(this._endNice.bind(this))
}
}

Test.prototype._endNice = function () {
if (!this._ended) this.end()
}

// parasitic
// Who says you can't do multiple inheritance in js?
Object.getOwnPropertyNames(assert).forEach(function (k) {
if (k === "prototype" || k === "name") return
var d = Object.getOwnPropertyDescriptor(assert, k)
, v = d.value
if (!v) return
d.value = assertParasite(v)
Object.defineProperty(Test.prototype, k, d)
})

function assertParasite (fn) { return function _testAssert () {
//console.error("_testAssert", fn.name, arguments)
if (this._bailedOut) return
var res = fn.apply(assert, arguments)
this.result(res)
}}

// a few tweaks on the EE emit function, because
// we want to catch all thrown errors and bubble up "bailout"
Test.prototype.emit = (function (em) { return function (t) {
// bailouts bubble until handled
if (t === "bailout" &&
this.listeners(t).length === 0 &&
this.harness) {
return this.harness.bailout(arguments[1])
}

if (t === "error") return em.apply(this, arguments)
try {
em.apply(this, arguments)
} catch (ex) {
// any exceptions in a test are a failure
//console.error("caught!", ex.stack)
this.threw(ex)
}
}})(Test.super.prototype.emit)

0 comments on commit 332ef85

Please sign in to comment.