Skip to content

Commit 8cd563f

Browse files
authored
Intl: Add substantive tests for Array.prototype.toLocaleString (#3741)
1 parent 3d939ef commit 8cd563f

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (C) 2022 Richard Gibson. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-array.prototype.tolocalestring
6+
description: >
7+
The toLocaleString method of each non-undefined non-null element
8+
must be called with no arguments.
9+
info: |
10+
Array.prototype.toLocaleString ( [ _reserved1_ [ , _reserved2_ ] ] )
11+
12+
4. Let _R_ be the empty String.
13+
5. Let _k_ be 0.
14+
6. Repeat, while _k_ < _len_,
15+
a. If _k_ > 0, then
16+
i. Set _R_ to the string-concatenation of _R_ and _separator_.
17+
b. Let _nextElement_ be ? Get(_array_, ! ToString(_k_)).
18+
c. If _nextElement_ is not *undefined* or *null*, then
19+
i. Let _S_ be ? ToString(? Invoke(_nextElement_, *"toLocaleString"*)).
20+
ii. Set _R_ to the string-concatenation of _R_ and _S_.
21+
d. Increase _k_ by 1.
22+
7. Return _R_.
23+
includes: [compareArray.js]
24+
---*/
25+
26+
const unique = {
27+
toString() {
28+
return "<sentinel object>";
29+
}
30+
};
31+
32+
const testCases = [
33+
{ label: "no arguments", args: [] },
34+
{ label: "undefined locale", args: [undefined] },
35+
{ label: "string locale", args: ["ar"] },
36+
{ label: "object locale", args: [unique] },
37+
{ label: "undefined locale and options", args: [undefined, unique] },
38+
{ label: "string locale and options", args: ["zh", unique] },
39+
{ label: "object locale and options", args: [unique, unique] },
40+
{ label: "extra arguments", args: [unique, unique, unique] },
41+
];
42+
43+
for (const { label, args } of testCases) {
44+
assert.sameValue([undefined].toLocaleString(...args), "",
45+
`must skip undefined elements when provided ${label}`);
46+
}
47+
for (const { label, args, expectedArgs } of testCases) {
48+
assert.sameValue([null].toLocaleString(...args), "",
49+
`must skip null elements when provided ${label}`);
50+
}
51+
52+
if (typeof Intl !== "object") {
53+
for (const { label, args } of testCases) {
54+
const spy = {
55+
toLocaleString(...receivedArgs) {
56+
assert.compareArray(receivedArgs, [],
57+
`must invoke element toLocaleString with no arguments when provided ${label}`);
58+
return "ok";
59+
}
60+
};
61+
assert.sameValue([spy].toLocaleString(...args), "ok");
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (C) 2022 Richard Gibson. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sup-array.prototype.tolocalestring
6+
description: >
7+
The toLocaleString method of each non-undefined non-null element
8+
must be called with two arguments.
9+
info: |
10+
Array.prototype.toLocaleString ( [ _locales_ [ , _options_ ] ] )
11+
12+
4. Let _R_ be the empty String.
13+
5. Let _k_ be 0.
14+
6. Repeat, while _k_ &lt; _len_,
15+
a. If _k_ &gt; 0, then
16+
i. Set _R_ to the string-concatenation of _R_ and _separator_.
17+
b. Let _nextElement_ be ? Get(_array_, ! ToString(_k_)).
18+
c. If _nextElement_ is not *undefined* or *null*, then
19+
i. Let _S_ be ? ToString(? Invoke(_nextElement_, *"toLocaleString"*, &laquo; _locales_, _options_ &raquo;)).
20+
ii. Set _R_ to the string-concatenation of _R_ and _S_.
21+
d. Increase _k_ by 1.
22+
7. Return _R_.
23+
includes: [compareArray.js]
24+
---*/
25+
26+
const unique = {
27+
toString() {
28+
return "<sentinel object>";
29+
}
30+
};
31+
32+
const testCases = [
33+
{ label: "no arguments", args: [], expectedArgs: [undefined, undefined] },
34+
{ label: "undefined locale", args: [undefined], expectedArgs: [undefined, undefined] },
35+
{ label: "string locale", args: ["ar"], expectedArgs: ["ar", undefined] },
36+
{ label: "object locale", args: [unique], expectedArgs: [unique, undefined] },
37+
{ label: "undefined locale and options", args: [undefined, unique], expectedArgs: [undefined, unique] },
38+
{ label: "string locale and options", args: ["zh", unique], expectedArgs: ["zh", unique] },
39+
{ label: "object locale and options", args: [unique, unique], expectedArgs: [unique, unique] },
40+
{ label: "extra arguments", args: [unique, unique, unique], expectedArgs: [unique, unique] },
41+
];
42+
43+
for (const { label, args, expectedArgs } of testCases) {
44+
assert.sameValue([undefined].toLocaleString(...args), "",
45+
`must skip undefined elements when provided ${label}`);
46+
}
47+
for (const { label, args, expectedArgs } of testCases) {
48+
assert.sameValue([null].toLocaleString(...args), "",
49+
`must skip null elements when provided ${label}`);
50+
}
51+
52+
for (const { label, args, expectedArgs } of testCases) {
53+
const spy = {
54+
toLocaleString(...receivedArgs) {
55+
assert.compareArray(receivedArgs, expectedArgs,
56+
`must invoke element toLocaleString with expected arguments when provided ${label}`);
57+
return "ok";
58+
}
59+
};
60+
assert.sameValue([spy].toLocaleString(...args), "ok");
61+
}

0 commit comments

Comments
 (0)