harmonyc
Version:
Harmony Code - model-driven BDD for Vitest
55 lines (54 loc) • 1.57 kB
JavaScript
import { xmur3 } from "../util/xmur3.js";
export class Router {
constructor(outs, seed = 'TODO') {
this.outs = outs;
this.index = 0;
this.started = new Set();
this.covered = new Set();
this.random = xmur3(seed);
}
next() {
if (this.outs.length === 0)
throw new Error('internal error: no outs');
if (this.index < this.outs.length)
return this.outs[this.index++];
else
return this.outs[this.random() % this.outs.length];
}
get incompleteCount() {
return this.outs.length - this.index;
}
}
export class Routers {
constructor(root) {
this.root = root;
this.routers = new Map();
this.discover(root);
}
discover(branch) {
const successors = branch.successors;
if (!this.routers.has(branch)) {
this.routers.set(branch, new Router(successors));
}
for (const s of successors)
this.discover(s);
}
get(branch) {
if (!this.routers.has(branch)) {
this.routers.set(branch, new Router(branch.successors));
}
return this.routers.get(branch);
}
nextWalk() {
const walk = [];
let current = this.root;
while (current.successors.length) {
current = this.get(current).next();
walk.push(current);
}
return walk;
}
getIncompleteCount() {
return Array.from(this.routers.values()).reduce((sum, r) => sum + r.incompleteCount, 0);
}
}