Skip to content

Commit

Permalink
[New] Add comment formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Millin authored and ljharb committed Sep 7, 2016
1 parent d15bc4b commit 5e86f30
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var deepEqual = require('deep-equal');
var defined = require('defined');
var path = require('path');
var format = require('util').format;
var inherits = require('inherits');
var EventEmitter = require('events').EventEmitter;
var has = require('has');
Expand Down Expand Up @@ -121,7 +122,11 @@ Test.prototype.test = function (name, opts, cb) {

Test.prototype.comment = function (msg) {
var that = this;
forEach(trim(msg).split('\n'), function (aMsg) {
// Previous behavior involved `toString` calls on objects, i.e. emitting `[object Object]`.
// `util.format`, however, will print stringified objects. To maintain backward compatibility
// check the args length and only call `util.format` if the invoker desires string expansion.
var message = arguments.length > 1 ? format.apply(null, arguments) : msg;
forEach(trim(message).split('\n'), function (aMsg) {
that.emit('result', trim(aMsg).replace(/^#\s*/, ''));
});
};
Expand Down
4 changes: 2 additions & 2 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ after `t` will not be run until all subtests finish.

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

## t.comment(message)
## t.comment(message[, ...])

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.)
Print a message without breaking the tap output. Accepts optional args for `util.format`-style formatting. Useful when using e.g. `tap-colorize` where output is buffered & `console.log` will print in incorrect order vis-a-vis tap output.

## var htest = test.createHarness()

Expand Down
54 changes: 54 additions & 0 deletions test/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,57 @@ tap.test('multiline string', function (assert) {
t.end();
});
});

tap.test('formatted string', function (assert) {
assert.plan(1);

var verify = function (output) {
assert.equal(output.toString('utf8'), [
'TAP version 13',
'# formatted string',
'# tip tap tape',
'',
'1..0',
'# tests 0',
'# pass 0',
'',
'# ok',
''
].join('\n'));
};

var test = tape.createHarness();
test.createStream().pipe(concat(verify));
test('formatted string', function (t) {
t.comment("tip %s t%s", "tap", "ape");
t.end();
});
});

tap.test('formatted multiline string', function (assert) {
assert.plan(1);

var verify = function (output) {
assert.equal(output.toString('utf8'), [
'TAP version 13',
'# formatted multiline string',
'# tip',
'# tap',
'# tape',
'',
'1..0',
'# tests 0',
'# pass 0',
'',
'# ok',
''
].join('\n'));
};

var test = tape.createHarness();
test.createStream().pipe(concat(verify));
test('formatted multiline string', function (t) {
t.comment("tip\n%s\nt%s", "tap", "ape");
t.end();
});
});

0 comments on commit 5e86f30

Please sign in to comment.