electron/spec/fixtures/module/inspector-binding.js

77 lines
2 KiB
JavaScript
Raw Normal View History

const inspector = require('node:inspector');
const path = require('node:path');
const { pathToFileURL } = require('node:url');
2017-11-06 14:25:48 +00:00
// This test case will set a breakpoint 4 lines below
function debuggedFunction () {
2020-03-20 20:28:31 +00:00
let i;
let accum = 0;
2017-11-06 14:25:48 +00:00
for (i = 0; i < 5; i++) {
2020-03-20 20:28:31 +00:00
accum += i;
2017-11-06 14:25:48 +00:00
}
2020-03-20 20:28:31 +00:00
return accum;
2017-11-06 14:25:48 +00:00
}
2020-03-20 20:28:31 +00:00
let scopeCallback = null;
2017-11-06 14:25:48 +00:00
function checkScope (session, scopeId) {
session.post('Runtime.getProperties', {
objectId: scopeId,
ownProperties: false,
accessorPropertiesOnly: false,
generatePreview: true
2020-03-20 20:28:31 +00:00
}, scopeCallback);
2017-11-06 14:25:48 +00:00
}
function debuggerPausedCallback (session, notification) {
2020-03-20 20:28:31 +00:00
const params = notification.params;
const callFrame = params.callFrames[0];
const scopeId = callFrame.scopeChain[0].object.objectId;
checkScope(session, scopeId);
2017-11-06 14:25:48 +00:00
}
function testSampleDebugSession () {
2020-03-20 20:28:31 +00:00
let cur = 0;
const failures = [];
2017-11-06 14:25:48 +00:00
const expects = {
i: [0, 1, 2, 3, 4],
accum: [0, 0, 1, 3, 6]
2020-03-20 20:28:31 +00:00
};
2017-11-06 14:25:48 +00:00
scopeCallback = function (error, result) {
2020-03-20 20:28:31 +00:00
if (error) failures.push(error);
const i = cur++;
let v, actual, expected;
for (v of result.result) {
2020-03-20 20:28:31 +00:00
actual = v.value.value;
expected = expects[v.name][i];
2017-11-06 14:25:48 +00:00
if (actual !== expected) {
failures.push(`Iteration ${i} variable: ${v.name} ` +
2020-03-20 20:28:31 +00:00
`expected: ${expected} actual: ${actual}`);
2017-11-06 14:25:48 +00:00
}
}
2020-03-20 20:28:31 +00:00
};
const session = new inspector.Session();
session.connect();
2017-11-06 14:25:48 +00:00
session.on('Debugger.paused',
2020-03-20 20:28:31 +00:00
(notification) => debuggerPausedCallback(session, notification));
let cbAsSecondArgCalled = false;
session.post('Debugger.enable', () => { cbAsSecondArgCalled = true; });
2017-11-06 14:25:48 +00:00
session.post('Debugger.setBreakpointByUrl', {
lineNumber: 9,
url: pathToFileURL(path.resolve(__dirname, __filename)).toString(),
columnNumber: 0,
condition: ''
2020-03-20 20:28:31 +00:00
});
2017-11-06 14:25:48 +00:00
2020-03-20 20:28:31 +00:00
debuggedFunction();
scopeCallback = null;
session.disconnect();
2017-11-06 14:25:48 +00:00
process.send({
cmd: 'assert',
debuggerEnabled: cbAsSecondArgCalled,
success: (cur === 5) && (failures.length === 0)
2020-03-20 20:28:31 +00:00
});
2017-11-06 14:25:48 +00:00
}
2020-03-20 20:28:31 +00:00
testSampleDebugSession();