|
| 1 | +/* Any copyright is dedicated to the Public Domain. |
| 2 | + * http://creativecommons.org/publicdomain/zero/1.0/ */ |
| 3 | + |
| 4 | +// Tests for stepping through Babel's compile output. |
| 5 | + |
| 6 | +async function breakpointSteps(dbg, fixture, { line, column }, steps) { |
| 7 | + const { selectors: { getBreakpoint, getBreakpoints }, getState } = dbg; |
| 8 | + |
| 9 | + const filename = `fixtures/${fixture}/input.js`; |
| 10 | + await waitForSources(dbg, filename); |
| 11 | + |
| 12 | + ok(true, "Original sources exist"); |
| 13 | + const source = findSource(dbg, filename); |
| 14 | + |
| 15 | + await selectSource(dbg, source); |
| 16 | + |
| 17 | + // Test that breakpoint is not off by a line. |
| 18 | + await addBreakpoint(dbg, source, line); |
| 19 | + |
| 20 | + is(getBreakpoints(getState()).size, 1, "One breakpoint exists"); |
| 21 | + ok( |
| 22 | + getBreakpoint(getState(), { sourceId: source.id, line, column }), |
| 23 | + "Breakpoint has correct line" |
| 24 | + ); |
| 25 | + |
| 26 | + const fnName = fixture.replace(/-([a-z])/g, (s, c) => c.toUpperCase()); |
| 27 | + |
| 28 | + const invokeResult = invokeInTab(fnName); |
| 29 | + |
| 30 | + let invokeFailed = await Promise.race([ |
| 31 | + waitForPaused(dbg), |
| 32 | + invokeResult.then(() => new Promise(() => {}), () => true) |
| 33 | + ]); |
| 34 | + |
| 35 | + if (invokeFailed) { |
| 36 | + return invokeResult; |
| 37 | + } |
| 38 | + |
| 39 | + assertPausedLocation(dbg); |
| 40 | + |
| 41 | + await removeBreakpoint(dbg, source.id, line, column); |
| 42 | + |
| 43 | + is(getBreakpoints(getState()).size, 0, "Breakpoint reverted"); |
| 44 | + |
| 45 | + await runSteps(dbg, source, steps); |
| 46 | + |
| 47 | + await resume(dbg); |
| 48 | + |
| 49 | + // If the invoke errored later somehow, capture here so the error is |
| 50 | + // reported nicely. |
| 51 | + await invokeResult; |
| 52 | + |
| 53 | + ok(true, `Ran tests for ${fixture} at line ${line} column ${column}`); |
| 54 | +} |
| 55 | + |
| 56 | +async function runSteps(dbg, source, steps) { |
| 57 | + const { selectors: { getVisibleSelectedFrame }, getState } = dbg; |
| 58 | + |
| 59 | + for (const [i, [type, position]] of steps.entries()) { |
| 60 | + switch (type) { |
| 61 | + case "stepOver": |
| 62 | + await stepOver(dbg); |
| 63 | + break; |
| 64 | + case "stepIn": |
| 65 | + await stepIn(dbg); |
| 66 | + break; |
| 67 | + default: |
| 68 | + throw new Error("Unknown stepping type"); |
| 69 | + } |
| 70 | + |
| 71 | + const { location } = getVisibleSelectedFrame(getState()); |
| 72 | + |
| 73 | + is(location.sourceId, source.id, `Step ${i} has correct sourceId`); |
| 74 | + is(location.line, position.line, `Step ${i} has correct line`); |
| 75 | + is(location.column, position.column, `Step ${i} has correct column`); |
| 76 | + |
| 77 | + assertPausedLocation(dbg); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +add_task(async function() { |
| 82 | + requestLongerTimeout(2); |
| 83 | + |
| 84 | + const dbg = await initDebugger("doc-babel.html"); |
| 85 | + |
| 86 | + await breakpointSteps(dbg, "step-over-for-of", { line: 4, column: 2 }, [ |
| 87 | + ["stepOver", { line: 6, column: 2 }], |
| 88 | + ["stepOver", { line: 7, column: 4 }], |
| 89 | + ["stepOver", { line: 6, column: 2 }], |
| 90 | + ["stepOver", { line: 7, column: 4 }], |
| 91 | + ["stepOver", { line: 6, column: 2 }], |
| 92 | + ["stepOver", { line: 10, column: 2 }] |
| 93 | + ]); |
| 94 | + |
| 95 | + // This codifies the current behavior, but stepping twice over the for |
| 96 | + // header isn't ideal. |
| 97 | + await breakpointSteps(dbg, "step-over-for-of-array", { line: 3, column: 2 }, [ |
| 98 | + ["stepOver", { line: 5, column: 2 }], |
| 99 | + ["stepOver", { line: 5, column: 7 }], |
| 100 | + ["stepOver", { line: 6, column: 4 }], |
| 101 | + ["stepOver", { line: 5, column: 2 }], |
| 102 | + ["stepOver", { line: 5, column: 7 }], |
| 103 | + ["stepOver", { line: 6, column: 4 }], |
| 104 | + ["stepOver", { line: 5, column: 2 }], |
| 105 | + ["stepOver", { line: 9, column: 2 }] |
| 106 | + ]); |
| 107 | + |
| 108 | + // The closure means it isn't actually possible to step into the for body, |
| 109 | + // and Babel doesn't map the _loop() call, so we step past it automatically. |
| 110 | + await breakpointSteps( |
| 111 | + dbg, |
| 112 | + "step-over-for-of-closure", |
| 113 | + { line: 6, column: 2 }, |
| 114 | + [ |
| 115 | + ["stepOver", { line: 8, column: 2 }], |
| 116 | + ["stepOver", { line: 12, column: 2 }] |
| 117 | + ] |
| 118 | + ); |
| 119 | + |
| 120 | + // Same as the previous, not possible to step into the body. The less |
| 121 | + // complicated array logic makes it possible to step into the header at least, |
| 122 | + // but this does end up double-visiting the for head. |
| 123 | + await breakpointSteps( |
| 124 | + dbg, |
| 125 | + "step-over-for-of-array-closure", |
| 126 | + { line: 3, column: 2 }, |
| 127 | + [ |
| 128 | + ["stepOver", { line: 5, column: 2 }], |
| 129 | + ["stepOver", { line: 5, column: 7 }], |
| 130 | + ["stepOver", { line: 5, column: 2 }], |
| 131 | + ["stepOver", { line: 5, column: 7 }], |
| 132 | + ["stepOver", { line: 5, column: 2 }], |
| 133 | + ["stepOver", { line: 9, column: 2 }] |
| 134 | + ] |
| 135 | + ); |
| 136 | + |
| 137 | + await breakpointSteps( |
| 138 | + dbg, |
| 139 | + "step-over-function-params", |
| 140 | + { line: 6, column: 2 }, |
| 141 | + [["stepOver", { line: 7, column: 2 }], ["stepIn", { line: 2, column: 2 }]] |
| 142 | + ); |
| 143 | + |
| 144 | + await breakpointSteps( |
| 145 | + dbg, |
| 146 | + "step-over-regenerator-await", |
| 147 | + { line: 2, column: 2 }, |
| 148 | + [ |
| 149 | + // Won't work until a fix to regenerator lands and we rebuild. |
| 150 | + // https://github.com/facebook/regenerator/issues/342 |
| 151 | + // ["stepOver", { line: 4, column: 2 }], |
| 152 | + ] |
| 153 | + ); |
| 154 | +}); |
0 commit comments