summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/DisplayNames/dayPeriod.js
blob: cfeafdf5eeaa9fccda7bb09dfa5f9a2f42b33ab9 (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
// |reftest| skip-if(!this.hasOwnProperty('Intl')||!this.hasOwnProperty('addIntlExtras'))

addMozIntlDisplayNames(this);

const tests = {
  "en": {
    long: {
      "am": "AM",
      "pm": "PM",
    },
    short: {},
    narrow: {},
  },
  "de": {
    long: {
      "am": "AM",
      "pm": "PM",
    },
    short: {},
    narrow: {},
  },
  "fr": {
    long: {
      "am": "AM",
      "pm": "PM",
    },
    short: {},
    narrow: {},
  },
  "zh": {
    long: {
      "am": "上午",
      "pm": "下午",
    },
    short: {},
    narrow: {},
  },
};

for (let [locale, localeTests] of Object.entries(tests)) {
  let defaultCalendar = new Intl.DateTimeFormat(locale).resolvedOptions().calendar;

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

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

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

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

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

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

  // Throws an error if not one of ["am", "pm"].
  assertThrowsInstanceOf(() => dn.of(""), RangeError);
  assertThrowsInstanceOf(() => dn.of("AM"), RangeError);
  assertThrowsInstanceOf(() => dn.of("PM"), RangeError);
}

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