Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 1 | import calendar |
| 2 | import unittest |
| 3 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 5 | |
| 6 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7 | result_2004_text = """ |
| 8 | 2004 |
| 9 | |
| 10 | January February March |
| 11 | Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su |
| 12 | 1 2 3 4 1 1 2 3 4 5 6 7 |
| 13 | 5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14 |
| 14 | 12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21 |
| 15 | 19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28 |
| 16 | 26 27 28 29 30 31 23 24 25 26 27 28 29 29 30 31 |
| 17 | |
| 18 | April May June |
| 19 | Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su |
| 20 | 1 2 3 4 1 2 1 2 3 4 5 6 |
| 21 | 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13 |
| 22 | 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20 |
| 23 | 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27 |
| 24 | 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30 |
| 25 | 31 |
| 26 | |
| 27 | July August September |
| 28 | Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su |
| 29 | 1 2 3 4 1 1 2 3 4 5 |
| 30 | 5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12 |
| 31 | 12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19 |
| 32 | 19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26 |
| 33 | 26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30 |
| 34 | 30 31 |
| 35 | |
| 36 | October November December |
| 37 | Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su |
| 38 | 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 |
| 39 | 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12 |
| 40 | 11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19 |
| 41 | 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26 |
| 42 | 25 26 27 28 29 30 31 29 30 27 28 29 30 31 |
| 43 | """ |
| 44 | |
| 45 | result_2004_html = """ |
| 46 | <?xml version="1.0" encoding="ascii"?> |
| 47 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 48 | <html> |
| 49 | <head> |
| 50 | <meta http-equiv="Content-Type" content="text/html; charset=ascii" /> |
| 51 | <link rel="stylesheet" type="text/css" href="calendar.css" /> |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 52 | <title>Calendar for 2004</title> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 53 | </head> |
| 54 | <body> |
| 55 | <table border="0" cellpadding="0" cellspacing="0" class="year"> |
| 56 | <tr><th colspan="3" class="year">2004</th></tr><tr><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 57 | <tr><th colspan="7" class="month">January</th></tr> |
| 58 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 59 | <tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="thu">1</td><td class="fri">2</td><td class="sat">3</td><td class="sun">4</td></tr> |
| 60 | <tr><td class="mon">5</td><td class="tue">6</td><td class="wed">7</td><td class="thu">8</td><td class="fri">9</td><td class="sat">10</td><td class="sun">11</td></tr> |
| 61 | <tr><td class="mon">12</td><td class="tue">13</td><td class="wed">14</td><td class="thu">15</td><td class="fri">16</td><td class="sat">17</td><td class="sun">18</td></tr> |
| 62 | <tr><td class="mon">19</td><td class="tue">20</td><td class="wed">21</td><td class="thu">22</td><td class="fri">23</td><td class="sat">24</td><td class="sun">25</td></tr> |
| 63 | <tr><td class="mon">26</td><td class="tue">27</td><td class="wed">28</td><td class="thu">29</td><td class="fri">30</td><td class="sat">31</td><td class="noday"> </td></tr> |
| 64 | </table> |
| 65 | </td><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 66 | <tr><th colspan="7" class="month">February</th></tr> |
| 67 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 68 | <tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="sun">1</td></tr> |
| 69 | <tr><td class="mon">2</td><td class="tue">3</td><td class="wed">4</td><td class="thu">5</td><td class="fri">6</td><td class="sat">7</td><td class="sun">8</td></tr> |
| 70 | <tr><td class="mon">9</td><td class="tue">10</td><td class="wed">11</td><td class="thu">12</td><td class="fri">13</td><td class="sat">14</td><td class="sun">15</td></tr> |
| 71 | <tr><td class="mon">16</td><td class="tue">17</td><td class="wed">18</td><td class="thu">19</td><td class="fri">20</td><td class="sat">21</td><td class="sun">22</td></tr> |
| 72 | <tr><td class="mon">23</td><td class="tue">24</td><td class="wed">25</td><td class="thu">26</td><td class="fri">27</td><td class="sat">28</td><td class="sun">29</td></tr> |
| 73 | </table> |
| 74 | </td><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 75 | <tr><th colspan="7" class="month">March</th></tr> |
| 76 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 77 | <tr><td class="mon">1</td><td class="tue">2</td><td class="wed">3</td><td class="thu">4</td><td class="fri">5</td><td class="sat">6</td><td class="sun">7</td></tr> |
| 78 | <tr><td class="mon">8</td><td class="tue">9</td><td class="wed">10</td><td class="thu">11</td><td class="fri">12</td><td class="sat">13</td><td class="sun">14</td></tr> |
| 79 | <tr><td class="mon">15</td><td class="tue">16</td><td class="wed">17</td><td class="thu">18</td><td class="fri">19</td><td class="sat">20</td><td class="sun">21</td></tr> |
| 80 | <tr><td class="mon">22</td><td class="tue">23</td><td class="wed">24</td><td class="thu">25</td><td class="fri">26</td><td class="sat">27</td><td class="sun">28</td></tr> |
| 81 | <tr><td class="mon">29</td><td class="tue">30</td><td class="wed">31</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr> |
| 82 | </table> |
| 83 | </td></tr><tr><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 84 | <tr><th colspan="7" class="month">April</th></tr> |
| 85 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 86 | <tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="thu">1</td><td class="fri">2</td><td class="sat">3</td><td class="sun">4</td></tr> |
| 87 | <tr><td class="mon">5</td><td class="tue">6</td><td class="wed">7</td><td class="thu">8</td><td class="fri">9</td><td class="sat">10</td><td class="sun">11</td></tr> |
| 88 | <tr><td class="mon">12</td><td class="tue">13</td><td class="wed">14</td><td class="thu">15</td><td class="fri">16</td><td class="sat">17</td><td class="sun">18</td></tr> |
| 89 | <tr><td class="mon">19</td><td class="tue">20</td><td class="wed">21</td><td class="thu">22</td><td class="fri">23</td><td class="sat">24</td><td class="sun">25</td></tr> |
| 90 | <tr><td class="mon">26</td><td class="tue">27</td><td class="wed">28</td><td class="thu">29</td><td class="fri">30</td><td class="noday"> </td><td class="noday"> </td></tr> |
| 91 | </table> |
| 92 | </td><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 93 | <tr><th colspan="7" class="month">May</th></tr> |
| 94 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 95 | <tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="sat">1</td><td class="sun">2</td></tr> |
| 96 | <tr><td class="mon">3</td><td class="tue">4</td><td class="wed">5</td><td class="thu">6</td><td class="fri">7</td><td class="sat">8</td><td class="sun">9</td></tr> |
| 97 | <tr><td class="mon">10</td><td class="tue">11</td><td class="wed">12</td><td class="thu">13</td><td class="fri">14</td><td class="sat">15</td><td class="sun">16</td></tr> |
| 98 | <tr><td class="mon">17</td><td class="tue">18</td><td class="wed">19</td><td class="thu">20</td><td class="fri">21</td><td class="sat">22</td><td class="sun">23</td></tr> |
| 99 | <tr><td class="mon">24</td><td class="tue">25</td><td class="wed">26</td><td class="thu">27</td><td class="fri">28</td><td class="sat">29</td><td class="sun">30</td></tr> |
| 100 | <tr><td class="mon">31</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr> |
| 101 | </table> |
| 102 | </td><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 103 | <tr><th colspan="7" class="month">June</th></tr> |
| 104 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 105 | <tr><td class="noday"> </td><td class="tue">1</td><td class="wed">2</td><td class="thu">3</td><td class="fri">4</td><td class="sat">5</td><td class="sun">6</td></tr> |
| 106 | <tr><td class="mon">7</td><td class="tue">8</td><td class="wed">9</td><td class="thu">10</td><td class="fri">11</td><td class="sat">12</td><td class="sun">13</td></tr> |
| 107 | <tr><td class="mon">14</td><td class="tue">15</td><td class="wed">16</td><td class="thu">17</td><td class="fri">18</td><td class="sat">19</td><td class="sun">20</td></tr> |
| 108 | <tr><td class="mon">21</td><td class="tue">22</td><td class="wed">23</td><td class="thu">24</td><td class="fri">25</td><td class="sat">26</td><td class="sun">27</td></tr> |
| 109 | <tr><td class="mon">28</td><td class="tue">29</td><td class="wed">30</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr> |
| 110 | </table> |
| 111 | </td></tr><tr><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 112 | <tr><th colspan="7" class="month">July</th></tr> |
| 113 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 114 | <tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="thu">1</td><td class="fri">2</td><td class="sat">3</td><td class="sun">4</td></tr> |
| 115 | <tr><td class="mon">5</td><td class="tue">6</td><td class="wed">7</td><td class="thu">8</td><td class="fri">9</td><td class="sat">10</td><td class="sun">11</td></tr> |
| 116 | <tr><td class="mon">12</td><td class="tue">13</td><td class="wed">14</td><td class="thu">15</td><td class="fri">16</td><td class="sat">17</td><td class="sun">18</td></tr> |
| 117 | <tr><td class="mon">19</td><td class="tue">20</td><td class="wed">21</td><td class="thu">22</td><td class="fri">23</td><td class="sat">24</td><td class="sun">25</td></tr> |
| 118 | <tr><td class="mon">26</td><td class="tue">27</td><td class="wed">28</td><td class="thu">29</td><td class="fri">30</td><td class="sat">31</td><td class="noday"> </td></tr> |
| 119 | </table> |
| 120 | </td><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 121 | <tr><th colspan="7" class="month">August</th></tr> |
| 122 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 123 | <tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="sun">1</td></tr> |
| 124 | <tr><td class="mon">2</td><td class="tue">3</td><td class="wed">4</td><td class="thu">5</td><td class="fri">6</td><td class="sat">7</td><td class="sun">8</td></tr> |
| 125 | <tr><td class="mon">9</td><td class="tue">10</td><td class="wed">11</td><td class="thu">12</td><td class="fri">13</td><td class="sat">14</td><td class="sun">15</td></tr> |
| 126 | <tr><td class="mon">16</td><td class="tue">17</td><td class="wed">18</td><td class="thu">19</td><td class="fri">20</td><td class="sat">21</td><td class="sun">22</td></tr> |
| 127 | <tr><td class="mon">23</td><td class="tue">24</td><td class="wed">25</td><td class="thu">26</td><td class="fri">27</td><td class="sat">28</td><td class="sun">29</td></tr> |
| 128 | <tr><td class="mon">30</td><td class="tue">31</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr> |
| 129 | </table> |
| 130 | </td><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 131 | <tr><th colspan="7" class="month">September</th></tr> |
| 132 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 133 | <tr><td class="noday"> </td><td class="noday"> </td><td class="wed">1</td><td class="thu">2</td><td class="fri">3</td><td class="sat">4</td><td class="sun">5</td></tr> |
| 134 | <tr><td class="mon">6</td><td class="tue">7</td><td class="wed">8</td><td class="thu">9</td><td class="fri">10</td><td class="sat">11</td><td class="sun">12</td></tr> |
| 135 | <tr><td class="mon">13</td><td class="tue">14</td><td class="wed">15</td><td class="thu">16</td><td class="fri">17</td><td class="sat">18</td><td class="sun">19</td></tr> |
| 136 | <tr><td class="mon">20</td><td class="tue">21</td><td class="wed">22</td><td class="thu">23</td><td class="fri">24</td><td class="sat">25</td><td class="sun">26</td></tr> |
| 137 | <tr><td class="mon">27</td><td class="tue">28</td><td class="wed">29</td><td class="thu">30</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr> |
| 138 | </table> |
| 139 | </td></tr><tr><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 140 | <tr><th colspan="7" class="month">October</th></tr> |
| 141 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 142 | <tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="fri">1</td><td class="sat">2</td><td class="sun">3</td></tr> |
| 143 | <tr><td class="mon">4</td><td class="tue">5</td><td class="wed">6</td><td class="thu">7</td><td class="fri">8</td><td class="sat">9</td><td class="sun">10</td></tr> |
| 144 | <tr><td class="mon">11</td><td class="tue">12</td><td class="wed">13</td><td class="thu">14</td><td class="fri">15</td><td class="sat">16</td><td class="sun">17</td></tr> |
| 145 | <tr><td class="mon">18</td><td class="tue">19</td><td class="wed">20</td><td class="thu">21</td><td class="fri">22</td><td class="sat">23</td><td class="sun">24</td></tr> |
| 146 | <tr><td class="mon">25</td><td class="tue">26</td><td class="wed">27</td><td class="thu">28</td><td class="fri">29</td><td class="sat">30</td><td class="sun">31</td></tr> |
| 147 | </table> |
| 148 | </td><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 149 | <tr><th colspan="7" class="month">November</th></tr> |
| 150 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 151 | <tr><td class="mon">1</td><td class="tue">2</td><td class="wed">3</td><td class="thu">4</td><td class="fri">5</td><td class="sat">6</td><td class="sun">7</td></tr> |
| 152 | <tr><td class="mon">8</td><td class="tue">9</td><td class="wed">10</td><td class="thu">11</td><td class="fri">12</td><td class="sat">13</td><td class="sun">14</td></tr> |
| 153 | <tr><td class="mon">15</td><td class="tue">16</td><td class="wed">17</td><td class="thu">18</td><td class="fri">19</td><td class="sat">20</td><td class="sun">21</td></tr> |
| 154 | <tr><td class="mon">22</td><td class="tue">23</td><td class="wed">24</td><td class="thu">25</td><td class="fri">26</td><td class="sat">27</td><td class="sun">28</td></tr> |
| 155 | <tr><td class="mon">29</td><td class="tue">30</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr> |
| 156 | </table> |
| 157 | </td><td><table border="0" cellpadding="0" cellspacing="0" class="month"> |
| 158 | <tr><th colspan="7" class="month">December</th></tr> |
| 159 | <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr> |
| 160 | <tr><td class="noday"> </td><td class="noday"> </td><td class="wed">1</td><td class="thu">2</td><td class="fri">3</td><td class="sat">4</td><td class="sun">5</td></tr> |
| 161 | <tr><td class="mon">6</td><td class="tue">7</td><td class="wed">8</td><td class="thu">9</td><td class="fri">10</td><td class="sat">11</td><td class="sun">12</td></tr> |
| 162 | <tr><td class="mon">13</td><td class="tue">14</td><td class="wed">15</td><td class="thu">16</td><td class="fri">17</td><td class="sat">18</td><td class="sun">19</td></tr> |
| 163 | <tr><td class="mon">20</td><td class="tue">21</td><td class="wed">22</td><td class="thu">23</td><td class="fri">24</td><td class="sat">25</td><td class="sun">26</td></tr> |
| 164 | <tr><td class="mon">27</td><td class="tue">28</td><td class="wed">29</td><td class="thu">30</td><td class="fri">31</td><td class="noday"> </td><td class="noday"> </td></tr> |
| 165 | </table> |
| 166 | </td></tr></table></body> |
| 167 | </html> |
| 168 | """ |
| 169 | |
| 170 | |
| 171 | class OutputTestCase(unittest.TestCase): |
| 172 | def normalize_calendar(self, s): |
| 173 | # Filters out locale dependant strings |
| 174 | def neitherspacenordigit(c): |
| 175 | return not c.isspace() and not c.isdigit() |
| 176 | |
| 177 | lines = [] |
| 178 | for line in s.splitlines(False): |
| 179 | # Drop texts, as they are locale dependent |
| 180 | if line and not filter(neitherspacenordigit, line): |
| 181 | lines.append(line) |
| 182 | return lines |
| 183 | |
| 184 | def test_output(self): |
| 185 | self.assertEqual( |
| 186 | self.normalize_calendar(calendar.calendar(2004)), |
| 187 | self.normalize_calendar(result_2004_text) |
| 188 | ) |
| 189 | |
| 190 | def test_output_textcalendar(self): |
| 191 | self.assertEqual( |
| 192 | calendar.TextCalendar().formatyear(2004).strip(), |
| 193 | result_2004_text.strip() |
| 194 | ) |
| 195 | |
| 196 | def test_output_htmlcalendar(self): |
Walter Dörwald | c49ebc0 | 2007-05-22 17:03:39 +0000 | [diff] [blame] | 197 | encoding = 'ascii' |
| 198 | cal = calendar.HTMLCalendar() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 199 | self.assertEqual( |
Walter Dörwald | c49ebc0 | 2007-05-22 17:03:39 +0000 | [diff] [blame] | 200 | cal.formatyearpage(2004, encoding=encoding).strip(b' \t\n'), |
| 201 | result_2004_html.strip(' \t\n').encode(encoding) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 202 | ) |
| 203 | |
| 204 | |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 205 | class CalendarTestCase(unittest.TestCase): |
| 206 | def test_isleap(self): |
| 207 | # Make sure that the return is right for a few years, and |
| 208 | # ensure that the return values are 1 or 0, not just true or |
| 209 | # false (see SF bug #485794). Specific additional tests may |
| 210 | # be appropriate; this tests a single "cycle". |
| 211 | self.assertEqual(calendar.isleap(2000), 1) |
| 212 | self.assertEqual(calendar.isleap(2001), 0) |
| 213 | self.assertEqual(calendar.isleap(2002), 0) |
| 214 | self.assertEqual(calendar.isleap(2003), 0) |
| 215 | |
| 216 | def test_setfirstweekday(self): |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 217 | self.assertRaises(TypeError, calendar.setfirstweekday, 'flabber') |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 218 | self.assertRaises(ValueError, calendar.setfirstweekday, -1) |
| 219 | self.assertRaises(ValueError, calendar.setfirstweekday, 200) |
| 220 | orig = calendar.firstweekday() |
| 221 | calendar.setfirstweekday(calendar.SUNDAY) |
| 222 | self.assertEqual(calendar.firstweekday(), calendar.SUNDAY) |
| 223 | calendar.setfirstweekday(calendar.MONDAY) |
| 224 | self.assertEqual(calendar.firstweekday(), calendar.MONDAY) |
| 225 | calendar.setfirstweekday(orig) |
| 226 | |
Skip Montanaro | 693c6c4 | 2002-03-15 03:57:04 +0000 | [diff] [blame] | 227 | def test_enumerateweekdays(self): |
| 228 | self.assertRaises(IndexError, calendar.day_abbr.__getitem__, -10) |
| 229 | self.assertRaises(IndexError, calendar.day_name.__getitem__, 10) |
| 230 | self.assertEqual(len([d for d in calendar.day_abbr]), 7) |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 231 | |
Tim Peters | 0c2c8e7 | 2002-03-23 03:26:53 +0000 | [diff] [blame] | 232 | def test_days(self): |
| 233 | for attr in "day_name", "day_abbr": |
| 234 | value = getattr(calendar, attr) |
| 235 | self.assertEqual(len(value), 7) |
| 236 | self.assertEqual(len(value[:]), 7) |
| 237 | # ensure they're all unique |
Tim Peters | bbc0d44 | 2004-11-13 16:18:32 +0000 | [diff] [blame] | 238 | self.assertEqual(len(set(value)), 7) |
| 239 | # verify it "acts like a sequence" in two forms of iteration |
| 240 | self.assertEqual(value[::-1], list(reversed(value))) |
Tim Peters | 0c2c8e7 | 2002-03-23 03:26:53 +0000 | [diff] [blame] | 241 | |
| 242 | def test_months(self): |
| 243 | for attr in "month_name", "month_abbr": |
| 244 | value = getattr(calendar, attr) |
| 245 | self.assertEqual(len(value), 13) |
| 246 | self.assertEqual(len(value[:]), 13) |
| 247 | self.assertEqual(value[0], "") |
| 248 | # ensure they're all unique |
Tim Peters | bbc0d44 | 2004-11-13 16:18:32 +0000 | [diff] [blame] | 249 | self.assertEqual(len(set(value)), 13) |
| 250 | # verify it "acts like a sequence" in two forms of iteration |
| 251 | self.assertEqual(value[::-1], list(reversed(value))) |
Tim Peters | 0c2c8e7 | 2002-03-23 03:26:53 +0000 | [diff] [blame] | 252 | |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 253 | |
| 254 | class MonthCalendarTestCase(unittest.TestCase): |
| 255 | def setUp(self): |
| 256 | self.oldfirstweekday = calendar.firstweekday() |
| 257 | calendar.setfirstweekday(self.firstweekday) |
| 258 | |
| 259 | def tearDown(self): |
| 260 | calendar.setfirstweekday(self.oldfirstweekday) |
| 261 | |
| 262 | def check_weeks(self, year, month, weeks): |
| 263 | cal = calendar.monthcalendar(year, month) |
| 264 | self.assertEqual(len(cal), len(weeks)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 265 | for i in range(len(weeks)): |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 266 | self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) |
| 267 | |
| 268 | |
| 269 | class MondayTestCase(MonthCalendarTestCase): |
| 270 | firstweekday = calendar.MONDAY |
| 271 | |
| 272 | def test_february(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 273 | # A 28-day february starting on monday (7+7+7+7 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 274 | self.check_weeks(1999, 2, (7, 7, 7, 7)) |
| 275 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 276 | # A 28-day february starting on tuesday (6+7+7+7+1 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 277 | self.check_weeks(2005, 2, (6, 7, 7, 7, 1)) |
| 278 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 279 | # A 28-day february starting on sunday (1+7+7+7+6 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 280 | self.check_weeks(1987, 2, (1, 7, 7, 7, 6)) |
| 281 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 282 | # A 29-day february starting on monday (7+7+7+7+1 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 283 | self.check_weeks(1988, 2, (7, 7, 7, 7, 1)) |
| 284 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 285 | # A 29-day february starting on tuesday (6+7+7+7+2 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 286 | self.check_weeks(1972, 2, (6, 7, 7, 7, 2)) |
| 287 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 288 | # A 29-day february starting on sunday (1+7+7+7+7 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 289 | self.check_weeks(2004, 2, (1, 7, 7, 7, 7)) |
| 290 | |
| 291 | def test_april(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 292 | # A 30-day april starting on monday (7+7+7+7+2 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 293 | self.check_weeks(1935, 4, (7, 7, 7, 7, 2)) |
| 294 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 295 | # A 30-day april starting on tuesday (6+7+7+7+3 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 296 | self.check_weeks(1975, 4, (6, 7, 7, 7, 3)) |
| 297 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 298 | # A 30-day april starting on sunday (1+7+7+7+7+1 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 299 | self.check_weeks(1945, 4, (1, 7, 7, 7, 7, 1)) |
| 300 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 301 | # A 30-day april starting on saturday (2+7+7+7+7 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 302 | self.check_weeks(1995, 4, (2, 7, 7, 7, 7)) |
| 303 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 304 | # A 30-day april starting on friday (3+7+7+7+6 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 305 | self.check_weeks(1994, 4, (3, 7, 7, 7, 6)) |
| 306 | |
| 307 | def test_december(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 308 | # A 31-day december starting on monday (7+7+7+7+3 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 309 | self.check_weeks(1980, 12, (7, 7, 7, 7, 3)) |
| 310 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 311 | # A 31-day december starting on tuesday (6+7+7+7+4 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 312 | self.check_weeks(1987, 12, (6, 7, 7, 7, 4)) |
| 313 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 314 | # A 31-day december starting on sunday (1+7+7+7+7+2 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 315 | self.check_weeks(1968, 12, (1, 7, 7, 7, 7, 2)) |
| 316 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 317 | # A 31-day december starting on thursday (4+7+7+7+6 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 318 | self.check_weeks(1988, 12, (4, 7, 7, 7, 6)) |
| 319 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 320 | # A 31-day december starting on friday (3+7+7+7+7 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 321 | self.check_weeks(2017, 12, (3, 7, 7, 7, 7)) |
| 322 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 323 | # A 31-day december starting on saturday (2+7+7+7+7+1 days) |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 324 | self.check_weeks(2068, 12, (2, 7, 7, 7, 7, 1)) |
| 325 | |
| 326 | |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 327 | class SundayTestCase(MonthCalendarTestCase): |
| 328 | firstweekday = calendar.SUNDAY |
| 329 | |
| 330 | def test_february(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 331 | # A 28-day february starting on sunday (7+7+7+7 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 332 | self.check_weeks(2009, 2, (7, 7, 7, 7)) |
| 333 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 334 | # A 28-day february starting on monday (6+7+7+7+1 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 335 | self.check_weeks(1999, 2, (6, 7, 7, 7, 1)) |
| 336 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 337 | # A 28-day february starting on saturday (1+7+7+7+6 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 338 | self.check_weeks(1997, 2, (1, 7, 7, 7, 6)) |
| 339 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 340 | # A 29-day february starting on sunday (7+7+7+7+1 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 341 | self.check_weeks(2004, 2, (7, 7, 7, 7, 1)) |
| 342 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | # A 29-day february starting on monday (6+7+7+7+2 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 344 | self.check_weeks(1960, 2, (6, 7, 7, 7, 2)) |
| 345 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | # A 29-day february starting on saturday (1+7+7+7+7 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 347 | self.check_weeks(1964, 2, (1, 7, 7, 7, 7)) |
| 348 | |
| 349 | def test_april(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 350 | # A 30-day april starting on sunday (7+7+7+7+2 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 351 | self.check_weeks(1923, 4, (7, 7, 7, 7, 2)) |
| 352 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 353 | # A 30-day april starting on monday (6+7+7+7+3 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 354 | self.check_weeks(1918, 4, (6, 7, 7, 7, 3)) |
| 355 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 356 | # A 30-day april starting on saturday (1+7+7+7+7+1 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 357 | self.check_weeks(1950, 4, (1, 7, 7, 7, 7, 1)) |
| 358 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 359 | # A 30-day april starting on friday (2+7+7+7+7 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 360 | self.check_weeks(1960, 4, (2, 7, 7, 7, 7)) |
| 361 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 362 | # A 30-day april starting on thursday (3+7+7+7+6 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 363 | self.check_weeks(1909, 4, (3, 7, 7, 7, 6)) |
| 364 | |
| 365 | def test_december(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 366 | # A 31-day december starting on sunday (7+7+7+7+3 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 367 | self.check_weeks(2080, 12, (7, 7, 7, 7, 3)) |
| 368 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 369 | # A 31-day december starting on monday (6+7+7+7+4 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 370 | self.check_weeks(1941, 12, (6, 7, 7, 7, 4)) |
| 371 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 372 | # A 31-day december starting on saturday (1+7+7+7+7+2 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 373 | self.check_weeks(1923, 12, (1, 7, 7, 7, 7, 2)) |
| 374 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 375 | # A 31-day december starting on wednesday (4+7+7+7+6 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 376 | self.check_weeks(1948, 12, (4, 7, 7, 7, 6)) |
| 377 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 378 | # A 31-day december starting on thursday (3+7+7+7+7 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 379 | self.check_weeks(1927, 12, (3, 7, 7, 7, 7)) |
| 380 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 381 | # A 31-day december starting on friday (2+7+7+7+7+1 days) |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 382 | self.check_weeks(1995, 12, (2, 7, 7, 7, 7, 1)) |
| 383 | |
| 384 | |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 385 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 386 | support.run_unittest( |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 387 | OutputTestCase, |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 388 | CalendarTestCase, |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 389 | MondayTestCase, |
| 390 | SundayTestCase |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 391 | ) |
| 392 | |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 393 | |
| 394 | if __name__ == "__main__": |
| 395 | test_main() |