numbering-system-calendar-options.js (2773B)
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-createdatetimeformat 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 ---*/ 11 12 let defaultLocale = new Intl.DateTimeFormat().resolvedOptions().locale; 13 14 let supportedNumberingSystems = ["latn", "arab"].filter(nu => 15 new Intl.DateTimeFormat(defaultLocale + "-u-nu-" + nu) 16 .resolvedOptions().numberingSystem === nu 17 ); 18 19 let supportedCalendars = ["gregory", "chinese"].filter(ca => 20 new Intl.DateTimeFormat(defaultLocale + "-u-ca-" + ca) 21 .resolvedOptions().calendar === ca 22 ); 23 24 let options = [ 25 {key: "nu", property: "numberingSystem", type: "string", values: supportedNumberingSystems}, 26 {key: "ca", property: "calendar", type: "string", values: supportedCalendars} 27 ]; 28 29 options.forEach(function (option) { 30 let dateTimeFormat, opt, result; 31 32 // find out which values are supported for a property in the default locale 33 let supportedValues = []; 34 option.values.forEach(function (value) { 35 opt = {}; 36 opt[option.property] = value; 37 dateTimeFormat = new Intl.DateTimeFormat([defaultLocale], opt); 38 result = dateTimeFormat.resolvedOptions()[option.property]; 39 if (result !== undefined && supportedValues.indexOf(result) === -1) { 40 supportedValues.push(result); 41 } 42 }); 43 44 // verify that the supported values can also be set through the locale 45 supportedValues.forEach(function (value) { 46 dateTimeFormat = new Intl.DateTimeFormat([defaultLocale + "-u-" + option.key + "-" + value]); 47 result = dateTimeFormat.resolvedOptions()[option.property]; 48 assert.sameValue(result, value, "Property " + option.property + " couldn't be set through locale extension key " + option.key + "."); 49 }); 50 51 // verify that the options setting overrides the locale setting 52 supportedValues.forEach(function (value) { 53 let otherValue; 54 option.values.forEach(function (possibleValue) { 55 if (possibleValue !== value) { 56 otherValue = possibleValue; 57 } 58 }); 59 if (otherValue !== undefined) { 60 opt = {}; 61 opt[option.property] = value; 62 dateTimeFormat = new Intl.DateTimeFormat([defaultLocale + "-u-" + option.key + "-" + otherValue], opt); 63 result = dateTimeFormat.resolvedOptions()[option.property]; 64 assert.sameValue(result, value, "Options value for property " + option.property + " doesn't override locale extension key " + option.key + "."); 65 } 66 }); 67 }); 68 69 reportCompare(0, 0);