PyXR

c:\python24\lib \ test \ test_calendar.py



0001 import calendar
0002 import unittest
0003 
0004 from test import test_support
0005 
0006 
0007 class CalendarTestCase(unittest.TestCase):
0008     def test_isleap(self):
0009         # Make sure that the return is right for a few years, and
0010         # ensure that the return values are 1 or 0, not just true or
0011         # false (see SF bug #485794).  Specific additional tests may
0012         # be appropriate; this tests a single "cycle".
0013         self.assertEqual(calendar.isleap(2000), 1)
0014         self.assertEqual(calendar.isleap(2001), 0)
0015         self.assertEqual(calendar.isleap(2002), 0)
0016         self.assertEqual(calendar.isleap(2003), 0)
0017 
0018     def test_setfirstweekday(self):
0019         self.assertRaises(ValueError, calendar.setfirstweekday, 'flabber')
0020         self.assertRaises(ValueError, calendar.setfirstweekday, -1)
0021         self.assertRaises(ValueError, calendar.setfirstweekday, 200)
0022         orig = calendar.firstweekday()
0023         calendar.setfirstweekday(calendar.SUNDAY)
0024         self.assertEqual(calendar.firstweekday(), calendar.SUNDAY)
0025         calendar.setfirstweekday(calendar.MONDAY)
0026         self.assertEqual(calendar.firstweekday(), calendar.MONDAY)
0027         calendar.setfirstweekday(orig)
0028 
0029     def test_enumerateweekdays(self):
0030         self.assertRaises(IndexError, calendar.day_abbr.__getitem__, -10)
0031         self.assertRaises(IndexError, calendar.day_name.__getitem__, 10)
0032         self.assertEqual(len([d for d in calendar.day_abbr]), 7)
0033 
0034     def test_days(self):
0035         for attr in "day_name", "day_abbr":
0036             value = getattr(calendar, attr)
0037             self.assertEqual(len(value), 7)
0038             self.assertEqual(len(value[:]), 7)
0039             # ensure they're all unique
0040             d = {}
0041             for v in value:
0042                 d[v] = 1
0043             self.assertEqual(len(d), 7)
0044 
0045     def test_months(self):
0046         for attr in "month_name", "month_abbr":
0047             value = getattr(calendar, attr)
0048             self.assertEqual(len(value), 13)
0049             self.assertEqual(len(value[:]), 13)
0050             self.assertEqual(value[0], "")
0051             # ensure they're all unique
0052             d = {}
0053             for v in value:
0054                 d[v] = 1
0055             self.assertEqual(len(d), 13)
0056 
0057 
0058 class MonthCalendarTestCase(unittest.TestCase):
0059     def setUp(self):
0060         self.oldfirstweekday = calendar.firstweekday()
0061         calendar.setfirstweekday(self.firstweekday)
0062 
0063     def tearDown(self):
0064         calendar.setfirstweekday(self.oldfirstweekday)
0065 
0066     def check_weeks(self, year, month, weeks):
0067         cal = calendar.monthcalendar(year, month)
0068         self.assertEqual(len(cal), len(weeks))
0069         for i in xrange(len(weeks)):
0070             self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
0071 
0072 
0073 class MondayTestCase(MonthCalendarTestCase):
0074     firstweekday = calendar.MONDAY
0075 
0076     def test_february(self):
0077         # A 28-day february starting of monday (7+7+7+7 days)
0078         self.check_weeks(1999, 2, (7, 7, 7, 7))
0079 
0080         # A 28-day february starting of tuesday (6+7+7+7+1 days)
0081         self.check_weeks(2005, 2, (6, 7, 7, 7, 1))
0082 
0083         # A 28-day february starting of sunday (1+7+7+7+6 days)
0084         self.check_weeks(1987, 2, (1, 7, 7, 7, 6))
0085 
0086         # A 29-day february starting of monday (7+7+7+7+1 days)
0087         self.check_weeks(1988, 2, (7, 7, 7, 7, 1))
0088 
0089         # A 29-day february starting of tuesday (6+7+7+7+2 days)
0090         self.check_weeks(1972, 2, (6, 7, 7, 7, 2))
0091 
0092         # A 29-day february starting of sunday (1+7+7+7+7 days)
0093         self.check_weeks(2004, 2, (1, 7, 7, 7, 7))
0094 
0095     def test_april(self):
0096         # A 30-day april starting of monday (7+7+7+7+2 days)
0097         self.check_weeks(1935, 4, (7, 7, 7, 7, 2))
0098 
0099         # A 30-day april starting of tuesday (6+7+7+7+3 days)
0100         self.check_weeks(1975, 4, (6, 7, 7, 7, 3))
0101 
0102         # A 30-day april starting of sunday (1+7+7+7+7+1 days)
0103         self.check_weeks(1945, 4, (1, 7, 7, 7, 7, 1))
0104 
0105         # A 30-day april starting of saturday (2+7+7+7+7 days)
0106         self.check_weeks(1995, 4, (2, 7, 7, 7, 7))
0107 
0108         # A 30-day april starting of friday (3+7+7+7+6 days)
0109         self.check_weeks(1994, 4, (3, 7, 7, 7, 6))
0110 
0111     def test_december(self):
0112         # A 31-day december starting of monday (7+7+7+7+3 days)
0113         self.check_weeks(1980, 12, (7, 7, 7, 7, 3))
0114 
0115         # A 31-day december starting of tuesday (6+7+7+7+4 days)
0116         self.check_weeks(1987, 12, (6, 7, 7, 7, 4))
0117 
0118         # A 31-day december starting of sunday (1+7+7+7+7+2 days)
0119         self.check_weeks(1968, 12, (1, 7, 7, 7, 7, 2))
0120 
0121         # A 31-day december starting of thursday (4+7+7+7+6 days)
0122         self.check_weeks(1988, 12, (4, 7, 7, 7, 6))
0123 
0124         # A 31-day december starting of friday (3+7+7+7+7 days)
0125         self.check_weeks(2017, 12, (3, 7, 7, 7, 7))
0126 
0127         # A 31-day december starting of saturday (2+7+7+7+7+1 days)
0128         self.check_weeks(2068, 12, (2, 7, 7, 7, 7, 1))
0129 
0130 
0131 class SundayTestCase(MonthCalendarTestCase):
0132     firstweekday = calendar.SUNDAY
0133 
0134     def test_february(self):
0135         # A 28-day february starting of sunday (7+7+7+7 days)
0136         self.check_weeks(2009, 2, (7, 7, 7, 7))
0137 
0138         # A 28-day february starting of monday (6+7+7+7+1 days)
0139         self.check_weeks(1999, 2, (6, 7, 7, 7, 1))
0140 
0141         # A 28-day february starting of saturday (1+7+7+7+6 days)
0142         self.check_weeks(1997, 2, (1, 7, 7, 7, 6))
0143 
0144         # A 29-day february starting of sunday (7+7+7+7+1 days)
0145         self.check_weeks(2004, 2, (7, 7, 7, 7, 1))
0146 
0147         # A 29-day february starting of monday (6+7+7+7+2 days)
0148         self.check_weeks(1960, 2, (6, 7, 7, 7, 2))
0149 
0150         # A 29-day february starting of saturday (1+7+7+7+7 days)
0151         self.check_weeks(1964, 2, (1, 7, 7, 7, 7))
0152 
0153     def test_april(self):
0154         # A 30-day april starting of sunday (7+7+7+7+2 days)
0155         self.check_weeks(1923, 4, (7, 7, 7, 7, 2))
0156 
0157         # A 30-day april starting of monday (6+7+7+7+3 days)
0158         self.check_weeks(1918, 4, (6, 7, 7, 7, 3))
0159 
0160         # A 30-day april starting of saturday (1+7+7+7+7+1 days)
0161         self.check_weeks(1950, 4, (1, 7, 7, 7, 7, 1))
0162 
0163         # A 30-day april starting of friday (2+7+7+7+7 days)
0164         self.check_weeks(1960, 4, (2, 7, 7, 7, 7))
0165 
0166         # A 30-day april starting of thursday (3+7+7+7+6 days)
0167         self.check_weeks(1909, 4, (3, 7, 7, 7, 6))
0168 
0169     def test_december(self):
0170         # A 31-day december starting of sunday (7+7+7+7+3 days)
0171         self.check_weeks(2080, 12, (7, 7, 7, 7, 3))
0172 
0173         # A 31-day december starting of monday (6+7+7+7+4 days)
0174         self.check_weeks(1941, 12, (6, 7, 7, 7, 4))
0175 
0176         # A 31-day december starting of saturday (1+7+7+7+7+2 days)
0177         self.check_weeks(1923, 12, (1, 7, 7, 7, 7, 2))
0178 
0179         # A 31-day december starting of wednesday (4+7+7+7+6 days)
0180         self.check_weeks(1948, 12, (4, 7, 7, 7, 6))
0181 
0182         # A 31-day december starting of thursday (3+7+7+7+7 days)
0183         self.check_weeks(1927, 12, (3, 7, 7, 7, 7))
0184 
0185         # A 31-day december starting of friday (2+7+7+7+7+1 days)
0186         self.check_weeks(1995, 12, (2, 7, 7, 7, 7, 1))
0187 
0188 
0189 def test_main():
0190     test_support.run_unittest(
0191         CalendarTestCase,
0192         MondayTestCase,
0193         SundayTestCase
0194     )
0195 
0196 
0197 if __name__ == "__main__":
0198     test_main()
0199 

Generated by PyXR 0.9.4
SourceForge.net Logo