-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
127 lines (113 loc) · 3.7 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
/*!
* always-callback <https://github.com/tunnckoCore/always-callback>
*
* 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 test = require('assertit')
var alwaysCallback = require('./index')
test('always-callback:', function () {
test('should throw TypeError if not a function given', function (done) {
function fixture () {
alwaysCallback(12345)
}
test.throws(fixture, TypeError)
test.throws(fixture, /is-async-function expect a function/)
done()
})
test('should throw TypeError when not callback given to returned async fn', function (done) {
function fixture () {
var JSONParse = alwaysCallback(JSON.parse)
JSONParse('{"foo":"bar"}')
}
test.throws(fixture, TypeError)
test.throws(fixture, /async `fn` .* expect a callback/)
done()
})
test('should JSON.parse(obj) and pass result to callback', function (done) {
var JSONParse = alwaysCallback(JSON.parse)
JSONParse('{"foo":"bar"}', function (err, res) {
test.ifError(err)
test.deepEqual(res, {foo: 'bar'})
done()
})
})
test('should JSON.parse(obj) and pass error to callback', function (done) {
var JSONParse = alwaysCallback(JSON.parse)
JSONParse('foo~bar~baz', function (err, res) {
test.ifError(!err)
test.equal(err.message, 'Unexpected token o')
test.equal(res, undefined)
done()
})
})
test('should fs.readFileSync(fp, utf8) and pass result to callback', function (done) {
var readFile = alwaysCallback(fs.readFileSync)
readFile('./package.json', 'utf8', function (err, res) {
test.ifError(err)
test.ok(res.indexOf('tunnckoCore/always-callback') !== -1)
done()
})
})
test('should fs.readFileSync(fp, utf8) and pass error to callback', function (done) {
var readFile = alwaysCallback(fs.readFileSync)
readFile('./pacfsdfsdfdfkage.json', 'utf8', function (err, res) {
test.ifError(!err)
test.ok(err)
test.equal(err.code, 'ENOENT')
test.equal(err.path, './pacfsdfsdfdfkage.json')
test.equal(res, undefined)
if (err.syscall) {
test.equal(err.syscall, 'open')
}
done()
})
})
test('should fs.stat(fp) and pass result to callback', function (done) {
var readFile = alwaysCallback(fs.stat)
readFile('./package.json', function (err, res) {
test.ifError(err)
test.ok(res.isFile())
done()
})
})
test('should fs.stat(fp) and pass error to callback', function (done) {
var readFile = alwaysCallback(fs.stat)
readFile('./packasdfsdfge.json', function (err, res) {
test.ifError(!err)
test.ok(err)
test.equal(err.code, 'ENOENT')
test.equal(err.path, './packasdfsdfge.json')
test.equal(res, undefined)
if (err.syscall) {
test.equal(err.syscall, 'stat')
}
done()
})
})
test('should fs.readFile(fp, utf8) and pass result to callback', function (done) {
var readFile = alwaysCallback(fs.readFile)
readFile('./package.json', 'utf8', function (err, res) {
test.ifError(err)
test.ok(res.indexOf('tunnckoCore/always-callback') !== -1)
done()
})
})
test('should fs.readFile(fp, utf8) and pass error to callback', function (done) {
var readFile = alwaysCallback(fs.readFile)
readFile('./package1235678.json', 'utf8', function (err, res) {
test.ifError(!err)
test.ok(err)
test.equal(err.code, 'ENOENT')
test.equal(err.path, './package1235678.json')
test.equal(res, undefined)
if (err.syscall) {
test.equal(err.syscall, 'open')
}
done()
})
})
})