diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /devtools/shared/acorn/tests/xpcshell | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/shared/acorn/tests/xpcshell')
5 files changed, 200 insertions, 0 deletions
diff --git a/devtools/shared/acorn/tests/xpcshell/head_acorn.js b/devtools/shared/acorn/tests/xpcshell/head_acorn.js new file mode 100644 index 0000000000..11cece94a3 --- /dev/null +++ b/devtools/shared/acorn/tests/xpcshell/head_acorn.js @@ -0,0 +1,74 @@ +"use strict"; +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); + + +function isObject(value) { + return typeof value === "object" && value !== null; +} + +function intersect(a, b) { + const seen = new Set(a); + return b.filter(value => seen.has(value)); +} + +function checkEquivalentASTs(expected, actual, prop = []) { + info("Checking: " + prop.join(" ")); + + if (!isObject(expected)) { + return void Assert.equal(expected, actual); + } + + Assert.ok(isObject(actual)); + + if (Array.isArray(expected)) { + Assert.ok(Array.isArray(actual)); + Assert.equal(expected.length, actual.length); + for (let i = 0; i < expected.length; i++) { + checkEquivalentASTs(expected[i], actual[i], prop.concat(i)); + } + } else { + // We must intersect the keys since acorn and Reflect have different + // extraneous properties on their AST nodes. + const keys = intersect(Object.keys(expected), Object.keys(actual)); + for (let key of keys) { + checkEquivalentASTs(expected[key], actual[key], prop.concat(key)); + } + } +} + + +// Register a console listener, so console messages don't just disappear +// into the ether. +var errorCount = 0; +var listener = { + observe: function (aMessage) { + errorCount++; + try { + // If we've been given an nsIScriptError, then we can print out + // something nicely formatted, for tools like Emacs to pick up. + var scriptError = aMessage.QueryInterface(Ci.nsIScriptError); + dump(aMessage.sourceName + ":" + aMessage.lineNumber + ": " + + scriptErrorFlagsToKind(aMessage.flags) + ": " + + aMessage.errorMessage + "\n"); + var string = aMessage.errorMessage; + } catch (x) { + // Be a little paranoid with message, as the whole goal here is to lose + // no information. + try { + var string = "" + aMessage.message; + } catch (x) { + var string = "<error converting error message to string>"; + } + } + + // Ignored until they are fixed in bug 1242968. + if (string.includes("JavaScript Warning")) { + return; + } + + do_throw("head_acorn.js got console message: " + string + "\n"); + } +}; + +var consoleService = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService); +consoleService.registerListener(listener); diff --git a/devtools/shared/acorn/tests/xpcshell/test_import_acorn.js b/devtools/shared/acorn/tests/xpcshell/test_import_acorn.js new file mode 100644 index 0000000000..f585914d00 --- /dev/null +++ b/devtools/shared/acorn/tests/xpcshell/test_import_acorn.js @@ -0,0 +1,18 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Test that we can require acorn. + */ + +function run_test() { + const acorn = require("acorn/acorn"); + const acorn_loose = require("acorn/acorn_loose"); + const walk = require("acorn/util/walk"); + Assert.ok(isObject(acorn)); + Assert.ok(isObject(acorn_loose)); + Assert.ok(isObject(walk)); + Assert.equal(typeof acorn.parse, "function"); + Assert.equal(typeof acorn_loose.parse_dammit, "function"); + Assert.equal(typeof walk.simple, "function"); +} diff --git a/devtools/shared/acorn/tests/xpcshell/test_lenient_parser.js b/devtools/shared/acorn/tests/xpcshell/test_lenient_parser.js new file mode 100644 index 0000000000..e45d3f75c9 --- /dev/null +++ b/devtools/shared/acorn/tests/xpcshell/test_lenient_parser.js @@ -0,0 +1,62 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Test that acorn's lenient parser gives something usable. + */ + +const acorn_loose = require("acorn/acorn_loose"); + +function run_test() { + let actualAST = acorn_loose.parse_dammit("let x = 10", {}); + + info("Actual AST:"); + info(JSON.stringify(actualAST, null, 2)); + info("Expected AST:"); + info(JSON.stringify(expectedAST, null, 2)); + + checkEquivalentASTs(expectedAST, actualAST); +} + +const expectedAST = { + "type": "Program", + "start": 0, + "end": 10, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 3, + "expression": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "let" + } + }, + { + "type": "ExpressionStatement", + "start": 4, + "end": 10, + "expression": { + "type": "AssignmentExpression", + "start": 4, + "end": 10, + "operator": "=", + "left": { + "type": "Identifier", + "start": 4, + "end": 5, + "name": "x" + }, + "right": { + "type": "Literal", + "start": 8, + "end": 10, + "value": 10, + "raw": "10" + } + } + } + ] +}; diff --git a/devtools/shared/acorn/tests/xpcshell/test_same_ast.js b/devtools/shared/acorn/tests/xpcshell/test_same_ast.js new file mode 100644 index 0000000000..8e3166b7f3 --- /dev/null +++ b/devtools/shared/acorn/tests/xpcshell/test_same_ast.js @@ -0,0 +1,37 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Test that Reflect and acorn create the same AST for ES5. + */ + +const acorn = require("acorn/acorn"); +const { Reflect } = require("resource://gre/modules/reflect.jsm"); + +const testCode = "" + function main () { + function makeAcc(n) { + return function () { + return ++n; + }; + } + + var acc = makeAcc(10); + + for (var i = 0; i < 10; i++) { + acc(); + } + + console.log(acc()); +}; + +function run_test() { + const reflectAST = Reflect.parse(testCode); + const acornAST = acorn.parse(testCode); + + info("Reflect AST:"); + info(JSON.stringify(reflectAST, null, 2)); + info("acorn AST:"); + info(JSON.stringify(acornAST, null, 2)); + + checkEquivalentASTs(reflectAST, acornAST); +} diff --git a/devtools/shared/acorn/tests/xpcshell/xpcshell.ini b/devtools/shared/acorn/tests/xpcshell/xpcshell.ini new file mode 100644 index 0000000000..68197e2130 --- /dev/null +++ b/devtools/shared/acorn/tests/xpcshell/xpcshell.ini @@ -0,0 +1,9 @@ +[DEFAULT] +tags = devtools +head = head_acorn.js +firefox-appdir = browser +skip-if = toolkit == 'android' + +[test_import_acorn.js] +[test_same_ast.js] +[test_lenient_parser.js] |