Skip to content

Commit 7510a6d

Browse files
committed
cleanup Stdin for boosting test coverage
1 parent f6023b1 commit 7510a6d

26 files changed

+375
-58
lines changed

lib/base.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ Base.prototype.debug = (/\btap\b/i.test(process.env.NODE_DEBUG || ''))
185185

186186
function nodebug () {}
187187

188+
/* istanbul ignore next */
188189
function debug () {
189190
var prefix = 'TAP ' + process.pid + ' ' + this.name + ': '
190191
var msg = util.format.apply(util, arguments).trim()

lib/clean-yaml-object.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function yamlFilter (propertyName, isRoot, source, target) {
4747

4848
return !(propertyName === 'todo' ||
4949
/^_?tapChild/.test(propertyName) ||
50+
/^tapStream/.test(propertyName) ||
5051
/^tapMochaTest/.test(propertyName) ||
5152
propertyName === 'cb' ||
5253
propertyName === 'name' ||

lib/parse-test-args.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,19 @@ module.exports = function (name_, extra_, cb_, defaultName) {
4242
if (!cb)
4343
extra.todo = true
4444

45+
if (!name && extra.name)
46+
name = extra.name
47+
4548
if (!name && cb && cb.name)
4649
name = cb.name
4750

4851
name = name || defaultName
4952
extra.name = name
50-
extra.cb = cb || function () {
51-
throw new Error('callback called for TODO test')
52-
}
53+
extra.cb = cb || todoCb
5354
return extra
5455
}
56+
57+
/* istanbul ignore next */
58+
function todoCb () {
59+
throw new Error('callback called for TODO test')
60+
}

lib/point.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ function TestPoint (ok, message, extra) {
99
if (typeof ok !== 'boolean')
1010
throw new TypeError('ok must be boolean')
1111

12+
if (!(this instanceof TestPoint))
13+
return new TestPoint(ok, message, extra)
14+
1215
this.ok = ok ? 'ok ' : 'not ok '
1316
this.message = tpMessage(message, extra)
1417
}

lib/spawn.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ function Spawn (options) {
2020
Base.call(this, options)
2121

2222
this.command = options.command
23-
if (!this.command) {
23+
24+
if (!this.command)
2425
throw new TypeError('no command provided')
25-
}
2626

2727
this.args = options.args
2828
// stdout must be a pipe
2929
if (options.stdio) {
30-
if (typeof options.stdio === 'string') {
30+
if (typeof options.stdio === 'string')
3131
this.stdio = [ options.stdio, 'pipe', options.stdio ]
32-
} else {
32+
else
3333
this.stdio = options.stdio.slice(0)
34-
}
3534
} else
3635
this.stdio = [ 0, 'pipe', 2 ]
3736

lib/stdin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ function Stdin (options) {
1616
Base.call(this, options)
1717

1818
// This has to be here for node 0.10's wonky streams
19-
process.stdin.pause()
19+
this.stream = ownOr(options, 'tapStream', process.stdin)
20+
this.stream.pause()
2021
}
2122

2223
Stdin.prototype.main = function (cb) {
23-
this.domain.add(process.stdin)
24+
this.domain.add(this.stream)
2425
this.setTimeout(this.options.timeout)
25-
process.stdin.pipe(this.parser)
26+
this.stream.pipe(this.parser)
27+
this.stream.resume()
2628
this.once('end', cb)
2729
}
2830

lib/tap.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var Test = require('./test.js')
2+
var Stdin = require('./stdin.js')
3+
var Spawn = require('./spawn.js')
24
var util = require('util')
35
var objToYaml = require('./obj-to-yaml.js')
46
var yaml = require('js-yaml')
@@ -59,6 +61,8 @@ process.on('exit', function (code) {
5961
})
6062

6163
tap.Test = Test
64+
tap.Spawn = Spawn
65+
tap.Stdin = Stdin
6266
tap.synonyms = require('./synonyms.js')
6367

6468
// SIGTERM means being forcibly killed, almost always by timeout

test/runner-read-stdin.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,3 @@ t.test('read from stdin', { skip: process.platform === 'win32' && 'skip stdin te
143143

144144
t.end()
145145
})
146-

test/test-args.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ var parseTestArgs = require('../lib/parse-test-args.js')
44

55
function namedFunction () {}
66
var fn = function () {}
7+
function clone (o) {
8+
return Object.keys(o).reduce(function (c, k) {
9+
c[k] = o[k]
10+
return c
11+
}, {})
12+
}
713
var obj = { thisIsMyObject: true }
14+
var cobj = clone.bind(null, obj)
815
var objTodo = { thisIsMyObject: true, todo: true }
16+
var cobjTodo = clone.bind(null, objTodo)
917

1018
function runTest (args, expect) {
11-
delete obj.todo
12-
1319
var result = parseTestArgs.apply(null, args)
1420
t.match(result, expect)
1521
}
@@ -22,30 +28,30 @@ function c (obj, props) {
2228
}, props)
2329
}
2430

25-
runTest(['name', obj, fn], c(obj, {name: 'name', cb: fn}))
31+
runTest(['name', cobj(), fn], c(cobj(), {name: 'name', cb: fn}))
2632
runTest(['name', fn], { name: 'name', cb: fn })
27-
runTest([obj, fn], c(obj, { name: /\(unnamed test\)|fn/, cb: fn }))
28-
runTest([obj, namedFunction], c(obj, { name: 'namedFunction', cb: namedFunction }))
29-
runTest(['name', obj], c(objTodo, { name: 'name' }))
33+
runTest([cobj(), fn], c(cobj(), { name: /\(unnamed test\)|fn/, cb: fn }))
34+
runTest([cobj(), namedFunction], c(cobj(), { name: 'namedFunction', cb: namedFunction }))
35+
runTest(['name', cobj()], c(cobjTodo(), { name: 'name' }))
3036
runTest(['name'], { name: 'name', todo: true })
31-
runTest([obj], c(objTodo, { name: /\(unnamed test\)|fn/ }))
37+
runTest([cobj()], c(cobjTodo(), { name: /\(unnamed test\)|fn/ }))
3238
runTest([fn], {name: /\(unnamed test\)|fn/, cb: fn})
3339
runTest([namedFunction], { name: 'namedFunction', cb: namedFunction })
3440
runTest([], { name: /\(unnamed test\)|fn/, todo: true })
3541

3642
var dn = 'defaultName'
3743
var _ = undefined
38-
runTest(['name', obj, fn, dn], c(obj, {name: 'name', cb: fn}))
44+
runTest(['name', cobj(), fn, dn], c(cobj(), {name: 'name', cb: fn}))
3945
runTest(['name', fn, _, dn], { name: 'name', cb: fn })
40-
runTest([obj, fn, _, dn], c(obj, { name: /defaultName|fn/, cb: fn }))
41-
runTest([obj, namedFunction, _, dn], c(obj, { name: 'namedFunction', cb: namedFunction }))
42-
runTest(['name', obj, _, dn], c(objTodo, { name: 'name' }))
46+
runTest([cobj(), fn, _, dn], c(cobj(), { name: /defaultName|fn/, cb: fn }))
47+
runTest([cobj(), namedFunction, _, dn], c(cobj(), { name: 'namedFunction', cb: namedFunction }))
48+
runTest(['name', cobj(), _, dn], c(cobjTodo(), { name: 'name' }))
4349
runTest(['name', _, _, dn], { name: 'name', todo: true })
44-
runTest([obj, _, _, dn], c(objTodo, { name: /defaultName|fn/ }))
50+
runTest([cobj(), _, _, dn], c(cobjTodo(), { name: /defaultName|fn/ }))
4551
runTest([fn, _, _, dn], {name: /defaultName|fn/, cb: fn})
4652
runTest([namedFunction, _, _, dn], { name: 'namedFunction', cb: namedFunction })
4753
runTest([_, _, _, dn], { name: /defaultName|fn/, todo: true })
4854

4955
t.throws(function () {
50-
runTest(['name', obj, 99], {})
56+
runTest(['name', cobj(), 99], {})
5157
})

test/test-test.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
var tap = require('../')
2-
var test = tap.test
3-
var Test = tap.Test
1+
var t = require('../')
2+
var Test = t.Test
43

5-
test('testing the test object', function (t) {
4+
t.test('testing the test object', function (t) {
65
t.isa(t, Test, 'test object should be instanceof Test')
76

87
// now test all the methods.
@@ -80,7 +79,7 @@ test('testing the test object', function (t) {
8079
t.end()
8180
})
8281

83-
test('plan stuff', function (t) {
82+
t.test('plan stuff', function (t) {
8483
t.throws(function () {
8584
var tt = new Test({ buffered: false })
8685
tt.plan(1)
@@ -98,20 +97,7 @@ test('plan stuff', function (t) {
9897
t.end()
9998
})
10099

101-
test('subtest with name and function', function (t) {
102-
var tt = new Test({ buffered: false })
103-
var out = ''
104-
tt.on('data', function (c) {
105-
out += c
106-
})
107-
tt.test('name', function (t){t.end()})
108-
tt.end()
109-
110-
t.equal(out, 'TAP version 13\n# Subtest: name\n 1..0\nok 1 - name\n\n1..1\n');
111-
t.end()
112-
})
113-
114-
test('invalid test arguments', function (t) {
100+
t.test('invalid test arguments', function (t) {
115101
t.throws(function () {
116102
var tt = new Test({ buffered: false })
117103
tt.test('name', { skip: false }, 'not a function')
@@ -120,7 +106,7 @@ test('invalid test arguments', function (t) {
120106
t.end()
121107
})
122108

123-
test('throws type', function (t) {
109+
t.test('throws type', function (t) {
124110
t.throws(function() {
125111
throw new TypeError('some type error');
126112
}, TypeError, 'should throw a TypeError');
@@ -144,3 +130,29 @@ test('throws type', function (t) {
144130

145131
t.end()
146132
})
133+
134+
t.test('test-point', function (t) {
135+
var TestPoint = require('../lib/point.js')
136+
t.throws(function () {
137+
new TestPoint(100, 'century!', { flerg: 'blooze' })
138+
}, new TypeError('ok must be boolean'))
139+
140+
var tp = TestPoint(true, 'msg', { a: 1 })
141+
t.isa(tp, TestPoint)
142+
t.match(tp, {
143+
ok: 'ok ',
144+
message: '- msg\n'
145+
})
146+
147+
t.match(TestPoint(true, 'msg', { a: 1, diagnostic: true }), {
148+
ok: 'ok ',
149+
message: ' - msg\n ---\n a: 1\n ...\n\n'
150+
})
151+
152+
t.match(TestPoint(true, 'msg', { a: 1, tapChildBuffer: 'subtest output' }), {
153+
ok: 'ok ',
154+
message: ' - msg {\nsubtest output\n}\n\n'
155+
})
156+
157+
t.end()
158+
})

0 commit comments

Comments
 (0)