summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/Collator/ignorePunctuation.js
blob: 281563cc5cc37e3f1de07933e174f45ec2694295 (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
// |reftest| skip-if(!this.hasOwnProperty("Intl"))

function testPunctuation(col, expectedIgnorePunctuation) {
  let ignorePunctuation = col.resolvedOptions().ignorePunctuation;
  assertEq(ignorePunctuation, expectedIgnorePunctuation);

  // Punctuation is ignored iff |ignorePunctuation| is true.
  assertEq(col.compare("", "*"), ignorePunctuation ? 0 : -1);

  // Whitespace is also ignored when |ignorePunctuation| is true due to ICU limitations.
  assertEq(col.compare("", " "), ignorePunctuation ? 0 : -1);
}

const locales = [
  "en", "de", "fr", "it", "es", "ar", "zh", "ja",

  // Thai, including some subtags.
  "th", "th-Thai", "th-TH", "th-u-kf-false",
];

for (let locale of locales) {
  // Thai ignores punctuation by default.
  let isThai = new Intl.Locale(locale).language === "th";

  // Using default "ignorePunctuation" option.
  testPunctuation(new Intl.Collator(locale, {}), isThai);

  // Using explicit "ignorePunctuation" option.
  for (let ignorePunctuation of [true, false]) {
    testPunctuation(new Intl.Collator(locale, {ignorePunctuation}), ignorePunctuation);
  }
}

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

const defaultLocale = getDefaultLocale();

function withLocale(locale, fn) {
  setDefaultLocale(locale);
  try {
    fn();
  } finally {
    setDefaultLocale(defaultLocale);
  }
}

// Ensure this works correctly when Thai is the default locale.
for (let locale of ["th", "th-TH"]) {
  withLocale(locale, () => {
    testPunctuation(new Intl.Collator(undefined, {}), true);
  });
}

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