From ae1131e54280da63a678eb3489b02a9fb292ee8b Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 5 Sep 2023 19:39:51 -0300 Subject: infra, test: new stubContentFunction utility Just like stubTemplate, but the result is ready for passing to evaluate.load's {mock} option, and the template's content is formatted to include the content function's provided arguments as well. --- test/lib/content-function.js | 109 +++++++++++++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 31 deletions(-) (limited to 'test/lib') diff --git a/test/lib/content-function.js b/test/lib/content-function.js index b706cd8..5cb499b 100644 --- a/test/lib/content-function.js +++ b/test/lib/content-function.js @@ -91,45 +91,92 @@ export function testContentFunctions(t, message, fn) { t.matchSnapshot(result, description); }; - evaluate.stubTemplate = name => { + evaluate.stubTemplate = name => // Creates a particularly permissable template, allowing any slot values // to be stored and just outputting the contents of those slots as-are. + _stubTemplate(name, false); - return new (class extends html.Template { - #slotValues = {}; + evaluate.stubContentFunction = name => + // Like stubTemplate, but instead of a template directly, returns + // an object describing a content function - suitable for passing + // into evaluate.mock. + _stubTemplate(name, true); - constructor() { - super({ - content: () => this.#getContent(this), - }); - } - - setSlots(slotNamesToValues) { - Object.assign(this.#slotValues, slotNamesToValues); - } + const _stubTemplate = (name, mockContentFunction) => { + const inspectNicely = (value, opts = {}) => + inspect(value, { + ...opts, + colors: false, + sort: true, + }); - setSlot(slotName, slotValue) { - this.#slotValues[slotName] = slotValue; - } + const makeTemplate = formatContentFn => + new (class extends html.Template { + #slotValues = {}; - #getContent() { - const toInspect = - Object.fromEntries( - Object.entries(this.#slotValues) - .filter(([key, value]) => value !== null)); - - const inspected = - inspect(toInspect, { - breakLength: Infinity, - colors: false, - compact: true, - depth: Infinity, - sort: true, + constructor() { + super({ + content: () => this.#getContent(formatContentFn), }); + } - return `${name}: ${inspected}`; - } - }); + setSlots(slotNamesToValues) { + Object.assign(this.#slotValues, slotNamesToValues); + } + + setSlot(slotName, slotValue) { + this.#slotValues[slotName] = slotValue; + } + + #getContent(formatContentFn) { + const toInspect = + Object.fromEntries( + Object.entries(this.#slotValues) + .filter(([key, value]) => value !== null)); + + const inspected = + inspectNicely(toInspect, { + breakLength: Infinity, + compact: true, + depth: Infinity, + }); + + return formatContentFn(inspected); `${name}: ${inspected}`; + } + }); + + if (mockContentFunction) { + return { + data: (...args) => ({args}), + generate: (data) => + makeTemplate(slots => { + const argsLines = + (empty(data.args) + ? [] + : inspectNicely(data.args, {depth: Infinity}) + .split('\n')); + + return (`[mocked: ${name}` + + + (empty(data.args) + ? `` + : argsLines.length === 1 + ? `\n args: ${argsLines[0]}` + : `\n args: ${argsLines[0]}\n` + + argsLines.slice(1).join('\n').replace(/^/gm, ' ')) + + + (!empty(data.args) + ? `\n ` + : ` - `) + + + (slots + ? `slots: ${slots}]` + : `slots: none]`)); + }), + }; + } else { + return makeTemplate(slots => `${name}: ${slots}`); + } }; evaluate.mock = (...opts) => { -- cgit 1.3.0-6-gf8a5