dayPeriod.js (2379B)
1 // |reftest| skip-if(!this.hasOwnProperty('Intl')||!this.hasOwnProperty('addIntlExtras')) 2 3 addMozIntlDisplayNames(this); 4 5 const tests = { 6 "en": { 7 long: { 8 "am": "AM", 9 "pm": "PM", 10 }, 11 short: { 12 "am": "AM", 13 "pm": "PM", 14 }, 15 narrow: { 16 "am": "a", 17 "pm": "p", 18 }, 19 }, 20 "de": { 21 long: { 22 "am": "AM", 23 "pm": "PM", 24 }, 25 short: {}, 26 narrow: {}, 27 }, 28 "fr": { 29 long: { 30 "am": "AM", 31 "pm": "PM", 32 }, 33 short: {}, 34 narrow: {}, 35 }, 36 "es": { 37 long: { 38 "am": "a.\xA0m.", 39 "pm": "p.\xA0m.", 40 }, 41 short: { 42 "am": "a.\u202Fm.", 43 "pm": "p.\u202Fm.", 44 }, 45 narrow: {}, 46 }, 47 "zh": { 48 long: { 49 "am": "上午", 50 "pm": "下午", 51 }, 52 short: {}, 53 narrow: {}, 54 }, 55 }; 56 57 for (let [locale, localeTests] of Object.entries(tests)) { 58 let defaultCalendar = new Intl.DateTimeFormat(locale).resolvedOptions().calendar; 59 60 for (let [style, styleTests] of Object.entries(localeTests)) { 61 let dn = new Intl.DisplayNames(locale, {type: "dayPeriod", style}); 62 63 let resolved = dn.resolvedOptions(); 64 assertEq(resolved.locale, locale); 65 assertEq(resolved.calendar, defaultCalendar); 66 assertEq(resolved.style, style); 67 assertEq(resolved.type, "dayPeriod"); 68 assertEq(resolved.fallback, "code"); 69 70 let inheritedTests = {...localeTests.long, ...localeTests.short, ...localeTests.narrow}; 71 for (let [dayPeriod, expected] of Object.entries({...inheritedTests, ...styleTests})) { 72 assertEq(dn.of(dayPeriod), expected); 73 74 // Also works with objects. 75 assertEq(dn.of(Object(dayPeriod)), expected); 76 } 77 } 78 } 79 80 { 81 let dn = new Intl.DisplayNames("en", {type: "dayPeriod"}); 82 83 // Performs ToString on the input and then validates the stringified result. 84 assertThrowsInstanceOf(() => dn.of(), RangeError); 85 assertThrowsInstanceOf(() => dn.of(null), RangeError); 86 assertThrowsInstanceOf(() => dn.of(Symbol()), TypeError); 87 assertThrowsInstanceOf(() => dn.of(0), RangeError); 88 assertThrowsInstanceOf(() => dn.of(1), RangeError); 89 90 // Throws an error if not one of ["am", "pm"]. 91 assertThrowsInstanceOf(() => dn.of(""), RangeError); 92 assertThrowsInstanceOf(() => dn.of("AM"), RangeError); 93 assertThrowsInstanceOf(() => dn.of("PM"), RangeError); 94 } 95 96 if (typeof reportCompare === "function") 97 reportCompare(true, true);