|
| 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_ < _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"*, « _locales_, _options_ »)). |
| 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