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