-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathexecutor.js
More file actions
283 lines (236 loc) · 8.11 KB
/
executor.js
File metadata and controls
283 lines (236 loc) · 8.11 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* Main execution program for Data Driven Test of ICU / Unicode / CLDR
functions and data.
This accepts test statements via StdIn in JSON format.
The type of test determines the corresponding test function that then
receives the test case. This includes the type of operation, e.g.,
collation, number format, locale matching, likely subtags, etc.
The data includes parameters needed to specify the function called as well
as the test data passed to the function.
Expected results are not read by this program.
Output includes test ID and actual results from the test, written to StdOut.
*/
let collator = require('./collator.js');
let numberformatter = require('./numberformat.js');
let displaynames = require('./displaynames.js');
let localedisplaynames = require('./localedisplaynames.js')
let likely_subtags = require('./likely_subtags.js');
let datetime_fmt = require('./datetime_fmt.js');
let list_fmt = require('./list_fmt.js');
let plural_rules = require('./plural_rules.js');
let rdt_fmt = require('./relativedatetime_fmt.js');
let segmenter = require('./segmenter.js');
/**
* TODOs:
* 1. Handle other types of test cases.
*/
/**
* 21-Mar-2024: Adding datetime and list formatting.
* 16-Sep-2022: Modularize this, moving functions to other files.
* 29-Aug-2022: Adding basic decimal test
* 16-Aug-2022: Collation tests all working now.
* 09-Aug-2022: Using updated Collation test data, about 10% of the tests fail
*
* Started 28-July-2022, ccornelius@google.com
*/
let doLogInput = 0; // TODO: How to turn this on from command line?
let doLogOutput = 0;
// Test type support. Add new items as they are implemented
const testTypes = {
TestCollationShort : Symbol("collation"),
TestCollShiftShort : Symbol("coll_shift_short"),
TestCollNonignorableShort : Symbol("coll_nonignorable_short"),
TestDecimalFormat : Symbol("decimal_fmt"),
TestNumberFormat : Symbol("number_fmt"),
TestDateTimeFormat : Symbol("datetime_fmt"),
TestPluralRules : Symbol("plural_rules"),
TestDisplayNames : Symbol("display_names"),
TestListFmt : Symbol("list_fmt"),
TestLocaleDisplayNames : Symbol("language_display_name"),
TestRelativeDateTimeFormat : Symbol("rdt_fmt"),
TestSegmenter : Symbol("segmenter")
};
const supported_test_types = [
Symbol("collation"),
Symbol("coll_shift_short"),
Symbol("coll_nonignorable_short"),
Symbol("decimal_fmt"),
Symbol("number_fmt"),
Symbol("display_names"),
Symbol("lang_names"),
Symbol("language_display_name"),
Symbol("local_info"),
Symbol("datetime_fmt"),
Symbol("list_fmt"),
Symbol("plural_rules"),
Symbol("rdt_fmt"),
Symbol("segmenter")
];
const supported_tests_json = {
"supported_tests": [
"collation",
"coll_shift_short",
"decimal_fmt",
"number_fmt",
"display_names",
"lang_names",
"language_display_name",
"list_fmt",
"plural_rules",
"rdt_fmt",
"segmenter"
]};
// Test line-by-line input, with output as string.
// Check on using Intl functions, e.g., DateTimeFormat()
let readline = require('readline');
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
/**
* Given a JSON data structure, check for "test_type". If not present, then
* infer the test ID from the label
* !!! Not used now.
*/
function parseJsonForTestId(parsed) {
let testId = parsed["test_type"];
if (testId == "coll_shift_short" || testId == "collation") {
return testTypes.TestCollationShort;
}
if (testId == "collation") {
return testTypes.TestCollationShort;
}
if (testId == "coll_shift_short") {
return testTypes.TestCollShiftShort;
}
if (testId == "coll_nonignorable_short"){
return testTypes.TestCollNonignorableShort;
}
if (testId == "decimal_fmt" || testId == "number_fmt") {
return testTypes.TestDecimalFormat;
}
if (testId == "display_names") {
return testTypes.TestDisplayNames;
}
if (testId == "language_display_name" || testId == "lang_names") {
return testTypes.TestLocaleDisplayNames;
}
if (testId == "datetime_fmt") {
return testTypes.TestDateTimeFormat;
}
if (testId == "list_fmt") {
return testTypes.TestListFmt;
}
if (testId == "rdt_fmt") {
return testTypes.TestRelativeDateTimeFmt;
}
if (testId == "plural_rules") {
return testTypes.TestPluralRules;
}
console.log("#*********** NODE Unknown test type = " + testId);
return null;
}
// Read JSON tests, each on a single line.
// Process the test and output a line of JSON results.
let lineId = 0;
rl.on('line', function(line) {
if (doLogInput > 0) {
console.log("## NODE RECEIVED " + lineId + ' ' + line + ' !!!!!');
}
// Protocol:
// #VERSION to get version information
// {....} test line
// EXIT
// Check for commands starting with "#".
if (line == "#VERSION") {
// JSON output of the test enviroment.
const versionJson = {'platform': 'NodeJS',
'platformVersion': process.version,
'icuVersion': process.versions.icu,
'cldrVersion': process.versions.cldr
};
// TODO: Make this more specific for JSON info.
lineOut = JSON.stringify(versionJson);
process.stdout.write(lineOut);
} else
if (line == "#EXIT") {
process.exit();
} else
if (line == "#TESTS") {
lineOut = JSON.stringify(supported_tests_json);
process.stdout.write(lineOut);
}
else {
// Handle test cases.
let testId;
let parsedJson;
try {
parsedJson = JSON.parse(line);
} catch (error) {
outputLine = {'error': error,
'message': 'Cannot parse input line',
'input_line': line,
"testId": testId};
// Send result to stdout for verification
const jsonOut = JSON.stringify(outputLine);
if (doLogOutput > 0) {
console.log("## ERROR " + lineId + ' ' + outputLine + ' !!!!!');
}
process.stdout.write(jsonOut);
}
if (doLogInput > 0) {
console.log("#----- PARSED JSON: " + JSON.stringify(parsedJson));
}
// Handle the string directly to call the correct function.
const test_type = parsedJson["test_type"];
if (test_type == "coll_shift_short" || test_type == "collation") {
outputLine = collator.testCollationShort(parsedJson);
} else
if (test_type == "decimal_fmt" || test_type == "number_fmt") {
outputLine = numberformatter.testDecimalFormat(parsedJson, doLogInput);
} else
if (test_type == "display_names") {
outputLine = displaynames.testDisplayNames(parsedJson);
} else
if (test_type == "language_display_name" || test_type == "lang_names") {
outputLine = localedisplaynames.testLocaleDisplayNames(parsedJson);
} else
if (test_type == "likely_subtags") {
outputLine = likely_subtags.testLikelySubtags(parsedJson);
} else
if (test_type == "datetime_fmt") {
outputLine = datetime_fmt.testDateTimeFmt(parsedJson);
} else
if (test_type == "list_fmt") {
outputLine = list_fmt.testListFmt(parsedJson);
} else
if (test_type == "rdt_fmt") {
outputLine = rdt_fmt.testRelativeDateTimeFmt(parsedJson);
} else
if (test_type == "plural_rules") {
outputLine = plural_rules.testPluralRules(parsedJson);
} else
if (test_type == "segmenter") {
outputLine = segmenter.testSegmenter(parsedJson);
} else {
// UNSUPPORTED TEST TYPE!
outputLine = {'label': 'UNKNOWN',
'error': 'unknown test type',
'error_detail': 'Requested unsupported test type'
'test_type': test_type,
'unsupported_test': test_type};
}
const jsonOut = JSON.stringify(outputLine);
if ('error' in outputLine && !('unsupported' in outputLine)) {
// To get the attention of the driver
console.log("#!! ERROR in NODE call: test_type: " + test_type + ", " + JSON.stringify(outputLine));
}
// Send result to stdout for verification
process.stdout.write(jsonOut + '\n');
if (doLogOutput > 0) {
console.log("##### NODE RETURNS " + lineId + ' ' + jsonOut + ' !!!!!');
}
}
lineId += 1;
}
)