cpython: 50a5c262b2e6 (original) (raw)
--- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -2,11 +2,22 @@ import calendar import unittest from test import test_support +from test.script_helper import assert_python_ok, assert_python_failure import locale import datetime - +import os -result_2004_text = """ +result_2004_01_text = """[](#l1.14)
1 2 3 4[](#l1.17)
- 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 = """[](#l1.24) 2004
January February March @@ -44,7 +55,7 @@ 18 19 20 21 22 23 24 22 23 24 25 26 25 26 27 28 29 30 31 29 30 27 28 29 30 31 """ -result_2004_html = """ +result_2004_html = """[](#l1.33) @@ -460,6 +471,124 @@ class LeapdaysTestCase(unittest.TestCase self.assertEqual(calendar.leapdays(1997,2020), 5) +class CommandLineTestCase(unittest.TestCase):
- def assertFailure(self, *args):
rc, stdout, stderr = assert_python_failure('-m', 'calendar', *args)[](#l1.46)
self.assertIn(b'Usage:', stderr)[](#l1.47)
self.assertEqual(rc, 2)[](#l1.48)
- def test_help(self):
stdout = self.run_ok('-h')[](#l1.51)
self.assertIn(b'Usage:', stdout)[](#l1.52)
self.assertIn(b'calendar.py', stdout)[](#l1.53)
self.assertIn(b'--help', stdout)[](#l1.54)
- def test_illegal_arguments(self):
self.assertFailure('-z')[](#l1.57)
#self.assertFailure('spam')[](#l1.58)
#self.assertFailure('2004', 'spam')[](#l1.59)
self.assertFailure('-t', 'html', '2004', '1')[](#l1.60)
- def test_output_current_year(self):
stdout = self.run_ok()[](#l1.63)
year = datetime.datetime.now().year[](#l1.64)
self.assertIn((' %s' % year).encode(), stdout)[](#l1.65)
self.assertIn(b'January', stdout)[](#l1.66)
self.assertIn(b'Mo Tu We Th Fr Sa Su', stdout)[](#l1.67)
- def test_output_year(self):
stdout = self.run_ok('2004')[](#l1.70)
self.assertEqual(stdout.strip(), result_2004_text.strip())[](#l1.71)
- def test_output_month(self):
stdout = self.run_ok('2004', '1')[](#l1.74)
self.assertEqual(stdout.strip(), result_2004_01_text.strip())[](#l1.75)
- def test_option_encoding(self):
self.assertFailure('-e')[](#l1.78)
self.assertFailure('--encoding')[](#l1.79)
stdout = self.run_ok('--encoding', 'rot-13', '2004')[](#l1.80)
self.assertEqual(stdout.strip(), result_2004_text.encode('rot-13').strip())[](#l1.81)
- def test_option_locale(self):
self.assertFailure('-L')[](#l1.84)
self.assertFailure('--locale')[](#l1.85)
self.assertFailure('-L', 'en')[](#l1.86)
lang, enc = locale.getdefaultlocale()[](#l1.87)
lang = lang or 'C'[](#l1.88)
enc = enc or 'UTF-8'[](#l1.89)
try:[](#l1.90)
oldlocale = locale.getlocale(locale.LC_TIME)[](#l1.91)
try:[](#l1.92)
locale.setlocale(locale.LC_TIME, (lang, enc))[](#l1.93)
finally:[](#l1.94)
locale.setlocale(locale.LC_TIME, oldlocale)[](#l1.95)
except (locale.Error, ValueError):[](#l1.96)
self.skipTest('cannot set the system default locale')[](#l1.97)
stdout = self.run_ok('--locale', lang, '--encoding', enc, '2004')[](#l1.98)
self.assertIn('2004'.encode(enc), stdout)[](#l1.99)
- def test_option_width(self):
self.assertFailure('-w')[](#l1.102)
self.assertFailure('--width')[](#l1.103)
self.assertFailure('-w', 'spam')[](#l1.104)
stdout = self.run_ok('--width', '3', '2004')[](#l1.105)
self.assertIn(b'Mon Tue Wed Thu Fri Sat Sun', stdout)[](#l1.106)
- def test_option_lines(self):
self.assertFailure('-l')[](#l1.109)
self.assertFailure('--lines')[](#l1.110)
self.assertFailure('-l', 'spam')[](#l1.111)
stdout = self.run_ok('--lines', '2', '2004')[](#l1.112)
self.assertIn('December\n\nMo Tu We', stdout)[](#l1.113)
- def test_option_spacing(self):
self.assertFailure('-s')[](#l1.116)
self.assertFailure('--spacing')[](#l1.117)
self.assertFailure('-s', 'spam')[](#l1.118)
stdout = self.run_ok('--spacing', '8', '2004')[](#l1.119)
self.assertIn(b'Su Mo', stdout)[](#l1.120)
- def test_option_months(self):
self.assertFailure('-m')[](#l1.123)
self.assertFailure('--month')[](#l1.124)
self.assertFailure('-m', 'spam')[](#l1.125)
stdout = self.run_ok('--months', '1', '2004')[](#l1.126)
self.assertIn('\nMo Tu We Th Fr Sa Su\n', stdout)[](#l1.127)
- def test_option_type(self):
self.assertFailure('-t')[](#l1.130)
self.assertFailure('--type')[](#l1.131)
self.assertFailure('-t', 'spam')[](#l1.132)
stdout = self.run_ok('--type', 'text', '2004')[](#l1.133)
self.assertEqual(stdout.strip(), result_2004_text.strip())[](#l1.134)
stdout = self.run_ok('--type', 'html', '2004')[](#l1.135)
self.assertEqual(stdout[:6], b'<?xml ')[](#l1.136)
self.assertIn(b'<title>Calendar for 2004</title>', stdout)[](#l1.137)
- def test_html_output_current_year(self):
stdout = self.run_ok('--type', 'html')[](#l1.140)
year = datetime.datetime.now().year[](#l1.141)
self.assertIn(('<title>Calendar for %s</title>' % year).encode(),[](#l1.142)
stdout)[](#l1.143)
self.assertIn(b'<tr><th colspan="7" class="month">January</th></tr>',[](#l1.144)
stdout)[](#l1.145)
- def test_html_output_year_encoding(self):
stdout = self.run_ok('-t', 'html', '--encoding', 'ascii', '2004')[](#l1.148)
self.assertEqual(stdout.strip(), result_2004_html.strip())[](#l1.149)
- def test_html_output_year_css(self):
self.assertFailure('-t', 'html', '-c')[](#l1.152)
self.assertFailure('-t', 'html', '--css')[](#l1.153)
stdout = self.run_ok('-t', 'html', '--css', 'custom.css', '2004')[](#l1.154)
self.assertIn(b'<link rel="stylesheet" type="text/css" '[](#l1.155)
b'href="custom.css" />', stdout)[](#l1.156)
+ + def test_main(): test_support.run_unittest( OutputTestCase, @@ -468,8 +597,10 @@ def test_main(): SundayTestCase, MonthRangeTestCase, LeapdaysTestCase,
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -98,6 +98,8 @@ Tools/Demos Tests ----- +- Issue #18982: Add tests for CLI of the calendar module. +