summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/test_get_command_and_arg.js
blob: b3f0ab8ec4454e1d3a91ddaf8013bccf6de51ccd (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const {
  getCommandAndArgs,
} = require("resource://devtools/server/actors/webconsole/commands/parser.js");

const testcases = [
  { input: ":help", expectedOutput: "help()" },
  {
    input: ":screenshot  --fullscreen",
    expectedOutput: 'screenshot({"fullscreen":true})',
  },
  {
    input: ":screenshot  --fullscreen true",
    expectedOutput: 'screenshot({"fullscreen":true})',
  },
  { input: ":screenshot  ", expectedOutput: "screenshot()" },
  {
    input: ":screenshot --dpr 0.5 --fullpage --chrome",
    expectedOutput: 'screenshot({"dpr":0.5,"fullpage":true,"chrome":true})',
  },
  {
    input: ":screenshot 'filename'",
    expectedOutput: 'screenshot({"filename":"filename"})',
  },
  {
    input: ":screenshot filename",
    expectedOutput: 'screenshot({"filename":"filename"})',
  },
  {
    input:
      ":screenshot --name 'filename' --name `filename` --name \"filename\"",
    expectedOutput: 'screenshot({"name":["filename","filename","filename"]})',
  },
  {
    input: ":screenshot 'filename1' 'filename2' 'filename3'",
    expectedOutput: 'screenshot({"filename":"filename1"})',
  },
  {
    input: ":screenshot --chrome --chrome",
    expectedOutput: 'screenshot({"chrome":true})',
  },
  {
    input: ':screenshot "file name with spaces"',
    expectedOutput: 'screenshot({"filename":"file name with spaces"})',
  },
  {
    input: ":screenshot 'filename1' --name 'filename2'",
    expectedOutput: 'screenshot({"filename":"filename1","name":"filename2"})',
  },
  {
    input: ":screenshot --name 'filename1' 'filename2'",
    expectedOutput: 'screenshot({"name":"filename1","filename":"filename2"})',
  },
  {
    input: ':screenshot "fo\\"o bar"',
    expectedOutput: 'screenshot({"filename":"fo\\\\\\"o bar"})',
  },
  {
    input: ':screenshot "foo b\\"ar"',
    expectedOutput: 'screenshot({"filename":"foo b\\\\\\"ar"})',
  },
];

const edgecases = [
  { input: ":", expectedError: /Missing a command name after ':'/ },
  { input: ":invalid", expectedError: /'invalid' is not a valid command/ },
  {
    input: ":screenshot :help",
    expectedError:
      /Executing multiple commands in one evaluation is not supported/,
  },
  { input: ":screenshot --", expectedError: /invalid flag/ },
  {
    input: ':screenshot "fo"o bar',
    expectedError:
      /String has unescaped `"` in \["fo"o\.\.\.\], may miss a space between arguments/,
  },
  {
    input: ':screenshot "foo b"ar',
    expectedError:
      // eslint-disable-next-line max-len
      /String has unescaped `"` in \["foo b"ar\.\.\.\], may miss a space between arguments/,
  },
  { input: ": screenshot", expectedError: /Missing a command name after ':'/ },
  {
    input: ':screenshot "file name',
    expectedError: /String does not terminate/,
  },
  {
    input: ':screenshot "file name --clipboard',
    expectedError: /String does not terminate before flag "clipboard"/,
  },
  {
    input: "::screenshot",
    expectedError: /':screenshot' is not a valid command/,
  },
];

function formatArgs(args) {
  return Object.keys(args).length ? JSON.stringify(args) : "";
}

function run_test() {
  testcases.forEach(testcase => {
    const { command, args } = getCommandAndArgs(testcase.input);
    const argsString = formatArgs(args);
    Assert.equal(`${command}(${argsString})`, testcase.expectedOutput);
  });

  edgecases.forEach(testcase => {
    Assert.throws(
      () => getCommandAndArgs(testcase.input),
      testcase.expectedError,
      `"${testcase.input}" should throw expected error`
    );
  });
}