ai_old/goja/builtin_weakmap_test.go

77 lines
1.3 KiB
Go
Raw Normal View History

2024-09-20 16:50:35 +08:00
package goja
import (
"testing"
)
func TestWeakMap(t *testing.T) {
vm := New()
_, err := vm.RunString(`
var m = new WeakMap();
var m1 = new WeakMap();
var key = {};
m.set(key, true);
m1.set(key, false);
if (!m.has(key)) {
throw new Error("has");
}
if (m.get(key) !== true) {
throw new Error("value does not match");
}
if (!m1.has(key)) {
throw new Error("has (m1)");
}
if (m1.get(key) !== false) {
throw new Error("m1 value does not match");
}
m.delete(key);
if (m.has(key)) {
throw new Error("m still has after delete");
}
if (!m1.has(key)) {
throw new Error("m1 does not have after delete from m");
}
`)
if err != nil {
t.Fatal(err)
}
}
func TestWeakMapGetAdderGetIteratorOrder(t *testing.T) {
const SCRIPT = `
let getterCalled = 0;
class M extends WeakMap {
get set() {
getterCalled++;
return null;
}
}
let getIteratorCalled = 0;
let iterable = {};
iterable[Symbol.iterator] = () => {
getIteratorCalled++
return {
next: 1
};
}
let thrown = false;
try {
new M(iterable);
} catch (e) {
if (e instanceof TypeError) {
thrown = true;
} else {
throw e;
}
}
thrown && getterCalled === 1 && getIteratorCalled === 0;
`
testScript(SCRIPT, valueTrue, t)
}