testComputedConstantsCountWhenRead

tcmts.cli.suites.test.testComputedConstantsCountWhenRead · #rw7m3rnj · term

A constant whose value is computed doing nothing else counts only when what ran refers to it: table, which main reads, and not unused, which nothing that ran does; one whose computation does something (reads the clock) counts on load. What a computed value only holds isn't run by loading it: tools holds a function reading the clock and is computed quietly.

import { quietLoad } from "#codebase/tcmts/cli/suites";
import { coveredRun, SOURCE } from "#codebase/tcmts/cli/suites/test";
import assert from "node:assert/strict";

export const testComputedConstantsCountWhenRead = () => {
  const { cb, def, run } = coveredRun(SOURCE);
  assert.deepEqual(quietLoad(cb, def("unused")), []);
  assert.deepEqual(quietLoad(cb, def("tools")), []);
  assert.equal(quietLoad(cb, def("stamp")), undefined);
  // (nor is one using a global that isn't JavaScript's own: it could change it)
  assert.equal(quietLoad(cb, def("globals")), undefined);
  const ran = run(["main", "main"], ["double", "double"], ["register", "register"]);
  assert.ok(ran.includes("table"));
  assert.ok(!ran.includes("unused"));
  assert.ok(ran.includes("stamp"));
  assert.ok(ran.includes("globals"));
  assert.ok(!ran.includes("tools"));
}

uses