-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
288 lines (244 loc) · 7.17 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
var apply = require('lie-apply')
var pocket = require('./')
var signature = require('./signature')
var tape = require('blue-tape')
function test (description, body) {
tape(description, function (t) {
return body.call(t, t, pocket())
})
}
test('.get', function (t, p) {
p.value('a', 1)
t.ok(p.get('a') instanceof Promise, '.get returns a Promise')
return join(
p.get('a').then(t.equal.bind(t, 1)),
p.get('b')
.then(t.fail.bind(t, 'Missing dep succeeded'))
.catch(t.pass.bind(t, 'missing dep returns error Promise'))
)
})
test('.get(name, callback)', function (t, p) {
p.value('a', 1)
return new Promise(function (resolve) {
p.get('a', function (err, value) {
t.equal(err, null)
t.equal(1, value)
resolve()
})
})
})
test('.get(name, callback) with error', function (t, p) {
return new Promise(function (resolve) {
p.get('a', function (err) {
t.ok(err)
t.equal(1, arguments.length)
resolve()
})
})
})
test('.run', function (t, p) {
p.value('something', 3)
return p.run(function (something) {
t.equal(something, 3)
})
})
test('.value', function (t, p) {
t.test('.value type checking', function (t) {
t.throws(function () { p.value(function () {}) },
'.value requires a name')
t.throws(function () { p.value(5) }, '.value is picky')
t.end()
})
t.test('.value takes an object', function (t) {
return p.pocket().value({
one: 1,
two: 2,
three: function (one, two) { return one + two }
}).run(function (one, two, three) {
t.equal(1, one)
t.equal(2, two)
t.equal(3, three)
})
})
})
test('.wrap', function (t, p) {
p.value('theValue', 3)
p.value('multiplier', 2)
p.wrap('theValue', function (multiplier, theValue) {
t.equal(multiplier, 2, 'unwrapped values are resolved')
t.ok(typeof theValue === 'function', 'wrapped value is a thunk')
theValue = theValue()
t.ok(typeof theValue.then === 'function', 'the thunk evaluates to a promise')
return theValue.then(function (theValue) {
t.equal(theValue, 3, 'the wrapped value resolves correctly')
return theValue * multiplier
})
})
return p.get('theValue').then(t.equal.bind(t, 6))
})
test('.wrap works on lazy values', function (t, p) {
p.value('theValue', function () { return 8 })
p.value('multiplier', function () { return 3 })
p.wrap('theValue', function (theValue, multiplier) {
t.equal(multiplier, 3)
t.ok(typeof theValue === 'function', 'wrapped value is a thunk')
theValue = theValue()
t.ok(typeof theValue.then === 'function', 'the thunk evaluates to a promise')
return theValue.then(function (theValue) {
t.equal(theValue, 8)
return theValue * multiplier
})
})
return p.get('theValue').then(t.equal.bind(t, 24))
})
test('.wrap can be stacked', function (t, p) {
p.value('theValue', function () { return 0 })
p.wrap('theValue', function (theValue) {
return theValue().then(function (v) { return v + 1 })
})
p.wrap('theValue', function (theValue) {
return theValue().then(function (v) { return v + 1 })
})
return p.get('theValue').then(t.equal.bind(t, 2))
})
test('.wrap validates arguments', function (t, p) {
p.value('theValue', 3)
p.value('multiplier', 2)
t.throws(function () {
p.wrap('theValue', 'tortilla')
}, 'wrapper function must be a function')
t.throws(function () {
p.wrap('someOtherValue', function () {})
}, 'wrapper must wrap a known name')
t.end()
})
test('.nodeValue', function (t, p) {
p.nodeValue('nodeStyle', function (callback) {
callback(null, 'Node Style')
})
p.nodeValue('nodeStyleError', function (callback) {
callback(new Error('node style callback error'))
})
t.plan(2)
var succeeds = p.run(function (nodeStyle) {
t.equals(nodeStyle, 'Node Style')
})
var fails = p.get('nodeStyleError').catch(function (error) {
t.equals(error.message, 'node style callback error')
})
return Promise.all([succeeds, fails])
})
test('.missingNames', function (t, p) {
p.value(function thing (a, b) { })
t.deepEquals(['a', 'b'], p.missingNames())
var child = p.pocket()
p.value('b', 2)
t.deepEquals(['a'], child.missingNames())
t.end()
})
test('.dependencyTree', function (t, parent) {
var child = parent.pocket()
parent.value('a', 'hello')
parent.value('b', function (a) {
return 'b(' + JSON.stringify(a) + ')'
})
child.value('c', function (b) {
return 'c(' + JSON.stringify(b) + ')'
})
child.value('d', function (thisIsMissing) {})
var expected = {}
expected.a = []
expected.b = ['a']
t.deepEqual(expected, parent.dependencies())
expected.c = ['b']
expected.d = ['thisismissing']
t.deepEqual(expected, child.dependencies())
// so code coverage doesn't complain
return child.get('c').then(t.equal.bind(t, 'c("b(\\"hello\\")")'))
})
test('value caching', function (t, p) {
var providerExecutionCount = 0
var myThing = {}
p.value(function getThing () {
providerExecutionCount++
return myThing
})
return apply(assertions, p.get('thing'), p.get('thing'))
function assertions (a, b) {
t.equal(a, b)
t.equal(providerExecutionCount, 1)
}
})
test('parent/child relationships', function (t, p) {
t.test('children can get deps from parent', function (t) {
p.value('four', 4)
p.value(function getFive () { return 5 })
var child = p.pocket()
child.value(function getTwenty (four, five) { return four * five })
return child.get('twenty').then(t.equal.bind(t, 20))
})
t.test('parent cannot retrieve deps from child', function (t) {
t.assert(!p.has('twenty'))
t.end()
})
})
test('grandchildren can get deps from their grandparents', function (t, gp) {
gp.value('five', function () { return 5 })
var parent = gp.pocket()
var child = parent.pocket()
t.ok(child.has('five'), 'Children "have" deps that grandparents "provide"')
return child.get('five').then(t.equal.bind(t, 5))
})
test('.alias', function (t, p) {
return p.pocket()
.value('src', 99)
.alias('al', 'src')
.get('al')
.then(t.equal.bind(t, 99))
})
test('.default', function (t, p) {
p.default('number', 1)
p.default('string', function () { return 'ok' })
p.default('replaceMe', 'fail')
p.value('replaceMe', 'ok')
return join(
p.get('number').then(t.equal.bind(t, 1)),
p.get('string').then(t.equal.bind(t, 'ok')),
p.get('replaceMe').then(t.equal.bind(t, 'ok'))
)
})
test('overwrite protection', function (t, p) {
var p1 = p.pocket()
p1.value('one', 1)
t.throws(function () {
p1.value(function one () {})
})
var p2 = p.pocket()
p2.value(function one () {})
t.throws(function () {
p1.value('one', 1)
})
var p3 = p.pocket()
p3.default(function one () {})
t.throws(function () {
p3.default('one', 1)
})
t.end()
})
test('signature parsing', function (t) {
t.throws(function () {
signature.parse('blah')
}, 'Can only parse functions')
t.end()
})
test('signature copying', function (t) {
function source (a, b, c) {}
function dest () {}
signature.copy(source, dest)
t.deepEqual(signature.parse(dest), ['a', 'b', 'c'])
t.end()
})
function join () {
var args = Array.prototype.slice.call(arguments)
return Promise.all(args)
}