Skip to content

Commit 5e86f30

Browse files
Eric Millinljharb
Eric Millin
authored andcommitted
[New] Add comment formatting
1 parent d15bc4b commit 5e86f30

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

lib/test.js

+6-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');
@@ -121,7 +122,11 @@ Test.prototype.test = function (name, opts, cb) {
121122

122123
Test.prototype.comment = function (msg) {
123124
var that = this;
124-
forEach(trim(msg).split('\n'), function (aMsg) {
125+
// Previous behavior involved `toString` calls on objects, i.e. emitting `[object Object]`.
126+
// `util.format`, however, will print stringified objects. To maintain backward compatibility
127+
// check the args length and only call `util.format` if the invoker desires string expansion.
128+
var message = arguments.length > 1 ? format.apply(null, arguments) : msg;
129+
forEach(trim(message).split('\n'), function (aMsg) {
125130
that.emit('result', trim(aMsg).replace(/^#\s*/, ''));
126131
});
127132
};

readme.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ after `t` will not be run until all subtests finish.
277277

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

280-
## t.comment(message)
280+
## t.comment(message[, ...])
281281

282-
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.)
282+
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.
283283

284284
## var htest = test.createHarness()
285285

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)