Debian Codegraph @

js/src/tests/non262/Intl/DisplayNames/dayPeriod.js

raw
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 narrow: {},
13 },
14 "de": {
15 long: {
16 "am": "AM",
17 "pm": "PM",
18 },
19 short: {},
20 narrow: {},
21 },
22 "fr": {
23 long: {
24 "am": "AM",
25 "pm": "PM",
26 },
27 short: {},
28 narrow: {},
29 },
30 "zh": {
31 long: {
32 "am": "上午",
33 "pm": "下午",
34 },
35 short: {},
36 narrow: {},
37 },
38 };
39
40 for (let [locale, localeTests] of Object.entries(tests)) {
41 let defaultCalendar = new Intl.DateTimeFormat(locale).resolvedOptions().calendar;
42
43 for (let [style, styleTests] of Object.entries(localeTests)) {
44 let dn = new Intl.DisplayNames(locale, {type: "dayPeriod", style});
45
46 let resolved = dn.resolvedOptions();
47 assertEq(resolved.locale, locale);
48 assertEq(resolved.calendar, defaultCalendar);
49 assertEq(resolved.style, style);
50 assertEq(resolved.type, "dayPeriod");
51 assertEq(resolved.fallback, "code");
52
53 let inheritedTests = {...localeTests.long, ...localeTests.short, ...localeTests.narrow};
54 for (let [dayPeriod, expected] of Object.entries({...inheritedTests, ...styleTests})) {
55 assertEq(dn.of(dayPeriod), expected);
56
57 // Also works with objects.
58 assertEq(dn.of(Object(dayPeriod)), expected);
59 }
60 }
61 }
62
63 {
64 let dn = new Intl.DisplayNames("en", {type: "dayPeriod"});
65
66 // Performs ToString on the input and then validates the stringified result.
67 assertThrowsInstanceOf(() => dn.of(), RangeError);
68 assertThrowsInstanceOf(() => dn.of(null), RangeError);
69 assertThrowsInstanceOf(() => dn.of(Symbol()), TypeError);
70 assertThrowsInstanceOf(() => dn.of(0), RangeError);
71 assertThrowsInstanceOf(() => dn.of(1), RangeError);
72
73 // Throws an error if not one of ["am", "pm"].
74 assertThrowsInstanceOf(() => dn.of(""), RangeError);
75 assertThrowsInstanceOf(() => dn.of("AM"), RangeError);
76 assertThrowsInstanceOf(() => dn.of("PM"), RangeError);
77 }
78
79 if (typeof reportCompare === "function")
80 reportCompare(true, true);