This repository has been archived by the owner on Jun 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
162 lines (144 loc) · 4.01 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
/* eslint-env mocha */
'use strict'
const { assert } = require('chai')
const scrumpy = require('.')
test('interface is correct', () => {
assert.isFunction(scrumpy)
assert.lengthOf(scrumpy, 2)
})
test('finds match at root', () =>
assert.deepEqual(scrumpy({ foo: 'bar' }, { foo: 'bar' }), [ { foo: 'bar' } ])
)
test('finds match among many properties', () =>
assert.deepEqual(
scrumpy(
{ foo: 'bar', baz: 'qux', wibble: 'blee' },
{ baz: 'qux' }
),
[ { foo: 'bar', baz: 'qux', wibble: 'blee' } ]
)
)
test('finds match in descendants', () =>
assert.deepEqual(
scrumpy(
{ foo: { bar: { baz: 'qux' } } },
{ baz: 'qux' }
),
[ { baz: 'qux' } ]
)
)
test('finds match spanning generations', () =>
assert.deepEqual(
scrumpy(
{ foo: 'foo', bar: { bar: 'bar', baz: { baz: 'baz', qux: 'qux' } } },
{ baz: { qux: 'qux' } }
),
[ { bar: 'bar', baz: { baz: 'baz', qux: 'qux' } } ]
)
)
test('finds multiple matches', () =>
assert.deepEqual(
scrumpy(
{ foo: 'foo', bar: { bar: 'bar', baz: { baz: 'baz', qux: 'qux' } }, baz: { baz: { wibble: 'wibble', blee: 'blee', qux: 'qux' } } },
{ baz: { qux: 'qux' } }
),
[
{ bar: 'bar', baz: { baz: 'baz', qux: 'qux' } },
{ baz: { wibble: 'wibble', blee: 'blee', qux: 'qux' } }
]
)
)
test('finds a single root that has multiple dependent matches', () =>
assert.deepEqual(
scrumpy(
{ foo: 'foo', bar: { bar: 'bar', baz: { baz: 'baz', qux: 'qux' } }, baz: { wibble: 'wibble', blee: 'blee', qux: 'qux' } },
{ baz: { qux: 'qux' } }
),
[
{ foo: 'foo', bar: { bar: 'bar', baz: { baz: 'baz', qux: 'qux' } }, baz: { wibble: 'wibble', blee: 'blee', qux: 'qux' } }
]
)
)
test('does not find match in descendants when recursion is disabled', () =>
assert.deepEqual(
scrumpy(
{ foo: { bar: { baz: 'qux' } } },
{ baz: 'qux' },
{ recursive: false }
),
[]
)
)
test('finds matches in array descendants', () =>
assert.deepEqual(
scrumpy(
[ { foo: 'bar' }, { baz: 'qux', v: 1 }, { wibble: 'blee', glarg: { baz: 'qux', v: 2 } } ],
{ baz: 'qux' }
),
[ { baz: 'qux', v: 1 }, { baz: 'qux', v: 2 } ]
)
)
test('finds immediate match in array descendants when recursion is disabled but arrays are enabled', () =>
assert.deepEqual(
scrumpy(
[ { foo: 'bar' }, { baz: 'qux', v: 1 }, { wibble: 'blee', glarg: { baz: 'qux', v: 2 } } ],
{ baz: 'qux' },
{ recursive: false }
),
[ { baz: 'qux', v: 1 } ]
)
)
test('finds matches in array descendants when array is disabled but recursion is enabled', () =>
assert.deepEqual(
scrumpy(
[ { foo: 'bar' }, { baz: 'qux', v: 1 }, { wibble: 'blee', glarg: { baz: 'qux', v: 2 } } ],
{ baz: 'qux' },
{ array: false }
),
[ { baz: 'qux', v: 1 }, { baz: 'qux', v: 2 } ]
)
)
test('does not find match in array descendants when recursion and arrays are disabled', () =>
assert.deepEqual(
scrumpy(
[ { foo: 'bar' }, { baz: 'qux' }, { wibble: 'blee', glarg: { baz: 'qux' } } ],
{ baz: 'qux' },
{ recursive: false, array: false }
),
[]
)
)
test('finds matches in non-root array descendants', () =>
assert.deepEqual(
scrumpy(
{ array: [ { foo: 'bar' }, { baz: 'qux', v: 1 }, { wibble: 'blee', glarg: { baz: 'qux', v: 2 } } ] },
{ baz: 'qux' }
),
[ { baz: 'qux', v: 1 }, { baz: 'qux', v: 2 } ]
)
)
test('finds match in infinitely recursive trees', () => {
const recursive = {
foo: {
bar: {
baz: {
qux: 'qux'
}
}
}
}
recursive.foo.bar.qux = recursive
assert.deepEqual(scrumpy(recursive, { qux: 'qux' }), [ { qux: 'qux' } ])
})
test('finds first match', () =>
assert.deepEqual(
scrumpy(
{ foo: 'foo', bar: { bar: 'bar', baz: { baz: 'baz', qux: 'qux' } }, baz: { baz: { wibble: 'wibble', blee: 'blee', qux: 'qux' } } },
{ baz: { qux: 'qux' } },
{ all: false }
),
[
{ bar: 'bar', baz: { baz: 'baz', qux: 'qux' } }
]
)
)