Skip to content

Commit 7ed3c4f

Browse files
authored
Merge pull request #13 from zoubin/refactor
Refactor.
2 parents 70c7087 + cae9287 commit 7ed3c4f

30 files changed

+616
-530
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ or in `package.json`
2828
```
2929
--no-ansi Disable ANSI formatting
3030
--no-progress Disable progress output during tests
31-
--markdown Format output as markdown
3231
```
3332

3433
## Example

bin/cmd.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
#!/usr/bin/env node
2-
var minimist = require('minimist')
32

4-
var opts = minimist(process.argv.slice(2), {
5-
boolean: true,
6-
alias: {
7-
ansi: 'a',
8-
progress: 'p',
9-
markdown: 'm',
10-
},
11-
default: {
12-
ansi: true,
13-
progress: true,
14-
markdown: false,
15-
},
16-
})
3+
var Command = require('commander').Command
4+
var program = new Command('tap-summary')
5+
6+
program
7+
.version(require('../package.json').version)
8+
.option('--no-ansi', 'Disable ANSI formatting.')
9+
.option('--no-progress', 'Disable progress output during tests.')
10+
.parse(process.argv)
1711

18-
var reporter = require('..')(opts)
12+
var reporter = require('..')({
13+
ansi: program.ansi,
14+
progress: program.progress,
15+
})
1916

2017
process.stdin
2118
.pipe(reporter)

example/comment.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var test = require('tape')
2+
3+
test('sync', function (t) {
4+
t.ok(true, 'should pass')
5+
console.log('Hello, ')
6+
t.end()
7+
})
8+
9+
test('async', function (t) {
10+
process.nextTick(function () {
11+
t.ok(true, 'should pass')
12+
console.log('world!')
13+
t.end()
14+
})
15+
})
16+

example/error.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var test = require('tape')
2+
3+
test('pass', function (t) {
4+
t.ok(true, 'should pass')
5+
t.end()
6+
})
7+
8+
test('fail', function (t) {
9+
t.ok(false, 'should fail')
10+
t.end()
11+
})
12+
13+
test('pass and fail', function (t) {
14+
t.ok(true, 'should pass')
15+
t.ok(false, 'should fail')
16+
t.end()
17+
})
File renamed without changes.
File renamed without changes.

example/run-test.js

-8
This file was deleted.

example/test-error.js

-30
This file was deleted.

example/test.js

+7-54
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var test = require('tape')
22
var math = require('./math')
33

4-
test('t.plan', function(t) {
4+
test('pass', function(t) {
55
t.plan(3)
66

77
t.equal(
@@ -20,7 +20,7 @@ test('t.plan', function(t) {
2020
})
2121
})
2222

23-
test('t.end', function(t) {
23+
test('fail', function(t) {
2424
t.equal(
2525
math.precision(0),
2626
0
@@ -39,67 +39,20 @@ test('t.end', function(t) {
3939
if (plan) {
4040
NEXT()
4141
} else {
42-
console.log('DONE')
4342
t.end()
4443
}
4544
})
4645
})()
4746
})
4847

49-
test('promise support', function(t) {
50-
var plan = 10
51-
t.equal(
52-
math.add(0.34, 0.01),
53-
0.35
54-
)
55-
t.equal(
56-
math.add(1.1111, -1.11),
57-
0.0011
58-
)
59-
// the test will end when the returned promise resolves
60-
return new Promise(function (rs) {
61-
(function NEXT() {
62-
next(function () {
63-
t.equal(
64-
math.add(1, 2),
65-
--plan === 5 ? 2 : 3
66-
)
67-
if (plan) {
68-
NEXT()
69-
} else {
70-
console.log('DONE')
71-
rs()
72-
}
73-
})
74-
})()
75-
})
76-
})
77-
78-
test('callback support', function(t, cb) {
48+
test('comment', function (t) {
7949
next(function () {
80-
t.equal(
81-
math.add(0.34, 0.01),
82-
0.35
83-
)
84-
cb()
85-
})
86-
})
87-
88-
test('check `task-tape` for more information', function(t) {
89-
t.task(function (cb) {
90-
next(function () {
91-
t.equal(
92-
math.precision(0.1),
93-
1
94-
)
95-
cb()
96-
})
50+
t.ok(true, 'should pass')
51+
console.log('This is something from `console.log`')
52+
t.end()
9753
})
9854
})
9955

10056
function next(fn) {
101-
setTimeout(function() {
102-
fn()
103-
}, 100)
57+
setTimeout(fn, 100)
10458
}
105-

example/unnamed-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var tapSummary = require('..')
21
var fs = require('fs')
2+
var tapSummary = require('..')
33

44
fs.createReadStream(__dirname + '/unnamed-test.tap')
55
.pipe(tapSummary())

index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1+
var reporter = require('./lib/reporter')
2+
var Formatter = require('./lib/formatter')
3+
4+
module.exports = function (opts) {
5+
return reporter(new Formatter(opts))
6+
}
7+
module.exports.Formatter = Formatter
8+
module.exports.reporter = reporter
9+
module.exports.parser = require('./lib/parser')
110

2-
module.exports = require('./lib/summary')

lib/formatter.js

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
var format = require('ansi-escape')
2+
var symbols = require('figures')
3+
var prettyMs = require('pretty-ms')
4+
var LF = '\n'
5+
6+
module.exports = Formatter
7+
8+
function Formatter(opts) {
9+
opts = opts || {}
10+
this.needDuration = opts.duration !== false
11+
this.needAnsi = opts.ansi !== false
12+
this.needProgress = this.needAnsi && opts.progress !== false
13+
}
14+
15+
Formatter.prototype.init = function (reporter) {
16+
var self = this
17+
18+
reporter.push(LF + this.splitter(' Tests '))
19+
20+
reporter.on('test.start', function (test) {
21+
Object.defineProperty(test, 'title', { get: getTitle })
22+
})
23+
24+
function getTitle() {
25+
return this.name + ' [' +
26+
'pass: ' + this.pass + ', fail: ' + this.fail +
27+
(self.needDuration ? ', duration: ' + prettyMs(this.duration || 0) : '') +
28+
']'
29+
}
30+
31+
if (this.needProgress) {
32+
reporter.on('test.start', function (test) {
33+
this.push(LF + self.format('# ' + test.title, 'cha.eraseLine'))
34+
})
35+
36+
reporter.on('test.pass', function (test) {
37+
this.push(self.format('# ' + test.title, 'cha.eraseLine'))
38+
})
39+
40+
reporter.on('test.fail', function (test) {
41+
this.push(self.format('# ' + test.title, 'cha.eraseLine'))
42+
})
43+
}
44+
}
45+
46+
Formatter.prototype.summary = function (summary) {
47+
var output = [LF]
48+
output.push(this.splitter(' Summary '))
49+
50+
if (this.needDuration) {
51+
output.push(this.format(
52+
'duration: ' + prettyMs(summary.duration),
53+
'cyan'
54+
))
55+
}
56+
output.push(this.format(
57+
'planned: ' + summary.planned,
58+
summary.planned instanceof Error ? 'red' : 'cyan'
59+
))
60+
output.push(this.format(
61+
'assertions: ' + summary.assertions,
62+
'cyan'
63+
))
64+
output.push(this.format(
65+
'pass: ' + summary.pass,
66+
summary.pass ? 'green' : 'cyan'
67+
))
68+
output.push(this.format(
69+
'fail: ' + summary.fail,
70+
summary.fail ? 'red' : 'cyan'
71+
))
72+
73+
return output.join(LF)
74+
}
75+
76+
Formatter.prototype.comment = function (comments) {
77+
var output = [LF]
78+
output.push(this.splitter(' Comments '))
79+
output.push(Object.keys(comments).map(function (name) {
80+
return this.format('# ' + name, 'cyan.underline') + LF + comments[name].join(LF)
81+
}, this).join(LF + LF))
82+
return output.join(LF)
83+
}
84+
85+
Formatter.prototype.fail = function (fail) {
86+
var output = [LF]
87+
output.push(this.splitter(' Fails '))
88+
output.push(
89+
Object.keys(fail).map(function (name) {
90+
var res = [this.format('# ' + name, 'cyan.underline')]
91+
fail[name].forEach(function (assertion) {
92+
res.push(this.format(' ' + symbols.cross + ' ' + assertion.name, 'red'))
93+
res.push(this.prettifyError(assertion))
94+
}, this)
95+
return res.join(LF)
96+
}, this).join(LF + LF)
97+
)
98+
return output.join(LF)
99+
}
100+
101+
Formatter.prototype.prettifyError = function (assertion) {
102+
var rawError = assertion.error.raw
103+
var ret = rawError.split(LF)
104+
var stack = assertion.error.stack
105+
if (stack) {
106+
stack = stack.split(LF)
107+
var padding = repeat(' ', ret[ret.length - 1].length)
108+
ret = ret.concat(stack.map(function (s) {
109+
return padding + s
110+
}))
111+
}
112+
return this.format(ret.join(LF), 'cyan')
113+
}
114+
115+
Formatter.prototype.splitter = function (s) {
116+
var len = s.length
117+
var max = 80
118+
var left = max - len >> 1
119+
return this.format(
120+
repeat('-', left) + (s || '') + repeat('-', max - len - left) + LF,
121+
'yellow'
122+
)
123+
}
124+
125+
Formatter.prototype.test = function (test) {
126+
if (this.needProgress) {
127+
if (test.fail) {
128+
return this.format(symbols.cross + ' ' + test.title, 'cha.red.eraseLine')
129+
}
130+
return this.format(symbols.tick + ' ' + test.title, 'cha.green.eraseLine')
131+
}
132+
133+
if (test.fail) {
134+
return LF + this.format(symbols.cross + ' ' + test.title, 'cha.red')
135+
}
136+
return LF + this.format(symbols.tick + ' ' + test.title, 'cha.green')
137+
}
138+
139+
Formatter.prototype.format = function (str, formats) {
140+
if (!this.needAnsi) {
141+
return str
142+
}
143+
// format.cyan.underline.escape(str)
144+
return formats.split('.').reduce(function (f, c) {
145+
f = f[c]
146+
return f
147+
}, format).escape(str)
148+
}
149+
150+
function splitter(s) {
151+
var len = s.length
152+
var max = 80
153+
var left = max - len >> 1
154+
return repeat('-', left) + (s || '') + repeat('-', max - len - left) + LF
155+
}
156+
157+
function repeat(str, n) {
158+
if (str.repeat) {
159+
return str.repeat(n)
160+
}
161+
return (new Array(n + 1)).join(str)
162+
}
163+

0 commit comments

Comments
 (0)