browser_webconsole_output_trimmed.js (3134B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Tests that we trim start and end whitespace in user input 5 // in the messages list 6 7 "use strict"; 8 9 const TEST_URI = `http://example.com/browser/devtools/client/webconsole/test/browser/test-console.html`; 10 11 const TEST_ITEMS = [ 12 { 13 name: "Commands without whitespace are not affected by trimming", 14 command: "Math.PI==='3.14159'", 15 expected: "Math.PI==='3.14159'", 16 }, 17 { 18 name: "Trims whitespace before and after a command (single line case)", 19 command: "\t\t (window.o_O || {}) [' O_o '] ", 20 expected: "(window.o_O || {}) [' O_o ']", 21 }, 22 { 23 name: 24 "When trimming a whitespace before and after a command, " + 25 "it keeps indentation for each contentful line", 26 command: " \n \n 1,\n 2,\n 3\n \n ", 27 expected: " 1,\n 2,\n 3", 28 }, 29 { 30 name: 31 "When trimming a whitespace before and after a command, " + 32 "it keeps trailing whitespace for all lines except the last", 33 command: 34 "\n" + 35 " let numbers = [1,\n" + 36 " 2, \n" + 37 " 3];\n" + 38 " \n" + 39 " \n" + 40 " function addNumber() { \n" + 41 " numbers.push(numbers.length + 1);\n" + 42 " } \n" + 43 " ", 44 expected: 45 " let numbers = [1,\n" + 46 " 2, \n" + 47 " 3];\n" + 48 " \n" + 49 " \n" + 50 " function addNumber() { \n" + 51 " numbers.push(numbers.length + 1);\n" + 52 " }", 53 }, 54 ]; 55 56 add_task(async function () { 57 const hud = await openNewTabAndConsole(TEST_URI); 58 59 // Check that expected output and actual trimmed output match 60 for (const { name, command, expected } of TEST_ITEMS) { 61 await clearOutput(hud); 62 await executeAndWaitForResultMessage(hud, command, ""); 63 64 const result = await getActualDisplayedInput(hud); 65 66 if (result === expected) { 67 ok(true, name); 68 } else { 69 ok(false, formatError(name, result, expected)); 70 } 71 } 72 }); 73 74 /** 75 * Get the text content of the latest command logged in the console 76 * 77 * @param {WebConsole} hud: The webconsole 78 * @return {string|null} 79 */ 80 async function getActualDisplayedInput(hud) { 81 const message = Array.from( 82 hud.ui.outputNode.querySelectorAll(".message.command") 83 ).pop(); 84 85 if (message) { 86 // Open the message if its collapsed 87 const toggleArrow = message.querySelector(".collapse-button"); 88 if (toggleArrow) { 89 toggleArrow.click(); 90 await waitFor(() => message.classList.contains("open") === true); 91 } 92 93 return message.querySelector("syntax-highlighted").textContent; 94 } 95 96 return null; 97 } 98 99 /** 100 * Format a "Got vs Expected" error message on multiple lines, 101 * making whitespace more visible in console output. 102 */ 103 function formatError(name, result, expected) { 104 const quote = str => 105 typeof str === "string" 106 ? "> " + str.replace(/ /g, "\u{B7}").replace(/\n/g, "\n> ") 107 : str; 108 109 return `${name}\nGot:\n${quote(result)}\nExpected:\n${quote(expected)}\n`; 110 }