duration_test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.LOCALE = 'en_US';
  15. goog.provide('goog.date.durationTest');
  16. goog.setTestOnly('goog.date.durationTest');
  17. goog.require('goog.date.duration');
  18. goog.require('goog.i18n.DateTimeFormat');
  19. goog.require('goog.i18n.DateTimeSymbols');
  20. goog.require('goog.i18n.DateTimeSymbols_bn'); // Bengali
  21. goog.require('goog.i18n.DateTimeSymbols_en');
  22. goog.require('goog.i18n.DateTimeSymbols_fa'); // Persian
  23. goog.require('goog.testing.jsunit');
  24. var MINUTE_MS = 60000;
  25. var HOUR_MS = 60 * MINUTE_MS;
  26. var DAY_MS = 24 * HOUR_MS;
  27. var duration = goog.date.duration.format;
  28. function testFormatDurationZeroMinutes() {
  29. assertEquals('0 minutes', duration(0));
  30. assertEquals('0 minutes', duration(MINUTE_MS - 1));
  31. }
  32. function testFormatDurationMinutes() {
  33. assertEquals('1 minute', duration(MINUTE_MS));
  34. assertEquals('1 minute', duration(MINUTE_MS + 1));
  35. assertEquals('5 minutes', duration(5 * MINUTE_MS));
  36. assertEquals('45 minutes', duration(45 * MINUTE_MS));
  37. }
  38. function testFormatDurationHours() {
  39. assertEquals('1 hour', duration(HOUR_MS));
  40. assertEquals('1 hour', duration(HOUR_MS + 1));
  41. assertEquals('1 hour 1 minute', duration(HOUR_MS + MINUTE_MS));
  42. assertEquals('1 hour 45 minutes', duration(HOUR_MS + 45 * MINUTE_MS));
  43. assertEquals('5 hours', duration(5 * HOUR_MS));
  44. assertEquals('5 hours', duration(5 * HOUR_MS + 1));
  45. assertEquals('5 hours 1 minute', duration(5 * HOUR_MS + MINUTE_MS));
  46. assertEquals('5 hours 45 minutes', duration(5 * HOUR_MS + 45 * MINUTE_MS));
  47. assertEquals('11 hours', duration(11 * HOUR_MS));
  48. assertEquals('11 hours', duration(11 * HOUR_MS + 1));
  49. assertEquals('11 hours 1 minute', duration(11 * HOUR_MS + MINUTE_MS));
  50. assertEquals('11 hours 45 minutes', duration(11 * HOUR_MS + 45 * MINUTE_MS));
  51. }
  52. function testFormatDurationDays() {
  53. assertEquals('1 day', duration(DAY_MS));
  54. assertEquals('1 day', duration(DAY_MS + 1));
  55. assertEquals('1 day 1 minute', duration(DAY_MS + MINUTE_MS));
  56. assertEquals('1 day 45 minutes', duration(DAY_MS + 45 * MINUTE_MS));
  57. assertEquals('1 day 1 hour', duration(DAY_MS + HOUR_MS));
  58. assertEquals('1 day 11 hours', duration(DAY_MS + 11 * HOUR_MS));
  59. assertEquals('1 day 1 hour 1 minute', duration(DAY_MS + HOUR_MS + MINUTE_MS));
  60. assertEquals(
  61. '1 day 1 hour 45 minutes', duration(DAY_MS + HOUR_MS + 45 * MINUTE_MS));
  62. assertEquals(
  63. '1 day 11 hours 1 minute', duration(DAY_MS + 11 * HOUR_MS + MINUTE_MS));
  64. assertEquals(
  65. '1 day 11 hours 45 minutes',
  66. duration(DAY_MS + 11 * HOUR_MS + 45 * MINUTE_MS));
  67. assertEquals('11 days', duration(11 * DAY_MS));
  68. assertEquals('11 days', duration(11 * DAY_MS + 1));
  69. assertEquals('11 days 1 minute', duration(11 * DAY_MS + MINUTE_MS));
  70. assertEquals('11 days 45 minutes', duration(11 * DAY_MS + 45 * MINUTE_MS));
  71. assertEquals('11 days 1 hour', duration(11 * DAY_MS + HOUR_MS));
  72. assertEquals('11 days 11 hours', duration(11 * DAY_MS + 11 * HOUR_MS));
  73. assertEquals(
  74. '11 days 1 hour 1 minute', duration(11 * DAY_MS + HOUR_MS + MINUTE_MS));
  75. assertEquals(
  76. '11 days 1 hour 45 minutes',
  77. duration(11 * DAY_MS + HOUR_MS + 45 * MINUTE_MS));
  78. assertEquals(
  79. '11 days 11 hours 1 minute',
  80. duration(11 * DAY_MS + 11 * HOUR_MS + MINUTE_MS));
  81. assertEquals(
  82. '11 days 11 hours 45 minutes',
  83. duration(11 * DAY_MS + 11 * HOUR_MS + 45 * MINUTE_MS));
  84. }
  85. function testFormatDurationPersianDigits() {
  86. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fa;
  87. // ۱ minute
  88. assertEquals(localizeNumber(1) + ' minute', duration(MINUTE_MS));
  89. // ۲ minutes
  90. assertEquals(localizeNumber(2) + ' minutes', duration(2 * MINUTE_MS));
  91. // ۱۰ hours
  92. assertEquals(localizeNumber(10) + ' hours', duration(10 * HOUR_MS));
  93. // ۲۳ days
  94. assertEquals(localizeNumber(23) + ' days', duration(23 * DAY_MS));
  95. // Restore to English, to make sure we don't mess up other tests
  96. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  97. }
  98. function testFormatDurationBengaliDigits() {
  99. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_bn;
  100. // ১ minute
  101. assertEquals(localizeNumber(1) + ' minute', duration(MINUTE_MS));
  102. // ২ minutes
  103. assertEquals(localizeNumber(2) + ' minutes', duration(2 * MINUTE_MS));
  104. // ১০ hours
  105. assertEquals(localizeNumber(10) + ' hours', duration(10 * HOUR_MS));
  106. // ২৩ days
  107. assertEquals(localizeNumber(23) + ' days', duration(23 * DAY_MS));
  108. // Restore to English, to make sure we don't mess up other tests
  109. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  110. }
  111. function localizeNumber(value) {
  112. // Quick conversion to national digits, to increase readability of the
  113. // tests above.
  114. return goog.i18n.DateTimeFormat.localizeNumbers(value);
  115. }