Skip to content

Commit d275611

Browse files
test(html): cover the printer a processor asks for on its first print
1 parent a036f29 commit d275611

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎test/unitCases/HtmlSyntax.unittest.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,6 +3226,28 @@ const find = (src, tagName) => {
32263226
return /** @type {MatElement} */ (found);
32273227
};
32283228

3229+
describe("HtmlSyntax — the facade", () => {
3230+
const syntax = require("../../lib/html/syntax");
3231+
3232+
it("names its two halves the way `javascript` does", () => {
3233+
expect(typeof syntax.parser.grammar).toBe("function");
3234+
expect(typeof syntax.printer.printer).toBe("function");
3235+
});
3236+
3237+
it("takes every name it publishes from the half that owns it", () => {
3238+
expect(syntax.parseHtml).toBe(syntax.parser.parseHtml);
3239+
expect(syntax.NodeType).toBe(syntax.parser.NodeType);
3240+
});
3241+
3242+
it("holds no printer until something prints", () => {
3243+
const processor = new syntax.SourceProcessor();
3244+
processor.process("<p>x</p>");
3245+
expect(processor._printer).toBeUndefined();
3246+
expect(processor.process("<p>x</p>", { mode: "minify" }).code).toBe("<p>x");
3247+
expect(typeof processor._printer).toBe("function");
3248+
});
3249+
});
3250+
32293251
describe("parseHtml", () => {
32303252
it("should produce an empty document with html/head/body scaffolding", () => {
32313253
const ast = parseHtml("");

‎test/unitCases/SourceProcessor.unittest.js‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
NodeType: HtmlNodeType,
99
SourceProcessor: HtmlSourceProcessor
1010
} = require("../../lib/html/syntax");
11+
const GenericSourceProcessor = require("../../lib/util/SourceProcessor");
1112
const { PrintContext } = require("../../lib/util/SourceProcessor");
1213

1314
const CSS = "a {\n\tcolor : #ff0000 ;\n}\n";
@@ -22,6 +23,55 @@ const LANGUAGES = [
2223
];
2324

2425
describe("SourceProcessor", () => {
26+
// A language whose printer is a module of its own hands one of these over
27+
// instead of the printer, so a walk that never prints never loads it.
28+
describe("deferred printer", () => {
29+
/**
30+
* @returns {{ processor: EXPECTED_ANY, loads: () => number }} a processor that counts what it was asked for
31+
*/
32+
const counting = () => {
33+
let loads = 0;
34+
const processor = new GenericSourceProcessor(
35+
() => {},
36+
undefined,
37+
() => {
38+
loads++;
39+
return () => "";
40+
}
41+
);
42+
return { processor, loads: () => loads };
43+
};
44+
45+
it("is not asked for by a walk", () => {
46+
const { processor, loads } = counting();
47+
expect(processor.process("x")).toBeUndefined();
48+
expect(loads()).toBe(0);
49+
});
50+
51+
it("is asked for once, by the first print", () => {
52+
const { processor, loads } = counting();
53+
processor.process("x");
54+
processor.process("x", { mode: "minify" });
55+
expect(loads()).toBe(1);
56+
});
57+
58+
it("is reused by every print after it", () => {
59+
const { processor, loads } = counting();
60+
processor.process("x", { mode: "minify" });
61+
const first = processor._printer;
62+
processor.process("x", { mode: "beautify" });
63+
expect(loads()).toBe(1);
64+
expect(processor._printer).toBe(first);
65+
});
66+
67+
it("stays out of a processor that was handed its printer", () => {
68+
const printer = () => "";
69+
const processor = new GenericSourceProcessor(() => {}, printer);
70+
processor.process("x", { mode: "minify" });
71+
expect(processor._printer).toBe(printer);
72+
});
73+
});
74+
2575
// Dropping the store has to let go of the text, not just stop answering for
2676
// it: a stylesheet prints ~300k nodes, which is the output over again.
2777
describe("printed-text store", () => {

0 commit comments

Comments
 (0)