-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
166 lines (138 loc) · 4.57 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
159
160
161
162
163
164
165
166
/*!
* always-promise <https://github.com/hybridables/always-promise>
*
* Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var fs = require('fs')
var semver = require('semver')
var test = require('assertit')
var isBuffer = require('is-buffer')
var alwaysPromise = require('./index')
function multipleArgs (one, two, three, callback) {
callback(null, one, two, three)
}
test('should throw TypeError if not a function given', function (done) {
function fixture () {
alwaysPromise(12345)()
}
test.throws(fixture, TypeError)
test.throws(fixture, /merz: expect `val` to be promise, stream, child process or sync, async, generator function/)
done()
})
test('alwaysPromise(JSON.parse)(\'{"foo":"bar"}\').then(res)', function (done) {
var JSONParse = alwaysPromise(JSON.parse)
JSONParse('{"foo":"bar"}')
.then(function (res) {
test.deepEqual(res, {foo: 'bar'})
done()
}, done)
})
test('alwaysPromise(JSON.parse)("foo~bar~baz").catch(err)', function (done) {
var JSONParse = alwaysPromise(JSON.parse)
JSONParse('foo~bar~baz')
.catch(function (err) {
test.ifError(!err)
test.strictEqual(err.message, 'Unexpected token o')
done()
})
})
test('alwaysPromise(fs.readFileSync)("./package.json", "utf8").then(res)', function (done) {
var readFile = alwaysPromise(fs.readFileSync)
readFile('./package.json', 'utf8')
.then(function (res) {
test.ok(res.indexOf('"license": "MIT"') !== -1)
done()
}, done)
})
test('alwaysPromise(fs.readFileSync)("./abc123.json", "utf8").catch(err)', function (done) {
var readFile = alwaysPromise(fs.readFileSync)
readFile('./pacfsdfsdfdfkage.json', 'utf8')
.catch(function (err) {
test.ifError(!err)
test.ok(err)
test.strictEqual(err.code, 'ENOENT')
test.strictEqual(err.path, './pacfsdfsdfdfkage.json')
if (err.syscall) {
test.strictEqual(err.syscall, 'open')
}
done()
})
})
test('alwaysPromise(fs.stat)("./package.json").then(res)', function (done) {
var readFile = alwaysPromise(fs.stat)
readFile('./package.json').then(function (stats) {
test.strictEqual(stats.isFile(), true)
done()
}, done)
})
test('alwaysPromise(fs.stat)("./packasdfsdfge.json").catch(err)', function (done) {
var readFile = alwaysPromise(fs.stat)
readFile('./packasdfsdfge.json')
.catch(function (err) {
test.ifError(!err)
test.strictEqual(err.code, 'ENOENT')
test.strictEqual(err.path, './packasdfsdfge.json')
if (err.syscall) {
test.strictEqual(err.syscall, 'stat')
}
done()
})
})
test('should use `promise` for promisify if native not available', function (done) {
var statFile = alwaysPromise(fs.stat, require('promise'))
var promise = statFile('package.json')
promise.then(function (stats) {
test.strictEqual(stats.isFile(), true)
if (semver.lt(process.version, '0.11.13')) {
test.strictEqual(promise.___customPromise, true)
}
done()
}, done)
})
test('should use `bluebird` if not `Prom` given and native not available', function (done) {
var statFile = alwaysPromise(fs.readFileSync)
var promise = statFile('package.json', 'utf-8')
promise.then(function (res) {
test.ok(res.indexOf('"license": "MIT"') !== -1)
if (semver.lt(process.version, '0.11.13')) {
test.strictEqual(promise.___bluebirdPromise, true)
}
done()
}, done)
})
test('should flatten multiple arguments by default', function (done) {
var fn = alwaysPromise(multipleArgs)
fn(11, 22, 33).then(function (res) {
test.deepEqual(res, [11, 22, 33])
done()
}, done)
})
test('should promisify with promise module given in `alwaysPromise.promise`', function (done) {
alwaysPromise.promise = require('promise')
var readFile = alwaysPromise(fs.readFile)
var promise = readFile('package.json')
promise.then(function (res) {
test.strictEqual(isBuffer(res), true)
if (semver.lt(process.version, '0.11.13')) {
test.strictEqual(promise.___customPromise, true)
test.strictEqual(promise.Prome.___customPromise, true)
}
done()
}, done)
})
test('should promisify with promise module given in `promisifiedFn.promise`', function (done) {
var readFile = alwaysPromise(fs.readFile)
readFile.promise = require('promise')
var promise = readFile('package.json')
promise.then(function (res) {
test.strictEqual(isBuffer(res), true)
if (semver.lt(process.version, '0.11.13')) {
test.strictEqual(promise.___customPromise, true)
test.strictEqual(promise.Prome.___customPromise, true)
}
done()
}, done)
})