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

addMozIntlDisplayNames(this);

const tests = {
  "en": {
    long: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
    // short: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
    short: ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
    narrow: ["M", "T", "W", "T", "F", "S", "S"],
  },
  "de": {
    long: ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"],
    // short: ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"],
    short: ["Mo.", "Di.", "Mi.", "Do.", "Fr.", "Sa.", "So."],
    narrow: ["M", "D", "M", "D", "F", "S", "S"],
  },
  "fr": {
    long: ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"],
    // short: ["lun.", "mar.", "mer.", "jeu.", "ven.", "sam.", "dim."],
    short: ["lu", "ma", "me", "je", "ve", "sa", "di"],
    narrow: ["L", "M", "M", "J", "V", "S", "D"],
  },
  "zh": {
    long: ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
    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: "weekday", style});

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

    for (let i = 0; i < 7; i++) {
      assertEq(dn.of(i + 1), styleTests[i]);

      // Also works with strings.
      assertEq(dn.of(String(i + 1)), styleTests[i]);

      // Also works with objects.
      assertEq(dn.of(Object(i + 1)), styleTests[i]);
    }
  }
}

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

  // 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);

  // Throws an error if |code| isn't an integer.
  assertThrowsInstanceOf(() => dn.of(1.5), RangeError);
  assertThrowsInstanceOf(() => dn.of(-Infinity), RangeError);
  assertThrowsInstanceOf(() => dn.of(Infinity), RangeError);
  assertThrowsInstanceOf(() => dn.of(NaN), RangeError);

  // Throws an error if outside of [1, 7].
  assertThrowsInstanceOf(() => dn.of(-1), RangeError);
  assertThrowsInstanceOf(() => dn.of(0), RangeError);
  assertThrowsInstanceOf(() => dn.of(8), RangeError);
}

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