|
| 1 | +// Copyright 2012 Mozilla Corporation. All rights reserved. |
| 2 | +// This code is governed by the BSD license found in the LICENSE file. |
| 3 | + |
| 4 | +/*--- |
| 5 | +esid: sec-initializedatetimeformat |
| 6 | +description: > |
| 7 | + Tests that the options numberingSystem and calendar can be set through |
| 8 | + either the locale or the options. |
| 9 | +author: Norbert Lindenberg, Daniel Ehrenberg |
| 10 | +includes: [testIntl.js] |
| 11 | +---*/ |
| 12 | + |
| 13 | +let defaultLocale = new Intl.DateTimeFormat().resolvedOptions().locale; |
| 14 | + |
| 15 | +let supportedNumberingSystems = ["latn", "arab"].filter(nu => |
| 16 | + new Intl.DateTimeFormat(defaultLocale + "-u-nu-" + nu) |
| 17 | + .resolvedOptions().numberingSystem === nu |
| 18 | +); |
| 19 | + |
| 20 | +let supportedCalendars = ["gregory", "chinese"].filter(ca => |
| 21 | + new Intl.DateTimeFormat(defaultLocale + "-u-ca-" + ca) |
| 22 | + .resolvedOptions().calendar === ca |
| 23 | +); |
| 24 | + |
| 25 | +let options = [ |
| 26 | + {key: "nu", property: "numberingSystem", type: "string", values: supportedNumberingSystems}, |
| 27 | + {key: "ca", property: "calendar", type: "string", values: supportedCalendars} |
| 28 | +]; |
| 29 | + |
| 30 | +options.forEach(function (option) { |
| 31 | + let dateTimeFormat, opt, result; |
| 32 | + |
| 33 | + // find out which values are supported for a property in the default locale |
| 34 | + let supportedValues = []; |
| 35 | + option.values.forEach(function (value) { |
| 36 | + opt = {}; |
| 37 | + opt[option.property] = value; |
| 38 | + dateTimeFormat = new Intl.DateTimeFormat([defaultLocale], opt); |
| 39 | + result = dateTimeFormat.resolvedOptions()[option.property]; |
| 40 | + if (result !== undefined && supportedValues.indexOf(result) === -1) { |
| 41 | + supportedValues.push(result); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + // verify that the supported values can also be set through the locale |
| 46 | + supportedValues.forEach(function (value) { |
| 47 | + dateTimeFormat = new Intl.DateTimeFormat([defaultLocale + "-u-" + option.key + "-" + value]); |
| 48 | + result = dateTimeFormat.resolvedOptions()[option.property]; |
| 49 | + assert.sameValue(result, value, "Property " + option.property + " couldn't be set through locale extension key " + option.key + "."); |
| 50 | + }); |
| 51 | + |
| 52 | + // verify that the options setting overrides the locale setting |
| 53 | + supportedValues.forEach(function (value) { |
| 54 | + let otherValue; |
| 55 | + option.values.forEach(function (possibleValue) { |
| 56 | + if (possibleValue !== value) { |
| 57 | + otherValue = possibleValue; |
| 58 | + } |
| 59 | + }); |
| 60 | + if (otherValue !== undefined) { |
| 61 | + opt = {}; |
| 62 | + opt[option.property] = value; |
| 63 | + dateTimeFormat = new Intl.DateTimeFormat([defaultLocale + "-u-" + option.key + "-" + otherValue], opt); |
| 64 | + result = dateTimeFormat.resolvedOptions()[option.property]; |
| 65 | + assert.sameValue(result, value, "Options value for property " + option.property + " doesn't override locale extension key " + option.key + "."); |
| 66 | + } |
| 67 | + }); |
| 68 | +}); |
0 commit comments