summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/xpcshell/test_WeakMapMap.js
blob: 94a006265b9d00b1d443c1713acbc2f069863af3 (plain)
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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test WeakMapMap.

"use strict";

const WeakMapMap = require("resource://devtools/client/shared/WeakMapMap.js");

const myWeakMapMap = new WeakMapMap();
const key = { randomObject: true };

// eslint-disable-next-line
function run_test() {
  test_set();
  test_has();
  test_get();
  test_delete();
  test_clear();
}

function test_set() {
  // myWeakMapMap.set
  myWeakMapMap.set(key, "text1", "value1");
  myWeakMapMap.set(key, "text2", "value2");
  myWeakMapMap.set(key, "text3", "value3");
}

function test_has() {
  // myWeakMapMap.has
  ok(myWeakMapMap.has(key, "text1"), "text1 exists");
  ok(myWeakMapMap.has(key, "text2"), "text2 exists");
  ok(myWeakMapMap.has(key, "text3"), "text3 exists");
  ok(!myWeakMapMap.has(key, "notakey"), "notakey does not exist");
}

function test_get() {
  // myWeakMapMap.get
  const value1 = myWeakMapMap.get(key, "text1");
  equal(value1, "value1", "test value1");

  const value2 = myWeakMapMap.get(key, "text2");
  equal(value2, "value2", "test value2");

  const value3 = myWeakMapMap.get(key, "text3");
  equal(value3, "value3", "test value3");

  const value4 = myWeakMapMap.get(key, "notakey");
  equal(value4, undefined, "test value4");
}

function test_delete() {
  // myWeakMapMap.delete
  myWeakMapMap.delete(key, "text2");

  // Check that the correct entry was deleted
  ok(myWeakMapMap.has(key, "text1"), "text1 exists");
  ok(!myWeakMapMap.has(key, "text2"), "text2 no longer exists");
  ok(myWeakMapMap.has(key, "text3"), "text3 exists");
}

function test_clear() {
  // myWeakMapMap.clear
  myWeakMapMap.clear();

  // Ensure myWeakMapMap was properly cleared
  ok(!myWeakMapMap.has(key, "text1"), "text1 no longer exists");
  ok(!myWeakMapMap.has(key, "text3"), "text3 no longer exists");
}