| | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|---|
| | 2 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|---|
| | 3 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
|---|
| | 4 | <head> |
|---|
| | 5 | <title>Prototype Unit test file</title> |
|---|
| | 6 | <meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
|---|
| | 7 | <script src="../../dist/prototype.js" type="text/javascript"></script> |
|---|
| | 8 | <script src="../lib/unittest.js" type="text/javascript"></script> |
|---|
| | 9 | <link rel="stylesheet" href="../test.css" type="text/css" /> |
|---|
| | 10 | <style type="text/css" media="screen"> |
|---|
| | 11 | /* <![CDATA[ */ |
|---|
| | 12 | #testcss1 { font-size:11px; color: #f00; } |
|---|
| | 13 | #testcss2 { font-size:12px; color: #0f0; display: none; } |
|---|
| | 14 | /* ]]> */ |
|---|
| | 15 | </style> |
|---|
| | 16 | </head> |
|---|
| | 17 | <body> |
|---|
| | 18 | <h1>Prototype Unit test file</h1> |
|---|
| | 19 | <p> |
|---|
| | 20 | Test of utility functions in date.js |
|---|
| | 21 | </p> |
|---|
| | 22 | |
|---|
| | 23 | <!-- Log output --> |
|---|
| | 24 | <div id="testlog"> </div> |
|---|
| | 25 | |
|---|
| | 26 | <!-- Tests follow --> |
|---|
| | 27 | <script type="text/javascript" language="javascript" charset="utf-8"> |
|---|
| | 28 | // <![CDATA[ |
|---|
| | 29 | var Fixtures = { |
|---|
| | 30 | leapYear: new Date(2004, 1, 1), |
|---|
| | 31 | nonLeapYear: new Date(2005, 1, 1), |
|---|
| | 32 | looksLikeLeapYear: new Date(1900, 1, 1), |
|---|
| | 33 | dateAt31Days: new Date('10/31/2007'), |
|---|
| | 34 | dateAt30Days: new Date('9/30/2007'), |
|---|
| | 35 | dateAt5Days: new Date('1/5/2007'), |
|---|
| | 36 | today: new Date() |
|---|
| | 37 | } |
|---|
| | 38 | |
|---|
| | 39 | new Test.Unit.Runner({ |
|---|
| | 40 | testMonthArray: function() { with(this) { |
|---|
| | 41 | assertEqual('January', Date.months[0]); |
|---|
| | 42 | assertEqual('December', Date.months[11]); |
|---|
| | 43 | assertUndefined(Date.months[12]); |
|---|
| | 44 | }}, |
|---|
| | 45 | testDayOfWeekArray: function() { with(this) { |
|---|
| | 46 | assertEqual('Sunday', Date.weekdays[0]); |
|---|
| | 47 | assertEqual('Saturday', Date.weekdays[6]); |
|---|
| | 48 | assertUndefined(Date.weekdays[7]); |
|---|
| | 49 | }}, |
|---|
| | 50 | testDaysInMonth: function() { with(this) { |
|---|
| | 51 | assertEqual(31, (new Date(2005, 0, 1).daysInMonth())); |
|---|
| | 52 | assertEqual(29, Fixtures.leapYear.daysInMonth()); |
|---|
| | 53 | assertEqual(28, Fixtures.nonLeapYear.daysInMonth()); |
|---|
| | 54 | assertEqual(28, Fixtures.looksLikeLeapYear.daysInMonth()); |
|---|
| | 55 | assertEqual(31, (new Date(2005, 2, 1).daysInMonth())); |
|---|
| | 56 | assertEqual(30, (new Date(2005, 3, 1).daysInMonth())); |
|---|
| | 57 | assertEqual(31, (new Date(2005, 4, 1).daysInMonth())); |
|---|
| | 58 | assertEqual(30, (new Date(2005, 5, 1).daysInMonth())); |
|---|
| | 59 | assertEqual(31, (new Date(2005, 6, 1).daysInMonth())); |
|---|
| | 60 | assertEqual(31, (new Date(2005, 7, 1).daysInMonth())); |
|---|
| | 61 | assertEqual(30, (new Date(2005, 8, 1).daysInMonth())); |
|---|
| | 62 | assertEqual(31, (new Date(2005, 9, 1).daysInMonth())); |
|---|
| | 63 | assertEqual(30, (new Date(2005, 10, 1).daysInMonth())); |
|---|
| | 64 | assertEqual(31, (new Date(2005, 11, 1).daysInMonth())); |
|---|
| | 65 | assertEqual(31, Date.daysInMonth({year: 2007, month: 0})); |
|---|
| | 66 | assertEqual(31, Date.daysInMonth(new Date(2007, 0, 1))); |
|---|
| | 67 | }}, |
|---|
| | 68 | testLeapYears: function() { with(this) { |
|---|
| | 69 | assert(Fixtures.leapYear.isLeapYear()); |
|---|
| | 70 | assert(Fixtures.nonLeapYear.isLeapYear() == false); |
|---|
| | 71 | assert(Fixtures.looksLikeLeapYear.isLeapYear() == false); |
|---|
| | 72 | }}, |
|---|
| | 73 | testChangingMonthsCorrectsDay: function() { with(this) { |
|---|
| | 74 | assertEqual(31, Fixtures.dateAt31Days.dayOfChangedMonth({month: 9, year: 2008})); |
|---|
| | 75 | assertEqual(29, Fixtures.dateAt31Days.dayOfChangedMonth({month: 1, year: 2008})); |
|---|
| | 76 | assertEqual(30, Fixtures.dateAt31Days.dayOfChangedMonth({month: 8, year: 2008})); |
|---|
| | 77 | assertEqual(30, Fixtures.dateAt30Days.dayOfChangedMonth({month: 9, year: 2008})); |
|---|
| | 78 | assertEqual(29, Fixtures.dateAt30Days.dayOfChangedMonth({month: 1, year: 2008})); |
|---|
| | 79 | assertEqual(30, Fixtures.dateAt30Days.dayOfChangedMonth({month: 8, year: 2008})); |
|---|
| | 80 | assertEqual(5, Fixtures.dateAt5Days.dayOfChangedMonth({month: 9, year: 2008})); |
|---|
| | 81 | assertEqual(5, Fixtures.dateAt5Days.dayOfChangedMonth({month: 1, year: 2008})); |
|---|
| | 82 | assertEqual(5, Fixtures.dateAt5Days.dayOfChangedMonth({month: 8, year: 2008})); |
|---|
| | 83 | }}, |
|---|
| | 84 | testAdvanceMonth: function() { with(this) { |
|---|
| | 85 | assertEqual(new Date('12/1/2008').beginningOfDay().getTime(), new Date('11/1/2007').beginningOfDay().advanceMonth(13).getTime()); |
|---|
| | 86 | assertEqual(new Date('10/1/2007').beginningOfDay().getTime(), new Date('11/1/2007').beginningOfDay().advanceMonth(-1).getTime()); |
|---|
| | 87 | assertEqual(new Date('9/30/2007').beginningOfDay().getTime(), new Date('10/31/2007').beginningOfDay().advanceMonth(-1).getTime()); |
|---|
| | 88 | assertEqual(new Date('2/29/2004').beginningOfDay().getTime(), new Date('3/31/2004').beginningOfDay().advanceMonth(-1).getTime()); |
|---|
| | 89 | assertEqual(new Date('2/28/2007').beginningOfDay().getTime(), new Date('1/29/2007').beginningOfDay().advanceMonth(1).getTime()); |
|---|
| | 90 | }}, |
|---|
| | 91 | testClone: function() { with(this) { |
|---|
| | 92 | assertEqual(Fixtures.dateAt5Days.getTime(), Fixtures.dateAt5Days.clone().getTime()); |
|---|
| | 93 | var newDate = Fixtures.dateAt5Days.clone(); |
|---|
| | 94 | newDate.setDate(newDate.getDate() + 5); |
|---|
| | 95 | assert(Fixtures.dateAt5Days.getTime() != newDate.getTime()); |
|---|
| | 96 | }}, |
|---|
| | 97 | testAdvance: function() { with(this) { |
|---|
| | 98 | assertEqual(new Date('12/1/2007').getTime(), new Date('11/1/2007').advance({months: 1}).getTime()); |
|---|
| | 99 | assertEqual(new Date('11/1/2008').getTime(), new Date('11/1/2007').advance({years: 1}).getTime()); |
|---|
| | 100 | assertEqual(new Date('11/10/2007').getTime(), new Date('11/1/2007').advance({days: 9}).getTime()); |
|---|
| | 101 | assertEqual(new Date('12/1/2007').getTime(), new Date('11/1/2007').advance({days: 30}).getTime()); |
|---|
| | 102 | assertEqual(new Date('10/31/2007').getTime(), new Date('11/1/2007').advance({days: -1}).getTime()); |
|---|
| | 103 | assertEqual(new Date('12/31/2007').getTime(), new Date('11/1/2007').advance({months: 1, days: 30}).getTime()); |
|---|
| | 104 | assertEqual(new Date('1/1/2008').getTime(), new Date('11/1/2007').advance({months: 1, days: 31}).getTime()); |
|---|
| | 105 | assertEqual(new Date('12/6/2009').getTime(), new Date('11/1/2007').advance({years: 2, months: 1, days: 5}).getTime()); |
|---|
| | 106 | }}, |
|---|
| | 107 | testStripTime: function() { with(this) { |
|---|
| | 108 | assertEqual(0, Fixtures.dateAt5Days.stripTime().getHours()); |
|---|
| | 109 | assertEqual(0, Fixtures.dateAt5Days.stripTime().getMinutes()); |
|---|
| | 110 | assertEqual(0, Fixtures.dateAt5Days.stripTime().getSeconds()); |
|---|
| | 111 | assertEqual(0, Fixtures.dateAt5Days.stripTime().getMilliseconds()); |
|---|
| | 112 | }}, |
|---|
| | 113 | testYearsAgo: function() { with(this) { |
|---|
| | 114 | assertEqual(new Date('1/1/2006').getTime(), new Date('1/1/2007').yearsAgo(1).getTime()); |
|---|
| | 115 | assertEqual(new Date('2/28/2001').getTime(), new Date('2/29/2004').yearsAgo(3).getTime()); |
|---|
| | 116 | }}, |
|---|
| | 117 | testMonthsAgo: function() { with(this) { |
|---|
| | 118 | assertEqual(new Date('1/1/2008').getTime(), new Date('3/1/2008').monthsAgo(2).getTime()); |
|---|
| | 119 | assertEqual(new Date('2/28/2007').getTime(), new Date('3/31/2008').monthsAgo(13).getTime()); |
|---|
| | 120 | }}, |
|---|
| | 121 | testDaysAgo: function() { with(this) { |
|---|
| | 122 | assertEqual(new Date('1/1/2008').getTime(), new Date('1/15/2008').daysAgo(14).getTime()); |
|---|
| | 123 | assertEqual(new Date('2/29/2004').getTime(), new Date('3/1/2004').daysAgo(1).getTime()); |
|---|
| | 124 | assertEqual(new Date('9/30/2007').getTime(), new Date('10/31/2007').daysAgo(31).getTime()); |
|---|
| | 125 | assertEqual(new Date('12/31/2006').getTime(), new Date('1/31/2007').daysAgo(31).getTime()); |
|---|
| | 126 | }}, |
|---|
| | 127 | testBeginningOfWeek: function() { with(this) { |
|---|
| | 128 | assertEqual(new Date('11/12/2007').getTime(), new Date('11/18/2007').beginningOfWeek().getTime()); |
|---|
| | 129 | assertEqual(new Date('11/12/2007').getTime(), new Date('11/12/2007').beginningOfWeek().getTime()); |
|---|
| | 130 | }}, |
|---|
| | 131 | testIsToday: function() { with(this) { |
|---|
| | 132 | assert(Fixtures.today.isToday()); |
|---|
| | 133 | assert(!Fixtures.today.daysAgo(1).isToday()); |
|---|
| | 134 | }}, |
|---|
| | 135 | testIsYesterday: function() { with(this) { |
|---|
| | 136 | assert(Fixtures.today.daysAgo(1).isYesterday()); |
|---|
| | 137 | assert(!Fixtures.today.isYesterday()); |
|---|
| | 138 | }} |
|---|
| | 139 | }, 'testlog'); |
|---|
| | 140 | // ]]> |
|---|
| | 141 | </script> |
|---|
| | 142 | </body> |
|---|
| | 143 | </html> |