deno.land / std@0.166.0 / encoding / front_matter / _test_utils.ts

_test_utils.ts
نووسراو ببینە
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "../../testing/asserts.ts";import { dirname, fromFileUrl, join, resolve } from "../../path/mod.ts";import { Format } from "./mod.ts";
const moduleDir = dirname(fromFileUrl(import.meta.url));const testdataDir = resolve(moduleDir, "testdata");
type ExtractTestData = { title: string; tags: string[]; "expanded-description": string;};type ExtractFn = (str: string) => { attrs: ExtractTestData; body: string; frontMatter: string;};
export function resolveTestDataPath(filename: string): string { return join(testdataDir, filename);}
export function runTestValidInputTests( format: Format, testFn: (str: string) => boolean,) { const testdata = [ `---${format}\nname = 'deno'\n---\n`, `= ${format} =\nname = 'deno'\n= ${format} =\n`, `= ${format} =\nname = 'deno'\n= ${format} =\ndeno is awesome\n`, ];
// yaml is the default format, so it should be recognized without the format name if (format === Format.YAML) { testdata.push(`---\nname: deno\n---\n`); }
testdata.forEach((str) => { assert(testFn(str)); });}
export function runTestInvalidInputTests( format: Format, testFn: (str: string) => boolean,) { [ "", "---", `---${format}`, `= ${format} =`, "---\n", `---${format}\n`, `= ${format} =\n`, `---\nasdasdasd`, ].forEach((str) => { assert(!testFn(str)); });}
export function runExtractTypeErrorTests( format: Format, extractFn: (str: string) => unknown,) { [ "", "---", `---${format}`, `= ${format} =`, "---\n", `---${format}\n`, `= ${format} =\n`, "---\nasdasdasd", ].forEach((str) => { assertThrows(() => extractFn(str), TypeError); });}
export async function runExtractJSONTests( extractFn: ExtractFn,) { const str = await Deno.readTextFile(resolveTestDataPath("json.md")); const content = extractFn(str);
assert(content !== undefined); 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", );}
export async function runExtractYAMLTests1( extractFn: ExtractFn,) { const str = await Deno.readTextFile(resolveTestDataPath("yaml1.md")); const content = extractFn(str);
assert(content !== undefined); assertEquals( content.frontMatter, `title: Three dashes marks the spottags: - yaml - front-matter - dashesexpanded-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", );}
export async function runExtractYAMLTests2( extractFn: ExtractFn,) { const str = await Deno.readTextFile(resolveTestDataPath("yaml2.md")); const content = extractFn(str);
assert(content !== undefined); assertEquals( content.frontMatter, `title: Three dashes marks the spottags: - yaml - front-matter - dashesexpanded-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", );}
export async function runExtractTOMLTests( extractFn: ExtractFn,) { const str = await Deno.readTextFile(resolveTestDataPath("toml.md")); const content = extractFn(str);
assert(content !== undefined); 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", );}
std

Version Info

Tagged at
a year ago