Skip to content

Commit 2327cec

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 a00c39f commit 2327cec

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,
@@ -177,6 +178,7 @@ function resolveTestSpecs(
177178
const result = [];
178179
// deno-lint-ignore no-explicit-any
179180
const verifyMap: Record<string, any> = {
181+
ensureCssRegexMatches,
180182
ensureEpubFileRegexMatches,
181183
ensureHtmlElements,
182184
ensureHtmlElementContents,

tests/verify.ts

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

646+
// Use this function to Regex match text in CSS files in the supporting files directory
647+
export const ensureCssRegexMatches = (
648+
file: string,
649+
matchesUntyped: (string | RegExp)[],
650+
noMatchesUntyped?: (string | RegExp)[],
651+
): Verify => {
652+
const asRegexp = (m: string | RegExp) => {
653+
if (typeof m === "string") {
654+
return new RegExp(m, "m");
655+
}
656+
return m;
657+
};
658+
const matches = matchesUntyped.map(asRegexp);
659+
const noMatches = noMatchesUntyped?.map(asRegexp);
660+
661+
return {
662+
name: `Inspecting CSS files for Regex matches`,
663+
verify: async (_output: ExecuteOutput[]) => {
664+
// Find support directory from file path
665+
const [dir, stem] = dirAndStem(file);
666+
const supportDir = join(dir, stem + "_files");
667+
668+
// Find all CSS files recursively and combine their content
669+
let combinedContent = "";
670+
for (const entry of walkSync(supportDir, { exts: [".css"] })) {
671+
combinedContent += await Deno.readTextFile(entry.path) + "\n";
672+
}
673+
674+
matches.forEach((regex) => {
675+
assert(
676+
regex.test(combinedContent),
677+
`Required CSS match ${String(regex)} is missing.`,
678+
);
679+
});
680+
681+
if (noMatches) {
682+
noMatches.forEach((regex) => {
683+
assert(
684+
!regex.test(combinedContent),
685+
`Illegal CSS match ${String(regex)} was found.`,
686+
);
687+
});
688+
}
689+
},
690+
};
691+
};
692+
646693
// Use this function to Regex match text in the intermediate kept file
647694
// FIXME: do this properly without resorting on file having keep-*
648695
// Note: keep-typ/keep-tex places files alongside source, not in output dir

0 commit comments

Comments
 (0)