| 1 |
// Copyright 2013 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
|
| 16 |
goog.provide('goog.i18n.collationTest'); |
| 17 |
goog.setTestOnly('goog.i18n.collationTest'); |
| 18 |
|
| 19 |
goog.require('goog.i18n.collation'); |
| 20 |
goog.require('goog.testing.ExpectedFailures'); |
| 21 |
goog.require('goog.testing.PropertyReplacer'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.userAgent'); |
| 24 |
|
| 25 |
var expectedFailures; |
| 26 |
var propertyReplacer; |
| 27 |
|
| 28 |
function setUpPage() { |
| 29 |
expectedFailures = new goog.testing.ExpectedFailures(); |
| 30 |
propertyReplacer = new goog.testing.PropertyReplacer(); |
| 31 |
} |
| 32 |
|
| 33 |
function tearDown() { |
| 34 |
expectedFailures.handleTearDown(); |
| 35 |
} |
| 36 |
|
| 37 |
function testGetEnComparator() { |
| 38 |
propertyReplacer.replace(goog, 'LOCALE', 'en'); |
| 39 |
var compare = goog.i18n.collation.createComparator(); |
| 40 |
// The côte/coté comparison fails in FF/Linux (v19.0) because |
| 41 |
// calling 'côte'.localeCompare('coté') gives a negative number (wrong) |
| 42 |
// when the test is run but a positive number (correct) when calling |
| 43 |
// it later in the web console. FF/OSX doesn't have this problem. |
| 44 |
// Mozilla bug: https://bugzilla.mozilla.org/show_bug.cgi?id=856115 |
| 45 |
expectedFailures.expectFailureFor( |
| 46 |
goog.userAgent.GECKO && goog.userAgent.LINUX); |
| 47 |
try { |
| 48 |
assertTrue(compare('côte', 'coté') > 0); |
| 49 |
} catch (e) { |
| 50 |
expectedFailures.handleException(e); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
function testGetFrComparator() { |
| 55 |
propertyReplacer.replace(goog, 'LOCALE', 'fr-CA'); |
| 56 |
var compare = goog.i18n.collation.createComparator(); |
| 57 |
if (!goog.i18n.collation.hasNativeComparator()) return; |
| 58 |
assertTrue(compare('côte', 'coté') < 0); |
| 59 |
} |
| 60 |
|
| 61 |
function testGetComparatorForSpecificLocale() { |
| 62 |
propertyReplacer.replace(goog, 'LOCALE', 'en'); |
| 63 |
var compare = goog.i18n.collation.createComparator('fr-CA'); |
| 64 |
if (!goog.i18n.collation.hasNativeComparator('fr-CA')) return; |
| 65 |
// 'côte' and 'coté' sort differently for en and fr-CA. |
| 66 |
assertTrue(compare('côte', 'coté') < 0); |
| 67 |
} |
| 68 |
|
| 69 |
function testGetNumericComparator() { |
| 70 |
var compare = goog.i18n.collation.createComparator('en', {numeric: true}); |
| 71 |
if (!goog.i18n.collation.hasNativeComparator('en')) return; |
| 72 |
assertTrue(compare('2', '10') < 0); |
| 73 |
} |
| 74 |
|
| 75 |
function testGetNonNumericComparator() { |
| 76 |
var compare = goog.i18n.collation.createComparator('en', {numeric: false}); |
| 77 |
if (!goog.i18n.collation.hasNativeComparator('en')) return; |
| 78 |
assertTrue(compare('2', '10') > 0); |
| 79 |
} |