22
33import os
44import unittest
5+ import textwrap
56
67from test .support .script_helper import assert_python_ok
78from test .test_tools import skip_if_missing , toolsdir
@@ -27,6 +28,41 @@ def get_header(self, data):
2728 headers [key ] = val .strip ()
2829 return headers
2930
31+ def get_msgids (self , data ):
32+ """ utility: return all msgids in .po file as a list of strings """
33+ msgids = []
34+ reading_msgid = False
35+ cur_msgid = []
36+ for line in data .split ('\n ' ):
37+ if reading_msgid :
38+ if line .startswith ('"' ):
39+ cur_msgid .append (line .strip ('"' ))
40+ else :
41+ msgids .append ('\n ' .join (cur_msgid ))
42+ cur_msgid = []
43+ reading_msgid = False
44+ continue
45+ if line .startswith ('msgid ' ):
46+ line = line [len ('msgid ' ):]
47+ cur_msgid .append (line .strip ('"' ))
48+ reading_msgid = True
49+ else :
50+ if reading_msgid :
51+ msgids .append ('\n ' .join (cur_msgid ))
52+
53+ return msgids
54+
55+ def extract_docstrings_from_str (self , module_content ):
56+ """ utility: return all msgids extracted from module_content """
57+ filename = 'test_docstrings.py'
58+ with temp_cwd (None ) as cwd :
59+ with open (filename , 'w' ) as fp :
60+ fp .write (module_content )
61+ assert_python_ok (self .script , '-D' , filename )
62+ with open ('messages.pot' ) as fp :
63+ data = fp .read ()
64+ return self .get_msgids (data )
65+
3066 def test_header (self ):
3167 """Make sure the required fields are in the header, according to:
3268 http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry
@@ -70,3 +106,55 @@ def test_POT_Creation_Date(self):
70106
71107 # This will raise if the date format does not exactly match.
72108 datetime .strptime (creationDate , '%Y-%m-%d %H:%M%z' )
109+
110+ def test_funcdocstring_annotated_args (self ):
111+ """ Test docstrings for functions with annotated args """
112+ msgids = self .extract_docstrings_from_str (textwrap .dedent ('''\
113+ def foo(bar: str):
114+ """doc"""
115+ ''' ))
116+ self .assertIn ('doc' , msgids )
117+
118+ def test_funcdocstring_annotated_return (self ):
119+ """ Test docstrings for functions with annotated return type """
120+ msgids = self .extract_docstrings_from_str (textwrap .dedent ('''\
121+ def foo(bar) -> str:
122+ """doc"""
123+ ''' ))
124+ self .assertIn ('doc' , msgids )
125+
126+ def test_funcdocstring_defvalue_args (self ):
127+ """ Test docstring for functions with default arg values """
128+ msgids = self .extract_docstrings_from_str (textwrap .dedent ('''\
129+ def foo(bar=()):
130+ """doc"""
131+ ''' ))
132+ self .assertIn ('doc' , msgids )
133+
134+ def test_funcdocstring_multiple_funcs (self ):
135+ """ Test docstring extraction for multiple functions combining
136+ annotated args, annotated return types and default arg values
137+ """
138+ msgids = self .extract_docstrings_from_str (textwrap .dedent ('''\
139+ def foo1(bar: tuple=()) -> str:
140+ """doc1"""
141+
142+ def foo2(bar: List[1:2]) -> (lambda x: x):
143+ """doc2"""
144+
145+ def foo3(bar: 'func'=lambda x: x) -> {1: 2}:
146+ """doc3"""
147+ ''' ))
148+ self .assertIn ('doc1' , msgids )
149+ self .assertIn ('doc2' , msgids )
150+ self .assertIn ('doc3' , msgids )
151+
152+ def test_classdocstring_early_colon (self ):
153+ """ Test docstring extraction for a class with colons occuring within
154+ the parentheses.
155+ """
156+ msgids = self .extract_docstrings_from_str (textwrap .dedent ('''\
157+ class D(L[1:2], F({1: 2}), metaclass=M(lambda x: x)):
158+ """doc"""
159+ ''' ))
160+ self .assertIn ('doc' , msgids )
0 commit comments