testPatchSaysWhatsNearest

tcmts.cli.main.testPatchSaysWhatsNearest · #oxd3wnuy · term

import { applyPatch } from "#codebase/tcmts/cli/main";
import assert from "node:assert/strict";

export const testPatchSaysWhatsNearest = () => {
  const text =
    'export const area = (s: number): number =>\n  s * s;\nexport const label = "area";\n';
  // wrapped otherwise by the formatter: found anyway
  assert.deepEqual(
    applyPatch(text, [
      { old: "(s: number): number => s * s", new: "(s: number): number => s ** 2" },
    ]),
    {
      text: 'export const area = (s: number): number => s ** 2;\nexport const label = "area";\n',
    },
  );
  // wrapped with a trailing comma, as the formatter wraps a call
  assert.deepEqual(
    applyPatch("assert.match(\n  ok(a, b),\n  /x/,\n);", [
      { old: "assert.match(ok(a, b), /x/);", new: "done;" },
    ]),
    { text: "done;" },
  );
  // but not two words run together, and not when it's there twice
  assert.ok("problem" in applyPatch(text, [{ old: "exportconst area", new: "x" }]));
  assert.match(
    (applyPatch("f(a, b); f(a,b)", [{ old: "f( a , b )", new: "g" }]) as { problem: string })
      .problem,
    /found 2 times, with other spacing/,
  );
  assert.deepEqual(applyPatch("f(a, b); f(a,b)", [{ old: "f( a , b )", new: "g" }], true), {
    text: "g; g",
  });
  const close = applyPatch(text, [{ old: "export const label = 'area';", new: "x" }]);
  assert.ok(
    "problem" in close &&
      /nearest line is 3: "export const label = \\"area\\";"/.test(close.problem),
    "problem" in close ? close.problem : "",
  );
  const far = applyPatch(text, [{ old: "something else entirely", new: "x" }]);
  assert.ok("problem" in far && !/nearest/.test(far.problem));
}

uses