Skip to content

Commit c878a3a

Browse files
author
Eric Millin
committed
Add comment formatting
1 parent bc6ecea commit c878a3a

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

lib/test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var deepEqual = require('deep-equal');
22
var defined = require('defined');
33
var path = require('path');
4+
var format = require('util').format;
45
var inherits = require('inherits');
56
var EventEmitter = require('events').EventEmitter;
67
var has = require('has');
@@ -106,8 +107,11 @@ Test.prototype.test = function (name, opts, cb) {
106107
});
107108
};
108109

109-
Test.prototype.comment = function (msg) {
110+
Test.prototype.comment = function () {
110111
var that = this;
112+
var keys = Object.keys(arguments)
113+
var msg = keys.length > 1 ? format.apply(null, arguments) : arguments[keys[0]];
114+
111115
trim(msg).split('\n').forEach(function (aMsg) {
112116
that.emit('result', trim(aMsg).replace(/^#\s*/, ''));
113117
});

readme.markdown

+3-2
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,10 @@ after `t` will not be run until all subtests finish.
271271

272272
You may pass the same options that [`test()`](#testname-opts-cb) accepts.
273273

274-
## t.comment(message)
274+
## t.comment(message[, ...])
275275

276-
Print a message without breaking the tap output. (Useful when using e.g. `tap-colorize` where output is buffered & `console.log` will print in incorrect order vis-a-vis tap output.)
276+
Print a message without breaking the tap output. Accepts optional args for `util.format`-style formatting.
277+
(Useful when using e.g. `tap-colorize` where output is buffered & `console.log` will print in incorrect order vis-a-vis tap output.)
277278

278279
## var htest = test.createHarness()
279280

test/comment.js

+54
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,57 @@ tap.test('multiline string', function (assert) {
173173
t.end();
174174
});
175175
});
176+
177+
tap.test('formatted string', function (assert) {
178+
assert.plan(1);
179+
180+
var verify = function (output) {
181+
assert.equal(output.toString('utf8'), [
182+
'TAP version 13',
183+
'# formatted string',
184+
'# tip tap tape',
185+
'',
186+
'1..0',
187+
'# tests 0',
188+
'# pass 0',
189+
'',
190+
'# ok',
191+
''
192+
].join('\n'));
193+
};
194+
195+
var test = tape.createHarness();
196+
test.createStream().pipe(concat(verify));
197+
test('formatted string', function (t) {
198+
t.comment("tip %s t%s", "tap", "ape");
199+
t.end();
200+
});
201+
});
202+
203+
tap.test('formatted multiline string', function (assert) {
204+
assert.plan(1);
205+
206+
var verify = function (output) {
207+
assert.equal(output.toString('utf8'), [
208+
'TAP version 13',
209+
'# formatted multiline string',
210+
'# tip',
211+
'# tap',
212+
'# tape',
213+
'',
214+
'1..0',
215+
'# tests 0',
216+
'# pass 0',
217+
'',
218+
'# ok',
219+
''
220+
].join('\n'));
221+
};
222+
223+
var test = tape.createHarness();
224+
test.createStream().pipe(concat(verify));
225+
test('formatted multiline string', function (t) {
226+
t.comment("tip\n%s\nt%s", "tap", "ape");
227+
t.end();
228+
});
229+
});

0 commit comments

Comments
 (0)