summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_calendar.py
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2004-07-02 19:00:09 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2004-07-02 19:00:09 (GMT)
commit59ad45689ba71f960201e681f98ed0afbac87d46 (patch)
treebaaf61dc03ce0d6fc474d56ee4e1f2b90f32f623 /Lib/test/test_calendar.py
parent0a4dd390bf653128de8bc2e99da64967c8cdf86e (diff)
downloadcpython-59ad45689ba71f960201e681f98ed0afbac87d46.zip
cpython-59ad45689ba71f960201e681f98ed0afbac87d46.tar.gz
cpython-59ad45689ba71f960201e681f98ed0afbac87d46.tar.bz2
Add tests that check the result of calendar.monthcalendar() for a set
of corner cases.
Diffstat (limited to 'Lib/test/test_calendar.py')
-rw-r--r--Lib/test/test_calendar.py80
1 files changed, 79 insertions, 1 deletions
diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py
index f8c6d56..58f27f1 100644
--- a/Lib/test/test_calendar.py
+++ b/Lib/test/test_calendar.py
@@ -54,8 +54,86 @@ class CalendarTestCase(unittest.TestCase):
d[v] = 1
self.assertEqual(len(d), 13)
+
+class MonthCalendarTestCase(unittest.TestCase):
+ def setUp(self):
+ self.oldfirstweekday = calendar.firstweekday()
+ calendar.setfirstweekday(self.firstweekday)
+
+ def tearDown(self):
+ calendar.setfirstweekday(self.oldfirstweekday)
+
+ def check_weeks(self, year, month, weeks):
+ cal = calendar.monthcalendar(year, month)
+ self.assertEqual(len(cal), len(weeks))
+ for i in xrange(len(weeks)):
+ self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
+
+
+class MondayTestCase(MonthCalendarTestCase):
+ firstweekday = calendar.MONDAY
+
+ def test_february(self):
+ # A 28-day february starting of monday (7+7+7+7 days)
+ self.check_weeks(1999, 2, (7, 7, 7, 7))
+
+ # A 28-day february starting of tuesday (6+7+7+7+1 days)
+ self.check_weeks(2005, 2, (6, 7, 7, 7, 1))
+
+ # A 28-day february starting of sunday (1+7+7+7+6 days)
+ self.check_weeks(1987, 2, (1, 7, 7, 7, 6))
+
+ # A 29-day february starting of monday (7+7+7+7+1 days)
+ self.check_weeks(1988, 2, (7, 7, 7, 7, 1))
+
+ # A 29-day february starting of tuesday (6+7+7+7+2 days)
+ self.check_weeks(1972, 2, (6, 7, 7, 7, 2))
+
+ # A 29-day february starting of sunday (1+7+7+7+7 days)
+ self.check_weeks(2004, 2, (1, 7, 7, 7, 7))
+
+ def test_april(self):
+ # A 30-day april starting of monday (7+7+7+7+2 days)
+ self.check_weeks(1935, 4, (7, 7, 7, 7, 2))
+
+ # A 30-day april starting of tuesday (6+7+7+7+3 days)
+ self.check_weeks(1975, 4, (6, 7, 7, 7, 3))
+
+ # A 30-day april starting of sunday (1+7+7+7+7+1 days)
+ self.check_weeks(1945, 4, (1, 7, 7, 7, 7, 1))
+
+ # A 30-day april starting of saturday (2+7+7+7+7 days)
+ self.check_weeks(1995, 4, (2, 7, 7, 7, 7))
+
+ # A 30-day april starting of friday (3+7+7+7+6 days)
+ self.check_weeks(1994, 4, (3, 7, 7, 7, 6))
+
+ def test_december(self):
+ # A 31-day december starting of monday (7+7+7+7+3 days)
+ self.check_weeks(1980, 12, (7, 7, 7, 7, 3))
+
+ # A 31-day december starting of tuesday (6+7+7+7+4 days)
+ self.check_weeks(1987, 12, (6, 7, 7, 7, 4))
+
+ # A 31-day december starting of sunday (1+7+7+7+7+2 days)
+ self.check_weeks(1968, 12, (1, 7, 7, 7, 7, 2))
+
+ # A 31-day december starting of thursday (4+7+7+7+6 days)
+ self.check_weeks(1988, 12, (4, 7, 7, 7, 6))
+
+ # A 31-day december starting of friday (3+7+7+7+7 days)
+ self.check_weeks(2017, 12, (3, 7, 7, 7, 7))
+
+ # A 31-day december starting of saturday (2+7+7+7+7+1 days)
+ self.check_weeks(2068, 12, (2, 7, 7, 7, 7, 1))
+
+
def test_main():
- test_support.run_unittest(CalendarTestCase)
+ test_support.run_unittest(
+ CalendarTestCase,
+ MondayTestCase
+ )
+
if __name__ == "__main__":
test_main()