std/front_matter/_test_utils.ts
Yusuke Tanaka d13ef17a5d
docs(front-matter): improve docs for stabilization (#4789)
This commit improves docs of the front-matter module.

Specifically, the following things are done:

- Use `@example` where appropriate to render the examples more beautifully
- Split the big example attached to `createExtractor` function into smaller pieces so that each one has minimal yet meaningful example
- Add `@returns` to the `test` function
- Deduplicate `Format` type definition
- Add a brief description to `Format` type

Towards #3764

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-05-27 18:14:52 -07:00

224 lines
5.7 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "@std/assert";
import { dirname, fromFileUrl, join, resolve } from "@std/path";
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 spot
tags:
- yaml
- front-matter
- dashes
expanded-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 spot
tags:
- yaml
- front-matter
- dashes
expanded-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",
);
}