-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.ts
131 lines (119 loc) · 3.08 KB
/
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
import {
Configuration,
DEFAULT_CONFIGURATION,
Options,
Seonbi,
transform,
} from "./mod.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
const hanjaInParens: Options = {
contentType: "text/html",
quote: "CurvedQuotes",
cite: null,
arrow: null,
ellipsis: false,
emDash: false,
stop: null,
hanja: {
rendering: "HanjaInParentheses",
reading: {
initialSoundLaw: true,
useDictionaries: ["kr-stdict"],
dictionary: {},
},
},
};
const customDict: Options = {
...hanjaInParens,
hanja: {
rendering: "HanjaInParentheses",
reading: {
initialSoundLaw: true,
useDictionaries: [],
dictionary: { "言語": "말", "文字": "글" },
},
},
};
let config: Configuration = {
...DEFAULT_CONFIGURATION,
process: { distType: "nightly" },
};
try {
const binPath = Deno.env.get("SEONBI_API");
if (binPath != null && "process" in config) config.process = { binPath };
} catch (e) {
if (!(e instanceof Deno.errors.PermissionDenied)) throw e;
}
try {
const port = Deno.env.get("SEONBI_API_PORT");
if (port != null && port.match(/^[0-9]+$/) && "process" in config) {
config.port = parseInt(port);
}
} catch (e) {
if (!(e instanceof Deno.errors.PermissionDenied)) throw e;
}
try {
const apiUrl = Deno.env.get("SEONBI_API_URL");
if (apiUrl != null) config = { apiUrl };
} catch (e) {
if (!(e instanceof Deno.errors.PermissionDenied)) throw e;
}
Deno.test("transform()", async () => {
const koKr = await transform("<p>言語와 文字</p>", config);
assertEquals(koKr, "<p>언어와 문자</p>");
});
Deno.test("Seonbi#start()", async () => {
const seonbi = new Seonbi(config);
await seonbi.start();
try {
for (let i = 0; i < 5; i++) {
try {
const response = await fetch(seonbi.apiUrl);
assertEquals(
{ message: "Unsupported method: GET", success: false },
await response.json(),
);
break;
} catch (e) {
if (
!(e instanceof TypeError) ||
e.message.indexOf("os error 61") < 0 &&
e.message.indexOf("os error 111") < 0
) {
throw e;
}
return new Promise((r) => setTimeout(r, 1000));
}
}
} finally {
await seonbi.stop();
}
});
function withSeonbi(fn: (s: Seonbi) => Promise<void>): () => Promise<void> {
return async () => {
const seonbi = new Seonbi(config);
await seonbi.start();
try {
await fn(seonbi);
} finally {
await seonbi.stop();
}
};
}
function testWithSeonbi(label: string, fn: (s: Seonbi) => Promise<void>): void {
Deno.test(label, withSeonbi(fn));
}
testWithSeonbi("Seonbi#transform()", async (seonbi: Seonbi) => {
assertEquals(
await seonbi.transform("<p>言語와 文字</p>"),
"<p>언어와 문자</p>",
);
assertEquals(
await seonbi.transform("<p>言語와 文字</p>", hanjaInParens),
"<p>언어(言語)와 문자(文字)</p>",
);
assertEquals(
await seonbi.transform("<p>言語와 文字</p>", customDict),
"<p>말(言語)와 글(文字)</p>",
);
});