-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypstToTextlintAst.test.ts
More file actions
112 lines (100 loc) · 3.38 KB
/
typstToTextlintAst.test.ts
File metadata and controls
112 lines (100 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import fs from "node:fs";
import path from "node:path";
import * as ASTTester from "@textlint/ast-tester";
import { describe, expect, it } from "vitest";
import {
convertRawTypstAstObjectToTextlintAstObject,
convertRawTypstAstStringToObject,
convertTypstSourceToTextlintAstObject,
extractRawSourceByLocation,
getRawTypstAstString,
paragraphizeTextlintAstObject,
} from "../../src/typstToTextlintAst";
const typstSource = fs.readFileSync(
path.join(__dirname, "example.typ"),
"utf-8",
);
const rawTypstAstString = fs
.readFileSync(path.join(__dirname, "rawTypstAstString.txt"), "utf-8")
.replace(/\n$/, "");
const rawTypstAstObject = JSON.parse(
fs.readFileSync(path.join(__dirname, "rawTypstAstObject.json"), "utf-8"),
);
const textlintAstObject = JSON.parse(
fs.readFileSync(path.join(__dirname, "textlintAstObject.json"), "utf-8"),
);
const paragraphizedTextlintAstObject = JSON.parse(
fs.readFileSync(
path.join(__dirname, "paragraphizedTextlintAstObject.json"),
"utf-8",
),
);
describe("getRawTypstAstString", () => {
it("should return raw typst ast string", async () => {
const actualRawTypstAstString = await getRawTypstAstString(typstSource);
expect(actualRawTypstAstString).toStrictEqual(rawTypstAstString);
});
});
describe("convertRawTypstAstStringToObject", () => {
it("should convert raw Typst AST to raw Typst AST object", async () => {
const actualRawTypstAstObject =
await convertRawTypstAstStringToObject(rawTypstAstString);
expect(actualRawTypstAstObject).toStrictEqual(rawTypstAstObject);
});
});
describe("extractRawSourceByLocation", () => {
it("should extract substring from a single line", async () => {
const location = {
start: { line: 1, column: 3 },
end: { line: 1, column: 8 },
};
const actualRawSource = await extractRawSourceByLocation(
typstSource,
location,
);
expect(actualRawSource).toStrictEqual("t pag");
});
it("should extract substring across multiple lines", async () => {
const location = {
start: { line: 1, column: 2 },
end: { line: 2, column: 12 },
};
const actualRawSource = await extractRawSourceByLocation(
typstSource,
location,
);
expect(actualRawSource).toStrictEqual(`et page(width: 10cm, height: auto)
#set heading`);
});
});
describe("convertRawTypstAstObjectToTextlintAstObject", () => {
it("should convert raw Typst AST object to textlint AST object", async () => {
const actualTextlintAstObject =
await convertRawTypstAstObjectToTextlintAstObject(
rawTypstAstObject,
typstSource,
);
expect(actualTextlintAstObject).toStrictEqual(textlintAstObject);
ASTTester.test(actualTextlintAstObject);
});
});
describe("paragraphizeTextlintAstObject", () => {
it("should convert textlint AST object to paragraphized textlint AST object", async () => {
const actualParagraphizedTextlintAstObject =
await paragraphizeTextlintAstObject(textlintAstObject);
expect(actualParagraphizedTextlintAstObject).toStrictEqual(
paragraphizedTextlintAstObject,
);
ASTTester.test(actualParagraphizedTextlintAstObject);
});
});
describe("convertTypstSourceToParagraphizedTextlintAstObject", () => {
it("should convert Typst source to textlint AST object", async () => {
const actualTextlintAstObject =
await convertTypstSourceToTextlintAstObject(typstSource);
expect(actualTextlintAstObject).toStrictEqual(
paragraphizedTextlintAstObject,
);
ASTTester.test(actualTextlintAstObject);
});
});