/
test.js
164 lines (122 loc) · 3.84 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
import test from 'ava';
import combos from './src';
function createObjectFinder(t, objects) {
const map = {};
return {
assertHasObjectOnce(finder) {
const index = objects.findIndex(finder);
t.true(index >= 0);
t.falsy(map[index]);
map[index] = true;
},
assertAllChecked() {
t.is(Object.keys(map).length, objects.length);
},
};
}
test('generates all possible combinations', t => {
const result = combos({
foo: [true, false],
bar: [1, 0, combos.UNDEF],
baz: ['hello', 'world'],
});
const objectFinder = createObjectFinder(t, result);
t.is(result.length, 12);
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === true && bar === 1 && baz === 'hello'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === true && bar === 1 && baz === 'world'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === true && bar === 0 && baz === 'hello'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === true && bar === 0 && baz === 'world'
));
objectFinder.assertHasObjectOnce(o => (
o.foo === true && !o.hasOwnProperty('bar') && o.baz === 'hello'
));
objectFinder.assertHasObjectOnce(o => (
o.foo === true && !o.hasOwnProperty('bar') && o.baz === 'world'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === false && bar === 1 && baz === 'hello'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === false && bar === 1 && baz === 'world'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === false && bar === 0 && baz === 'hello'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === false && bar === 0 && baz === 'world'
));
objectFinder.assertHasObjectOnce(o => (
o.foo === false && !o.hasOwnProperty('bar') && o.baz === 'hello'
));
objectFinder.assertHasObjectOnce(o => (
o.foo === false && !o.hasOwnProperty('bar') && o.baz === 'world'
));
objectFinder.assertAllChecked();
});
test('generates all possible combinations, holding one constant', t => {
const result = combos({
foo: [true, false],
bar: [1, 0],
baz: ['hello'],
});
const objectFinder = createObjectFinder(t, result);
t.is(result.length, 4);
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === true && bar === 1 && baz === 'hello'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === true && bar === 0 && baz === 'hello'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === false && bar === 1 && baz === 'hello'
));
objectFinder.assertHasObjectOnce(({ foo, bar, baz }) => (
foo === false && bar === 0 && baz === 'hello'
));
objectFinder.assertAllChecked();
});
test('handles single key with one value', t => {
const result = combos({ foo: ['bar'] });
t.deepEqual(result, [{ foo: 'bar' }]);
});
test('handles single key with multiple values', t => {
const result = combos({ foo: ['bar', 'baz'] });
t.deepEqual(result, [
{ foo: 'bar' },
{ foo: 'baz' },
]);
});
test('throws without argument', t => {
t.throws(_ => combos());
});
test('throws with null', t => {
t.throws(_ => combos(null));
});
test('throws with empty object', t => {
t.throws(_ => combos({}));
});
test('throws with array', t => {
t.throws(_ => combos([]));
});
test('throws with non-object', t => {
t.throws(_ => combos(42));
});
test('throws with single key and no values', t => {
t.throws(_ => combos({ foo: null }));
});
test('throws with single key and empty array', t => {
t.throws(_ => combos({ foo: [] }));
});
test('throws when a key is missing values', t => {
t.throws(_ => combos({ foo: ['hello'], bar: null }));
});
test('throws when a key has an empty array of values', t => {
t.throws(_ => combos({ foo: ['hello'], bar: [] }));
});