Skip to content

Commit e1e051b

Browse files
lumirlumirnzakas
andauthored
test: create tests for MarkdownLanguage (#329)
* test: create tests for `MarkdownLanguage` * Update tests/language/markdown-language.test.js Co-authored-by: Nicholas C. Zakas <[email protected]> * Update tests/language/markdown-language.test.js Co-authored-by: Nicholas C. Zakas <[email protected]> --------- Co-authored-by: Nicholas C. Zakas <[email protected]>
1 parent 5f11a3d commit e1e051b

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Diff for: tests/language/markdown-language.test.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @fileoverview Tests for MarkdownLanguage
3+
* @author 루밀LuMir(lumirlumir)
4+
*/
5+
6+
//-----------------------------------------------------------------------------
7+
// Imports
8+
//-----------------------------------------------------------------------------
9+
10+
import { MarkdownLanguage } from "../../src/language/markdown-language.js";
11+
import assert from "node:assert";
12+
13+
//-----------------------------------------------------------------------------
14+
// Tests
15+
//-----------------------------------------------------------------------------
16+
17+
describe("MarkdownLanguage", () => {
18+
describe("parse()", () => {
19+
it("should parse markdown", () => {
20+
const language = new MarkdownLanguage();
21+
const result = language.parse({
22+
body: "# Hello, World!\n\nHello, World!",
23+
path: "test.css",
24+
});
25+
26+
assert.strictEqual(result.ok, true);
27+
assert.strictEqual(result.ast.type, "root");
28+
assert.strictEqual(result.ast.children[0].type, "heading");
29+
assert.strictEqual(result.ast.children[1].type, "paragraph");
30+
});
31+
32+
it("should not parse gfm features in commonmark mode", () => {
33+
const language = new MarkdownLanguage({ mode: "commonmark" });
34+
const result = language.parse({
35+
body: "| Column 1 | Column 2 |\n| -------- | -------- |\n| Cell 1 | Cell 2 |",
36+
path: "test.md",
37+
});
38+
39+
assert.strictEqual(result.ok, true);
40+
// The table should not be parsed and should be recognized as plain text.
41+
assert.strictEqual(result.ast.children[0].type, "paragraph");
42+
});
43+
44+
it("should parse gfm features in gfm mode", () => {
45+
const language = new MarkdownLanguage({ mode: "gfm" });
46+
const result = language.parse({
47+
body: "| Column 1 | Column 2 |\n| -------- | -------- |\n| Cell 1 | Cell 2 |",
48+
path: "test.md",
49+
});
50+
51+
assert.strictEqual(result.ok, true);
52+
// The table should be parsed correctly.
53+
assert.strictEqual(result.ast.children[0].type, "table");
54+
});
55+
});
56+
57+
describe("createSourceCode()", () => {
58+
it("should create a MarkdownSourceCode instance for commonmark", () => {
59+
const language = new MarkdownLanguage({ mode: "commonmark" });
60+
const file = {
61+
body: "| Column 1 | Column 2 |\n| -------- | -------- |\n| Cell 1 | Cell 2 |",
62+
path: "test.json",
63+
};
64+
const parseResult = language.parse(file);
65+
const sourceCode = language.createSourceCode(file, parseResult);
66+
67+
assert.strictEqual(
68+
sourceCode.constructor.name,
69+
"MarkdownSourceCode",
70+
);
71+
assert.strictEqual(sourceCode.ast.type, "root");
72+
assert.strictEqual(sourceCode.ast.children[0].type, "paragraph");
73+
assert.strictEqual(
74+
sourceCode.text,
75+
"| Column 1 | Column 2 |\n| -------- | -------- |\n| Cell 1 | Cell 2 |",
76+
);
77+
});
78+
79+
it("should create a MarkdownSourceCode instance for gfm", () => {
80+
const language = new MarkdownLanguage({ mode: "gfm" });
81+
const file = {
82+
body: "| Column 1 | Column 2 |\n| -------- | -------- |\n| Cell 1 | Cell 2 |",
83+
path: "test.json",
84+
};
85+
const parseResult = language.parse(file);
86+
const sourceCode = language.createSourceCode(file, parseResult);
87+
88+
assert.strictEqual(
89+
sourceCode.constructor.name,
90+
"MarkdownSourceCode",
91+
);
92+
assert.strictEqual(sourceCode.ast.type, "root");
93+
assert.strictEqual(sourceCode.ast.children[0].type, "table");
94+
assert.strictEqual(
95+
sourceCode.text,
96+
"| Column 1 | Column 2 |\n| -------- | -------- |\n| Cell 1 | Cell 2 |",
97+
);
98+
});
99+
});
100+
});

0 commit comments

Comments
 (0)