Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 1 | import calendar |
| 2 | import unittest |
| 3 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 4 | from test import test_support |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 5 | |
| 6 | |
Walter Dörwald | bc96609 | 2006-04-12 10:09:16 +0000 | [diff] [blame] | 7 | result_2004_text = """ |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 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 | |
Walter Dörwald | bc96609 | 2006-04-12 10:09:16 +0000 | [diff] [blame] | 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" /> |
| 52 | <title>Calendar for 2004</title |
| 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 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 170 | |
| 171 | class OutputTestCase(unittest.TestCase): |
| 172 | def normalize_calendar(self, s): |
Walter Dörwald | bc96609 | 2006-04-12 10:09:16 +0000 | [diff] [blame] | 173 | # Filters out locale dependant strings |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 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)), |
Walter Dörwald | bc96609 | 2006-04-12 10:09:16 +0000 | [diff] [blame] | 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): |
| 197 | self.assertEqual( |
| 198 | calendar.HTMLCalendar().formatyearpage(2004).strip(), |
| 199 | result_2004_html.strip() |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 200 | ) |
| 201 | |
| 202 | |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 203 | class CalendarTestCase(unittest.TestCase): |
| 204 | def test_isleap(self): |
| 205 | # Make sure that the return is right for a few years, and |
| 206 | # ensure that the return values are 1 or 0, not just true or |
| 207 | # false (see SF bug #485794). Specific additional tests may |
| 208 | # be appropriate; this tests a single "cycle". |
| 209 | self.assertEqual(calendar.isleap(2000), 1) |
| 210 | self.assertEqual(calendar.isleap(2001), 0) |
| 211 | self.assertEqual(calendar.isleap(2002), 0) |
| 212 | self.assertEqual(calendar.isleap(2003), 0) |
| 213 | |
| 214 | def test_setfirstweekday(self): |
| 215 | self.assertRaises(ValueError, calendar.setfirstweekday, 'flabber') |
| 216 | self.assertRaises(ValueError, calendar.setfirstweekday, -1) |
| 217 | self.assertRaises(ValueError, calendar.setfirstweekday, 200) |
| 218 | orig = calendar.firstweekday() |
| 219 | calendar.setfirstweekday(calendar.SUNDAY) |
| 220 | self.assertEqual(calendar.firstweekday(), calendar.SUNDAY) |
| 221 | calendar.setfirstweekday(calendar.MONDAY) |
| 222 | self.assertEqual(calendar.firstweekday(), calendar.MONDAY) |
| 223 | calendar.setfirstweekday(orig) |
| 224 | |
Skip Montanaro | 693c6c4 | 2002-03-15 03:57:04 +0000 | [diff] [blame] | 225 | def test_enumerateweekdays(self): |
| 226 | self.assertRaises(IndexError, calendar.day_abbr.__getitem__, -10) |
| 227 | self.assertRaises(IndexError, calendar.day_name.__getitem__, 10) |
| 228 | self.assertEqual(len([d for d in calendar.day_abbr]), 7) |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 229 | |
Tim Peters | 0c2c8e7 | 2002-03-23 03:26:53 +0000 | [diff] [blame] | 230 | def test_days(self): |
| 231 | for attr in "day_name", "day_abbr": |
| 232 | value = getattr(calendar, attr) |
| 233 | self.assertEqual(len(value), 7) |
| 234 | self.assertEqual(len(value[:]), 7) |
| 235 | # ensure they're all unique |
Tim Peters | bbc0d44 | 2004-11-13 16:18:32 +0000 | [diff] [blame] | 236 | self.assertEqual(len(set(value)), 7) |
| 237 | # verify it "acts like a sequence" in two forms of iteration |
| 238 | self.assertEqual(value[::-1], list(reversed(value))) |
Tim Peters | 0c2c8e7 | 2002-03-23 03:26:53 +0000 | [diff] [blame] | 239 | |
| 240 | def test_months(self): |
| 241 | for attr in "month_name", "month_abbr": |
| 242 | value = getattr(calendar, attr) |
| 243 | self.assertEqual(len(value), 13) |
| 244 | self.assertEqual(len(value[:]), 13) |
| 245 | self.assertEqual(value[0], "") |
| 246 | # ensure they're all unique |
Tim Peters | bbc0d44 | 2004-11-13 16:18:32 +0000 | [diff] [blame] | 247 | self.assertEqual(len(set(value)), 13) |
| 248 | # verify it "acts like a sequence" in two forms of iteration |
| 249 | self.assertEqual(value[::-1], list(reversed(value))) |
Tim Peters | 0c2c8e7 | 2002-03-23 03:26:53 +0000 | [diff] [blame] | 250 | |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 251 | |
| 252 | class MonthCalendarTestCase(unittest.TestCase): |
| 253 | def setUp(self): |
| 254 | self.oldfirstweekday = calendar.firstweekday() |
| 255 | calendar.setfirstweekday(self.firstweekday) |
| 256 | |
| 257 | def tearDown(self): |
| 258 | calendar.setfirstweekday(self.oldfirstweekday) |
| 259 | |
| 260 | def check_weeks(self, year, month, weeks): |
| 261 | cal = calendar.monthcalendar(year, month) |
| 262 | self.assertEqual(len(cal), len(weeks)) |
| 263 | for i in xrange(len(weeks)): |
| 264 | self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) |
| 265 | |
| 266 | |
| 267 | class MondayTestCase(MonthCalendarTestCase): |
| 268 | firstweekday = calendar.MONDAY |
| 269 | |
| 270 | def test_february(self): |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 271 | # 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] | 272 | self.check_weeks(1999, 2, (7, 7, 7, 7)) |
| 273 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 274 | # 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] | 275 | self.check_weeks(2005, 2, (6, 7, 7, 7, 1)) |
| 276 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 277 | # 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] | 278 | self.check_weeks(1987, 2, (1, 7, 7, 7, 6)) |
| 279 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 280 | # 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] | 281 | self.check_weeks(1988, 2, (7, 7, 7, 7, 1)) |
| 282 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 283 | # 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] | 284 | self.check_weeks(1972, 2, (6, 7, 7, 7, 2)) |
| 285 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 286 | # 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] | 287 | self.check_weeks(2004, 2, (1, 7, 7, 7, 7)) |
| 288 | |
| 289 | def test_april(self): |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 290 | # 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] | 291 | self.check_weeks(1935, 4, (7, 7, 7, 7, 2)) |
| 292 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 293 | # 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] | 294 | self.check_weeks(1975, 4, (6, 7, 7, 7, 3)) |
| 295 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 296 | # 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] | 297 | self.check_weeks(1945, 4, (1, 7, 7, 7, 7, 1)) |
| 298 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 299 | # 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] | 300 | self.check_weeks(1995, 4, (2, 7, 7, 7, 7)) |
| 301 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 302 | # 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] | 303 | self.check_weeks(1994, 4, (3, 7, 7, 7, 6)) |
| 304 | |
| 305 | def test_december(self): |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 306 | # 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] | 307 | self.check_weeks(1980, 12, (7, 7, 7, 7, 3)) |
| 308 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 309 | # 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] | 310 | self.check_weeks(1987, 12, (6, 7, 7, 7, 4)) |
| 311 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 312 | # 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] | 313 | self.check_weeks(1968, 12, (1, 7, 7, 7, 7, 2)) |
| 314 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 315 | # 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] | 316 | self.check_weeks(1988, 12, (4, 7, 7, 7, 6)) |
| 317 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 318 | # 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] | 319 | self.check_weeks(2017, 12, (3, 7, 7, 7, 7)) |
| 320 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 321 | # 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] | 322 | self.check_weeks(2068, 12, (2, 7, 7, 7, 7, 1)) |
| 323 | |
| 324 | |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 325 | class SundayTestCase(MonthCalendarTestCase): |
| 326 | firstweekday = calendar.SUNDAY |
| 327 | |
| 328 | def test_february(self): |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 329 | # 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] | 330 | self.check_weeks(2009, 2, (7, 7, 7, 7)) |
| 331 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 332 | # 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] | 333 | self.check_weeks(1999, 2, (6, 7, 7, 7, 1)) |
| 334 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 335 | # 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] | 336 | self.check_weeks(1997, 2, (1, 7, 7, 7, 6)) |
| 337 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 338 | # 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] | 339 | self.check_weeks(2004, 2, (7, 7, 7, 7, 1)) |
| 340 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 341 | # 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] | 342 | self.check_weeks(1960, 2, (6, 7, 7, 7, 2)) |
| 343 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 344 | # 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] | 345 | self.check_weeks(1964, 2, (1, 7, 7, 7, 7)) |
| 346 | |
| 347 | def test_april(self): |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 348 | # 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] | 349 | self.check_weeks(1923, 4, (7, 7, 7, 7, 2)) |
| 350 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 351 | # 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] | 352 | self.check_weeks(1918, 4, (6, 7, 7, 7, 3)) |
| 353 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 354 | # 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] | 355 | self.check_weeks(1950, 4, (1, 7, 7, 7, 7, 1)) |
| 356 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 357 | # 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] | 358 | self.check_weeks(1960, 4, (2, 7, 7, 7, 7)) |
| 359 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 360 | # 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] | 361 | self.check_weeks(1909, 4, (3, 7, 7, 7, 6)) |
| 362 | |
| 363 | def test_december(self): |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 364 | # 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] | 365 | self.check_weeks(2080, 12, (7, 7, 7, 7, 3)) |
| 366 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 367 | # 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] | 368 | self.check_weeks(1941, 12, (6, 7, 7, 7, 4)) |
| 369 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 370 | # 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] | 371 | self.check_weeks(1923, 12, (1, 7, 7, 7, 7, 2)) |
| 372 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 373 | # 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] | 374 | self.check_weeks(1948, 12, (4, 7, 7, 7, 6)) |
| 375 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 376 | # 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] | 377 | self.check_weeks(1927, 12, (3, 7, 7, 7, 7)) |
| 378 | |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 379 | # 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] | 380 | self.check_weeks(1995, 12, (2, 7, 7, 7, 7, 1)) |
| 381 | |
| 382 | |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 383 | def test_main(): |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 384 | test_support.run_unittest( |
Walter Dörwald | 58917a6 | 2006-03-31 15:26:22 +0000 | [diff] [blame] | 385 | OutputTestCase, |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 386 | CalendarTestCase, |
Walter Dörwald | 302fa6d | 2004-07-08 17:14:17 +0000 | [diff] [blame] | 387 | MondayTestCase, |
| 388 | SundayTestCase |
Walter Dörwald | 59ad456 | 2004-07-02 19:00:09 +0000 | [diff] [blame] | 389 | ) |
| 390 | |
Fred Drake | d077ca1 | 2001-12-12 05:38:08 +0000 | [diff] [blame] | 391 | |
| 392 | if __name__ == "__main__": |
| 393 | test_main() |