-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
158 lines (143 loc) · 4.77 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*!
* callback2stream <https://github.com/hybridables/callback2stream>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var fs = require('fs')
var test = require('mukla')
var callback2stream = require('./index')
var isBuffer = require('is-buffer')
test('should always return a function', function (done) {
test.strictEqual(typeof callback2stream(), 'function')
test.strictEqual(typeof callback2stream(123), 'function')
test.strictEqual(typeof callback2stream('abc'), 'function')
done()
})
test('should create stream from async function (fs.readFile)', function (done) {
var readFile = callback2stream(fs.readFile)
var stream = readFile('./package.json', 'utf8')
stream
.on('data', function (val) {
test.strictEqual(typeof val, 'string')
test.strictEqual(val, fs.readFileSync('./package.json', 'utf8'))
test.ok(val.indexOf('"license":"MIT"'))
})
.once('error', done)
.once('end', done)
})
test('should create stream with buffer contents', function (done) {
var readBuffer = callback2stream(fs.readFile)
var stream = readBuffer('./package.json')
stream
.on('data', function (val) {
test.strictEqual(isBuffer(val), true)
test.ok(val.toString('utf8').indexOf('"license":"MIT"'))
})
.once('error', done)
.once('end', done)
})
test('should get fs.stat stream', function (done) {
var statFile = callback2stream(fs.stat)
statFile('./package.json')
.on('data', function (val) {
test.strictEqual(typeof val, 'object')
test.ok(val.mtime)
test.ok(val.size)
})
.once('error', done)
.once('end', done)
})
test('should work for sync functions (fs.readFileSync)', function (done) {
var createReadStream = callback2stream(fs.readFileSync)
createReadStream('./package.json', 'utf8')
.on('data', function (val) {
test.strictEqual(typeof val, 'string')
test.ok(val.indexOf('"license":"MIT"'))
})
.once('error', done)
.once('end', done)
})
test('should work for natives like JSON.parse', function (done) {
var JSONParseStream = callback2stream(JSON.parse)
JSONParseStream('{"foo":"bar"}')
.on('data', function (val) {
test.strictEqual(typeof val, 'object')
test.strictEqual(val.foo, 'bar')
})
.once('error', done)
.once('end', done)
})
test('should work for natives like JSON.stringify', function (done) {
var JSONStringify = callback2stream(JSON.stringify)
JSONStringify({ a: 'b', c: 'd' })
.on('data', function (val) {
test.strictEqual(typeof val, 'string')
test.strictEqual(val, '{"a":"b","c":"d"}')
})
.once('error', done)
.once('end', done)
})
test('should work for JSON.stringify with identation', function (done) {
var stringifyStream = callback2stream(JSON.stringify)
stringifyStream({ bar: 'qux', xxx: 'yyy' }, null, 2)
.on('data', function (val) {
test.strictEqual(typeof val, 'string')
test.strictEqual(val, '{\n "bar": "qux",\n "xxx": "yyy"\n}')
})
.once('error', done)
.once('end', done)
})
test('should handle async errors correctly (fs.readFile ENOENT)', function (done) {
var readFileError = callback2stream(fs.readFile)
readFileError('not exist', 'utf8')
.once('error', function (err) {
test.ifError(!err)
test.strictEqual(err instanceof Error, true)
test.strictEqual(err.code, 'ENOENT')
done()
})
})
test('should handle sync errors correctly (fs.readFileSync ENOENT)', function (done) {
var syncError = callback2stream(fs.readFileSync)
syncError('not exist', 'utf8').once('error', function (err) {
test.strictEqual(err instanceof Error, true)
test.strictEqual(/no such file or directory, open/.test(err.message), true)
done()
})
})
test('should handle json parse error', function (done) {
var parseStream = callback2stream(JSON.parse)
parseStream('{foo: bar qux, invalid')
.once('error', function (err) {
test.ifError(!err)
test.strictEqual(err instanceof Error, true)
test.strictEqual(err.name, 'SyntaxError')
test.strictEqual(err.message, 'Unexpected token f')
done()
})
})
test('should work for successful generator function', function (done) {
var generatorStream = callback2stream(function * (a, b) {
var val = yield [a + 111]
return yield [val[0] + b]
})
generatorStream(444, 222)
.on('data', function (val) {
test.deepEqual(val, [777])
})
.once('error', done)
.once('end', done)
})
test('should handle error from generator function', function (done) {
var genStream = callback2stream(function * (foo) {
throw new Error('msg ' + foo)
})
genStream('hi')
.once('error', function (err) {
test.ifError(!err)
test.strictEqual(err.message, 'msg hi')
done()
})
})