daterange_test.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright 2006 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.DateRangeTest');
  15. goog.setTestOnly('goog.date.DateRangeTest');
  16. goog.require('goog.date.Date');
  17. goog.require('goog.date.DateRange');
  18. goog.require('goog.date.Interval');
  19. goog.require('goog.i18n.DateTimeSymbols');
  20. goog.require('goog.testing.jsunit');
  21. var gd = goog.date.Date;
  22. var gdr = goog.date.DateRange;
  23. var gdi = goog.date.Interval;
  24. function testDateRange() {
  25. var date1 = new gd(2000, 0, 1);
  26. var date2 = new gd(2000, 1, 1);
  27. var range = new gdr(date1, date2);
  28. assertTrue('startDate matches', date1.equals(range.getStartDate()));
  29. assertTrue('endDate matches', date2.equals(range.getEndDate()));
  30. }
  31. function testDateRangeEquals() {
  32. var date1 = new gd(2000, 0, 1);
  33. var date2 = new gd(2000, 1, 1);
  34. var range1 = new gdr(date1, date2);
  35. var range2 = new gdr(date1, date2);
  36. assertTrue('equals', gdr.equals(range1, range2));
  37. }
  38. function testDateRangeNotEquals() {
  39. var date1 = new gd(2000, 0, 1);
  40. var date2 = new gd(2000, 1, 1);
  41. var range1 = new gdr(date1, date2);
  42. var range2 = new gdr(date2, date1);
  43. assertFalse('not equals', gdr.equals(range1, range2));
  44. }
  45. function testOffsetInDays() {
  46. var d = new gd(2000, 0, 1);
  47. var f = gdr.offsetInDays_;
  48. assertTrue('same day', d.equals(f(d, 0)));
  49. assertTrue('next day', new gd(2000, 0, 2).equals(f(d, 1)));
  50. assertTrue('last day', new gd(1999, 11, 31).equals(f(d, -1)));
  51. }
  52. function testOffsetInMonths() {
  53. var d = new gd(2008, 9, 13);
  54. var f = gdr.offsetInMonths_;
  55. assertTrue('this month', new gd(2008, 9, 1).equals(f(d, 0)));
  56. assertTrue('last month', new gd(2008, 8, 1).equals(f(d, -1)));
  57. assertTrue('next month', new gd(2008, 10, 1).equals(f(d, 1)));
  58. assertTrue('next year', new gd(2009, 9, 1).equals(f(d, 12)));
  59. assertTrue('last year', new gd(2007, 9, 1).equals(f(d, -12)));
  60. }
  61. function testYesterday() {
  62. var d = new gd(2008, 9, 13);
  63. var s = new gd(2008, 9, 12);
  64. var e = new gd(2008, 9, 12);
  65. assertStartEnd('yesterday', s, e, gdr.yesterday(d));
  66. }
  67. function testToday() {
  68. var d = new gd(2008, 9, 13);
  69. assertStartEnd('today', d, d, gdr.today(d));
  70. }
  71. function testLast7Days() {
  72. var d = new gd(2008, 9, 13);
  73. var s = new gd(2008, 9, 6);
  74. var e = new gd(2008, 9, 12);
  75. assertStartEnd('last7Days', s, e, gdr.last7Days(d));
  76. assertStartEnd(
  77. 'last7Days by key', s, e,
  78. gdr.standardDateRange(gdr.StandardDateRangeKeys.LAST_7_DAYS, d));
  79. }
  80. function testThisMonth() {
  81. var d = new gd(2008, 9, 13);
  82. var s = new gd(2008, 9, 1);
  83. var e = new gd(2008, 9, 31);
  84. assertStartEnd('thisMonth', s, e, gdr.thisMonth(d));
  85. assertStartEnd(
  86. 'thisMonth by key', s, e,
  87. gdr.standardDateRange(gdr.StandardDateRangeKeys.THIS_MONTH, d));
  88. }
  89. function testLastMonth() {
  90. var d = new gd(2008, 9, 13);
  91. var s = new gd(2008, 8, 1);
  92. var e = new gd(2008, 8, 30);
  93. assertStartEnd('lastMonth', s, e, gdr.lastMonth(d));
  94. assertStartEnd(
  95. 'lastMonth by key', s, e,
  96. gdr.standardDateRange(gdr.StandardDateRangeKeys.LAST_MONTH, d));
  97. }
  98. function testThisWeek() {
  99. var startDates = [
  100. new gd(2011, 2, 28), new gd(2011, 2, 29), new gd(2011, 2, 30),
  101. new gd(2011, 2, 31), new gd(2011, 3, 1), new gd(2011, 2, 26),
  102. new gd(2011, 2, 27)
  103. ];
  104. var endDates = [
  105. new gd(2011, 3, 3), new gd(2011, 3, 4), new gd(2011, 3, 5),
  106. new gd(2011, 3, 6), new gd(2011, 3, 7), new gd(2011, 3, 1),
  107. new gd(2011, 3, 2)
  108. ];
  109. // 0 - is Monday, 6 is Sunday.
  110. for (var i = 0; i < 7; i++) {
  111. var date = new gd(2011, 3, 1);
  112. date.setFirstDayOfWeek(i);
  113. assertStartEnd(
  114. 'thisWeek, ' + i, startDates[i], endDates[i], gdr.thisWeek(date));
  115. }
  116. assertStartEnd(
  117. 'thisWeek by key ', startDates[goog.i18n.DateTimeSymbols.FIRSTDAYOFWEEK],
  118. endDates[goog.i18n.DateTimeSymbols.FIRSTDAYOFWEEK],
  119. gdr.standardDateRange(
  120. gdr.StandardDateRangeKeys.THIS_WEEK, new gd(2011, 3, 1)));
  121. }
  122. function testLastWeek() {
  123. var startDates = [
  124. new gd(2011, 2, 21), new gd(2011, 2, 22), new gd(2011, 2, 23),
  125. new gd(2011, 2, 24), new gd(2011, 2, 25), new gd(2011, 2, 19),
  126. new gd(2011, 2, 20)
  127. ];
  128. var endDates = [
  129. new gd(2011, 2, 27), new gd(2011, 2, 28), new gd(2011, 2, 29),
  130. new gd(2011, 2, 30), new gd(2011, 2, 31), new gd(2011, 2, 25),
  131. new gd(2011, 2, 26)
  132. ];
  133. // 0 - is Monday, 6 is Sunday.
  134. for (var i = 0; i < 7; i++) {
  135. var date = new gd(2011, 3, 1);
  136. date.setFirstDayOfWeek(i);
  137. assertStartEnd(
  138. 'lastWeek, ' + i, startDates[i], endDates[i], gdr.lastWeek(date));
  139. }
  140. assertStartEnd(
  141. 'lastWeek by key', startDates[goog.i18n.DateTimeSymbols.FIRSTDAYOFWEEK],
  142. endDates[goog.i18n.DateTimeSymbols.FIRSTDAYOFWEEK],
  143. gdr.standardDateRange(
  144. gdr.StandardDateRangeKeys.LAST_WEEK, new gd(2011, 3, 1)));
  145. }
  146. function testLastBusinessWeek() {
  147. var d = new gd(2008, 9, 13);
  148. var s = new gd(2008, 9, 6);
  149. var e = new gd(2008, 9, 10);
  150. assertStartEnd('lastBusinessWeek', s, e, gdr.lastBusinessWeek(d));
  151. assertStartEnd(
  152. 'lastBusinessWeek by key', s, e,
  153. gdr.standardDateRange(gdr.StandardDateRangeKeys.LAST_BUSINESS_WEEK, d));
  154. }
  155. function testAllTime() {
  156. var s = new gd(0, 0, 1);
  157. var e = new gd(9999, 11, 31);
  158. assertStartEnd('allTime', s, e, gdr.allTime());
  159. assertStartEnd(
  160. 'allTime by key', s, e,
  161. gdr.standardDateRange(gdr.StandardDateRangeKeys.ALL_TIME));
  162. }
  163. function testIterator() {
  164. var s = new gd(2008, 9, 1);
  165. var e = new gd(2008, 9, 10);
  166. var i = new gdr(s, e).iterator();
  167. assertTrue('day 0', new gd(2008, 9, 1).equals(i.next()));
  168. assertTrue('day 1', new gd(2008, 9, 2).equals(i.next()));
  169. assertTrue('day 2', new gd(2008, 9, 3).equals(i.next()));
  170. assertTrue('day 3', new gd(2008, 9, 4).equals(i.next()));
  171. assertTrue('day 4', new gd(2008, 9, 5).equals(i.next()));
  172. assertTrue('day 5', new gd(2008, 9, 6).equals(i.next()));
  173. assertTrue('day 6', new gd(2008, 9, 7).equals(i.next()));
  174. assertTrue('day 7', new gd(2008, 9, 8).equals(i.next()));
  175. assertTrue('day 8', new gd(2008, 9, 9).equals(i.next()));
  176. assertTrue('day 9', new gd(2008, 9, 10).equals(i.next()));
  177. assertThrows('day 10', goog.bind(i.next, i));
  178. }
  179. function testContains() {
  180. var r = new gdr(new gd(2008, 9, 10), new gd(2008, 9, 12));
  181. assertFalse('min date', r.contains(goog.date.DateRange.MINIMUM_DATE));
  182. assertFalse('9/10/2007', r.contains(new gd(2007, 9, 10)));
  183. assertFalse('9/9/2008', r.contains(new gd(2008, 9, 9)));
  184. assertTrue('9/10/2008', r.contains(new gd(2008, 9, 10)));
  185. assertTrue('9/11/2008', r.contains(new gd(2008, 9, 11)));
  186. assertTrue('9/12/2008', r.contains(new gd(2008, 9, 12)));
  187. assertFalse('9/13/2008', r.contains(new gd(2008, 9, 13)));
  188. assertFalse('max date', r.contains(goog.date.DateRange.MAXIMUM_DATE));
  189. }
  190. function testSeparateDateObjects() {
  191. // see http://b/23820818
  192. var dr = goog.date.DateRange.today();
  193. var endDate = dr.getEndDate();
  194. endDate.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
  195. var startDate = dr.getStartDate();
  196. assertFalse(startDate.equals(endDate));
  197. }
  198. function assertStartEnd(name, start, end, actual) {
  199. assertTrue(
  200. name + ' start should be ' + start + ' but was ' + actual.getStartDate(),
  201. start.equals(actual.getStartDate()));
  202. assertTrue(
  203. name + ' end should be ' + end + ' but was ' + actual.getEndDate(),
  204. end.equals(actual.getEndDate()));
  205. // see http://b/23820818
  206. assertFalse(
  207. name + ' start date was the same object as the end date',
  208. actual.getStartDate() === actual.getEndDate());
  209. }