diff --git a/lib/test.js b/lib/test.js index 9f0916ff..034bdad4 100644 --- a/lib/test.js +++ b/lib/test.js @@ -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'); @@ -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*/, '')); }); }; diff --git a/readme.markdown b/readme.markdown index cf9296db..11456387 100644 --- a/readme.markdown +++ b/readme.markdown @@ -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() diff --git a/test/comment.js b/test/comment.js index b3f9bcc3..359a9bf9 100644 --- a/test/comment.js +++ b/test/comment.js @@ -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(); + }); +});