Skip to content

Commit dc84858

Browse files
MaleDongtargos
MaleDong
authored andcommitted
test,util: add missing tests and conditions
1) Add missing unit tests by `ucs-2` in different kinds of cases. 2) Add missing unit tests by `usc-2` in different kinds of cases. 3) Fix a bug:We cannot find `ucs-2` in `case 5`'s `if` condition after `toLowerCase()` PR-URL: #21455 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 1044baf commit dc84858

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

benchmark/util/normalize-encoding.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@ const common = require('../common.js');
44
const assert = require('assert');
55

66
const groupedInputs = {
7-
group_common: ['undefined', 'utf8', 'utf-8', 'base64', 'binary', 'latin1'],
8-
group_upper: ['UTF-8', 'UTF8', 'UCS2', 'UTF-16LE', 'UTF16LE', 'BASE64'],
9-
group_uncommon: [ 'foo', '1', 'false', 'undefined', '[]'],
7+
group_common: ['undefined', 'utf8', 'utf-8', 'base64',
8+
'binary', 'latin1', 'ucs-2', 'usc-2'],
9+
group_upper: ['UTF-8', 'UTF8', 'UCS2', 'UTF-16LE',
10+
'UTF16LE', 'BASE64', 'UCS-2', 'USC-2'],
11+
group_uncommon: ['foo', '1', 'false', 'undefined', '[]', '{}'],
1012
group_misc: ['', 'utf16le', 'usc2', 'hex', 'HEX', 'BINARY']
1113
};
1214

1315
const inputs = [
14-
'', 'utf8', 'utf-8', 'UTF-8',
15-
'UTF8', 'Utf8', 'uTf-8', 'utF-8', 'ucs2',
16-
'UCS2', 'utf16le', 'utf-16le', 'UTF-16LE', 'UTF16LE',
16+
'',
17+
'utf8', 'utf-8', 'UTF-8',
18+
'UTF8', 'Utf8', 'uTf-8', 'utF-8',
19+
'ucs2', 'UCS2', 'UcS2',
20+
'USC2', 'usc2', 'uSc2',
21+
'ucs-2', 'UCS-2', 'UcS-2',
22+
'usc-2', 'USC-2', 'uSc-2',
23+
'utf16le', 'utf-16le', 'UTF-16LE', 'UTF16LE',
1724
'binary', 'BINARY', 'latin1', 'base64', 'BASE64',
18-
'hex', 'HEX', 'foo', '1', 'false', 'undefined', '[]'];
25+
'hex', 'HEX', 'foo', '1', 'false', 'undefined', '[]', '{}'];
1926

2027
const bench = common.createBenchmark(main, {
2128
input: inputs.concat(Object.keys(groupedInputs)),
@@ -42,6 +49,8 @@ function getInput(input) {
4249
return [undefined];
4350
case '[]':
4451
return [[]];
52+
case '{}':
53+
return [{}];
4554
default:
4655
return [input];
4756
}
@@ -53,7 +62,7 @@ function main({ input, n }) {
5362
var noDead = '';
5463

5564
bench.start();
56-
for (var i = 0; i < n; i += 1) {
65+
for (var i = 0; i < n; ++i) {
5766
for (var j = 0; j < inputs.length; ++j) {
5867
noDead = normalizeEncoding(inputs[j]);
5968
}

lib/internal/util.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function slowCases(enc) {
115115
if (enc === 'ucs2' || enc === 'UCS2') return 'utf16le';
116116
enc = `${enc}`.toLowerCase();
117117
if (enc === 'utf8') return 'utf8';
118-
if (enc === 'ucs2' || enc === 'UCS2') return 'utf16le';
118+
if (enc === 'ucs2') return 'utf16le';
119119
break;
120120
case 3:
121121
if (enc === 'hex' || enc === 'HEX' || `${enc}`.toLowerCase() === 'hex')
@@ -131,6 +131,7 @@ function slowCases(enc) {
131131
if (enc === 'utf-8') return 'utf8';
132132
if (enc === 'ascii') return 'ascii';
133133
if (enc === 'usc-2') return 'utf16le';
134+
if (enc === 'ucs-2') return 'utf16le';
134135
break;
135136
case 6:
136137
if (enc === 'base64') return 'base64';

test/parallel/test-internal-util-normalizeencoding.js

+7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ const tests = [
1818
['utF-8', 'utf8'],
1919
['ucs2', 'utf16le'],
2020
['UCS2', 'utf16le'],
21+
['ucs-2', 'utf16le'],
22+
['UCS-2', 'utf16le'],
23+
['UcS-2', 'utf16le'],
2124
['utf16le', 'utf16le'],
2225
['utf-16le', 'utf16le'],
2326
['UTF-16LE', 'utf16le'],
2427
['UTF16LE', 'utf16le'],
28+
['usc-2', 'utf16le'],
29+
['USC-2', 'utf16le'],
30+
['uSc-2', 'utf16le'],
2531
['binary', 'latin1'],
2632
['BINARY', 'latin1'],
2733
['latin1', 'latin1'],
@@ -36,6 +42,7 @@ const tests = [
3642
[NaN, undefined],
3743
[0, undefined],
3844
[[], undefined],
45+
[{}, undefined]
3946
];
4047

4148
tests.forEach((e, i) => {

0 commit comments

Comments
 (0)