collation_test.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. goog.provide('goog.i18n.collationTest');
  15. goog.setTestOnly('goog.i18n.collationTest');
  16. goog.require('goog.i18n.collation');
  17. goog.require('goog.testing.ExpectedFailures');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.userAgent');
  20. var expectedFailures;
  21. function setUpPage() {
  22. expectedFailures = new goog.testing.ExpectedFailures();
  23. }
  24. function tearDown() {
  25. expectedFailures.handleTearDown();
  26. }
  27. function testGetEnComparator() {
  28. goog.LOCALE = 'en';
  29. var compare = goog.i18n.collation.createComparator();
  30. // The côte/coté comparison fails in FF/Linux (v19.0) because
  31. // calling 'côte'.localeCompare('coté') gives a negative number (wrong)
  32. // when the test is run but a positive number (correct) when calling
  33. // it later in the web console. FF/OSX doesn't have this problem.
  34. // Mozilla bug: https://bugzilla.mozilla.org/show_bug.cgi?id=856115
  35. expectedFailures.expectFailureFor(
  36. goog.userAgent.GECKO && goog.userAgent.LINUX);
  37. try {
  38. assertTrue(compare('côte', 'coté') > 0);
  39. } catch (e) {
  40. expectedFailures.handleException(e);
  41. }
  42. }
  43. function testGetFrComparator() {
  44. goog.LOCALE = 'fr-CA';
  45. var compare = goog.i18n.collation.createComparator();
  46. if (!goog.i18n.collation.hasNativeComparator()) return;
  47. assertTrue(compare('côte', 'coté') < 0);
  48. }
  49. function testGetComparatorForSpecificLocale() {
  50. goog.LOCALE = 'en';
  51. var compare = goog.i18n.collation.createComparator('fr-CA');
  52. if (!goog.i18n.collation.hasNativeComparator('fr-CA')) return;
  53. // 'côte' and 'coté' sort differently for en and fr-CA.
  54. assertTrue(compare('côte', 'coté') < 0);
  55. }
  56. function testGetNumericComparator() {
  57. var compare = goog.i18n.collation.createComparator('en', {numeric: true});
  58. if (!goog.i18n.collation.hasNativeComparator('en')) return;
  59. assertTrue(compare('2', '10') < 0);
  60. }
  61. function testGetNonNumericComparator() {
  62. var compare = goog.i18n.collation.createComparator('en', {numeric: false});
  63. if (!goog.i18n.collation.hasNativeComparator('en')) return;
  64. assertTrue(compare('2', '10') > 0);
  65. }