Convert TreeProcessor to a class

This commit is contained in:
Steve Gravrock
2025-08-12 17:43:54 -07:00
parent 8eee6ebb91
commit b009cd2922
2 changed files with 352 additions and 320 deletions

View File

@@ -1712,7 +1712,7 @@ getJasmineRequireObj().Env = function(j$) {
totalSpecsDefined: () => suiteBuilder.totalSpecsDefined, totalSpecsDefined: () => suiteBuilder.totalSpecsDefined,
focusedRunables: () => suiteBuilder.focusedRunables, focusedRunables: () => suiteBuilder.focusedRunables,
runableResources, runableResources,
reporter: reportDispatcher, reportDispatcher,
runQueue, runQueue,
TreeProcessor: j$.TreeProcessor, TreeProcessor: j$.TreeProcessor,
globalErrors, globalErrors,
@@ -9421,7 +9421,7 @@ getJasmineRequireObj().Runner = function(j$) {
this.#runQueue = options.runQueue; this.#runQueue = options.runQueue;
this.#TreeProcessor = options.TreeProcessor; this.#TreeProcessor = options.TreeProcessor;
this.#globalErrors = options.globalErrors; this.#globalErrors = options.globalErrors;
this.#reportDispatcher = options.reporter; this.#reportDispatcher = options.reportDispatcher;
this.#getConfig = options.getConfig; this.#getConfig = options.getConfig;
this.#reportSpecDone = options.reportSpecDone; this.#reportSpecDone = options.reportSpecDone;
this.hasFailures = false; this.hasFailures = false;
@@ -11305,81 +11305,97 @@ getJasmineRequireObj().Timer = function() {
}; };
getJasmineRequireObj().TreeProcessor = function() { getJasmineRequireObj().TreeProcessor = function() {
function TreeProcessor(attrs) { const defaultMin = Infinity;
const tree = attrs.tree; const defaultMax = 1 - Infinity;
const runnableIds = attrs.runnableIds;
const runQueue = attrs.runQueue;
const nodeStart = attrs.nodeStart || function() {};
const nodeComplete = attrs.nodeComplete || function() {};
const failSpecWithNoExpectations = !!attrs.failSpecWithNoExpectations;
const detectLateRejectionHandling = !!attrs.detectLateRejectionHandling;
const globalErrors = attrs.globalErrors;
const orderChildren = class TreeProcessor {
#tree;
#runQueue;
#runnableIds;
#nodeStart;
#nodeComplete;
#failSpecWithNoExpectations;
#detectLateRejectionHandling;
#globalErrors;
#orderChildren;
#excludeNode;
#stats;
#processed;
constructor(attrs) {
this.#tree = attrs.tree;
this.#runnableIds = attrs.runnableIds;
this.#runQueue = attrs.runQueue;
this.#nodeStart = attrs.nodeStart || function() {};
this.#nodeComplete = attrs.nodeComplete || function() {};
this.#failSpecWithNoExpectations = !!attrs.failSpecWithNoExpectations;
this.#detectLateRejectionHandling = !!attrs.detectLateRejectionHandling;
this.#globalErrors = attrs.globalErrors;
this.#orderChildren =
attrs.orderChildren || attrs.orderChildren ||
function(node) { function(node) {
return node.children; return node.children;
}; };
const excludeNode = this.#excludeNode =
attrs.excludeNode || attrs.excludeNode ||
function(node) { function(node) {
return false; return false;
}; };
let stats = { valid: true }; this.#stats = { valid: true };
let processed = false; this.#processed = false;
const defaultMin = Infinity; }
const defaultMax = 1 - Infinity;
this.processTree = function() { processTree() {
processNode(tree, true); this.#processNode(this.#tree, true);
processed = true; this.#processed = true;
return stats; return this.#stats;
}; }
this.execute = async function() { async execute() {
if (!processed) { if (!this.#processed) {
this.processTree(); this.processTree();
} }
if (!stats.valid) { if (!this.#stats.valid) {
throw 'invalid order'; throw 'invalid order';
} }
const childFns = wrapChildren(tree, 0); const childFns = this.#wrapChildren(this.#tree, 0);
await new Promise(function(resolve) { await new Promise(resolve => {
runQueue({ this.#runQueue({
queueableFns: childFns, queueableFns: childFns,
userContext: tree.sharedUserContext(), userContext: this.#tree.sharedUserContext(),
onException: function() { onException: function() {
tree.handleException.apply(tree, arguments); this.#tree.handleException.apply(this.#tree, arguments);
}, }.bind(this),
onComplete: resolve, onComplete: resolve,
onMultipleDone: tree.onMultipleDone onMultipleDone: this.#tree.onMultipleDone
? tree.onMultipleDone.bind(tree) ? this.#tree.onMultipleDone.bind(this.#tree)
: null : null
}); });
}); });
}; }
function runnableIndex(id) { #runnableIndex(id) {
for (let i = 0; i < runnableIds.length; i++) { for (let i = 0; i < this.#runnableIds.length; i++) {
if (runnableIds[i] === id) { if (this.#runnableIds[i] === id) {
return i; return i;
} }
} }
} }
function processNode(node, parentExcluded) { #processNode(node, parentExcluded) {
const executableIndex = runnableIndex(node.id); const executableIndex = this.#runnableIndex(node.id);
if (executableIndex !== undefined) { if (executableIndex !== undefined) {
parentExcluded = false; parentExcluded = false;
} }
if (!node.children) { if (!node.children) {
const excluded = parentExcluded || excludeNode(node); const excluded = parentExcluded || this.#excludeNode(node);
stats[node.id] = { this.#stats[node.id] = {
excluded: excluded, excluded: excluded,
willExecute: !excluded && !node.markedPending, willExecute: !excluded && !node.markedPending,
segments: [ segments: [
@@ -11395,49 +11411,104 @@ getJasmineRequireObj().TreeProcessor = function() {
} else { } else {
let hasExecutableChild = false; let hasExecutableChild = false;
const orderedChildren = orderChildren(node); const orderedChildren = this.#orderChildren(node);
for (let i = 0; i < orderedChildren.length; i++) { for (let i = 0; i < orderedChildren.length; i++) {
const child = orderedChildren[i]; const child = orderedChildren[i];
processNode(child, parentExcluded); this.#processNode(child, parentExcluded);
if (!stats.valid) { if (!this.#stats.valid) {
return; return;
} }
const childStats = stats[child.id]; const childStats = this.#stats[child.id];
hasExecutableChild = hasExecutableChild || childStats.willExecute; hasExecutableChild = hasExecutableChild || childStats.willExecute;
} }
stats[node.id] = { this.#stats[node.id] = {
excluded: parentExcluded, excluded: parentExcluded,
willExecute: hasExecutableChild willExecute: hasExecutableChild
}; };
segmentChildren(node, orderedChildren, stats[node.id], executableIndex); segmentChildren(node, orderedChildren, this.#stats, executableIndex);
if (!node.canBeReentered() && stats[node.id].segments.length > 1) { if (
stats = { valid: false }; !node.canBeReentered() &&
} this.#stats[node.id].segments.length > 1
}
}
function startingMin(executableIndex) {
return executableIndex === undefined ? defaultMin : executableIndex;
}
function startingMax(executableIndex) {
return executableIndex === undefined ? defaultMax : executableIndex;
}
function segmentChildren(
node,
orderedChildren,
nodeStats,
executableIndex
) { ) {
this.#stats = { valid: false };
}
}
}
#wrapChildren(node, segmentNumber) {
const result = [],
segmentChildren = this.#stats[node.id].segments[segmentNumber].nodes;
for (let i = 0; i < segmentChildren.length; i++) {
result.push(
this.#executeNode(segmentChildren[i].owner, segmentChildren[i].index)
);
}
if (!this.#stats[node.id].willExecute) {
return result;
}
return node.beforeAllFns.concat(result).concat(node.afterAllFns);
}
#executeNode(node, segmentNumber) {
if (node.children) {
return {
fn: function(done) {
const onStart = {
fn: next => {
this.#nodeStart(node, next);
}
};
this.#runQueue({
onComplete: function() {
const args = Array.prototype.slice.call(arguments, [0]);
node.cleanupBeforeAfter();
this.#nodeComplete(node, node.getResult(), () => {
done.apply(undefined, args);
});
}.bind(this),
queueableFns: [onStart].concat(
this.#wrapChildren(node, segmentNumber)
),
userContext: node.sharedUserContext(),
onException: function() {
node.handleException.apply(node, arguments);
},
onMultipleDone: node.onMultipleDone
? node.onMultipleDone.bind(node)
: null
});
}.bind(this)
};
} else {
return {
fn: done => {
node.execute(
this.#runQueue,
this.#globalErrors,
done,
this.#stats[node.id].excluded,
this.#failSpecWithNoExpectations,
this.#detectLateRejectionHandling
);
}
};
}
}
}
function segmentChildren(node, orderedChildren, stats, executableIndex) {
let currentSegment = { let currentSegment = {
index: 0, index: 0,
owner: node, owner: node,
@@ -11447,7 +11518,7 @@ getJasmineRequireObj().TreeProcessor = function() {
}, },
result = [currentSegment], result = [currentSegment],
lastMax = defaultMax, lastMax = defaultMax,
orderedChildSegments = orderChildSegments(orderedChildren); orderedChildSegments = orderChildSegments(orderedChildren, stats);
function isSegmentBoundary(minIndex) { function isSegmentBoundary(minIndex) {
return ( return (
@@ -11479,10 +11550,10 @@ getJasmineRequireObj().TreeProcessor = function() {
lastMax = maxIndex; lastMax = maxIndex;
} }
nodeStats.segments = result; stats[node.id].segments = result;
} }
function orderChildSegments(children) { function orderChildSegments(children, stats) {
const specifiedOrder = [], const specifiedOrder = [],
unspecifiedOrder = []; unspecifiedOrder = [];
@@ -11508,67 +11579,12 @@ getJasmineRequireObj().TreeProcessor = function() {
return specifiedOrder.concat(unspecifiedOrder); return specifiedOrder.concat(unspecifiedOrder);
} }
function executeNode(node, segmentNumber) { function startingMin(executableIndex) {
if (node.children) { return executableIndex === undefined ? defaultMin : executableIndex;
return {
fn: function(done) {
const onStart = {
fn: function(next) {
nodeStart(node, next);
}
};
runQueue({
onComplete: function() {
const args = Array.prototype.slice.call(arguments, [0]);
node.cleanupBeforeAfter();
nodeComplete(node, node.getResult(), function() {
done.apply(undefined, args);
});
},
queueableFns: [onStart].concat(wrapChildren(node, segmentNumber)),
userContext: node.sharedUserContext(),
onException: function() {
node.handleException.apply(node, arguments);
},
onMultipleDone: node.onMultipleDone
? node.onMultipleDone.bind(node)
: null
});
}
};
} else {
return {
fn: function(done) {
node.execute(
runQueue,
globalErrors,
done,
stats[node.id].excluded,
failSpecWithNoExpectations,
detectLateRejectionHandling
);
}
};
}
} }
function wrapChildren(node, segmentNumber) { function startingMax(executableIndex) {
const result = [], return executableIndex === undefined ? defaultMax : executableIndex;
segmentChildren = stats[node.id].segments[segmentNumber].nodes;
for (let i = 0; i < segmentChildren.length; i++) {
result.push(
executeNode(segmentChildren[i].owner, segmentChildren[i].index)
);
}
if (!stats[node.id].willExecute) {
return result;
}
return node.beforeAllFns.concat(result).concat(node.afterAllFns);
}
} }
return TreeProcessor; return TreeProcessor;

View File

@@ -1,79 +1,95 @@
getJasmineRequireObj().TreeProcessor = function() { getJasmineRequireObj().TreeProcessor = function() {
function TreeProcessor(attrs) { const defaultMin = Infinity;
const tree = attrs.tree; const defaultMax = 1 - Infinity;
const runnableIds = attrs.runnableIds;
const runQueue = attrs.runQueue;
const nodeStart = attrs.nodeStart || function() {};
const nodeComplete = attrs.nodeComplete || function() {};
const failSpecWithNoExpectations = !!attrs.failSpecWithNoExpectations;
const detectLateRejectionHandling = !!attrs.detectLateRejectionHandling;
const globalErrors = attrs.globalErrors;
const orderChildren = class TreeProcessor {
#tree;
#runQueue;
#runnableIds;
#nodeStart;
#nodeComplete;
#failSpecWithNoExpectations;
#detectLateRejectionHandling;
#globalErrors;
#orderChildren;
#excludeNode;
#stats;
#processed;
constructor(attrs) {
this.#tree = attrs.tree;
this.#runnableIds = attrs.runnableIds;
this.#runQueue = attrs.runQueue;
this.#nodeStart = attrs.nodeStart || function() {};
this.#nodeComplete = attrs.nodeComplete || function() {};
this.#failSpecWithNoExpectations = !!attrs.failSpecWithNoExpectations;
this.#detectLateRejectionHandling = !!attrs.detectLateRejectionHandling;
this.#globalErrors = attrs.globalErrors;
this.#orderChildren =
attrs.orderChildren || attrs.orderChildren ||
function(node) { function(node) {
return node.children; return node.children;
}; };
const excludeNode = this.#excludeNode =
attrs.excludeNode || attrs.excludeNode ||
function(node) { function(node) {
return false; return false;
}; };
let stats = { valid: true }; this.#stats = { valid: true };
let processed = false; this.#processed = false;
const defaultMin = Infinity; }
const defaultMax = 1 - Infinity;
this.processTree = function() { processTree() {
processNode(tree, true); this.#processNode(this.#tree, true);
processed = true; this.#processed = true;
return stats; return this.#stats;
}; }
this.execute = async function() { async execute() {
if (!processed) { if (!this.#processed) {
this.processTree(); this.processTree();
} }
if (!stats.valid) { if (!this.#stats.valid) {
throw 'invalid order'; throw 'invalid order';
} }
const childFns = wrapChildren(tree, 0); const childFns = this.#wrapChildren(this.#tree, 0);
await new Promise(function(resolve) { await new Promise(resolve => {
runQueue({ this.#runQueue({
queueableFns: childFns, queueableFns: childFns,
userContext: tree.sharedUserContext(), userContext: this.#tree.sharedUserContext(),
onException: function() { onException: function() {
tree.handleException.apply(tree, arguments); this.#tree.handleException.apply(this.#tree, arguments);
}, }.bind(this),
onComplete: resolve, onComplete: resolve,
onMultipleDone: tree.onMultipleDone onMultipleDone: this.#tree.onMultipleDone
? tree.onMultipleDone.bind(tree) ? this.#tree.onMultipleDone.bind(this.#tree)
: null : null
}); });
}); });
}; }
function runnableIndex(id) { #runnableIndex(id) {
for (let i = 0; i < runnableIds.length; i++) { for (let i = 0; i < this.#runnableIds.length; i++) {
if (runnableIds[i] === id) { if (this.#runnableIds[i] === id) {
return i; return i;
} }
} }
} }
function processNode(node, parentExcluded) { #processNode(node, parentExcluded) {
const executableIndex = runnableIndex(node.id); const executableIndex = this.#runnableIndex(node.id);
if (executableIndex !== undefined) { if (executableIndex !== undefined) {
parentExcluded = false; parentExcluded = false;
} }
if (!node.children) { if (!node.children) {
const excluded = parentExcluded || excludeNode(node); const excluded = parentExcluded || this.#excludeNode(node);
stats[node.id] = { this.#stats[node.id] = {
excluded: excluded, excluded: excluded,
willExecute: !excluded && !node.markedPending, willExecute: !excluded && !node.markedPending,
segments: [ segments: [
@@ -89,49 +105,104 @@ getJasmineRequireObj().TreeProcessor = function() {
} else { } else {
let hasExecutableChild = false; let hasExecutableChild = false;
const orderedChildren = orderChildren(node); const orderedChildren = this.#orderChildren(node);
for (let i = 0; i < orderedChildren.length; i++) { for (let i = 0; i < orderedChildren.length; i++) {
const child = orderedChildren[i]; const child = orderedChildren[i];
processNode(child, parentExcluded); this.#processNode(child, parentExcluded);
if (!stats.valid) { if (!this.#stats.valid) {
return; return;
} }
const childStats = stats[child.id]; const childStats = this.#stats[child.id];
hasExecutableChild = hasExecutableChild || childStats.willExecute; hasExecutableChild = hasExecutableChild || childStats.willExecute;
} }
stats[node.id] = { this.#stats[node.id] = {
excluded: parentExcluded, excluded: parentExcluded,
willExecute: hasExecutableChild willExecute: hasExecutableChild
}; };
segmentChildren(node, orderedChildren, stats[node.id], executableIndex); segmentChildren(node, orderedChildren, this.#stats, executableIndex);
if (!node.canBeReentered() && stats[node.id].segments.length > 1) { if (
stats = { valid: false }; !node.canBeReentered() &&
} this.#stats[node.id].segments.length > 1
}
}
function startingMin(executableIndex) {
return executableIndex === undefined ? defaultMin : executableIndex;
}
function startingMax(executableIndex) {
return executableIndex === undefined ? defaultMax : executableIndex;
}
function segmentChildren(
node,
orderedChildren,
nodeStats,
executableIndex
) { ) {
this.#stats = { valid: false };
}
}
}
#wrapChildren(node, segmentNumber) {
const result = [],
segmentChildren = this.#stats[node.id].segments[segmentNumber].nodes;
for (let i = 0; i < segmentChildren.length; i++) {
result.push(
this.#executeNode(segmentChildren[i].owner, segmentChildren[i].index)
);
}
if (!this.#stats[node.id].willExecute) {
return result;
}
return node.beforeAllFns.concat(result).concat(node.afterAllFns);
}
#executeNode(node, segmentNumber) {
if (node.children) {
return {
fn: function(done) {
const onStart = {
fn: next => {
this.#nodeStart(node, next);
}
};
this.#runQueue({
onComplete: function() {
const args = Array.prototype.slice.call(arguments, [0]);
node.cleanupBeforeAfter();
this.#nodeComplete(node, node.getResult(), () => {
done.apply(undefined, args);
});
}.bind(this),
queueableFns: [onStart].concat(
this.#wrapChildren(node, segmentNumber)
),
userContext: node.sharedUserContext(),
onException: function() {
node.handleException.apply(node, arguments);
},
onMultipleDone: node.onMultipleDone
? node.onMultipleDone.bind(node)
: null
});
}.bind(this)
};
} else {
return {
fn: done => {
node.execute(
this.#runQueue,
this.#globalErrors,
done,
this.#stats[node.id].excluded,
this.#failSpecWithNoExpectations,
this.#detectLateRejectionHandling
);
}
};
}
}
}
function segmentChildren(node, orderedChildren, stats, executableIndex) {
let currentSegment = { let currentSegment = {
index: 0, index: 0,
owner: node, owner: node,
@@ -141,7 +212,7 @@ getJasmineRequireObj().TreeProcessor = function() {
}, },
result = [currentSegment], result = [currentSegment],
lastMax = defaultMax, lastMax = defaultMax,
orderedChildSegments = orderChildSegments(orderedChildren); orderedChildSegments = orderChildSegments(orderedChildren, stats);
function isSegmentBoundary(minIndex) { function isSegmentBoundary(minIndex) {
return ( return (
@@ -173,10 +244,10 @@ getJasmineRequireObj().TreeProcessor = function() {
lastMax = maxIndex; lastMax = maxIndex;
} }
nodeStats.segments = result; stats[node.id].segments = result;
} }
function orderChildSegments(children) { function orderChildSegments(children, stats) {
const specifiedOrder = [], const specifiedOrder = [],
unspecifiedOrder = []; unspecifiedOrder = [];
@@ -202,67 +273,12 @@ getJasmineRequireObj().TreeProcessor = function() {
return specifiedOrder.concat(unspecifiedOrder); return specifiedOrder.concat(unspecifiedOrder);
} }
function executeNode(node, segmentNumber) { function startingMin(executableIndex) {
if (node.children) { return executableIndex === undefined ? defaultMin : executableIndex;
return {
fn: function(done) {
const onStart = {
fn: function(next) {
nodeStart(node, next);
}
};
runQueue({
onComplete: function() {
const args = Array.prototype.slice.call(arguments, [0]);
node.cleanupBeforeAfter();
nodeComplete(node, node.getResult(), function() {
done.apply(undefined, args);
});
},
queueableFns: [onStart].concat(wrapChildren(node, segmentNumber)),
userContext: node.sharedUserContext(),
onException: function() {
node.handleException.apply(node, arguments);
},
onMultipleDone: node.onMultipleDone
? node.onMultipleDone.bind(node)
: null
});
}
};
} else {
return {
fn: function(done) {
node.execute(
runQueue,
globalErrors,
done,
stats[node.id].excluded,
failSpecWithNoExpectations,
detectLateRejectionHandling
);
}
};
}
} }
function wrapChildren(node, segmentNumber) { function startingMax(executableIndex) {
const result = [], return executableIndex === undefined ? defaultMax : executableIndex;
segmentChildren = stats[node.id].segments[segmentNumber].nodes;
for (let i = 0; i < segmentChildren.length; i++) {
result.push(
executeNode(segmentChildren[i].owner, segmentChildren[i].index)
);
}
if (!stats[node.id].willExecute) {
return result;
}
return node.beforeAllFns.concat(result).concat(node.afterAllFns);
}
} }
return TreeProcessor; return TreeProcessor;