Skip to content

Commit

Permalink
test: Add parsing test (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
3w36zj6 authored Apr 1, 2024
1 parent c4f389b commit 7df3dac
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/fixtures/mdxFlowExpression/input.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
a + 1
}
33 changes: 33 additions & 0 deletions test/fixtures/mdxFlowExpression/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"type": "Document",
"children": [
{
"type": "CodeBlock",
"value": "\na + 1\n",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"range": [0, 9],
"raw": "{\na + 1\n}"
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 0
}
},
"range": [0, 10],
"raw": "{\na + 1\n}\n"
}
1 change: 1 addition & 0 deletions test/fixtures/mdxJsxFlowElement/input.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Component {...props}/>
33 changes: 33 additions & 0 deletions test/fixtures/mdxJsxFlowElement/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"type": "Document",
"children": [
{
"type": "Html",
"children": [],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"range": [0, 23],
"raw": "<Component {...props}/>"
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
},
"range": [0, 24],
"raw": "<Component {...props}/>\n"
}
1 change: 1 addition & 0 deletions test/fixtures/mdxJsxTextElement/input.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a<Component>b</Component>c
99 changes: 99 additions & 0 deletions test/fixtures/mdxJsxTextElement/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"type": "Document",
"children": [
{
"type": "Paragraph",
"children": [
{
"type": "Str",
"value": "a",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
},
"range": [0, 1],
"raw": "a"
},
{
"type": "Paragraph",
"children": [
{
"type": "Str",
"value": "b",
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 13
}
},
"range": [12, 13],
"raw": "b"
}
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 25
}
},
"range": [1, 25],
"raw": "<Component>b</Component>"
},
{
"type": "Str",
"value": "c",
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 26
}
},
"range": [25, 26],
"raw": "c"
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 26
}
},
"range": [0, 26],
"raw": "a<Component>b</Component>c"
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
},
"range": [0, 27],
"raw": "a<Component>b</Component>c\n"
}
1 change: 1 addition & 0 deletions test/fixtures/mdxTextExpression/input.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b {true}.
82 changes: 82 additions & 0 deletions test/fixtures/mdxTextExpression/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"type": "Document",
"children": [
{
"type": "Paragraph",
"children": [
{
"type": "Str",
"value": "b ",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"range": [0, 2],
"raw": "b "
},
{
"type": "Code",
"value": "true",
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 8
}
},
"range": [2, 8],
"raw": "{true}"
},
{
"type": "Str",
"value": ".",
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
}
},
"range": [8, 9],
"raw": "."
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
},
"range": [0, 9],
"raw": "b {true}."
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
},
"range": [0, 10],
"raw": "b {true}.\n"
}
2 changes: 2 additions & 0 deletions test/fixtures/mdxjsEsm/input.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import a from 'b'
export var c = ''
33 changes: 33 additions & 0 deletions test/fixtures/mdxjsEsm/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"type": "Document",
"children": [
{
"type": "CodeBlock",
"value": "import a from 'b'\nexport var c = ''",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 17
}
},
"range": [0, 35],
"raw": "import a from 'b'\nexport var c = ''"
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 0
}
},
"range": [0, 36],
"raw": "import a from 'b'\nexport var c = ''\n"
}
28 changes: 28 additions & 0 deletions test/parsing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// parse all fixture and should has
import { test } from "@textlint/ast-tester";
import fs from "node:fs";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { parse } from "../src/parse";

describe("parsing", () => {
const fixtureDir = path.join(__dirname, "fixtures");
for (const filePath of fs.readdirSync(fixtureDir)) {
const dirName = path.basename(filePath);
it(`${dirName} match AST`, () => {
const input = fs.readFileSync(
path.join(fixtureDir, filePath, "input.mdx"),
"utf-8",
);
const AST = parse(input);
test(AST);
const output = JSON.parse(
fs.readFileSync(
path.join(fixtureDir, filePath, "output.json"),
"utf-8",
),
);
expect(AST).toEqual(output);
});
}
});

0 comments on commit 7df3dac

Please sign in to comment.