summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/script/tests/browser_script_command_execute_throw.js
blob: 8680193ecbc3b58fcc6671707c063c0591b67d87 (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
70
71
72
73
74
75
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Testing evaluating thowing expressions
const {
  DevToolsServer,
} = require("resource://devtools/server/devtools-server.js");

add_task(async () => {
  const tab = await addTab(`data:text/html;charset=utf-8,Test throw`);
  const commands = await CommandsFactory.forTab(tab);
  await commands.targetCommand.startListening();

  const falsyValues = [
    "-0",
    "null",
    "undefined",
    "Infinity",
    "-Infinity",
    "NaN",
  ];
  for (const value of falsyValues) {
    const response = await commands.scriptCommand.execute(`throw ${value};`);
    is(
      response.exception.type,
      value,
      `Got the expected value for response.exception.type when throwing "${value}"`
    );
  }

  const identityTestValues = [false, 0];
  for (const value of identityTestValues) {
    const response = await commands.scriptCommand.execute(`throw ${value};`);
    is(
      response.exception,
      value,
      `Got the expected value for response.exception when throwing "${value}"`
    );
  }

  const symbolTestValues = [
    ["Symbol.iterator", "Symbol(Symbol.iterator)"],
    ["Symbol('foo')", "Symbol(foo)"],
    ["Symbol()", "Symbol()"],
  ];
  for (const [expr, message] of symbolTestValues) {
    const response = await commands.scriptCommand.execute(`throw ${expr};`);
    is(
      response.exceptionMessage,
      message,
      `Got the expected value for response.exceptionMessage when throwing "${expr}"`
    );
  }

  const longString = Array(DevToolsServer.LONG_STRING_LENGTH + 1).join("a"),
    shortedString = longString.substring(
      0,
      DevToolsServer.LONG_STRING_INITIAL_LENGTH
    );
  const response = await commands.scriptCommand.execute(
    "throw '" + longString + "';"
  );
  is(
    response.exception.initial,
    shortedString,
    "Got the expected value for exception.initial when throwing a longString"
  );
  is(
    response.exceptionMessage.initial,
    shortedString,
    "Got the expected value for exceptionMessage.initial when throwing a longString"
  );
});