Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | import calendar |
2 | import unittest | |
3 | ||
4 | from test import test_support | |
5 | ||
6 | ||
7 | class CalendarTestCase(unittest.TestCase): | |
8 | def test_isleap(self): | |
9 | # Make sure that the return is right for a few years, and | |
10 | # ensure that the return values are 1 or 0, not just true or | |
11 | # false (see SF bug #485794). Specific additional tests may | |
12 | # be appropriate; this tests a single "cycle". | |
13 | self.assertEqual(calendar.isleap(2000), 1) | |
14 | self.assertEqual(calendar.isleap(2001), 0) | |
15 | self.assertEqual(calendar.isleap(2002), 0) | |
16 | self.assertEqual(calendar.isleap(2003), 0) | |
17 | ||
18 | def test_setfirstweekday(self): | |
19 | self.assertRaises(ValueError, calendar.setfirstweekday, 'flabber') | |
20 | self.assertRaises(ValueError, calendar.setfirstweekday, -1) | |
21 | self.assertRaises(ValueError, calendar.setfirstweekday, 200) | |
22 | orig = calendar.firstweekday() | |
23 | calendar.setfirstweekday(calendar.SUNDAY) | |
24 | self.assertEqual(calendar.firstweekday(), calendar.SUNDAY) | |
25 | calendar.setfirstweekday(calendar.MONDAY) | |
26 | self.assertEqual(calendar.firstweekday(), calendar.MONDAY) | |
27 | calendar.setfirstweekday(orig) | |
28 | ||
29 | def test_enumerateweekdays(self): | |
30 | self.assertRaises(IndexError, calendar.day_abbr.__getitem__, -10) | |
31 | self.assertRaises(IndexError, calendar.day_name.__getitem__, 10) | |
32 | self.assertEqual(len([d for d in calendar.day_abbr]), 7) | |
33 | ||
34 | def test_days(self): | |
35 | for attr in "day_name", "day_abbr": | |
36 | value = getattr(calendar, attr) | |
37 | self.assertEqual(len(value), 7) | |
38 | self.assertEqual(len(value[:]), 7) | |
39 | # ensure they're all unique | |
40 | self.assertEqual(len(set(value)), 7) | |
41 | # verify it "acts like a sequence" in two forms of iteration | |
42 | self.assertEqual(value[::-1], list(reversed(value))) | |
43 | ||
44 | def test_months(self): | |
45 | for attr in "month_name", "month_abbr": | |
46 | value = getattr(calendar, attr) | |
47 | self.assertEqual(len(value), 13) | |
48 | self.assertEqual(len(value[:]), 13) | |
49 | self.assertEqual(value[0], "") | |
50 | # ensure they're all unique | |
51 | self.assertEqual(len(set(value)), 13) | |
52 | # verify it "acts like a sequence" in two forms of iteration | |
53 | self.assertEqual(value[::-1], list(reversed(value))) | |
54 | ||
55 | ||
56 | class MonthCalendarTestCase(unittest.TestCase): | |
57 | def setUp(self): | |
58 | self.oldfirstweekday = calendar.firstweekday() | |
59 | calendar.setfirstweekday(self.firstweekday) | |
60 | ||
61 | def tearDown(self): | |
62 | calendar.setfirstweekday(self.oldfirstweekday) | |
63 | ||
64 | def check_weeks(self, year, month, weeks): | |
65 | cal = calendar.monthcalendar(year, month) | |
66 | self.assertEqual(len(cal), len(weeks)) | |
67 | for i in xrange(len(weeks)): | |
68 | self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) | |
69 | ||
70 | ||
71 | class MondayTestCase(MonthCalendarTestCase): | |
72 | firstweekday = calendar.MONDAY | |
73 | ||
74 | def test_february(self): | |
75 | # A 28-day february starting of monday (7+7+7+7 days) | |
76 | self.check_weeks(1999, 2, (7, 7, 7, 7)) | |
77 | ||
78 | # A 28-day february starting of tuesday (6+7+7+7+1 days) | |
79 | self.check_weeks(2005, 2, (6, 7, 7, 7, 1)) | |
80 | ||
81 | # A 28-day february starting of sunday (1+7+7+7+6 days) | |
82 | self.check_weeks(1987, 2, (1, 7, 7, 7, 6)) | |
83 | ||
84 | # A 29-day february starting of monday (7+7+7+7+1 days) | |
85 | self.check_weeks(1988, 2, (7, 7, 7, 7, 1)) | |
86 | ||
87 | # A 29-day february starting of tuesday (6+7+7+7+2 days) | |
88 | self.check_weeks(1972, 2, (6, 7, 7, 7, 2)) | |
89 | ||
90 | # A 29-day february starting of sunday (1+7+7+7+7 days) | |
91 | self.check_weeks(2004, 2, (1, 7, 7, 7, 7)) | |
92 | ||
93 | def test_april(self): | |
94 | # A 30-day april starting of monday (7+7+7+7+2 days) | |
95 | self.check_weeks(1935, 4, (7, 7, 7, 7, 2)) | |
96 | ||
97 | # A 30-day april starting of tuesday (6+7+7+7+3 days) | |
98 | self.check_weeks(1975, 4, (6, 7, 7, 7, 3)) | |
99 | ||
100 | # A 30-day april starting of sunday (1+7+7+7+7+1 days) | |
101 | self.check_weeks(1945, 4, (1, 7, 7, 7, 7, 1)) | |
102 | ||
103 | # A 30-day april starting of saturday (2+7+7+7+7 days) | |
104 | self.check_weeks(1995, 4, (2, 7, 7, 7, 7)) | |
105 | ||
106 | # A 30-day april starting of friday (3+7+7+7+6 days) | |
107 | self.check_weeks(1994, 4, (3, 7, 7, 7, 6)) | |
108 | ||
109 | def test_december(self): | |
110 | # A 31-day december starting of monday (7+7+7+7+3 days) | |
111 | self.check_weeks(1980, 12, (7, 7, 7, 7, 3)) | |
112 | ||
113 | # A 31-day december starting of tuesday (6+7+7+7+4 days) | |
114 | self.check_weeks(1987, 12, (6, 7, 7, 7, 4)) | |
115 | ||
116 | # A 31-day december starting of sunday (1+7+7+7+7+2 days) | |
117 | self.check_weeks(1968, 12, (1, 7, 7, 7, 7, 2)) | |
118 | ||
119 | # A 31-day december starting of thursday (4+7+7+7+6 days) | |
120 | self.check_weeks(1988, 12, (4, 7, 7, 7, 6)) | |
121 | ||
122 | # A 31-day december starting of friday (3+7+7+7+7 days) | |
123 | self.check_weeks(2017, 12, (3, 7, 7, 7, 7)) | |
124 | ||
125 | # A 31-day december starting of saturday (2+7+7+7+7+1 days) | |
126 | self.check_weeks(2068, 12, (2, 7, 7, 7, 7, 1)) | |
127 | ||
128 | ||
129 | class SundayTestCase(MonthCalendarTestCase): | |
130 | firstweekday = calendar.SUNDAY | |
131 | ||
132 | def test_february(self): | |
133 | # A 28-day february starting of sunday (7+7+7+7 days) | |
134 | self.check_weeks(2009, 2, (7, 7, 7, 7)) | |
135 | ||
136 | # A 28-day february starting of monday (6+7+7+7+1 days) | |
137 | self.check_weeks(1999, 2, (6, 7, 7, 7, 1)) | |
138 | ||
139 | # A 28-day february starting of saturday (1+7+7+7+6 days) | |
140 | self.check_weeks(1997, 2, (1, 7, 7, 7, 6)) | |
141 | ||
142 | # A 29-day february starting of sunday (7+7+7+7+1 days) | |
143 | self.check_weeks(2004, 2, (7, 7, 7, 7, 1)) | |
144 | ||
145 | # A 29-day february starting of monday (6+7+7+7+2 days) | |
146 | self.check_weeks(1960, 2, (6, 7, 7, 7, 2)) | |
147 | ||
148 | # A 29-day february starting of saturday (1+7+7+7+7 days) | |
149 | self.check_weeks(1964, 2, (1, 7, 7, 7, 7)) | |
150 | ||
151 | def test_april(self): | |
152 | # A 30-day april starting of sunday (7+7+7+7+2 days) | |
153 | self.check_weeks(1923, 4, (7, 7, 7, 7, 2)) | |
154 | ||
155 | # A 30-day april starting of monday (6+7+7+7+3 days) | |
156 | self.check_weeks(1918, 4, (6, 7, 7, 7, 3)) | |
157 | ||
158 | # A 30-day april starting of saturday (1+7+7+7+7+1 days) | |
159 | self.check_weeks(1950, 4, (1, 7, 7, 7, 7, 1)) | |
160 | ||
161 | # A 30-day april starting of friday (2+7+7+7+7 days) | |
162 | self.check_weeks(1960, 4, (2, 7, 7, 7, 7)) | |
163 | ||
164 | # A 30-day april starting of thursday (3+7+7+7+6 days) | |
165 | self.check_weeks(1909, 4, (3, 7, 7, 7, 6)) | |
166 | ||
167 | def test_december(self): | |
168 | # A 31-day december starting of sunday (7+7+7+7+3 days) | |
169 | self.check_weeks(2080, 12, (7, 7, 7, 7, 3)) | |
170 | ||
171 | # A 31-day december starting of monday (6+7+7+7+4 days) | |
172 | self.check_weeks(1941, 12, (6, 7, 7, 7, 4)) | |
173 | ||
174 | # A 31-day december starting of saturday (1+7+7+7+7+2 days) | |
175 | self.check_weeks(1923, 12, (1, 7, 7, 7, 7, 2)) | |
176 | ||
177 | # A 31-day december starting of wednesday (4+7+7+7+6 days) | |
178 | self.check_weeks(1948, 12, (4, 7, 7, 7, 6)) | |
179 | ||
180 | # A 31-day december starting of thursday (3+7+7+7+7 days) | |
181 | self.check_weeks(1927, 12, (3, 7, 7, 7, 7)) | |
182 | ||
183 | # A 31-day december starting of friday (2+7+7+7+7+1 days) | |
184 | self.check_weeks(1995, 12, (2, 7, 7, 7, 7, 1)) | |
185 | ||
186 | ||
187 | def test_main(): | |
188 | test_support.run_unittest( | |
189 | CalendarTestCase, | |
190 | MondayTestCase, | |
191 | SundayTestCase | |
192 | ) | |
193 | ||
194 | ||
195 | if __name__ == "__main__": | |
196 | test_main() |