relativewithplurals_test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2009 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.date.relativeWithPluralsTest');
  15. goog.setTestOnly('goog.date.relativeWithPluralsTest');
  16. goog.require('goog.date.relative');
  17. /** @suppress {extraRequire} Include shared tests. */
  18. goog.require('goog.date.relativeTest');
  19. /** @suppress {extraRequire} Included for side effects. */
  20. goog.require('goog.date.relativeWithPlurals');
  21. goog.require('goog.i18n.DateTimeFormat');
  22. goog.require('goog.i18n.DateTimeSymbols');
  23. goog.require('goog.i18n.DateTimeSymbols_bn'); // Bengali
  24. goog.require('goog.i18n.DateTimeSymbols_en');
  25. goog.require('goog.i18n.DateTimeSymbols_fa'); // Persian
  26. goog.require('goog.i18n.NumberFormatSymbols');
  27. goog.require('goog.i18n.NumberFormatSymbols_bn'); // Bengali
  28. goog.require('goog.i18n.NumberFormatSymbols_en');
  29. goog.require('goog.i18n.NumberFormatSymbols_fa'); // Persian
  30. function tearDown() {
  31. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  32. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_en;
  33. }
  34. function testFormatRelativeForPastDatesPersianDigits() {
  35. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fa;
  36. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_fa;
  37. var fn = goog.date.relative.format;
  38. // The text here is English, as it comes from localized resources, not
  39. // from CLDR. It works properly in production, but it's not loaded here.
  40. // Will need to wait for CLDR 24, when the data we need will be available,
  41. // so that we can add it to DateTimeSymbols and out of localization.
  42. // For Persian \u06F0 is the base, so \u6F0 = digit 0, \u6F5 = digit 5 ...
  43. // "Western" digits in square brackets for convenience
  44. assertEquals(
  45. 'Should round seconds to the minute below',
  46. localizeNumber(0) + ' minutes ago', // ۰ minutes ago
  47. fn(timestamp('23 March 2009 14:30:10')));
  48. assertEquals(
  49. 'Should round seconds to the minute below',
  50. localizeNumber(1) + ' minute ago', // ۱ minute ago
  51. fn(timestamp('23 March 2009 14:29:56')));
  52. assertEquals(
  53. 'Should round seconds to the minute below',
  54. localizeNumber(2) + ' minutes ago', // ۲ minutes ago
  55. fn(timestamp('23 March 2009 14:29:00')));
  56. assertEquals(
  57. localizeNumber(10) + ' minutes ago', // ۱۰ minutes ago
  58. fn(timestamp('23 March 2009 14:20:10')));
  59. assertEquals(
  60. localizeNumber(59) + ' minutes ago', // ۵۹ minutes ago
  61. fn(timestamp('23 March 2009 13:31:42')));
  62. assertEquals(
  63. localizeNumber(2) + ' hours ago', // ۲ hours ago
  64. fn(timestamp('23 March 2009 12:20:56')));
  65. assertEquals(
  66. localizeNumber(23) + ' hours ago', // ۲۳ hours ago
  67. fn(timestamp('22 March 2009 15:30:56')));
  68. assertEquals(
  69. localizeNumber(1) + ' day ago', // ۱ day ago
  70. fn(timestamp('22 March 2009 12:11:04')));
  71. assertEquals(
  72. localizeNumber(1) + ' day ago', // ۱ day ago
  73. fn(timestamp('22 March 2009 00:00:00')));
  74. assertEquals(
  75. localizeNumber(2) + ' days ago', // ۲ days ago
  76. fn(timestamp('21 March 2009 23:59:59')));
  77. assertEquals(
  78. localizeNumber(2) + ' days ago', // ۲ days ago
  79. fn(timestamp('21 March 2009 10:30:56')));
  80. assertEquals(
  81. localizeNumber(2) + ' days ago', // ۲ days ago
  82. fn(timestamp('21 March 2009 00:00:00')));
  83. assertEquals(
  84. localizeNumber(3) + ' days ago', // ۳ days ago
  85. fn(timestamp('20 March 2009 23:59:59')));
  86. }
  87. function testFormatRelativeForFutureDatesBengaliDigits() {
  88. var fn = goog.date.relative.format;
  89. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_bn;
  90. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_bn;
  91. // For Bengali \u09E6 is the base, so \u09E6 = digit 0, \u09EB = digit 5
  92. // "Western" digits in square brackets for convenience
  93. assertEquals(
  94. 'Should round seconds to the minute below',
  95. 'in ' + localizeNumber(1) + ' minute', // in ১ minute
  96. fn(timestamp('23 March 2009 14:32:05')));
  97. assertEquals(
  98. 'Should round seconds to the minute below',
  99. 'in ' + localizeNumber(2) + ' minutes', // in ২ minutes
  100. fn(timestamp('23 March 2009 14:33:00')));
  101. assertEquals(
  102. 'in ' + localizeNumber(10) + ' minutes', // in ১০ minutes
  103. fn(timestamp('23 March 2009 14:40:10')));
  104. assertEquals(
  105. 'in ' + localizeNumber(59) + ' minutes', // in ৫৯ minutes
  106. fn(timestamp('23 March 2009 15:29:15')));
  107. assertEquals(
  108. 'in ' + localizeNumber(2) + ' hours', // in ২ hours
  109. fn(timestamp('23 March 2009 17:20:56')));
  110. assertEquals(
  111. 'in ' + localizeNumber(23) + ' hours', // in ২৩ hours
  112. fn(timestamp('24 March 2009 13:30:56')));
  113. assertEquals(
  114. 'in ' + localizeNumber(1) + ' day', // in ১ day
  115. fn(timestamp('24 March 2009 14:31:07')));
  116. assertEquals(
  117. 'in ' + localizeNumber(1) + ' day', // in ১ day
  118. fn(timestamp('24 March 2009 16:11:04')));
  119. assertEquals(
  120. 'in ' + localizeNumber(1) + ' day', // in ১ day
  121. fn(timestamp('24 March 2009 23:59:59')));
  122. assertEquals(
  123. 'in ' + localizeNumber(2) + ' days', // in ২ days
  124. fn(timestamp('25 March 2009 00:00:00')));
  125. assertEquals(
  126. 'in ' + localizeNumber(2) + ' days', // in ২ days
  127. fn(timestamp('25 March 2009 10:30:56')));
  128. assertEquals(
  129. 'in ' + localizeNumber(2) + ' days', // in ২ days
  130. fn(timestamp('25 March 2009 23:59:59')));
  131. assertEquals(
  132. 'in ' + localizeNumber(3) + ' days', // in ৩ days
  133. fn(timestamp('26 March 2009 00:00:00')));
  134. }
  135. function localizeNumber(value) {
  136. // Quick conversion to national digits, to increase readability of the
  137. // tests above.
  138. return goog.i18n.DateTimeFormat.localizeNumbers(value);
  139. }