Modifed the test to verify that callbackfn is not called if the key cannot be held weakly.

This commit is contained in:
Jonas Haukenes 2025-09-14 18:22:58 +02:00 committed by Ms2ger
parent a5e69a1534
commit 557b748bff

View File

@ -13,29 +13,22 @@ info: |
... ...
features: [Symbol, WeakMap, upsert] features: [Symbol, WeakMap, upsert]
---*/ ---*/
var log = [];
var invalidKeys = [1, false, undefined, 'string', null];
var s = new WeakMap(); var s = new WeakMap();
assert.throws(TypeError, function() { for (let invalidKey of invalidKeys) {
s.getOrInsertComputed(1, () => 1); assert.throws(TypeError, function () {
}); s.getOrInsertComputed(invalidKey,
() => log.push(`Unexpected evaluation of callback function, key: ${typeof invalidKey}`));
}, `${typeof invalidKey} not allowed as WeakMap key`);
}
assert.throws(TypeError, function() { assert.throws(TypeError, function () {
s.getOrInsertComputed(false, () => 1); s.getOrInsertComputed(Symbol.for('registered symbol'),
}); () => log.push("Unexpected callback evaluation"));
assert.throws(TypeError, function() {
s.getOrInsertComputed(undefined, () => 1);
});
assert.throws(TypeError, function() {
s.getOrInsertComputed('string', () => 1);
});
assert.throws(TypeError, function() {
s.getOrInsertComputed(null, () => 1);
});
assert.throws(TypeError, function() {
s.getOrInsertComputed(Symbol.for('registered symbol'), () => 1);
}, 'Registered symbol not allowed as WeakMap key'); }, 'Registered symbol not allowed as WeakMap key');
assert.compareArray(log, []);