Debian Codegraph @

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

raw
1 // |reftest| skip-if(!this.hasOwnProperty('Intl'))
2
3 const tests = {
4 "en": {
5 long: {
6 "gregory": "Gregorian Calendar",
7 "iso8601": "ISO-8601 Calendar",
8 "japanese": "Japanese Calendar",
9 "islamic-civil": "Hijri Calendar (tabular, civil epoch)",
10 "islamicc": "Hijri Calendar (tabular, civil epoch)",
11 "ethioaa": "Ethiopic Amete Alem Calendar",
12 "ethiopic-amete-alem": "Ethiopic Amete Alem Calendar",
13 },
14 short: {},
15 narrow: {},
16 },
17 "de": {
18 long: {
19 "gregory": "Gregorianischer Kalender",
20 "iso8601": "ISO-8601-Kalender",
21 "japanese": "Japanischer Kalender",
22 "islamic-civil": "Bürgerlicher Hidschri-Kalender (tabellarisch)",
23 "islamicc": "Bürgerlicher Hidschri-Kalender (tabellarisch)",
24 "ethioaa": "Äthiopischer Amätä-Aläm-Kalender",
25 "ethiopic-amete-alem": "Äthiopischer Amätä-Aläm-Kalender",
26 },
27 short: {},
28 narrow: {},
29 },
30 "fr": {
31 long: {
32 "gregory": "calendrier grégorien",
33 "iso8601": "calendrier ISO 8601",
34 "japanese": "calendrier japonais",
35 "islamic-civil": "calendrier hégirien (tabulaire, époque civile)",
36 "islamicc": "calendrier hégirien (tabulaire, époque civile)",
37 "ethioaa": "calendrier éthiopien Amete Alem",
38 "ethiopic-amete-alem": "calendrier éthiopien Amete Alem",
39 },
40 short: {},
41 narrow: {},
42 },
43 "zh": {
44 long: {
45 "gregory": "公历",
46 "iso8601": "国际标准历法",
47 "japanese": "和历",
48 "islamic-civil": "表格式伊斯兰历(民用纪元)",
49 "islamicc": "表格式伊斯兰历(民用纪元)",
50 "ethioaa": "埃塞俄比亚阿米特阿莱姆日历",
51 "ethiopic-amete-alem": "埃塞俄比亚阿米特阿莱姆日历",
52 },
53 short: {},
54 narrow: {},
55 },
56 };
57
58 for (let [locale, localeTests] of Object.entries(tests)) {
59 for (let [style, styleTests] of Object.entries(localeTests)) {
60 let dn = new Intl.DisplayNames(locale, {type: "calendar", style});
61
62 let resolved = dn.resolvedOptions();
63 assertEq(resolved.locale, locale);
64 assertEq(resolved.style, style);
65 assertEq(resolved.type, "calendar");
66 assertEq(resolved.fallback, "code");
67
68 let inheritedTests = {...localeTests.long, ...localeTests.short, ...localeTests.narrow};
69 for (let [calendar, expected] of Object.entries({...inheritedTests, ...styleTests})) {
70 assertEq(dn.of(calendar), expected);
71
72 // Also works with objects.
73 assertEq(dn.of(Object(calendar)), expected);
74 }
75 }
76 }
77
78 {
79 let dn = new Intl.DisplayNames("en", {type: "calendar"});
80
81 // Performs ToString on the input and then validates the stringified result.
82 assertThrowsInstanceOf(() => dn.of(), RangeError);
83 assertThrowsInstanceOf(() => dn.of(undefined), RangeError);
84 assertThrowsInstanceOf(() => dn.of(Symbol()), TypeError);
85 assertThrowsInstanceOf(() => dn.of(0), RangeError);
86
87 // Throws an error if |code| isn't a well-formed calendar type.
88 assertThrowsInstanceOf(() => dn.of("gregorian"), RangeError);
89 assertThrowsInstanceOf(() => dn.of("grëgory"), RangeError);
90 assertThrowsInstanceOf(() => dn.of("grēgory"), RangeError);
91 }
92
93 // Test fallback behaviour.
94 {
95 let dn1 = new Intl.DisplayNames("en", {type: "calendar"});
96 let dn2 = new Intl.DisplayNames("en", {type: "calendar", fallback: "code"});
97 let dn3 = new Intl.DisplayNames("en", {type: "calendar", fallback: "none"});
98
99 assertEq(dn1.resolvedOptions().fallback, "code");
100 assertEq(dn2.resolvedOptions().fallback, "code");
101 assertEq(dn3.resolvedOptions().fallback, "none");
102
103 // "invalid" isn't a known calendar type.
104 assertEq(dn1.of("invalid"), "invalid");
105 assertEq(dn2.of("invalid"), "invalid");
106 assertEq(dn3.of("invalid"), undefined);
107
108 // The returned fallback is in canonical case.
109 assertEq(dn1.of("INVALID"), "invalid");
110 assertEq(dn2.of("INVALID"), "invalid");
111 assertEq(dn3.of("INVALID"), undefined);
112 }
113
114 // Test when case isn't canonical.
115 {
116 let dn = new Intl.DisplayNames("en", {type: "calendar", fallback: "none"});
117
118 assertEq(dn.of("gregory"), "Gregorian Calendar");
119 assertEq(dn.of("GREGORY"), "Gregorian Calendar");
120 }
121
122 if (typeof reportCompare === "function")
123 reportCompare(true, true);