@@ -899,6 +899,50 @@ def test_synopsis(self):
899
899
synopsis = pydoc .synopsis (TESTFN , {})
900
900
self .assertEqual (synopsis , 'line 1: h\xe9 ' )
901
901
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.\n Second line.\n Third 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\n y = 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
+
902
946
@requires_docstrings
903
947
def test_synopsis_sourceless (self ):
904
948
os = import_helper .import_fresh_module ('os' )
0 commit comments