Skip to content

test(front-matter): remove double tests in any_test.ts #6474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 46 additions & 163 deletions front_matter/any_test.ts
Original file line number Diff line number Diff line change
@@ -1,184 +1,67 @@
// Copyright 2018-2025 the Deno authors. MIT license.

import { extract } from "./any.ts";

import { extract } from "@std/front-matter/any";
import { assertEquals } from "../assert/equals.ts";
import { assertThrows } from "../assert/throws.ts";

Deno.test("extract() extracts type error on invalid input", () => {
assertThrows(() => extract(""));
assertThrows(() => extract("---"));
assertThrows(() => extract(`---yaml`));
assertThrows(() => extract(`= yaml =`));
assertThrows(() => extract("---\n"));
assertThrows(() => extract(`---yaml\n`));
assertThrows(() => extract(`= yaml =\n`));
assertThrows(() => extract("---\nasdasdasd"));
});

Deno.test("extract() parses yaml delineate by `---`", () => {
Deno.test("extract() handles yaml data", () => {
const input = `---
title: Three dashes marks the spot
tags:
- yaml
- front-matter
- dashes
expanded-description: with some --- crazy stuff in it
---
don't break
foo: bar
---
Also this shouldn't be a problem
`;
const content = extract<Record<string, unknown>>(input);
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",
);
});

Deno.test("extract() parses yaml delineate by `---yaml`", () => {
const input = `---yaml
title: Three dashes marks the spot
tags:
- yaml
- front-matter
- dashes
expanded-description: with some --- crazy stuff in it
---
don't break
---
Also this shouldn't be a problem
`;
const content = extract<Record<string, unknown>>(input);
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",
);
Hello, world!`;
const actual = extract(input);
const expected = {
attrs: {
foo: "bar",
},
body: "Hello, world!",
frontMatter: "foo: bar",
};
assertEquals(actual, expected);
});

Deno.test("extract() extracts type error on invalid json input", () => {
assertThrows(() => extract(""));
assertThrows(() => extract("---"));
assertThrows(() => extract(`---json`));
assertThrows(() => extract(`= json =`));
assertThrows(() => extract("---\n"));
assertThrows(() => extract(`---json\n`));
assertThrows(() => extract(`= json =\n`));
assertThrows(() => extract("---\nasdasdasd"));
Deno.test("extract() handles toml data", () => {
const input = `+++
foo = "bar"
+++
Hello, world!`;
const actual = extract(input);
const expected = {
attrs: {
foo: "bar",
},
body: "Hello, world!",
frontMatter: 'foo = "bar"',
};
assertEquals(actual, expected);
});

Deno.test("json() parses json delineate by ---json", () => {
Deno.test("extract() handles json data", () => {
const input = `---json
{
"title": "Three dashes followed by the format marks the spot",
"tags": [
"json",
"front-matter"
],
"expanded-description": "with some ---json 🙃 crazy stuff in it"
"foo": "bar"
}
---
don't break
---
{Also: "---json this shouldn't be a problem"}
`;
Hello, world!`;

const content = extract<Record<string, unknown>>(input);
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",
);
const actual = extract(input);
const expected = {
attrs: { foo: "bar" },
body: "Hello, world!",
frontMatter: '{\n "foo": "bar"\n}',
};
assertEquals(actual, expected);
});

Deno.test("extract() extracts type error on invalid toml input", () => {
assertThrows(() => extract(""));
assertThrows(() => extract("---"));
assertThrows(() => extract(`---toml`));
assertThrows(() => extract(`= toml =`));
assertThrows(() => extract("---\n"));
assertThrows(() => extract(`---toml\n`));
assertThrows(() => extract(`= toml =\n`));
assertThrows(() => extract("---\nasdasdasd"));
});

Deno.test("extract() parses toml delineate by ---toml", () => {
const input = `---toml
title = 'Three dashes followed by the format marks the spot'
tags = ['toml', 'front-matter']
'expanded-description' = 'with some ---toml 👌 crazy stuff in it'
---
don't break
Deno.test("extract() throws on unsupported format", () => {
const input = `---unsupported
foo: bar
---
Also = '---toml this shouldn't be a problem'
`;
const content = extract<Record<string, unknown>>(input);
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",
Hello, world!`;

assertThrows(
() => extract(input),
TypeError,
"Unsupported front matter format",
);
});
Loading