Skip to content

Commit d9fe8dd

Browse files
committed
Add tests for doc strings
1 parent 9e0835c commit d9fe8dd

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Lib/test/test_pydoc/test_pydoc.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,50 @@ def test_synopsis(self):
899899
synopsis = pydoc.synopsis(TESTFN, {})
900900
self.assertEqual(synopsis, 'line 1: h\xe9')
901901

902+
def test_source_synopsis(self):
903+
test_cases = [
904+
# Single line docstring
905+
('"""Single line docstring."""',
906+
"Single line docstring."),
907+
908+
# Multi-line docstring - should only return first line
909+
('"""First line of docstring.\nSecond line.\nThird line."""',
910+
"First line of docstring."),
911+
912+
# Docstring with leading/trailing whitespace
913+
('""" Whitespace around docstring. """',
914+
"Whitespace around docstring."),
915+
916+
# No docstring
917+
('x = 1\ny = 2',
918+
None),
919+
920+
# Comments before docstring
921+
('# Comment\n"""Docstring after comment."""',
922+
"Docstring after comment."),
923+
924+
# Empty docstring
925+
('""""""',
926+
""),
927+
928+
# Unicode docstring
929+
('"""Café and résumé."""',
930+
"Café and résumé."),
931+
]
932+
933+
for source, expected in test_cases:
934+
with self.subTest(source=source):
935+
source_file = StringIO(source)
936+
result = pydoc.source_synopsis(source_file)
937+
self.assertEqual(result, expected)
938+
939+
with tempfile.NamedTemporaryFile(mode='w+', encoding='utf-8') as temp_file:
940+
temp_file.write('"""Real file test."""\n')
941+
temp_file.flush()
942+
temp_file.seek(0)
943+
result = pydoc.source_synopsis(temp_file)
944+
self.assertEqual(result, "Real file test.")
945+
902946
@requires_docstrings
903947
def test_synopsis_sourceless(self):
904948
os = import_helper.import_fresh_module('os')

0 commit comments

Comments
 (0)