Skip to content

Commit cde93a2

Browse files
committed
Merge pull request #228 from idrarig/multiline-comments
Multiline comments
2 parents 5ca8362 + c66f25e commit cde93a2

File tree

2 files changed

+181
-1
lines changed

2 files changed

+181
-1
lines changed

lib/test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ Test.prototype.test = function (name, opts, cb) {
102102
};
103103

104104
Test.prototype.comment = function (msg) {
105-
this.emit('result', trim(msg).replace(/^#\s*/, ''));
105+
var that = this;
106+
trim(msg).split('\n').forEach(function (aMsg) {
107+
that.emit('result', trim(aMsg).replace(/^#\s*/, ''));
108+
});
106109
};
107110

108111
Test.prototype.plan = function (n) {

test/comment.js

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
var concat = require('concat-stream');
2+
var tap = require('tap');
3+
var tape = require('../');
4+
5+
// Exploratory test to ascertain proper output when no t.comment() call
6+
// is made.
7+
tap.test('no comment', function (assert) {
8+
assert.plan(1);
9+
10+
var verify = function (output) {
11+
assert.equal(output.toString('utf8'), [
12+
'TAP version 13',
13+
'# no comment',
14+
'',
15+
'1..0',
16+
'# tests 0',
17+
'# pass 0',
18+
'',
19+
'# ok',
20+
''
21+
].join('\n'));
22+
};
23+
24+
var test = tape.createHarness();
25+
test.createStream().pipe(concat(verify));
26+
test('no comment', function (t) {
27+
t.end();
28+
});
29+
});
30+
31+
// Exploratory test, can we call t.comment() passing nothing?
32+
tap.test('missing argument', function (assert) {
33+
assert.plan(2);
34+
var test = tape.createHarness();
35+
test.createStream();
36+
test('missing argument', function (t) {
37+
try {
38+
t.comment();
39+
t.end();
40+
} catch (err) {
41+
assert.equal(err.constructor, TypeError);
42+
assert.equal(err.message, 'Cannot call method on undefined');
43+
} finally {
44+
assert.end();
45+
}
46+
});
47+
});
48+
49+
// Exploratory test, can we call t.comment() passing nothing?
50+
tap.test('null argument', function (assert) {
51+
assert.plan(2);
52+
var test = tape.createHarness();
53+
test.createStream();
54+
test('null argument', function (t) {
55+
try {
56+
t.comment(null);
57+
t.end();
58+
} catch (err) {
59+
assert.equal(err.constructor, TypeError);
60+
assert.equal(err.message, 'Cannot call method on null');
61+
} finally {
62+
assert.end();
63+
}
64+
});
65+
});
66+
67+
68+
// Exploratory test, how is whitespace treated?
69+
tap.test('whitespace', function (assert) {
70+
assert.plan(1);
71+
72+
var verify = function (output) {
73+
assert.equal(output.toString('utf8'), [
74+
'TAP version 13',
75+
'# whitespace',
76+
'# ',
77+
'# a',
78+
'# a',
79+
'# a',
80+
'',
81+
'1..0',
82+
'# tests 0',
83+
'# pass 0',
84+
'',
85+
'# ok',
86+
''
87+
].join('\n'));
88+
};
89+
90+
var test = tape.createHarness();
91+
test.createStream().pipe(concat(verify));
92+
test('whitespace', function (t) {
93+
t.comment(' ');
94+
t.comment(' a');
95+
t.comment('a ');
96+
t.comment(' a ');
97+
t.end();
98+
});
99+
});
100+
101+
// Exploratory test, how about passing types other than strings?
102+
tap.test('non-string types', function (assert) {
103+
assert.plan(1);
104+
105+
var verify = function (output) {
106+
assert.equal(output.toString('utf8'), [
107+
'TAP version 13',
108+
'# non-string types',
109+
'# true',
110+
'# false',
111+
'# 42',
112+
'# 6.66',
113+
'# [object Object]',
114+
'# [object Object]',
115+
'# [object Object]',
116+
'# function ConstructorFunction() {}',
117+
'',
118+
'1..0',
119+
'# tests 0',
120+
'# pass 0',
121+
'',
122+
'# ok',
123+
''
124+
].join('\n'));
125+
};
126+
127+
var test = tape.createHarness();
128+
test.createStream().pipe(concat(verify));
129+
test('non-string types', function (t) {
130+
t.comment(true);
131+
t.comment(false);
132+
t.comment(42);
133+
t.comment(6.66);
134+
t.comment({});
135+
t.comment({"answer": 42});
136+
function ConstructorFunction() {}
137+
t.comment(new ConstructorFunction());
138+
t.comment(ConstructorFunction);
139+
t.end();
140+
});
141+
});
142+
143+
tap.test('multiline string', function (assert) {
144+
assert.plan(1);
145+
146+
var verify = function (output) {
147+
assert.equal(output.toString('utf8'), [
148+
'TAP version 13',
149+
'# multiline strings',
150+
'# a',
151+
'# b',
152+
'# c',
153+
'# d',
154+
'',
155+
'1..0',
156+
'# tests 0',
157+
'# pass 0',
158+
'',
159+
'# ok',
160+
''
161+
].join('\n'));
162+
};
163+
164+
var test = tape.createHarness();
165+
test.createStream().pipe(concat(verify));
166+
test('multiline strings', function (t) {
167+
t.comment([
168+
'a',
169+
'b',
170+
].join('\n'));
171+
t.comment([
172+
'c',
173+
'd',
174+
].join('\r\n'));
175+
t.end();
176+
});
177+
});

0 commit comments

Comments
 (0)