| 1 |
// Copyright 2016 Mozilla Corporation. All rights reserved. |
| 2 |
// This code is governed by the license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
esid: sec-intl.getcanonicallocales |
| 6 |
description: Test Intl.getCanonicalLocales for step 5. |
| 7 |
info: | |
| 8 |
9.2.1 CanonicalizeLocaleList (locales) |
| 9 |
5. Let len be ? ToLength(? Get(O, "length")). |
| 10 |
includes: [compareArray.js] |
| 11 |
features: [Symbol] |
| 12 |
---*/ |
| 13 |
|
| 14 |
var locales = { |
| 15 |
'0': 'en-US', |
| 16 |
}; |
| 17 |
|
| 18 |
Object.defineProperty(locales, "length", { |
| 19 |
get: function() { throw new Test262Error() } |
| 20 |
}); |
| 21 |
|
| 22 |
assert.throws(Test262Error, function() { |
| 23 |
Intl.getCanonicalLocales(locales); |
| 24 |
}, "should throw if locales.length throws"); |
| 25 |
|
| 26 |
var locales = { |
| 27 |
'0': 'en-US', |
| 28 |
'1': 'pt-BR', |
| 29 |
}; |
| 30 |
|
| 31 |
Object.defineProperty(locales, "length", { |
| 32 |
get: function() { return "1" } |
| 33 |
}); |
| 34 |
|
| 35 |
assert.compareArray( |
| 36 |
Intl.getCanonicalLocales(locales), |
| 37 |
['en-US'], |
| 38 |
"should return one element if locales.length is '1'" |
| 39 |
); |
| 40 |
|
| 41 |
var locales = { |
| 42 |
'0': 'en-US', |
| 43 |
'1': 'pt-BR', |
| 44 |
}; |
| 45 |
|
| 46 |
Object.defineProperty(locales, "length", { |
| 47 |
get: function() { return 1.3 } |
| 48 |
}); |
| 49 |
|
| 50 |
assert.compareArray( |
| 51 |
Intl.getCanonicalLocales(locales), |
| 52 |
['en-US'], |
| 53 |
"should return one element if locales.length is 1.3" |
| 54 |
); |
| 55 |
|
| 56 |
var locales = { |
| 57 |
'0': 'en-US', |
| 58 |
'1': 'pt-BR', |
| 59 |
}; |
| 60 |
|
| 61 |
Object.defineProperty(locales, "length", { |
| 62 |
get: function() { return Symbol("1.8") } |
| 63 |
}); |
| 64 |
|
| 65 |
assert.throws(TypeError, function() { |
| 66 |
Intl.getCanonicalLocales(locales); |
| 67 |
}, "should throw if locales.length is a Symbol"); |
| 68 |
|
| 69 |
var locales = { |
| 70 |
'0': 'en-US', |
| 71 |
'1': 'pt-BR', |
| 72 |
}; |
| 73 |
|
| 74 |
Object.defineProperty(locales, "length", { |
| 75 |
get: function() { return -Infinity } |
| 76 |
}); |
| 77 |
|
| 78 |
assert.compareArray( |
| 79 |
Intl.getCanonicalLocales(locales), |
| 80 |
[], |
| 81 |
"should return empty array if locales.length is -Infinity" |
| 82 |
); |
| 83 |
|
| 84 |
var locales = { |
| 85 |
length: -Math.pow(2, 32) + 1 |
| 86 |
}; |
| 87 |
|
| 88 |
Object.defineProperty(locales, "0", { |
| 89 |
get: function() { throw new Error("must not be gotten!"); } |
| 90 |
}) |
| 91 |
|
| 92 |
assert.compareArray( |
| 93 |
Intl.getCanonicalLocales(locales), |
| 94 |
[], |
| 95 |
"should return empty array if locales.length is a negative value" |
| 96 |
); |
| 97 |
|
| 98 |
var count = 0; |
| 99 |
var locs = { get length() { if (count++ > 0) throw 42; return 0; } }; |
| 100 |
var locales = Intl.getCanonicalLocales(locs); // shouldn't throw 42 |
| 101 |
assert.sameValue(locales.length, 0); |
| 102 |
|
| 103 |
reportCompare(0, 0); |