deno.land / std@0.166.0 / encoding / front_matter / mod_test.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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, assertThrows } from "../../testing/asserts.ts";import { createExtractor, Format, Parser, test } from "./mod.ts";import { parse as parseYAML } from "../yaml.ts";import { parse as parseTOML } from "../toml.ts";import { resolveTestDataPath, runExtractJSONTests, runExtractTOMLTests, runExtractTypeErrorTests, runExtractYAMLTests1, runExtractYAMLTests2, runTestInvalidInputTests, runTestValidInputTests,} from "./_test_utils.ts";
const extractYAML = createExtractor({ [Format.YAML]: parseYAML as Parser });const extractTOML = createExtractor({ [Format.TOML]: parseTOML as Parser });const extractJSON = createExtractor({ [Format.JSON]: JSON.parse as Parser });const extractYAMLOrJSON = createExtractor({ [Format.YAML]: parseYAML as Parser, [Format.JSON]: JSON.parse as Parser,});const extractAny = createExtractor({ [Format.YAML]: parseYAML as Parser, [Format.JSON]: JSON.parse as Parser, [Format.TOML]: parseTOML as Parser,});
// GENERAL TESTS //
Deno.test("[ANY] try to test for unknown format", () => { assertThrows( () => test("foo", [Format.UNKNOWN]), TypeError, "Unable to test for unknown front matter format", );});
// YAML //
Deno.test("[YAML] test valid input true", () => { runTestValidInputTests(Format.YAML, test);});
Deno.test("[YAML] test invalid input false", () => { runTestInvalidInputTests(Format.YAML, test);});
Deno.test("[YAML] extract type error on invalid input", () => { runExtractTypeErrorTests(Format.YAML, extractYAML);});
Deno.test("[YAML] parse yaml delineate by `---`", async () => { await runExtractYAMLTests1(extractYAML);});
Deno.test("[YAML] parse yaml delineate by `---yaml`", async () => { await runExtractYAMLTests2(extractYAML);});
Deno.test({ name: "[YAML] text between horizontal rules should not be recognized", async fn() { const str = await Deno.readTextFile( resolveTestDataPath("./horizontal_rules.md"), );
assert(!test(str)); assertThrows( () => { extractAny(str); }, TypeError, "Unsupported front matter format", ); },});
// JSON //
Deno.test("[JSON] test valid input true", () => { runTestValidInputTests(Format.JSON, test);});
Deno.test("[JSON] test invalid input false", () => { runTestInvalidInputTests(Format.JSON, test);});
Deno.test("[JSON] extract type error on invalid input", () => { runExtractTypeErrorTests(Format.JSON, extractJSON);});
Deno.test("[JSON] parse json delineate by ---json", async () => { await runExtractJSONTests(extractJSON);});
// TOML //
Deno.test("[TOML] test valid input true", () => { runTestValidInputTests(Format.TOML, test);});
Deno.test("[TOML] test invalid input false", () => { runTestInvalidInputTests(Format.TOML, test);});
Deno.test("[TOML] extract type error on invalid input", () => { runExtractTypeErrorTests(Format.TOML, extractTOML);});
Deno.test("[TOML] parse toml delineate by ---toml", async () => { await runExtractTOMLTests(extractTOML);});
// MULTIPLE FORMATS //
Deno.test("[YAML or JSON] parse input", async () => { await runExtractYAMLTests1(extractYAMLOrJSON); await runExtractYAMLTests2(extractYAMLOrJSON); await runExtractJSONTests(extractYAMLOrJSON);});
Deno.test("[ANY] parse input", async () => { await runExtractYAMLTests1(extractAny); await runExtractYAMLTests2(extractAny); await runExtractJSONTests(extractAny); await runExtractTOMLTests(extractAny);});
std

Version Info

Tagged at
a year ago