diff options
Diffstat (limited to 'Lib/test/test_calendar.py')
| -rw-r--r-- | Lib/test/test_calendar.py | 223 | 
1 files changed, 213 insertions, 10 deletions
diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py index d3093ac939..e0f63997d5 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -5,6 +5,17 @@ from test import support  from test.script_helper import assert_python_ok  import time  import locale +import sys + +result_2004_01_text = """ +    January 2004 +Mo Tu We Th Fr Sa Su +          1  2  3  4 + 5  6  7  8  9 10 11 +12 13 14 15 16 17 18 +19 20 21 22 23 24 25 +26 27 28 29 30 31 +"""  result_2004_text = """                                    2004 @@ -45,11 +56,11 @@ Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su  """  result_2004_html = """ -<?xml version="1.0" encoding="ascii"?> +<?xml version="1.0" encoding="%(e)s"?>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  <html>  <head> -<meta http-equiv="Content-Type" content="text/html; charset=ascii" /> +<meta http-equiv="Content-Type" content="text/html; charset=%(e)s" />  <link rel="stylesheet" type="text/css" href="calendar.css" />  <title>Calendar for 2004</title>  </head> @@ -169,6 +180,135 @@ result_2004_html = """  </html>  """ +result_2004_days = [ +    [[[0, 0, 0, 1, 2, 3, 4], +      [5, 6, 7, 8, 9, 10, 11], +      [12, 13, 14, 15, 16, 17, 18], +      [19, 20, 21, 22, 23, 24, 25], +      [26, 27, 28, 29, 30, 31, 0]], +     [[0, 0, 0, 0, 0, 0, 1], +      [2, 3, 4, 5, 6, 7, 8], +      [9, 10, 11, 12, 13, 14, 15], +      [16, 17, 18, 19, 20, 21, 22], +      [23, 24, 25, 26, 27, 28, 29]], +     [[1, 2, 3, 4, 5, 6, 7], +      [8, 9, 10, 11, 12, 13, 14], +      [15, 16, 17, 18, 19, 20, 21], +      [22, 23, 24, 25, 26, 27, 28], +      [29, 30, 31, 0, 0, 0, 0]]], +    [[[0, 0, 0, 1, 2, 3, 4], +      [5, 6, 7, 8, 9, 10, 11], +      [12, 13, 14, 15, 16, 17, 18], +      [19, 20, 21, 22, 23, 24, 25], +      [26, 27, 28, 29, 30, 0, 0]], +     [[0, 0, 0, 0, 0, 1, 2], +      [3, 4, 5, 6, 7, 8, 9], +      [10, 11, 12, 13, 14, 15, 16], +      [17, 18, 19, 20, 21, 22, 23], +      [24, 25, 26, 27, 28, 29, 30], +      [31, 0, 0, 0, 0, 0, 0]], +     [[0, 1, 2, 3, 4, 5, 6], +      [7, 8, 9, 10, 11, 12, 13], +      [14, 15, 16, 17, 18, 19, 20], +      [21, 22, 23, 24, 25, 26, 27], +      [28, 29, 30, 0, 0, 0, 0]]], +    [[[0, 0, 0, 1, 2, 3, 4], +      [5, 6, 7, 8, 9, 10, 11], +      [12, 13, 14, 15, 16, 17, 18], +      [19, 20, 21, 22, 23, 24, 25], +      [26, 27, 28, 29, 30, 31, 0]], +     [[0, 0, 0, 0, 0, 0, 1], +      [2, 3, 4, 5, 6, 7, 8], +      [9, 10, 11, 12, 13, 14, 15], +      [16, 17, 18, 19, 20, 21, 22], +      [23, 24, 25, 26, 27, 28, 29], +      [30, 31, 0, 0, 0, 0, 0]], +     [[0, 0, 1, 2, 3, 4, 5], +      [6, 7, 8, 9, 10, 11, 12], +      [13, 14, 15, 16, 17, 18, 19], +      [20, 21, 22, 23, 24, 25, 26], +      [27, 28, 29, 30, 0, 0, 0]]], +    [[[0, 0, 0, 0, 1, 2, 3], +      [4, 5, 6, 7, 8, 9, 10], +      [11, 12, 13, 14, 15, 16, 17], +      [18, 19, 20, 21, 22, 23, 24], +      [25, 26, 27, 28, 29, 30, 31]], +     [[1, 2, 3, 4, 5, 6, 7], +      [8, 9, 10, 11, 12, 13, 14], +      [15, 16, 17, 18, 19, 20, 21], +      [22, 23, 24, 25, 26, 27, 28], +      [29, 30, 0, 0, 0, 0, 0]], +     [[0, 0, 1, 2, 3, 4, 5], +      [6, 7, 8, 9, 10, 11, 12], +      [13, 14, 15, 16, 17, 18, 19], +      [20, 21, 22, 23, 24, 25, 26], +      [27, 28, 29, 30, 31, 0, 0]]] +] + +result_2004_dates = \ +    [[['12/29/03 12/30/03 12/31/03 01/01/04 01/02/04 01/03/04 01/04/04', +       '01/05/04 01/06/04 01/07/04 01/08/04 01/09/04 01/10/04 01/11/04', +       '01/12/04 01/13/04 01/14/04 01/15/04 01/16/04 01/17/04 01/18/04', +       '01/19/04 01/20/04 01/21/04 01/22/04 01/23/04 01/24/04 01/25/04', +       '01/26/04 01/27/04 01/28/04 01/29/04 01/30/04 01/31/04 02/01/04'], +      ['01/26/04 01/27/04 01/28/04 01/29/04 01/30/04 01/31/04 02/01/04', +       '02/02/04 02/03/04 02/04/04 02/05/04 02/06/04 02/07/04 02/08/04', +       '02/09/04 02/10/04 02/11/04 02/12/04 02/13/04 02/14/04 02/15/04', +       '02/16/04 02/17/04 02/18/04 02/19/04 02/20/04 02/21/04 02/22/04', +       '02/23/04 02/24/04 02/25/04 02/26/04 02/27/04 02/28/04 02/29/04'], +      ['03/01/04 03/02/04 03/03/04 03/04/04 03/05/04 03/06/04 03/07/04', +       '03/08/04 03/09/04 03/10/04 03/11/04 03/12/04 03/13/04 03/14/04', +       '03/15/04 03/16/04 03/17/04 03/18/04 03/19/04 03/20/04 03/21/04', +       '03/22/04 03/23/04 03/24/04 03/25/04 03/26/04 03/27/04 03/28/04', +       '03/29/04 03/30/04 03/31/04 04/01/04 04/02/04 04/03/04 04/04/04']], +     [['03/29/04 03/30/04 03/31/04 04/01/04 04/02/04 04/03/04 04/04/04', +       '04/05/04 04/06/04 04/07/04 04/08/04 04/09/04 04/10/04 04/11/04', +       '04/12/04 04/13/04 04/14/04 04/15/04 04/16/04 04/17/04 04/18/04', +       '04/19/04 04/20/04 04/21/04 04/22/04 04/23/04 04/24/04 04/25/04', +       '04/26/04 04/27/04 04/28/04 04/29/04 04/30/04 05/01/04 05/02/04'], +      ['04/26/04 04/27/04 04/28/04 04/29/04 04/30/04 05/01/04 05/02/04', +       '05/03/04 05/04/04 05/05/04 05/06/04 05/07/04 05/08/04 05/09/04', +       '05/10/04 05/11/04 05/12/04 05/13/04 05/14/04 05/15/04 05/16/04', +       '05/17/04 05/18/04 05/19/04 05/20/04 05/21/04 05/22/04 05/23/04', +       '05/24/04 05/25/04 05/26/04 05/27/04 05/28/04 05/29/04 05/30/04', +       '05/31/04 06/01/04 06/02/04 06/03/04 06/04/04 06/05/04 06/06/04'], +      ['05/31/04 06/01/04 06/02/04 06/03/04 06/04/04 06/05/04 06/06/04', +       '06/07/04 06/08/04 06/09/04 06/10/04 06/11/04 06/12/04 06/13/04', +       '06/14/04 06/15/04 06/16/04 06/17/04 06/18/04 06/19/04 06/20/04', +       '06/21/04 06/22/04 06/23/04 06/24/04 06/25/04 06/26/04 06/27/04', +       '06/28/04 06/29/04 06/30/04 07/01/04 07/02/04 07/03/04 07/04/04']], +     [['06/28/04 06/29/04 06/30/04 07/01/04 07/02/04 07/03/04 07/04/04', +       '07/05/04 07/06/04 07/07/04 07/08/04 07/09/04 07/10/04 07/11/04', +       '07/12/04 07/13/04 07/14/04 07/15/04 07/16/04 07/17/04 07/18/04', +       '07/19/04 07/20/04 07/21/04 07/22/04 07/23/04 07/24/04 07/25/04', +       '07/26/04 07/27/04 07/28/04 07/29/04 07/30/04 07/31/04 08/01/04'], +      ['07/26/04 07/27/04 07/28/04 07/29/04 07/30/04 07/31/04 08/01/04', +       '08/02/04 08/03/04 08/04/04 08/05/04 08/06/04 08/07/04 08/08/04', +       '08/09/04 08/10/04 08/11/04 08/12/04 08/13/04 08/14/04 08/15/04', +       '08/16/04 08/17/04 08/18/04 08/19/04 08/20/04 08/21/04 08/22/04', +       '08/23/04 08/24/04 08/25/04 08/26/04 08/27/04 08/28/04 08/29/04', +       '08/30/04 08/31/04 09/01/04 09/02/04 09/03/04 09/04/04 09/05/04'], +      ['08/30/04 08/31/04 09/01/04 09/02/04 09/03/04 09/04/04 09/05/04', +       '09/06/04 09/07/04 09/08/04 09/09/04 09/10/04 09/11/04 09/12/04', +       '09/13/04 09/14/04 09/15/04 09/16/04 09/17/04 09/18/04 09/19/04', +       '09/20/04 09/21/04 09/22/04 09/23/04 09/24/04 09/25/04 09/26/04', +       '09/27/04 09/28/04 09/29/04 09/30/04 10/01/04 10/02/04 10/03/04']], +     [['09/27/04 09/28/04 09/29/04 09/30/04 10/01/04 10/02/04 10/03/04', +       '10/04/04 10/05/04 10/06/04 10/07/04 10/08/04 10/09/04 10/10/04', +       '10/11/04 10/12/04 10/13/04 10/14/04 10/15/04 10/16/04 10/17/04', +       '10/18/04 10/19/04 10/20/04 10/21/04 10/22/04 10/23/04 10/24/04', +       '10/25/04 10/26/04 10/27/04 10/28/04 10/29/04 10/30/04 10/31/04'], +      ['11/01/04 11/02/04 11/03/04 11/04/04 11/05/04 11/06/04 11/07/04', +       '11/08/04 11/09/04 11/10/04 11/11/04 11/12/04 11/13/04 11/14/04', +       '11/15/04 11/16/04 11/17/04 11/18/04 11/19/04 11/20/04 11/21/04', +       '11/22/04 11/23/04 11/24/04 11/25/04 11/26/04 11/27/04 11/28/04', +       '11/29/04 11/30/04 12/01/04 12/02/04 12/03/04 12/04/04 12/05/04'], +      ['11/29/04 11/30/04 12/01/04 12/02/04 12/03/04 12/04/04 12/05/04', +       '12/06/04 12/07/04 12/08/04 12/09/04 12/10/04 12/11/04 12/12/04', +       '12/13/04 12/14/04 12/15/04 12/16/04 12/17/04 12/18/04 12/19/04', +       '12/20/04 12/21/04 12/22/04 12/23/04 12/24/04 12/25/04 12/26/04', +       '12/27/04 12/28/04 12/29/04 12/30/04 12/31/04 01/01/05 01/02/05']]] +  class OutputTestCase(unittest.TestCase):      def normalize_calendar(self, s): @@ -177,12 +317,19 @@ class OutputTestCase(unittest.TestCase):              return not c.isspace() and not c.isdigit()          lines = [] -        for line in s.splitlines(False): +        for line in s.splitlines(keepends=False):              # Drop texts, as they are locale dependent              if line and not filter(neitherspacenordigit, line):                  lines.append(line)          return lines +    def check_htmlcalendar_encoding(self, req, res): +        cal = calendar.HTMLCalendar() +        self.assertEqual( +            cal.formatyearpage(2004, encoding=req).strip(b' \t\n'), +            (result_2004_html % {'e': res}).strip(' \t\n').encode(res) +        ) +      def test_output(self):          self.assertEqual(              self.normalize_calendar(calendar.calendar(2004)), @@ -195,12 +342,60 @@ class OutputTestCase(unittest.TestCase):              result_2004_text.strip()          ) -    def test_output_htmlcalendar(self): -        encoding = 'ascii' -        cal = calendar.HTMLCalendar() +    def test_output_htmlcalendar_encoding_ascii(self): +        self.check_htmlcalendar_encoding('ascii', 'ascii') + +    def test_output_htmlcalendar_encoding_utf8(self): +        self.check_htmlcalendar_encoding('utf-8', 'utf-8') + +    def test_output_htmlcalendar_encoding_default(self): +        self.check_htmlcalendar_encoding(None, sys.getdefaultencoding()) + +    def test_yeardatescalendar(self): +        def shrink(cal): +            return [[[' '.join('{:02d}/{:02d}/{}'.format( +                                d.month, d.day, str(d.year)[-2:]) for d in z) +                            for z in y] for y in x] for x in cal] +        self.assertEqual( +            shrink(calendar.Calendar().yeardatescalendar(2004)), +            result_2004_dates +        ) + +    def test_yeardayscalendar(self): +        self.assertEqual( +            calendar.Calendar().yeardayscalendar(2004), +            result_2004_days +        ) + +    def test_formatweekheader_short(self): +        self.assertEqual( +            calendar.TextCalendar().formatweekheader(2), +            'Mo Tu We Th Fr Sa Su' +        ) + +    def test_formatweekheader_long(self):          self.assertEqual( -            cal.formatyearpage(2004, encoding=encoding).strip(b' \t\n'), -            result_2004_html.strip(' \t\n').encode(encoding) +            calendar.TextCalendar().formatweekheader(9), +            '  Monday   Tuesday  Wednesday  Thursday ' +            '  Friday   Saturday   Sunday ' +        ) + +    def test_formatmonth(self): +        self.assertEqual( +            calendar.TextCalendar().formatmonth(2004, 1).strip(), +            result_2004_01_text.strip() +        ) + +    def test_formatmonthname_with_year(self): +        self.assertEqual( +            calendar.HTMLCalendar().formatmonthname(2004, 1, withyear=True), +            '<tr><th colspan="7" class="month">January 2004</th></tr>' +        ) + +    def test_formatmonthname_without_year(self): +        self.assertEqual( +            calendar.HTMLCalendar().formatmonthname(2004, 1, withyear=False), +            '<tr><th colspan="7" class="month">January</th></tr>'          ) @@ -226,7 +421,11 @@ class CalendarTestCase(unittest.TestCase):          self.assertEqual(calendar.firstweekday(), calendar.MONDAY)          calendar.setfirstweekday(orig) -    def test_enumerateweekdays(self): +    def test_illegal_weekday_reported(self): +        with self.assertRaisesRegex(calendar.IllegalWeekdayError, '123'): +            calendar.setfirstweekday(123) + +    def test_enumerate_weekdays(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) @@ -252,7 +451,7 @@ class CalendarTestCase(unittest.TestCase):              # verify it "acts like a sequence" in two forms of iteration              self.assertEqual(value[::-1], list(reversed(value))) -    def test_localecalendars(self): +    def test_locale_calendars(self):          # ensure that Locale{Text,HTML}Calendar resets the locale properly          # (it is still not thread-safe though)          old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) @@ -431,6 +630,10 @@ class MonthRangeTestCase(unittest.TestCase):          with self.assertRaises(calendar.IllegalMonthError):              calendar.monthrange(2004, 13) +    def test_illegal_month_reported(self): +        with self.assertRaisesRegex(calendar.IllegalMonthError, '65'): +            calendar.monthrange(2004, 65) +  class LeapdaysTestCase(unittest.TestCase):      def test_no_range(self):          # test when no range i.e. two identical years as args  | 
