/
test.js
68 lines (61 loc) · 1.68 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var fs = require('fs')
var path = require('path')
var test = require('tape')
var linecount = require('./')
var lcPromised = require('./promise')
var getStream = function() {
return fs.createReadStream(path.join(__dirname, 'fixtures', '74.txt'))
}
// Callback-style, file
test('counts correct lines for file', function (t) {
linecount(path.join(__dirname, 'fixtures', '74.txt'), function (err, count) {
t.ifError(err)
t.equal(count, 74)
t.end()
})
})
test('errors on nonexistant file', function (t) {
linecount(path.join(__dirname, 'fixtures', 'nope.txt'), function (err, count) {
t.ok(err instanceof Error, 'should error')
t.equal(err.code, 'ENOENT')
t.end()
})
})
// Promise-style, file
test('counts correct lines for file (promise)', function (t) {
lcPromised(path.join(__dirname, 'fixtures', '74.txt')).then(function (count) {
t.equal(count, 74)
t.end()
}).catch(function (err) {
t.ifError(err)
t.end()
})
})
test('errors on nonexistant file (promise)', function (t) {
lcPromised(path.join(__dirname, 'fixtures', 'nope.txt')).then(function (count) {
t.fail('Should not succeed')
t.end()
}).catch(function (err) {
t.ok(err instanceof Error, 'should error')
t.equal(err.code, 'ENOENT')
t.end()
})
})
// Callback-style, stream
test('counts correct lines for file', function (t) {
linecount(getStream(), function (err, count) {
t.ifError(err)
t.equal(count, 74)
t.end()
})
})
// Promise-style, stream
test('counts correct lines for file (promise)', function (t) {
lcPromised(getStream()).then(function (count) {
t.equal(count, 74)
t.end()
}).catch(function (err) {
t.ifError(err)
t.end()
})
})