mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
6dbb2121b5
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
78 lines
1.7 KiB
TypeScript
78 lines
1.7 KiB
TypeScript
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { extract, test } from "./front_matter.ts";
|
|
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
|
|
|
|
Deno.test("test valid input true", () => {
|
|
[
|
|
"---\nname: deno\n---\n",
|
|
"= yaml =\nname: deno\n= yaml =\n",
|
|
"= yaml =\nname: deno\n= yaml =\ndeno is awesome\n",
|
|
].forEach((str) => {
|
|
assert(test(str));
|
|
});
|
|
});
|
|
|
|
Deno.test("test invalid input false", () => {
|
|
[
|
|
"",
|
|
"---",
|
|
"= yaml =",
|
|
"---\n",
|
|
"= yaml =\n",
|
|
"---\nasdasdasd",
|
|
].forEach((str) => {
|
|
assert(!test(str));
|
|
});
|
|
});
|
|
|
|
Deno.test("extract type error on invalid input", () => {
|
|
[
|
|
"",
|
|
"---",
|
|
"= yaml =",
|
|
"---\n",
|
|
"= yaml =\n",
|
|
"---\nasdasdasd",
|
|
].forEach((str) => {
|
|
assertThrows(() => extract(str), TypeError);
|
|
});
|
|
});
|
|
|
|
Deno.test("parse yaml delinetead by `---`", () => {
|
|
const content = extract<
|
|
{ title: string; tags: string[]; "expaned-description": string }
|
|
>(`---
|
|
title: Three dashes marks the spot
|
|
tags:
|
|
- yaml
|
|
- front-matter
|
|
- dashes
|
|
expaned-description: with some --- crazy stuff in it
|
|
---
|
|
don't break
|
|
---
|
|
Also this shouldn't be a problem
|
|
`);
|
|
assert(content !== undefined);
|
|
assertEquals(
|
|
content.frontMatter,
|
|
`title: Three dashes marks the spot
|
|
tags:
|
|
- yaml
|
|
- front-matter
|
|
- dashes
|
|
expaned-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["expaned-description"],
|
|
"with some --- crazy stuff in it",
|
|
);
|
|
});
|