js/src/tests/Intl/Collator/compare.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/Intl/Collator/compare.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +// |reftest| skip-if(!this.hasOwnProperty("Intl"))
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +// Tests the compare function with a diverse set of locales and options.
    1.10 +
    1.11 +var input = [
    1.12 +    "Argentina",
    1.13 +    "Oerlikon",
    1.14 +    "Offenbach",
    1.15 +    "Sverige",
    1.16 +    "Vaticano",
    1.17 +    "Zimbabwe",
    1.18 +    "la France",
    1.19 +    "¡viva España!",
    1.20 +    "Österreich",
    1.21 +    "中国",
    1.22 +    "日本",
    1.23 +    "한국",
    1.24 +];
    1.25 +
    1.26 +var collator, expected;
    1.27 +
    1.28 +function assertEqualArray(actual, expected, collator) {
    1.29 +    var description = JSON.stringify(collator.resolvedOptions());
    1.30 +    assertEq(actual.length, expected.length, "array length, " + description);
    1.31 +    for (var i = 0; i < actual.length; i++) {
    1.32 +        assertEq(actual[i], expected[i], "element " + i + ", " + description);
    1.33 +    }
    1.34 +}
    1.35 +
    1.36 +
    1.37 +// Locale en-US; default options.
    1.38 +collator = new Intl.Collator("en-US");
    1.39 +expected = [
    1.40 +    "¡viva España!",
    1.41 +    "Argentina",
    1.42 +    "la France",
    1.43 +    "Oerlikon",
    1.44 +    "Offenbach",
    1.45 +    "Österreich",
    1.46 +    "Sverige",
    1.47 +    "Vaticano",
    1.48 +    "Zimbabwe",
    1.49 +    "한국",
    1.50 +    "中国",
    1.51 +    "日本",
    1.52 +];
    1.53 +assertEqualArray(input.sort(collator.compare), expected, collator);
    1.54 +
    1.55 +// Locale sv-SE; default options.
    1.56 +// Swedish treats "Ö" as a separate character, which sorts after "Z".
    1.57 +collator = new Intl.Collator("sv-SE");
    1.58 +expected = [
    1.59 +    "¡viva España!",
    1.60 +    "Argentina",
    1.61 +    "la France",
    1.62 +    "Oerlikon",
    1.63 +    "Offenbach",
    1.64 +    "Sverige",
    1.65 +    "Vaticano",
    1.66 +    "Zimbabwe",
    1.67 +    "Österreich",
    1.68 +    "한국",
    1.69 +    "中国",
    1.70 +    "日本",
    1.71 +];
    1.72 +assertEqualArray(input.sort(collator.compare), expected, collator);
    1.73 +
    1.74 +// Locale sv-SE; ignore punctuation.
    1.75 +collator = new Intl.Collator("sv-SE", {ignorePunctuation: true});
    1.76 +expected = [
    1.77 +    "Argentina",
    1.78 +    "la France",
    1.79 +    "Oerlikon",
    1.80 +    "Offenbach",
    1.81 +    "Sverige",
    1.82 +    "Vaticano",
    1.83 +    "¡viva España!",
    1.84 +    "Zimbabwe",
    1.85 +    "Österreich",
    1.86 +    "한국",
    1.87 +    "中国",
    1.88 +    "日本",
    1.89 +];
    1.90 +assertEqualArray(input.sort(collator.compare), expected, collator);
    1.91 +
    1.92 +// Locale de-DE; default options.
    1.93 +// In German standard sorting, umlauted characters are treated as variants
    1.94 +// of their base characters: ä ≅ a, ö ≅ o, ü ≅ u.
    1.95 +collator = new Intl.Collator("de-DE");
    1.96 +expected = [
    1.97 +    "¡viva España!",
    1.98 +    "Argentina",
    1.99 +    "la France",
   1.100 +    "Oerlikon",
   1.101 +    "Offenbach",
   1.102 +    "Österreich",
   1.103 +    "Sverige",
   1.104 +    "Vaticano",
   1.105 +    "Zimbabwe",
   1.106 +    "한국",
   1.107 +    "中国",
   1.108 +    "日本",
   1.109 +];
   1.110 +assertEqualArray(input.sort(collator.compare), expected, collator);
   1.111 +
   1.112 +// Locale de-DE; phonebook sort order.
   1.113 +// In German phonebook sorting, umlauted characters are expanded to two-vowel
   1.114 +// sequences: ä → ae, ö → oe, ü → ue.
   1.115 +collator = new Intl.Collator("de-DE-u-co-phonebk");
   1.116 +expected = [
   1.117 +    "¡viva España!",
   1.118 +    "Argentina",
   1.119 +    "la France",
   1.120 +    "Oerlikon",
   1.121 +    "Österreich",
   1.122 +    "Offenbach",
   1.123 +    "Sverige",
   1.124 +    "Vaticano",
   1.125 +    "Zimbabwe",
   1.126 +    "한국",
   1.127 +    "中国",
   1.128 +    "日本",
   1.129 +];
   1.130 +assertEqualArray(input.sort(collator.compare), expected, collator);
   1.131 +
   1.132 +reportCompare(0, 0, 'ok');

mercurial