-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.mjs
171 lines (156 loc) · 5.16 KB
/
test.mjs
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
// @ts-check
import { readFileSync } from 'node:fs';
import TshetUinh, { 音韻地位 } from 'tshet-uinh';
import { 推導方案 } from 'tshet-uinh-deriver-tools';
import 音韻地位fromTUPA from './index.mjs';
/**
* @template T, U
* @param {Iterable<T>} iter
* @param {(x: T) => U} f
* @returns {Iterable<U>}
*/
function* mapIter(iter, f) {
for (const x of iter) {
yield f(x);
}
}
/**
* @param {string} title
* @param {Iterable<string | null>} testCases
* @param {number} printLimit
* @returns {[number, number]}
*/
function runTests(title, testCases, printLimit = 30) {
console.log(`Testo: ${title}`);
let runCount = 0;
let failedCount = 0;
for (const failMessage of testCases) {
runCount += 1;
if (failMessage) {
failedCount += 1;
if (!printLimit || failedCount <= printLimit) {
console.log(' ' + failMessage);
} else if (failedCount === printLimit + 1) {
console.log(' (... kaj tiel plu)');
}
}
}
if (failedCount) {
const j = failedCount === 1 ? '' : 'j';
console.log(`${failedCount}/${runCount} testo${j} malsukcesis.`);
} else {
console.log(`Ĉiuj ${runCount} testoj sukcesis.`);
}
return [runCount, failedCount];
}
/**
* @param {string} tupa
* @param {音韻地位} expected
* @param {TshetUinh.邊緣地位種類指定=} marginalTypes
* @returns {string | null}
*/
function expectResult(tupa, expected, marginalTypes) {
let failMessage = '';
try {
const res = 音韻地位fromTUPA(tupa, marginalTypes);
if (!expected.等於(res)) {
failMessage = `${res} liverita`;
}
} catch (e) {
failMessage = `eraro okazinta: ${e}`;
}
return failMessage ? `${tupa}: ${expected} atendata, ${failMessage}` : null;
}
/**
* @param {string} tupa
* @param {string | RegExp} expected
* @returns {string | null}
*/
function expectError(tupa, expected) {
try {
const res = 音韻地位fromTUPA(tupa);
return `${tupa}: erarenda, sed ${res.描述} liverita`;
} catch (e) {
if (e.message.search(expected) === -1) {
return `${tupa}: erarmesaĝo ne enhavanta '${expected}': ${e}`;
}
return null;
}
}
function testOnData() {
const deriveTUPA = new 推導方案(
/** @type {import('tshet-uinh-deriver-tools').原始推導函數<string>} */ (new Function(
'TshetUinh',
'選項',
'音韻地位',
'字頭',
readFileSync('data/tupa.js', { encoding: 'utf-8' }),
).bind(null, TshetUinh)),
)();
return runTests(
'Datumoj de TshetUinh.js',
mapIter(TshetUinh.資料.iter音韻地位(), (地位) => expectResult(deriveTUPA(地位), 地位)),
)[1] === 0;
}
function testSupplement() {
/** @type {[string, string, string[]][]} */
const data = [
['taeq', '端開二麻上', []],
['tu', '端四尤平', []],
['pyng', '幫三C蒸平', []],
];
return runTests(
'Aldonaj datumoj',
mapIter(
data,
([tupa, 描述, 邊緣地位種類]) => expectResult(tupa, 音韻地位.from描述(描述, false, 邊緣地位種類), 邊緣地位種類),
),
0,
)[1] === 0;
}
function testInvalid() {
/** @type {[string, string | RegExp][]} */
const data = [
['ngiox', /無法識別聲調.*【提示:上聲用 -q】/],
['ngioq', /音韻地位「疑開三A魚上」不合法.*【提示:用C類】/],
['ngyan', /韻地位「疑開三C寒平」不合法.*【提示:元韻用 y\/uon】/],
['qow', /無法識別韻基.*【提示:豪韻用 aw,侯韻用 ou】/],
['qyw', /無法識別韻基.*【提示:尤韻用 u,幽韻用 \(y\)iw】/],
['qai', /無法識別元音.*【提示:切韻拼音用 -j -w 尾】/],
['tshryet', /無法識別聲母.*【提示:初母為 tsrh】/],
['cyang', /【提示:精母為 ts,章母為 tj】/],
['kyung', /音韻地位「見開三B東平」不合法.*【提示:用C類】/],
['pwan', /脣音不拼.*【提示:用開口形式】/],
['tryin', /莊組以外銳音聲母不拼B類拼寫形式/],
['wuo', /無法識別元音.*【提示:云母不寫】/],
['puotq', /無法識別韻尾\/聲調 -tq/],
['kwyn', /元音 wy 僅可用於無尾或 -ng\(k\) 尾/],
['kweon', /元音 weo 僅可用於 -ng\(k\) 尾/],
['deih', /端組聲母不拼 \(w\)ei 形式.*【提示:脂韻用原形式 \(w\)i,齊韻用 \(w\)ej】/],
['kei', /音韻地位「見開四脂平」不合法.*【提示:齊韻用 \(w\)ej】/],
['hing', /音韻地位「曉開三A蒸平」不合法/],
['trio', /銳音聲母不拼 io 形式.*【提示:用C類形式】/],
['tring', /銳音聲母不拼蒸韻A類形式.*【提示:用C類形式】/],
['sym', /銳音聲母不拼侵韻C類形式.*【提示:用A\/B類形式/],
['tsoymq', /音韻地位「精開一侵上」不合法.*/],
];
return runTests(
'nevalidaj latinigoj',
mapIter(data, ([tupa, expected]) => expectError(tupa, expected)),
0,
)[1] === 0;
}
(function main() {
let success = true;
const suites = [testOnData, testSupplement, testInvalid];
let first = true;
for (const suite of suites) {
if (first) {
first = false;
} else {
console.log();
}
success = suite() && success;
}
process.exit(success ? 0 : 1);
})();