summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/DateTimeFormat/implied-script-has-consistent-output.js
blob: 6c0b4d2f594f6088d123309bb2b37a683e7b5f56 (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
// |reftest| skip-if(!this.hasOwnProperty("Intl"))

if (typeof getAvailableLocalesOf === "undefined") {
  var getAvailableLocalesOf = SpecialPowers.Cu.getJSTestingFunctions().getAvailableLocalesOf;
}

// Retrieve all available locales of Intl.DateTimeFormat.
const available = getAvailableLocalesOf("DateTimeFormat");

const options = [
  // Include "hour" to catch hour cycle differences.
  //
  // For example "ff-Latn-GH" (Fulah as spoken in Ghana) uses a 12-hour clock,
  // whereas "ff" (Fulah) uses a 24-hour clock. When the user creates the
  // formatter for "ff-GH", it should use the same formatter data as "ff-Latn-GH"
  // and it shouldn't fallback to "ff".
  {hour: "2-digit", minute: "2-digit", timeZone: "UTC"},

  // Include "timeZoneName" to catch script differences, e.g traditional or
  // simplified Chinese characters.
  {timeZoneName: "long", timeZone: "America/Los_Angeles"},
];

// Pick a date after 12 pm to catch any hour cycle differences.
const date = Date.UTC(2021, 6-1, 7, 15, 0);

available.map(x => {
  return new Intl.Locale(x);
}).filter(loc => {
  // Find all locales which have both a script and a region subtag.
  return loc.script && loc.region;
}).filter(loc => {
  // Skip "sd-Deva-IN" and Fulah because of <https://unicode-org.atlassian.net/browse/ICU-21974>.
  return !((loc.language === "sd" && loc.script === "Deva" && loc.region === "IN") ||
           (loc.language === "ff" && (loc.script === "Adlm" || loc.script === "Latn") &&
            (loc.region === "GH" || loc.region === "GM" || loc.region === "LR" || loc.region === "SL"))
          );
}).forEach(loc => {
  // Remove the script subtag from the locale.
  let noScript = new Intl.Locale(`${loc.language}-${loc.region}`);

  // Add the likely script subtag.
  let maximized = noScript.maximize();

  for (let opt of options) {
    // Formatter for the locale without a script subtag.
    let df1 = new Intl.DateTimeFormat(noScript, opt);

    // Formatter for the locale with the likely script subtag added.
    let df2 = new Intl.DateTimeFormat(maximized, opt);

    // The output for the locale without a script subtag should match the output
    // with the likely script subtag added.
    assertEq(df1.format(date), df2.format(date), `Mismatch for locale "${noScript}" (${maximized})`);
  }
});

if (typeof reportCompare === "function")
  reportCompare(0, 0, "ok");