-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathama_json_test.ts
More file actions
117 lines (102 loc) · 3.09 KB
/
ama_json_test.ts
File metadata and controls
117 lines (102 loc) · 3.09 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
113
114
115
116
117
import { assert, fail } from "https://deno.land/std@0.224.0/assert/mod.ts";
function assertString(
expr: unknown,
): asserts expr is string {
assert(
typeof expr === "string",
`文字列ではありません: ${expr} は ${typeof expr} です`,
);
}
function assertStartsWith(
expr: unknown,
...strings: string[]
): asserts expr {
assertString(expr);
assert(
strings.some((str) => expr.startsWith(str)),
`${strings.join(" または ")} で始まっていません`,
);
}
function assertDateString(expr: unknown): asserts expr {
assertString(expr);
const REGEXP = /20\d{2}-\d{2}-\d{2}(T\d{2}:\d{2}\+09:00)?/;
assert(REGEXP.test(expr), "日付のフォーマットが正しくありません");
const time = new Date(expr).getTime();
assert(!Number.isNaN(time), "存在しない日付です");
}
async function getAmaJson(): Promise<Record<string, string>[]> {
const text = await Deno.readTextFile("./ama.json");
return JSON.parse(text);
}
Deno.test("JSONの形式が正しいことを確認", async () => {
try {
const json = await getAmaJson();
assert(Array.isArray(json));
} catch {
fail("JSONの形式が正しくありません");
}
});
Deno.test("各項目の確認", async (t) => {
const json = await getAmaJson();
for (let i = 0; i < json.length; i++) {
const ama = json[i];
assert(ama.title != null, `${i + 1} 番目の項目にタイトルがありません`);
await t.step(`【${ama.title}】`, async (t) => {
await t.step("タイトルを確認", () => {
assertString(ama.title);
});
if (ama.date != null) {
await t.step("日付を確認", () => {
assertDateString(ama.date);
});
}
if (ama.note != null) {
await t.step("注釈を確認", () => {
assertString(ama.note);
});
}
if (ama.website != null) {
await t.step("Websiteを確認", () => {
assertStartsWith(ama.website, "http://", "https://");
});
}
if (ama.twitter != null) {
await t.step("Twitterを確認", () => {
assertStartsWith(ama.twitter, "https://twitter.com", "https://x.com");
});
}
if (ama.discord != null) {
await t.step("Discordを確認", () => {
assertStartsWith(
ama.discord,
"https://discord.gg",
"https://discord.com/invite",
);
});
}
if (ama.telegram != null) {
await t.step("Telegramを確認", () => {
assertStartsWith(ama.telegram, "https://t.me");
});
}
await t.step("不要なキーの検出", () => {
const acceptableKeys = [
"title",
"date",
"note",
"website",
"twitter",
"discord",
"telegram",
];
const unnecessaryKeys = Object.keys(ama).filter((key) =>
!acceptableKeys.includes(key)
);
assert(
unnecessaryKeys.length === 0,
`不要なキーが含まれています: ${unnecessaryKeys.join(", ")}`,
);
});
});
}
});