2001-12-12 01:38:08 -04:00
|
|
|
import calendar
|
|
|
|
import unittest
|
|
|
|
|
2003-05-01 14:45:56 -03:00
|
|
|
from test import test_support
|
2001-12-12 01:38:08 -04:00
|
|
|
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
result_2004 = """
|
|
|
|
2004
|
|
|
|
|
|
|
|
January February March
|
|
|
|
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
|
|
|
|
1 2 3 4 1 1 2 3 4 5 6 7
|
|
|
|
5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14
|
|
|
|
12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21
|
|
|
|
19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28
|
|
|
|
26 27 28 29 30 31 23 24 25 26 27 28 29 29 30 31
|
|
|
|
|
|
|
|
April May June
|
|
|
|
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
|
|
|
|
1 2 3 4 1 2 1 2 3 4 5 6
|
|
|
|
5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
|
|
|
|
12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
|
|
|
|
19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
|
|
|
|
26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
|
|
|
|
31
|
|
|
|
|
|
|
|
July August September
|
|
|
|
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
|
|
|
|
1 2 3 4 1 1 2 3 4 5
|
|
|
|
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
|
|
|
|
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
|
|
|
|
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
|
|
|
|
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
|
|
|
|
30 31
|
|
|
|
|
|
|
|
October November December
|
|
|
|
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
|
|
|
|
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
|
|
|
|
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
|
|
|
|
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
|
|
|
|
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
|
|
|
|
25 26 27 28 29 30 31 29 30 27 28 29 30 31
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class OutputTestCase(unittest.TestCase):
|
|
|
|
def normalize_calendar(self, s):
|
|
|
|
def neitherspacenordigit(c):
|
|
|
|
return not c.isspace() and not c.isdigit()
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
for line in s.splitlines(False):
|
|
|
|
# Drop texts, as they are locale dependent
|
|
|
|
if line and not filter(neitherspacenordigit, line):
|
|
|
|
lines.append(line)
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def test_output(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self.normalize_calendar(calendar.calendar(2004)),
|
|
|
|
self.normalize_calendar(result_2004)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2001-12-12 01:38:08 -04:00
|
|
|
class CalendarTestCase(unittest.TestCase):
|
|
|
|
def test_isleap(self):
|
|
|
|
# Make sure that the return is right for a few years, and
|
|
|
|
# ensure that the return values are 1 or 0, not just true or
|
|
|
|
# false (see SF bug #485794). Specific additional tests may
|
|
|
|
# be appropriate; this tests a single "cycle".
|
|
|
|
self.assertEqual(calendar.isleap(2000), 1)
|
|
|
|
self.assertEqual(calendar.isleap(2001), 0)
|
|
|
|
self.assertEqual(calendar.isleap(2002), 0)
|
|
|
|
self.assertEqual(calendar.isleap(2003), 0)
|
|
|
|
|
|
|
|
def test_setfirstweekday(self):
|
|
|
|
self.assertRaises(ValueError, calendar.setfirstweekday, 'flabber')
|
|
|
|
self.assertRaises(ValueError, calendar.setfirstweekday, -1)
|
|
|
|
self.assertRaises(ValueError, calendar.setfirstweekday, 200)
|
|
|
|
orig = calendar.firstweekday()
|
|
|
|
calendar.setfirstweekday(calendar.SUNDAY)
|
|
|
|
self.assertEqual(calendar.firstweekday(), calendar.SUNDAY)
|
|
|
|
calendar.setfirstweekday(calendar.MONDAY)
|
|
|
|
self.assertEqual(calendar.firstweekday(), calendar.MONDAY)
|
|
|
|
calendar.setfirstweekday(orig)
|
|
|
|
|
2002-03-14 23:57:04 -04:00
|
|
|
def test_enumerateweekdays(self):
|
|
|
|
self.assertRaises(IndexError, calendar.day_abbr.__getitem__, -10)
|
|
|
|
self.assertRaises(IndexError, calendar.day_name.__getitem__, 10)
|
|
|
|
self.assertEqual(len([d for d in calendar.day_abbr]), 7)
|
2001-12-12 01:38:08 -04:00
|
|
|
|
2002-03-22 23:26:53 -04:00
|
|
|
def test_days(self):
|
|
|
|
for attr in "day_name", "day_abbr":
|
|
|
|
value = getattr(calendar, attr)
|
|
|
|
self.assertEqual(len(value), 7)
|
|
|
|
self.assertEqual(len(value[:]), 7)
|
|
|
|
# ensure they're all unique
|
2004-11-13 12:18:32 -04:00
|
|
|
self.assertEqual(len(set(value)), 7)
|
|
|
|
# verify it "acts like a sequence" in two forms of iteration
|
|
|
|
self.assertEqual(value[::-1], list(reversed(value)))
|
2002-03-22 23:26:53 -04:00
|
|
|
|
|
|
|
def test_months(self):
|
|
|
|
for attr in "month_name", "month_abbr":
|
|
|
|
value = getattr(calendar, attr)
|
|
|
|
self.assertEqual(len(value), 13)
|
|
|
|
self.assertEqual(len(value[:]), 13)
|
|
|
|
self.assertEqual(value[0], "")
|
|
|
|
# ensure they're all unique
|
2004-11-13 12:18:32 -04:00
|
|
|
self.assertEqual(len(set(value)), 13)
|
|
|
|
# verify it "acts like a sequence" in two forms of iteration
|
|
|
|
self.assertEqual(value[::-1], list(reversed(value)))
|
2002-03-22 23:26:53 -04:00
|
|
|
|
2004-07-02 16:00:09 -03:00
|
|
|
|
|
|
|
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):
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 28-day february starting on monday (7+7+7+7 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1999, 2, (7, 7, 7, 7))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 28-day february starting on tuesday (6+7+7+7+1 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(2005, 2, (6, 7, 7, 7, 1))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 28-day february starting on sunday (1+7+7+7+6 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1987, 2, (1, 7, 7, 7, 6))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 29-day february starting on monday (7+7+7+7+1 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1988, 2, (7, 7, 7, 7, 1))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 29-day february starting on tuesday (6+7+7+7+2 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1972, 2, (6, 7, 7, 7, 2))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 29-day february starting on sunday (1+7+7+7+7 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(2004, 2, (1, 7, 7, 7, 7))
|
|
|
|
|
|
|
|
def test_april(self):
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on monday (7+7+7+7+2 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1935, 4, (7, 7, 7, 7, 2))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on tuesday (6+7+7+7+3 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1975, 4, (6, 7, 7, 7, 3))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on sunday (1+7+7+7+7+1 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1945, 4, (1, 7, 7, 7, 7, 1))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on saturday (2+7+7+7+7 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1995, 4, (2, 7, 7, 7, 7))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on friday (3+7+7+7+6 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1994, 4, (3, 7, 7, 7, 6))
|
|
|
|
|
|
|
|
def test_december(self):
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on monday (7+7+7+7+3 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1980, 12, (7, 7, 7, 7, 3))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on tuesday (6+7+7+7+4 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1987, 12, (6, 7, 7, 7, 4))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on sunday (1+7+7+7+7+2 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1968, 12, (1, 7, 7, 7, 7, 2))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on thursday (4+7+7+7+6 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(1988, 12, (4, 7, 7, 7, 6))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on friday (3+7+7+7+7 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(2017, 12, (3, 7, 7, 7, 7))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on saturday (2+7+7+7+7+1 days)
|
2004-07-02 16:00:09 -03:00
|
|
|
self.check_weeks(2068, 12, (2, 7, 7, 7, 7, 1))
|
|
|
|
|
|
|
|
|
2004-07-08 14:14:17 -03:00
|
|
|
class SundayTestCase(MonthCalendarTestCase):
|
|
|
|
firstweekday = calendar.SUNDAY
|
|
|
|
|
|
|
|
def test_february(self):
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 28-day february starting on sunday (7+7+7+7 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(2009, 2, (7, 7, 7, 7))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 28-day february starting on monday (6+7+7+7+1 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1999, 2, (6, 7, 7, 7, 1))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 28-day february starting on saturday (1+7+7+7+6 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1997, 2, (1, 7, 7, 7, 6))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 29-day february starting on sunday (7+7+7+7+1 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(2004, 2, (7, 7, 7, 7, 1))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 29-day february starting on monday (6+7+7+7+2 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1960, 2, (6, 7, 7, 7, 2))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 29-day february starting on saturday (1+7+7+7+7 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1964, 2, (1, 7, 7, 7, 7))
|
|
|
|
|
|
|
|
def test_april(self):
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on sunday (7+7+7+7+2 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1923, 4, (7, 7, 7, 7, 2))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on monday (6+7+7+7+3 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1918, 4, (6, 7, 7, 7, 3))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on saturday (1+7+7+7+7+1 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1950, 4, (1, 7, 7, 7, 7, 1))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on friday (2+7+7+7+7 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1960, 4, (2, 7, 7, 7, 7))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 30-day april starting on thursday (3+7+7+7+6 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1909, 4, (3, 7, 7, 7, 6))
|
|
|
|
|
|
|
|
def test_december(self):
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on sunday (7+7+7+7+3 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(2080, 12, (7, 7, 7, 7, 3))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on monday (6+7+7+7+4 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1941, 12, (6, 7, 7, 7, 4))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on saturday (1+7+7+7+7+2 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1923, 12, (1, 7, 7, 7, 7, 2))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on wednesday (4+7+7+7+6 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1948, 12, (4, 7, 7, 7, 6))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on thursday (3+7+7+7+7 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1927, 12, (3, 7, 7, 7, 7))
|
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
# A 31-day december starting on friday (2+7+7+7+7+1 days)
|
2004-07-08 14:14:17 -03:00
|
|
|
self.check_weeks(1995, 12, (2, 7, 7, 7, 7, 1))
|
|
|
|
|
|
|
|
|
2001-12-12 01:38:08 -04:00
|
|
|
def test_main():
|
2004-07-02 16:00:09 -03:00
|
|
|
test_support.run_unittest(
|
2006-03-31 11:26:22 -04:00
|
|
|
OutputTestCase,
|
2004-07-02 16:00:09 -03:00
|
|
|
CalendarTestCase,
|
2004-07-08 14:14:17 -03:00
|
|
|
MondayTestCase,
|
|
|
|
SundayTestCase
|
2004-07-02 16:00:09 -03:00
|
|
|
)
|
|
|
|
|
2001-12-12 01:38:08 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|