deno.land / std@0.224.0 / front_matter / _test_utils.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "../assert/mod.ts";import { dirname, fromFileUrl, join, resolve } from "../path/mod.ts";
const moduleDir = dirname(fromFileUrl(import.meta.url));const testdataDir = resolve(moduleDir, "testdata");
type ExtractTestData = { title: string; tags: string[]; "expanded-description": string;};type ExtractFn = (str: string) => { attrs: ExtractTestData; body: string; frontMatter: string;};
export function resolveTestDataPath(filename: string): string { return join(testdataDir, filename);}
export function runTestValidInputTests( format: "yaml" | "toml" | "json" | "unknown", testFn: (str: string) => boolean,) { const testdata = [ `---${format}\nname = 'deno'\n---\n`, `= ${format} =\nname = 'deno'\n= ${format} =\n`, `= ${format} =\nname = 'deno'\n= ${format} =\ndeno is awesome\n`, ];
// yaml is the default format, so it should be recognized without the format name if (format === "yaml") { testdata.push(`---\nname: deno\n---\n`); }
testdata.forEach((str) => { assert(testFn(str)); });}
export function runTestInvalidInputTests( format: "yaml" | "toml" | "json" | "unknown", testFn: (str: string) => boolean,) { [ "", "---", `---${format}`, `= ${format} =`, "---\n", `---${format}\n`, `= ${format} =\n`, `---\nasdasdasd`, ].forEach((str) => { assert(!testFn(str)); });}
export function runExtractTypeErrorTests( format: "yaml" | "toml" | "json" | "unknown", extractFn: (str: string) => unknown,) { [ "", "---", `---${format}`, `= ${format} =`, "---\n", `---${format}\n`, `= ${format} =\n`, "---\nasdasdasd", ].forEach((str) => { assertThrows(() => extractFn(str), TypeError); });}
export async function runExtractJSONTests( extractFn: ExtractFn,) { const str = await Deno.readTextFile(resolveTestDataPath("json.md")); const content = extractFn(str);
assert(content !== undefined); assertEquals( content.frontMatter, `{ "title": "Three dashes followed by the format marks the spot", "tags": [ "json", "front-matter" ], "expanded-description": "with some ---json 🙃 crazy stuff in it"}`, ); assertEquals( content.body, "don't break\n---\n{Also: \"---json this shouldn't be a problem\"}\n", ); assertEquals( content.attrs.title, "Three dashes followed by the format marks the spot", ); assertEquals(content.attrs.tags, ["json", "front-matter"]); assertEquals( content.attrs["expanded-description"], "with some ---json 🙃 crazy stuff in it", );}
export async function runExtractYAMLTests1( extractFn: ExtractFn,) { const str = await Deno.readTextFile(resolveTestDataPath("yaml1.md")); const content = extractFn(str);
assert(content !== undefined); assertEquals( content.frontMatter, `title: Three dashes marks the spottags: - yaml - front-matter - dashesexpanded-description: with some --- crazy stuff in it`, ); assertEquals( content.body, "don't break\n---\nAlso this shouldn't be a problem\n", ); assertEquals(content.attrs.title, "Three dashes marks the spot"); assertEquals(content.attrs.tags, ["yaml", "front-matter", "dashes"]); assertEquals( content.attrs["expanded-description"], "with some --- crazy stuff in it", );}
export async function runExtractYAMLTests2( extractFn: ExtractFn,) { const str = await Deno.readTextFile(resolveTestDataPath("yaml2.md")); const content = extractFn(str);
assert(content !== undefined); assertEquals( content.frontMatter, `title: Three dashes marks the spottags: - yaml - front-matter - dashesexpanded-description: with some --- crazy stuff in it`, ); assertEquals( content.body, "don't break\n---\nAlso this shouldn't be a problem\n", ); assertEquals(content.attrs.title, "Three dashes marks the spot"); assertEquals(content.attrs.tags, ["yaml", "front-matter", "dashes"]); assertEquals( content.attrs["expanded-description"], "with some --- crazy stuff in it", );}
export async function runExtractTOMLTests( extractFn: ExtractFn,) { const str = await Deno.readTextFile(resolveTestDataPath("toml.md")); const content = extractFn(str);
assert(content !== undefined); assertEquals( content.frontMatter, `title = 'Three dashes followed by the format marks the spot'tags = ['toml', 'front-matter']'expanded-description' = 'with some ---toml 👌 crazy stuff in it'`, ); assertEquals( content.body, "don't break\n---\nAlso = '---toml this shouldn't be a problem'\n", ); assertEquals( content.attrs.title, "Three dashes followed by the format marks the spot", ); assertEquals(content.attrs.tags, ["toml", "front-matter"]); assertEquals( content.attrs["expanded-description"], "with some ---toml 👌 crazy stuff in it", );}
export async function runExtractTOMLTests2( extractFn: ExtractFn,) { const str = await Deno.readTextFile(resolveTestDataPath("toml2.md")); const content = extractFn(str);
assert(content !== undefined); assertEquals( content.frontMatter, `title = 'Three pluses followed by the format marks the spot'tags = ['toml', 'front-matter']'expanded-description' = 'with some +++toml 👌 crazy stuff in it'`, ); assertEquals( content.body, "don't break\n+++\nAlso = '+++toml this shouldn't be a problem'\n", ); assertEquals( content.attrs.title, "Three pluses followed by the format marks the spot", ); assertEquals(content.attrs.tags, ["toml", "front-matter"]); assertEquals( content.attrs["expanded-description"], "with some +++toml 👌 crazy stuff in it", );}
Version Info