github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/builtin_weakmap_test.go (about)

     1  package goja
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestWeakMap(t *testing.T) {
     8  	vm := New()
     9  	_, err := vm.RunString(`
    10  	var m = new WeakMap();
    11  	var m1 = new WeakMap();
    12  	var key = {};
    13  	m.set(key, true);
    14  	m1.set(key, false);
    15  	if (!m.has(key)) {
    16  		throw new Error("has");
    17  	}
    18  	if (m.get(key) !== true) {
    19  		throw new Error("value does not match");
    20  	}
    21  	if (!m1.has(key)) {
    22  		throw new Error("has (m1)");
    23  	}
    24  	if (m1.get(key) !== false) {
    25  		throw new Error("m1 value does not match");
    26  	}
    27  	m.delete(key);
    28  	if (m.has(key)) {
    29  		throw new Error("m still has after delete");
    30  	}
    31  	if (!m1.has(key)) {
    32  		throw new Error("m1 does not have after delete from m");
    33  	}
    34  	`)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  }
    39  
    40  func TestWeakMapGetAdderGetIteratorOrder(t *testing.T) {
    41  	const SCRIPT = `
    42  	let getterCalled = 0;
    43  
    44  	class M extends WeakMap {
    45  	    get set() {
    46  	        getterCalled++;
    47  	        return null;
    48  	    }
    49  	}
    50  
    51  	let getIteratorCalled = 0;
    52  
    53  	let iterable = {};
    54  	iterable[Symbol.iterator] = () => {
    55  	    getIteratorCalled++
    56  	    return {
    57  	        next: 1
    58  	    };
    59  	}
    60  
    61  	let thrown = false;
    62  
    63  	try {
    64  	    new M(iterable);
    65  	} catch (e) {
    66  	    if (e instanceof TypeError) {
    67  	        thrown = true;
    68  	    } else {
    69  	        throw e;
    70  	    }
    71  	}
    72  
    73  	thrown && getterCalled === 1 && getIteratorCalled === 0;
    74  	`
    75  	testScript(SCRIPT, valueTrue, t)
    76  }