test_get_command_and_arg.js (3639B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { 7 getCommandAndArgs, 8 } = require("resource://devtools/server/actors/webconsole/commands/parser.js"); 9 10 const testcases = [ 11 { input: ":help", expectedOutput: "help()" }, 12 { 13 input: ":screenshot --fullscreen", 14 expectedOutput: 'screenshot({"fullscreen":true})', 15 }, 16 { 17 input: ":screenshot --fullscreen true", 18 expectedOutput: 'screenshot({"fullscreen":true})', 19 }, 20 { input: ":screenshot ", expectedOutput: "screenshot()" }, 21 { 22 input: ":screenshot --dpr 0.5 --fullpage --chrome", 23 expectedOutput: 'screenshot({"dpr":0.5,"fullpage":true,"chrome":true})', 24 }, 25 { 26 input: ":screenshot 'filename'", 27 expectedOutput: 'screenshot({"filename":"filename"})', 28 }, 29 { 30 input: ":screenshot filename", 31 expectedOutput: 'screenshot({"filename":"filename"})', 32 }, 33 { 34 input: 35 ":screenshot --name 'filename' --name `filename` --name \"filename\"", 36 expectedOutput: 'screenshot({"name":["filename","filename","filename"]})', 37 }, 38 { 39 input: ":screenshot 'filename1' 'filename2' 'filename3'", 40 expectedOutput: 'screenshot({"filename":"filename1"})', 41 }, 42 { 43 input: ":screenshot --chrome --chrome", 44 expectedOutput: 'screenshot({"chrome":true})', 45 }, 46 { 47 input: ':screenshot "file name with spaces"', 48 expectedOutput: 'screenshot({"filename":"file name with spaces"})', 49 }, 50 { 51 input: ":screenshot 'filename1' --name 'filename2'", 52 expectedOutput: 'screenshot({"filename":"filename1","name":"filename2"})', 53 }, 54 { 55 input: ":screenshot --name 'filename1' 'filename2'", 56 expectedOutput: 'screenshot({"name":"filename1","filename":"filename2"})', 57 }, 58 { 59 input: ':screenshot "fo\\"o bar"', 60 expectedOutput: 'screenshot({"filename":"fo\\\\\\"o bar"})', 61 }, 62 { 63 input: ':screenshot "foo b\\"ar"', 64 expectedOutput: 'screenshot({"filename":"foo b\\\\\\"ar"})', 65 }, 66 ]; 67 68 const edgecases = [ 69 { input: ":", expectedError: /Missing a command name after ':'/ }, 70 { input: ":invalid", expectedError: /'invalid' is not a valid command/ }, 71 { 72 input: ":screenshot :help", 73 expectedError: 74 /Executing multiple commands in one evaluation is not supported/, 75 }, 76 { input: ":screenshot --", expectedError: /invalid flag/ }, 77 { 78 input: ':screenshot "fo"o bar', 79 expectedError: 80 /String has unescaped `"` in \["fo"o\.\.\.\], may miss a space between arguments/, 81 }, 82 { 83 input: ':screenshot "foo b"ar', 84 expectedError: 85 // eslint-disable-next-line max-len 86 /String has unescaped `"` in \["foo b"ar\.\.\.\], may miss a space between arguments/, 87 }, 88 { input: ": screenshot", expectedError: /Missing a command name after ':'/ }, 89 { 90 input: ':screenshot "file name', 91 expectedError: /String does not terminate/, 92 }, 93 { 94 input: ':screenshot "file name --clipboard', 95 expectedError: /String does not terminate before flag "clipboard"/, 96 }, 97 { 98 input: "::screenshot", 99 expectedError: /':screenshot' is not a valid command/, 100 }, 101 ]; 102 103 function formatArgs(args) { 104 return Object.keys(args).length ? JSON.stringify(args) : ""; 105 } 106 107 function run_test() { 108 testcases.forEach(testcase => { 109 const { command, args } = getCommandAndArgs(testcase.input); 110 const argsString = formatArgs(args); 111 Assert.equal(`${command}(${argsString})`, testcase.expectedOutput); 112 }); 113 114 edgecases.forEach(testcase => { 115 Assert.throws( 116 () => getCommandAndArgs(testcase.input), 117 testcase.expectedError, 118 `"${testcase.input}" should throw expected error` 119 ); 120 }); 121 }