Skip to content

Commit

Permalink
test(mark): add block-level tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MKRhere committed Jan 7, 2025
1 parent 17eb264 commit 8dc70c9
Showing 1 changed file with 226 additions and 0 deletions.
226 changes: 226 additions & 0 deletions packages/mark/src/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,229 @@ describe("Inline", () => {
});
});
});

describe("Blocks", () => {
describe("Paragraph", () => {
it("parses a plaintext paragraph", () => {
const ast = parse("Hello world");
expect(ast.blocks[0]).toEqual(new Block.Paragraph([new Inline.Text("Hello world")]));
});

it("preserves single newlines within paragraphs", () => {
const ast = parse("Hello\nworld");
expect(ast.blocks[0]).toEqual(
new Block.Paragraph([
new Inline.Text("Hello"),
new Inline.Text("\n"),
new Inline.Text("world"),
]),
);
});

it("splits on double newlines", () => {
const ast = parse("Hello\n\nworld");
expect(ast.blocks).toEqual([
new Block.Paragraph([new Inline.Text("Hello")]),
new Block.Paragraph([new Inline.Text("world")]),
]);
});
});

describe("Heading", () => {
it("parses headings of different levels", () => {
const ast = parse("# H1\n## H2\n### H3\n#### H4\n##### H5\n###### H6");
expect(ast.blocks).toEqual([
new Block.Heading(1, [new Inline.Text("H1")]),
new Block.Heading(2, [new Inline.Text("H2")]),
new Block.Heading(3, [new Inline.Text("H3")]),
new Block.Heading(4, [new Inline.Text("H4")]),
new Block.Heading(5, [new Inline.Text("H5")]),
new Block.Heading(6, [new Inline.Text("H6")]),
]);
});

it("allows formatted text in headings", () => {
const ast = parse("# Hello *bold* _italic_");
expect(ast.blocks).toEqual([
new Block.Heading(1, [
new Inline.Text("Hello "),
new Inline.Strong([new Inline.Text("bold")]),
new Inline.Text(" "),
new Inline.Emphasis([new Inline.Text("italic")]),
]),
]);
});

it("throws on invalid heading levels", () => {
expect(() => parse("#######")).toThrow();
});
});

describe("Quote", () => {
it("parses a simple quote", () => {
const ast = parse("> Hello world");
expect(ast.blocks).toEqual([
new Block.Quote([new Block.Paragraph([new Inline.Text("Hello world")])]),
]);
});

it("parses multi-line quotes", () => {
const ast = parse("> Line 1\n> Line 2");
expect(ast.blocks).toEqual([
new Block.Quote([
new Block.Paragraph([
new Inline.Text("Line 1"),
new Inline.Text("\n"),
new Inline.Text("Line 2"),
]),
]),
]);
});

it("parses nested quotes", () => {
const ast = parse("> Outer\n>\n> > Inner");
expect(ast.blocks).toEqual([
new Block.Quote([
new Block.Paragraph([new Inline.Text("Outer")]),
new Block.Quote([new Block.Paragraph([new Inline.Text("Inner")])]),
]),
]);
});
});

describe("Code Block", () => {
it("parses a simple code block", () => {
const ast = parse("```js\nconst x = 1;\n```");
expect(ast.blocks).toEqual([
new Block.CodeBlock("const x = 1;", { language: "js", title: "" }),
]);
});

it("parses code block with title", () => {
const ast = parse("```js example.js\nconst x = 1;\n```");
expect(ast.blocks).toEqual([
new Block.CodeBlock("const x = 1;", {
language: "js",
title: "example.js",
}),
]);
});

it("parses code block with line numbers", () => {
const ast = parse("```js :line-numbers\nconst x = 1;\n```");
expect(ast.blocks).toEqual([
new Block.CodeBlock("const x = 1;", {
language: "js",
lineNumbers: true,
}),
]);
});

it("parses code block with highlight", () => {
const ast = parse("```js :highlight=1-2,4\ncode\n```");
expect(ast.blocks).toEqual([
new Block.CodeBlock("code", {
language: "js",
highlight: [
{ start: 1, end: 2 },
{ start: 4, end: 4 },
],
}),
]);
});

it("parses code block with custom end delimiter", () => {
const ast = parse("```js :end=END\ncode\n```END");
expect(ast.blocks).toEqual([
new Block.CodeBlock("code", {
language: "js",
end: "END",
}),
]);
});

it("throws on unclosed code block", () => {
expect(() => parse("```js\ncode")).toThrow();
});

it("throws on unclosed code block with custom end delimiter", () => {
expect(() => parse("```js :end=END\ncode")).toThrow();
});
});

describe("Comment", () => {
it("parses single-line comments", () => {
const ast = parse("-- This is a comment");
expect(ast.blocks).toEqual([new Block.Comment("This is a comment")]);
});
});

describe("Table", () => {
it("parses simple tables", () => {
const ast = parse("|A|B|\n|C|D|");
expect(ast.blocks).toEqual([
new Block.Table([
new Block.TableRow([
new Block.TableCell([new Inline.Text("A")]),
new Block.TableCell([new Inline.Text("B")]),
]),
new Block.TableRow([
new Block.TableCell([new Inline.Text("C")]),
new Block.TableCell([new Inline.Text("D")]),
]),
]),
]);
});

it("parses tables with alignment", () => {
const ast = parse("|A|B|\n|:--|--:|\n|C|D|");
expect(ast.blocks).toEqual([
new Block.Table(
[
new Block.TableRow([
new Block.TableCell([new Inline.Text("C")]),
new Block.TableCell([new Inline.Text("D")]),
]),
],
new Block.TableRow([
new Block.TableCell([new Inline.Text("A")]),
new Block.TableCell([new Inline.Text("B")]),
]),
new Block.TableAlignmentRow(["left", "right"]),
),
]);
});

it("normalizes table cell counts", () => {
const ast = parse("|A|B|C|\n|D|E|");
expect(ast.blocks).toEqual([
new Block.Table([
new Block.TableRow([
new Block.TableCell([new Inline.Text("A")]),
new Block.TableCell([new Inline.Text("B")]),
new Block.TableCell([new Inline.Text("C")]),
]),
new Block.TableRow([
new Block.TableCell([new Inline.Text("D")]),
new Block.TableCell([new Inline.Text("E")]),
new Block.TableCell([]),
]),
]),
]);
});
});

describe("Footnote", () => {
it("parses footnotes", () => {
const ast = parse("Reference [^1]\n[^1] Content");
expect(ast.blocks).toEqual([
new Block.Paragraph([
//
new Inline.Text("Reference "),
new Inline.FootnoteReference("1"),
]),
new Block.Footnote("1", [new Inline.Text("Content")]),
]);
});
});
});

0 comments on commit 8dc70c9

Please sign in to comment.