1import * as assert from 'node:assert'; 2import * as fs from 'node:fs'; 3import { type TreeAdapterTypeMap, type SerializerOptions, parse } from 'parse5'; 4import { generateTestsForEachTreeAdapter, getStringDiffMsg } from './common.js'; 5 6export function generateSerializerTests( 7 name: string, 8 prefix: string, 9 serialize: ( 10 document: TreeAdapterTypeMap['document'], 11 opts: SerializerOptions<TreeAdapterTypeMap> 12 ) => Promise<string> | string 13): void { 14 const data = fs.readFileSync(new URL('../data/serialization/tests.json', import.meta.url)).toString('utf8'); 15 const tests = JSON.parse(data) as { 16 name: string; 17 options?: SerializerOptions<TreeAdapterTypeMap>; 18 input: string; 19 expected: string; 20 }[]; 21 22 generateTestsForEachTreeAdapter(name, (treeAdapter) => { 23 for (const [idx, test] of tests.entries()) { 24 it(`${prefix} - ${idx}.${test.name}`, async () => { 25 const opts = { ...test.options, treeAdapter }; 26 const document = parse(test.input, opts); 27 const serializedResult = await serialize(document, opts); 28 29 //NOTE: use ok assertion, so output will not be polluted by the whole content of the strings 30 assert.ok(serializedResult === test.expected, getStringDiffMsg(serializedResult, test.expected)); 31 }); 32 } 33 }); 34} 35