-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathtest-copyMap.js
209 lines (195 loc) · 5.18 KB
/
test-copyMap.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
import { test } from './prepare-test-env-ava.js';
// eslint-disable-next-line import/order
import { makeTagged, getTag, passStyleOf } from '@endo/marshal';
import {
isCopyMap,
assertCopyMap,
makeCopyMap,
getCopyMapEntries,
getCopyMapEntryArray,
} from '../src/keys/checkKey.js';
import { matches } from '../src/patterns/patternMatchers.js';
import '../src/types.js';
const { Fail } = assert;
const assertIsCopyMap = (t, m) => {
t.is(passStyleOf(m), 'tagged');
t.is(getTag(m), 'copyMap');
t.notThrows(() => assertCopyMap(m));
t.true(isCopyMap(m));
};
const assertIsInvalidCopyMap = (t, m, message) => {
t.is(passStyleOf(m), 'tagged');
t.is(getTag(m), 'copyMap');
t.throws(() => assertCopyMap(m), { message });
t.is(isCopyMap(m), false);
};
test('makeCopyMap', t => {
// @ts-ignore Mixed-type values
const m = makeCopyMap([
['z', undefined],
['a', null],
['b', true],
['c', 4],
['d', 5n],
['e', 'foo'],
['f', 'bar'],
['g', 'baz'],
]);
assertIsCopyMap(t, m);
t.deepEqual(
[...getCopyMapEntries(m)],
[
['z', undefined],
['g', 'baz'],
['f', 'bar'],
['e', 'foo'],
['d', 5n],
['c', 4],
['b', true],
['a', null],
],
'entries are reverse-sorted by key',
);
});
test('backwards-compatible static shape', t => {
// @ts-ignore Mixed-type values
const sortedEntries = new Map([
['z', undefined],
['g', 'baz'],
['f', 'bar'],
['e', 'foo'],
['d', 5n],
['c', 4],
['b', true],
['a', null],
]);
const manualMap = harden(
Object.defineProperties(
{},
{
[Symbol.for('passStyle')]: { value: 'tagged' },
[Symbol.toStringTag]: { value: 'copyMap' },
payload: {
enumerable: true,
value: {
keys: [...sortedEntries.keys()],
values: [...sortedEntries.values()],
},
},
},
),
);
assertIsCopyMap(t, manualMap);
const maps = {
makeTagged: makeTagged('copyMap', {
keys: [...sortedEntries.keys()],
values: [...sortedEntries.values()],
}),
makeCopyMap: makeCopyMap([...sortedEntries].reverse()),
};
for (const [label, m] of Object.entries(maps)) {
t.deepEqual(
Object.getOwnPropertyDescriptors(m),
Object.getOwnPropertyDescriptors(manualMap),
label,
);
}
});
test('key uniqueness', t => {
t.throws(
() =>
makeCopyMap([
['a', 1n],
['a', 1n],
]),
{ message: /value has duplicate keys/ },
);
assertIsInvalidCopyMap(
t,
makeTagged('copyMap', { keys: ['a', 'a'], values: [1n, 1n] }),
/value has duplicate keys/,
);
});
// TODO: incorporate fast-check for property-based testing that construction
// reverse rank sorts keys and validation rejects any other key order.
test('getCopyMapEntries', t => {
const m = makeCopyMap([
['x', 8],
['y', 7],
]);
t.deepEqual([...getCopyMapEntries(m)], getCopyMapEntryArray(m));
const entriesIterable = getCopyMapEntries(m);
t.is(passStyleOf(entriesIterable), 'remotable');
const entriesIterator = entriesIterable[Symbol.iterator]();
t.is(passStyleOf(entriesIterator), 'remotable');
const iterResult = entriesIterator.next();
t.is(passStyleOf(iterResult), 'copyRecord');
});
test('matching', t => {
// TODO CopyMap matching depends upon comparison, the semantics for which have
// not yet been decided.
// See https://github.com/endojs/endo/pull/1737#pullrequestreview-1596595411
try {
matches(makeCopyMap([]), makeCopyMap([])) || Fail`Unexpected match failure`;
t.fail('CopyMap comparison support (time to test unconditionally?)');
} catch (err) {
// no CopyMap comparison support
t.pass();
return;
}
const copyMap = makeCopyMap([
['z', null],
['a', undefined],
]);
const missingKey = makeCopyMap([['z', null]]);
const extraKey = makeCopyMap([
['z', null],
['m', 'foo'],
['a', undefined],
]);
const differentValue = makeCopyMap([
['z', null],
['a', null],
]);
const assertNoMatch = maps => {
const [[label1, map1], [label2, map2]] = Object.entries(maps);
t.is(
matches(map1, map2),
false,
`${label1} specimen must not be matched by ${label2} pattern`,
);
t.is(
matches(map2, map1),
false,
`${label2} specimen must not be matched by ${label1} pattern`,
);
};
assertNoMatch({
'non-empty': copyMap,
empty: makeCopyMap([]),
});
assertNoMatch({ copyMap, missingKey });
assertNoMatch({ copyMap, extraKey });
assertNoMatch({ copyMap, differentValue });
const tagEntries = entries =>
makeTagged('copyMap', {
keys: entries.map(entry => entry[0]),
values: entries.map(entry => entry[1]),
});
t.true(matches(copyMap, copyMap), 'matches itself');
t.true(
matches(copyMap, tagEntries([...getCopyMapEntries(copyMap)])),
'matches a manually-cloned pattern',
);
t.throws(
() =>
matches(copyMap, tagEntries([...getCopyMapEntries(copyMap)].reverse())),
{ message: /pattern expected/ },
'key-reversed pattern is rejected',
);
t.is(
matches(tagEntries([...getCopyMapEntries(copyMap)].reverse()), copyMap),
false,
"key-reversed specimen doesn't match",
);
});