This repository has been archived by the owner on Mar 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhigher.js
252 lines (219 loc) · 7.1 KB
/
higher.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
var extended = require('./extended')
, elementary = require('./elementary')
, traverser = require('traverser')
, render = require('render')
, fail = require('./failure').fail
module.exports = {
every: every,
has: has,
apply: apply,
throws: throws,
path: path,
noop: noop,
atIndex: atIndex,
property: property,
hasKeys: hasKeys
}
function property (actual, property, value, message){
function explain(err, template) {
throw fail(err).explain(
'property:'+template
, { actual: actual
, property: property
, path: [property]
, assertion: value
}
, message
)
}
if(!actual[property] && value == null)
//checks that property is defined on actual, even if it is undefined (but not deleted)
explain(new Error(), 'property: {actual:render}{path:path} must exist')
//if value is a function, assume it is an assertion... apply it to actual[property]
if('function' == typeof value) {
try { value(actual[property]) } catch (err) {
explain(err, 'property: ({actual:render}){path:path} must pass {assertion:function}')
}
} else if (value != null) //else if value is exiting, check it's equal to actual[property]
try { elementary.equal(actual[property], value) } catch (err) {
explain(err, 'property: ({actual:render}){path:path} must equal {assertion:function}')
}
//if you want to assert a value is null or undefined,
//use .property(name,it.equal(null|undefined))
}
function path (actual, expected, assertion, message) {
var current = actual
, soFar = []
if('string' == typeof expected) expected = [expected] //allow a singe key as a string
if('function' != typeof assertion) message = assertion, assertion = noop
for ( var i in expected) {
var key = expected[i]
current = current[key]
soFar.push(key)
if(!(typeof current !== 'undefined')) // check if there actually is a property
throw fail (new Error()).explain(
'path: ({actual:render}){soFar:path} must exist, (is undefined), expected path: {path:path}'
, { actual: actual, soFar: soFar, path:expected}
, message
)
}
try {
assertion(current)
} catch (err) {
throw fail(err).explain(
'path: expected ({actual:render}){path:path} to pass {assertion:function}'
, { actual: actual
, path: expected
, assertion: assertion
, current: current }
, message
)
}
return current
}
function apply (actual, assertion, message) {
//catch and wrap in message
try {
assertion (actual)
} catch (err) {
throw fail(err).explain('apply: {actual:render} did not pass {assertion:function}', {
actual: actual,
expected: assertion,
assertion: assertion
}, message)
}
}
function noop () {}
function throws (actual, assertion, message) {
if('string' == typeof assertion) message = assertion, assertion= noop
if(!assertion) assertion = noop
try {
actual()
} catch (err) {
try {
return apply (err, assertion, message)
} catch (f) {
throw f.explain(
'throws: {actual} threw an exception that did not pass {assertion:function}'
, {actual: actual, assertion: assertion}
, message
)
}
}
throw fail().explain('throws: {actual} did not throw', {actual: actual, expected: assertion}, message)
}
function every (array, assertion, message){
try{
extended.isArray(array)
}catch(err){
throw fail(err).explain('every: {actual:render} must be an Array')
}
for(var i in array){
try {
assertion.call(null,array[i])
} catch (err) {
throw fail(err).explain(
'every: every[{index}] (== {actual:render}) must pass {assertion:function}, \n ({index} out of {every.length} have passed)'
, { index: i
, every: array
, assertion: assertion
, actual: array[i]
}
, message
)
}
}
}
function has(obj, props, message) {
var pathTo = []
//traverser has lots of functions, so it needs a longer stack trace.
var orig = Error.stackTraceLimit
Error.stackTraceLimit = orig + 20
if('object' !== typeof props)
return elementary.equal(obj, props)
try{
traverser(props, {leaf:leaf, branch: branch})
} catch (err){
err = fail(err).explain(
'has: ({actual:render}){path:path} must match {expected:render}){path:path}'
, { actual: obj
, expected: props
, path: pathTo
}
, message
)
Error.stackTraceLimit = orig
throw err
}
function leaf(p){
pathTo = p.path
var other = path(obj,p.path)
if('function' == typeof p.value){
p.value.call(p.value.parent,other)
}
else {
//since this is the leaf function, it cannot be an object.
elementary.equal(other,p.value)
}
}
function branch (p){
pathTo = p.path
var other = path(obj,p.path)
if('function' !== typeof p.value)
extended.complex(other)
p.each()
}
}
function atIndex (actual, relativeIndex, assertion, message) {
var index = relativeIndex < 0 ? actual.length + relativeIndex : relativeIndex
, minLength = Math.abs(relativeIndex)
;
function explain(err, template) {
throw fail(err).explain('atIndex: ' + template
, {actual: actual
, index: index
, relativeIndex: relativeIndex
, minLength: minLength
, assertion: assertion
}
, message)
}
if(!(actual))
explain(new Error(), '{actual:render} must not be null')
if(!(actual.length))
explain(new Error(), '{actual:render} must have length property')
if(!(actual.length > minLength))
explain(new Error(), '{actual:render}.length (== {actual.length}) must be greater than {minLength}')
if(!('function' == typeof assertion))
message = assertion, assertion == noop
try {
assertion(actual[index])
} catch (err) {
explain(err, '({actual:render})[{index}] must pass {assertion:function}')
}
}
function hasKeys (actual, keys, assertion, message) {
if('string' == typeof assertion) message = assertion, assertion= noop
if(!assertion) assertion = noop
function explain(err, template) {
throw fail(err).explain('hasKeys: ' + template,
{actual: actual, key: key, path: [key], keys: keys, assertion: assertion},
message)
}
keys.forEach (function (key) {
if('undefined' == typeof actual[key])
explain(new Error(),'{actual:render} must have key: {key:JSON}')
try {
assertion (actual[key])
} catch (err) {
explain('hasKeys: ({actual:render}){path:path} must pass: {assertion:function}')
}
})
}
/*
higher level assections that could be implemented:
* all(actual, assertions..., message) //all assertions must pass
* any(actual, assertions..., message) //at least one assertion must pass
//if index is negative, take index as length + index
refactor so that whereever it says 'assertion', if it's not a function : use equal.
*/