Bug 1472291 - Refactor the column offset tests to use a simple helper. r=jorendorff draft
authorLogan Smyth <loganfsmyth@gmail.com>
Tue, 03 Jul 2018 14:45:32 -0700
changeset 813848 5ea75656b7e0c88ce5ef1fb45fa84c385df0805d
parent 813847 593a589d7d3fe79ba9489cea01effc50ae8df38e
child 813849 6b1e189ca4cce7463c63f4cfe23a44923a206e77
push id115020
push userbmo:loganfsmyth@gmail.com
push dateTue, 03 Jul 2018 22:59:25 +0000
reviewersjorendorff
bugs1472291
milestone63.0a1
Bug 1472291 - Refactor the column offset tests to use a simple helper. r=jorendorff MozReview-Commit-ID: 43xVzUTUxbx
js/src/jit-test/lib/assert-offset-columns.js
js/src/jit-test/tests/debug/Script-getAllColumnOffsets-01.js
js/src/jit-test/tests/debug/Script-getAllColumnOffsets-02.js
js/src/jit-test/tests/debug/Script-getAllColumnOffsets-03.js
js/src/jit-test/tests/debug/Script-getAllColumnOffsets-04.js
js/src/jit-test/tests/debug/Script-getAllColumnOffsets-05.js
js/src/jit-test/tests/debug/Script-getAllColumnOffsets-06.js
js/src/jit-test/tests/debug/Script-getAllColumnOffsets.js
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/lib/assert-offset-columns.js
@@ -0,0 +1,74 @@
+// Set breakpoints "everywhere" in a function, then call the function and check that
+// the breakpoints were added are at the expected columns, and the breakpoints
+// were executed in th expected order.
+//
+// `code` is a JS Script. The final line should define a function `f` to validate.
+// `expectedBpts` is a string of spaces and carets ('^'). Throws if we don't hit
+// breakpoints on exactly the columns indicated by the carets.
+// `expectedOrdering` is a string of integer indices for the offsets that are
+// executed, in the order that then are executed. Test code can also push
+// additional items into this string using items.push("!").
+function assertOffsetColumns(code, expectedBpts, expectedOrdering = null) {
+    if (expectedOrdering === null) {
+        // The default ordering simply runs the breakpoints in order.
+        expectedOrdering = Array.from(expectedBpts.match(/\^/g), (_, i) => i).join(" ");
+    }
+
+    // Define the function `f` in a new global.
+    const global = newGlobal();
+
+    const lines = code.split(/\r?\n|\r]/g);
+    const initCode = lines.slice(0, -1).join("\n");
+    const execCode = lines[lines.length - 1];
+
+    // Treat everything but the last line as initialization code.
+    global.eval(initCode);
+
+    // Run the test code itself.
+    global.eval(execCode);
+
+    // Allow some tests to append to a log that will show up in expected ordering.
+    const hits = global.hits = [];
+    const bpts = new Set();
+
+    // Set breakpoints everywhere and call the function.
+    const dbg = new Debugger;
+    const script = dbg.addDebuggee(global).makeDebuggeeValue(global.f).script;
+    for (const offset of script.getAllColumnOffsets()) {
+        assertEq(offset.lineNumber, 1);
+        assertEq(offset.columnNumber < execCode.length, true);
+        bpts.add(offset.columnNumber);
+
+        script.setBreakpoint(offset.offset, {
+            hit(frame) {
+                hits.push(offset.columnNumber);
+            },
+        });
+    }
+    global.f(3);
+
+    const actualBpts = Array.from(execCode, (_, i) => {
+        return bpts.has(i) ? "^" : " ";
+    }).join("");
+
+    if (actualBpts.trimEnd() !== expectedBpts.trimEnd()) {
+        throw new Error(`Assertion failed:
+                     code: ${execCode}
+            expected bpts: ${expectedBpts}
+              actual bpts: ${actualBpts}\n`);
+    }
+
+    const indexLookup = new Map(
+        Array.from(bpts).sort().map((col, i) => [col, i]));
+    const actualOrdering = hits
+        .map(item => typeof item === "number" ? indexLookup.get(item) : item)
+        .join(" ");
+
+    if (actualOrdering.trimEnd() !== expectedOrdering.trimEnd()) {
+        throw new Error(`Assertion failed:
+                     code: ${execCode}
+                     bpts: ${expectedBpts}
+           expected order: ${expectedOrdering}
+             actual order: ${actualOrdering}\n`);
+    }
+}
deleted file mode 100644
--- a/js/src/jit-test/tests/debug/Script-getAllColumnOffsets-01.js
+++ /dev/null
@@ -1,19 +0,0 @@
-// getColumnOffsets correctly places the various parts of a ForStatement.
-
-var global = newGlobal();
-Debugger(global).onDebuggerStatement = function (frame) {
-    var script = frame.eval("f").return.script;
-    script.getAllColumnOffsets().forEach(function (offset) {
-        script.setBreakpoint(offset.offset, {
-            hit: function (frame) {
-                assertEq(offset.lineNumber, 1);
-                global.log += offset.columnNumber + " ";
-            }
-        });
-    });
-};
-
-global.log = '';
-global.eval("function f(n) { for (var i = 0; i < n; ++i) log += '. '; log += '! '; } debugger;");
-global.f(3);
-assertEq(global.log, "25 32 44 . 39 32 44 . 39 32 44 . 39 32 57 ! 70 ");
deleted file mode 100644
--- a/js/src/jit-test/tests/debug/Script-getAllColumnOffsets-02.js
+++ /dev/null
@@ -1,21 +0,0 @@
-// getColumnOffsets correctly places multiple variable declarations.
-
-var global = newGlobal();
-Debugger(global).onDebuggerStatement = function (frame) {
-    var script = frame.eval("f").return.script;
-    script.getAllColumnOffsets().forEach(function (offset) {
-        script.setBreakpoint(offset.offset, {
-            hit: function (frame) {
-                assertEq(offset.lineNumber, 1);
-                global.log += offset.columnNumber + " ";
-            }
-        });
-    });
-};
-
-global.log = '';
-global.eval("function f(n){var w0,x1=3,y2=4,z3=9} debugger;");
-global.f(3);
-
-// Should have hit each variable declared.
-assertEq(global.log, "21 26 31 35 ");
deleted file mode 100644
--- a/js/src/jit-test/tests/debug/Script-getAllColumnOffsets-03.js
+++ /dev/null
@@ -1,20 +0,0 @@
-// getColumnOffsets correctly places comma separated expressions.
-
-var global = newGlobal();
-Debugger(global).onDebuggerStatement = function (frame) {
-    var script = frame.eval("f").return.script;
-    script.getAllColumnOffsets().forEach(function (offset) {
-        script.setBreakpoint(offset.offset, {
-            hit: function (frame) {
-                assertEq(offset.lineNumber, 1);
-                global.log += offset.columnNumber + " ";
-            }
-        });
-    });
-};
-
-global.log = '';
-global.eval("function f(n){print(n),print(n),print(n)} debugger;");
-global.f(3);
-// Should hit each call that was separated by commas.
-assertEq(global.log, "14 23 32 40 ");
deleted file mode 100644
--- a/js/src/jit-test/tests/debug/Script-getAllColumnOffsets-04.js
+++ /dev/null
@@ -1,20 +0,0 @@
-// getColumnOffsets correctly places object properties.
-
-var global = newGlobal();
-Debugger(global).onDebuggerStatement = function (frame) {
-    var script = frame.eval("f").return.script;
-    script.getAllColumnOffsets().forEach(function (offset) {
-        script.setBreakpoint(offset.offset, {
-            hit: function (frame) {
-                assertEq(offset.lineNumber, 1);
-                global.log += offset.columnNumber + " ";
-            }
-        });
-    });
-};
-
-global.log = '';
-global.eval("function f(n){var o={a:1,b:2,c:3}} debugger;");
-global.f(3);
-// Should hit each property in the object.
-assertEq(global.log, "18 21 25 29 33 ");
deleted file mode 100644
--- a/js/src/jit-test/tests/debug/Script-getAllColumnOffsets-05.js
+++ /dev/null
@@ -1,20 +0,0 @@
-// getColumnOffsets correctly places array properties.
-
-var global = newGlobal();
-Debugger(global).onDebuggerStatement = function (frame) {
-    var script = frame.eval("f").return.script;
-    script.getAllColumnOffsets().forEach(function (offset) {
-        script.setBreakpoint(offset.offset, {
-            hit: function (frame) {
-                assertEq(offset.lineNumber, 1);
-                global.log += offset.columnNumber + " ";
-            }
-        });
-    });
-};
-
-global.log = '';
-global.eval("function f(n){var a=[1,2,n]} debugger;");
-global.f(3);
-// Should hit each item in the array.
-assertEq(global.log, "18 21 23 25 27 ");
deleted file mode 100644
--- a/js/src/jit-test/tests/debug/Script-getAllColumnOffsets-06.js
+++ /dev/null
@@ -1,28 +0,0 @@
-// getColumnOffsets correctly places function calls.
-
-var global = newGlobal();
-Debugger(global).onDebuggerStatement = function (frame) {
-    var script = frame.eval("f").return.script;
-    script.getAllColumnOffsets().forEach(function (offset) {
-        script.setBreakpoint(offset.offset, {
-            hit: function (frame) {
-                assertEq(offset.lineNumber, 1);
-                global.log += offset.columnNumber + " ";
-            }
-        });
-    });
-};
-
-global.log = "";
-global.eval("function ppppp() { return 1; }");
-//                     1         2         3         4
-//           01234567890123456789012345678901234567890123456789
-global.eval("function f(){ 1 && ppppp(ppppp()) && new Error() } debugger;");
-global.f();
-
-// 14 - Enter the function body
-// 25 - Inner print()
-// 19 - Outer print()
-// 37 - new Error()
-// 49 - Exit the function body
-assertEq(global.log, "14 25 19 37 49 ");
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/debug/Script-getAllColumnOffsets.js
@@ -0,0 +1,42 @@
+load(libdir + "assert-offset-columns.js");
+
+// getColumnOffsets correctly places the various parts of a ForStatement.
+assertOffsetColumns(
+    "function f(n) { for (var i = 0; i < n; ++i) hits.push('.'); hits.push('!'); } debugger;",
+    "                         ^      ^      ^    ^               ^               ^          ",
+    "0 1 3 . 2 1 3 . 2 1 3 . 2 1 4 ! 5",
+);
+
+// getColumnOffsets correctly places multiple variable declarations.
+assertOffsetColumns(
+    "function f(n){var w0,x1=3,y2=4,z3=9} debugger;",
+    "                     ^    ^    ^   ^          ",
+);
+
+// getColumnOffsets correctly places comma separated expressions.
+assertOffsetColumns(
+    "function f(n){print(n),print(n),print(n)} debugger;",
+    "              ^        ^        ^       ^          ",
+);
+
+// getColumnOffsets correctly places object properties.
+assertOffsetColumns(
+    // Should hit each property in the object.
+    "function f(n){var o={a:1,b:2,c:3}} debugger;",
+    "                  ^  ^   ^   ^   ^          ",
+);
+
+// getColumnOffsets correctly places array properties.
+assertOffsetColumns(
+    // Should hit each item in the array.
+    "function f(n){var a=[1,2,n]} debugger;",
+    "                  ^  ^ ^ ^ ^          ",
+);
+
+// getColumnOffsets correctly places function calls.
+assertOffsetColumns(
+    "function ppppp() { return 1; }\n" +
+    "function f(){ 1 && ppppp(ppppp()) && new Error() } debugger;",
+    "              ^    ^     ^           ^           ^          ",
+    "0 2 1 3 4",
+);