summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/DisplayNames/calendar.js
blob: 0700ee2888c68c36e306090a7e8d7b0e54e29fc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// |reftest| skip-if(!this.hasOwnProperty('Intl'))

const tests = {
  "en": {
    long: {
      "gregory": "Gregorian Calendar",
      "iso8601": "ISO-8601 Calendar",
      "japanese": "Japanese Calendar",
      "islamic-civil": "Islamic Calendar (tabular, civil epoch)",
      "islamicc": "Islamic Calendar (tabular, civil epoch)",
      "ethioaa": "Ethiopic Amete Alem Calendar",
      "ethiopic-amete-alem": "Ethiopic Amete Alem Calendar",
    },
    short: {},
    narrow: {},
  },
  "de": {
    long: {
      "gregory": "Gregorianischer Kalender",
      "iso8601": "ISO-8601-Kalender",
      "japanese": "Japanischer Kalender",
      "islamic-civil": "Bürgerlicher islamischer Kalender (tabellarisch)",
      "islamicc": "Bürgerlicher islamischer Kalender (tabellarisch)",
      "ethioaa": "Äthiopischer Amätä-Aläm-Kalender",
      "ethiopic-amete-alem": "Äthiopischer Amätä-Aläm-Kalender",
    },
    short: {},
    narrow: {},
  },
  "fr": {
    long: {
      "gregory": "calendrier grégorien",
      "iso8601": "calendrier ISO 8601",
      "japanese": "calendrier japonais",
      "islamic-civil": "calendrier musulman (tabulaire, époque civile)",
      "islamicc": "calendrier musulman (tabulaire, époque civile)",
      "ethioaa": "calendrier éthiopien Amete Alem",
      "ethiopic-amete-alem": "calendrier éthiopien Amete Alem",
    },
    short: {},
    narrow: {},
  },
  "zh": {
    long: {
      "gregory": "公历",
      "iso8601": "国际标准历法",
      "japanese": "和历",
      "islamic-civil": "伊斯兰希吉来日历",
      "islamicc": "伊斯兰希吉来日历",
      "ethioaa": "埃塞俄比亚阿米特阿莱姆日历",
      "ethiopic-amete-alem": "埃塞俄比亚阿米特阿莱姆日历",
    },
    short: {},
    narrow: {},
  },
};

for (let [locale, localeTests] of Object.entries(tests)) {
  for (let [style, styleTests] of Object.entries(localeTests)) {
    let dn = new Intl.DisplayNames(locale, {type: "calendar", style});

    let resolved = dn.resolvedOptions();
    assertEq(resolved.locale, locale);
    assertEq(resolved.style, style);
    assertEq(resolved.type, "calendar");
    assertEq(resolved.fallback, "code");

    let inheritedTests = {...localeTests.long, ...localeTests.short, ...localeTests.narrow};
    for (let [calendar, expected] of Object.entries({...inheritedTests, ...styleTests})) {
      assertEq(dn.of(calendar), expected);

      // Also works with objects.
      assertEq(dn.of(Object(calendar)), expected);
    }
  }
}

{
  let dn = new Intl.DisplayNames("en", {type: "calendar"});

  // Performs ToString on the input and then validates the stringified result.
  assertThrowsInstanceOf(() => dn.of(), RangeError);
  assertThrowsInstanceOf(() => dn.of(undefined), RangeError);
  assertThrowsInstanceOf(() => dn.of(Symbol()), TypeError);
  assertThrowsInstanceOf(() => dn.of(0), RangeError);

  // Throws an error if |code| isn't a well-formed calendar type.
  assertThrowsInstanceOf(() => dn.of("gregorian"), RangeError);
  assertThrowsInstanceOf(() => dn.of("grëgory"), RangeError);
  assertThrowsInstanceOf(() => dn.of("grēgory"), RangeError);
}

// Test fallback behaviour.
{
  let dn1 = new Intl.DisplayNames("en", {type: "calendar"});
  let dn2 = new Intl.DisplayNames("en", {type: "calendar", fallback: "code"});
  let dn3 = new Intl.DisplayNames("en", {type: "calendar", fallback: "none"});

  assertEq(dn1.resolvedOptions().fallback, "code");
  assertEq(dn2.resolvedOptions().fallback, "code");
  assertEq(dn3.resolvedOptions().fallback, "none");

  // "invalid" isn't a known calendar type.
  assertEq(dn1.of("invalid"), "invalid");
  assertEq(dn2.of("invalid"), "invalid");
  assertEq(dn3.of("invalid"), undefined);

  // The returned fallback is in canonical case.
  assertEq(dn1.of("INVALID"), "invalid");
  assertEq(dn2.of("INVALID"), "invalid");
  assertEq(dn3.of("INVALID"), undefined);
}

// Test when case isn't canonical.
{
  let dn = new Intl.DisplayNames("en", {type: "calendar", fallback: "none"});

  assertEq(dn.of("gregory"), "Gregorian Calendar");
  assertEq(dn.of("GREGORY"), "Gregorian Calendar");
}

if (typeof reportCompare === "function")
  reportCompare(true, true);