canonicalize-locale-list-take-locale.js (1618B)
1 // Copyright 2019 Google Inc. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 esid: sec-intl.locale 6 description: > 7 Verifies CanonicalizeLocaleList will take Intl.Locale as locales. 8 info: | 9 CanonicalizeLocaleList ( locales ) 10 3. If Type(locales) is String or locales has an [[InitializedLocale]] internal slot, then 11 a. Let O be CreateArrayFromList(« locales »). 12 13 c. iii. If Type(kValue) is Object and kValue has an [[InitializedLocale]] internal slot, then 14 1. Let tag be kValue.[[Locale]]. 15 iv. Else, 16 1. Let tag be ? ToString(kValue). 17 features: [Intl.Locale] 18 ---*/ 19 20 const tag = "ar"; 21 const tag2 = "fa"; 22 const tag3 = "zh"; 23 const loc = new Intl.Locale(tag); 24 25 // Monkey-patching Intl.Locale 26 class PatchedLocale extends Intl.Locale { 27 constructor(tag, options) { 28 super(tag, options); 29 } 30 toString() { 31 // this should NOT get called. 32 assert(false, "toString should not be called") 33 } 34 } 35 const ploc = new PatchedLocale(tag2); 36 37 // Test Intl.Locale as the only argument 38 let res = Intl.getCanonicalLocales(loc); 39 assert.sameValue(res.length, 1); 40 assert.sameValue(res[0], tag); 41 42 // Test Monkey-patched Intl.Locale as the only argument 43 res = Intl.getCanonicalLocales(ploc); 44 assert.sameValue(res.length, 1); 45 assert.sameValue(res[0], tag2); 46 47 // Test Intl.Locale and the Monkey-patched one are in 48 // array. 49 res = Intl.getCanonicalLocales([loc, tag3, ploc]); 50 assert.sameValue(res.length, 3); 51 assert.sameValue(res[0], tag); 52 assert.sameValue(res[1], tag3); 53 assert.sameValue(res[2], tag2); 54 55 reportCompare(0, 0);