-
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
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import {
assertObjectMatch,
unreachable,
} from "../deps/deno.land/std/assert/mod.ts";
import { VaultParser } from "../metadata_parser/vault_parser.ts";
import { MemoryFsReader } from "../filesystem_reader/memory_fs.ts";
import type { ContentParser } from "./interface.ts";
import { oneof } from "./oneof.ts";
function literal(x: string): ContentParser {
return {
async parse({ fileReader }) {
const content = new TextDecoder().decode(await fileReader.read());
if (content !== x) {
throw new Error(`This is not ${x}, but ${content}`);
}
return {
kind: x,
content: x,
};
},
};
}
Deno.test("Should combine parsers", async () => {
const fs = new MemoryFsReader([
{ path: "foo.md", content: "foo" },
{ path: "bar.md", content: "bar" },
]);
const metadataParser = new VaultParser();
const parser = oneof(
literal("foo"),
literal("bar"),
);
const root = await fs.getRootDirectory();
let count: number = 0;
for (const item of await root.read()) {
if (item.type !== "file") {
unreachable("MemoryFS gave a directory where expecting a file");
}
const metadata = await metadataParser.parse(item);
if ("skip" in metadata) {
unreachable(
"Metadata Parser skipped where it expected to return metadata",
);
}
const content = await parser.parse({
documentMetadata: metadata,
fileReader: item,
});
switch (count++) {
case 0:
assertObjectMatch(content, {
kind: "foo",
content: "foo",
});
break;
case 1:
assertObjectMatch(content, {
kind: "bar",
content: "bar",
});
break;
default:
unreachable("Unexpected iteration");
}
}
});