Skip to content

Commit afea67b

Browse files
claude: Add ensureCssRegexMatches test assertion
Add a new test assertion function that checks regex patterns against CSS files in a document's supporting files directory. This is useful for verifying CSS content that is generated during rendering. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5e05270 commit afea67b

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

tests/smoke/smoke-all.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { breakQuartoMd } from "../../src/core/lib/break-quarto-md.ts";
1818
import { parse } from "../../src/core/yaml.ts";
1919
import { cleanoutput } from "./render/render.ts";
2020
import {
21+
ensureCssRegexMatches,
2122
ensureEpubFileRegexMatches,
2223
ensureDocxRegexMatches,
2324
ensureDocxXpath,
@@ -171,6 +172,7 @@ function resolveTestSpecs(
171172
const result = [];
172173
// deno-lint-ignore no-explicit-any
173174
const verifyMap: Record<string, any> = {
175+
ensureCssRegexMatches,
174176
ensureEpubFileRegexMatches,
175177
ensureHtmlElements,
176178
ensureHtmlElementContents,

tests/verify.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,53 @@ export const ensureFileRegexMatches = (
584584
return(verifyFileRegexMatches(regexChecker)(file, matchesUntyped, noMatchesUntyped));
585585
};
586586

587+
// Use this function to Regex match text in CSS files in the supporting files directory
588+
export const ensureCssRegexMatches = (
589+
file: string,
590+
matchesUntyped: (string | RegExp)[],
591+
noMatchesUntyped?: (string | RegExp)[],
592+
): Verify => {
593+
const asRegexp = (m: string | RegExp) => {
594+
if (typeof m === "string") {
595+
return new RegExp(m, "m");
596+
}
597+
return m;
598+
};
599+
const matches = matchesUntyped.map(asRegexp);
600+
const noMatches = noMatchesUntyped?.map(asRegexp);
601+
602+
return {
603+
name: `Inspecting CSS files for Regex matches`,
604+
verify: async (_output: ExecuteOutput[]) => {
605+
// Find support directory from file path
606+
const [dir, stem] = dirAndStem(file);
607+
const supportDir = join(dir, stem + "_files");
608+
609+
// Find all CSS files recursively and combine their content
610+
let combinedContent = "";
611+
for (const entry of walkSync(supportDir, { exts: [".css"] })) {
612+
combinedContent += await Deno.readTextFile(entry.path) + "\n";
613+
}
614+
615+
matches.forEach((regex) => {
616+
assert(
617+
regex.test(combinedContent),
618+
`Required CSS match ${String(regex)} is missing.`,
619+
);
620+
});
621+
622+
if (noMatches) {
623+
noMatches.forEach((regex) => {
624+
assert(
625+
!regex.test(combinedContent),
626+
`Illegal CSS match ${String(regex)} was found.`,
627+
);
628+
});
629+
}
630+
},
631+
};
632+
};
633+
587634
// Use this function to Regex match text in the intermediate kept file
588635
// FIXME: do this properly without resorting on file having keep-*
589636
export const verifyKeepFileRegexMatches = (

0 commit comments

Comments
 (0)