Skip to content

Commit 8199426

Browse files
committed
TST: More unit tests for the Yields section.
1 parent b618d52 commit 8199426

File tree

2 files changed

+78
-15
lines changed

2 files changed

+78
-15
lines changed

numpydoc/docscrape_sphinx.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def _str_summary(self):
4646
def _str_extended_summary(self):
4747
return self['Extended Summary'] + ['']
4848

49-
def _str_returns(self):
49+
def _str_returns(self, name='Returns'):
5050
out = []
51-
if self['Returns']:
52-
out += self._str_field_list('Returns')
51+
if self[name]:
52+
out += self._str_field_list(name)
5353
out += ['']
54-
for param, param_type, desc in self['Returns']:
54+
for param, param_type, desc in self[name]:
5555
if param_type:
5656
out += self._str_indent(['**%s** : %s' % (param.strip(),
5757
param_type)])
@@ -225,7 +225,7 @@ def __str__(self, indent=0, func_role="obj"):
225225
out += self._str_extended_summary()
226226
out += self._str_param_list('Parameters')
227227
out += self._str_returns()
228-
out += self._str_param_list('Yields')
228+
out += self._str_returns('Yields')
229229
for param_list in ('Other Parameters', 'Raises', 'Warns'):
230230
out += self._str_param_list(param_list)
231231
out += self._str_warnings()

numpydoc/tests/test_docscrape.py

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@
122122
'''
123123
doc = NumpyDocString(doc_txt)
124124

125+
doc_yields_txt = """
126+
Test generator
127+
128+
Yields
129+
------
130+
a : int
131+
The number of apples.
132+
b : int
133+
The number of bananas.
134+
int
135+
The number of unknowns.
136+
"""
137+
doc_yields = NumpyDocString(doc_yields_txt)
138+
125139

126140
def test_signature():
127141
assert doc['Signature'].startswith('numpy.multivariate_normal(')
@@ -165,8 +179,25 @@ def test_returns():
165179
assert desc[-1].endswith('anonymous return values.')
166180

167181
def test_yields():
182+
section = doc_yields['Yields']
183+
assert_equal(len(section), 3)
184+
truth = [('a', 'int', 'apples.'),
185+
('b', 'int', 'bananas.'),
186+
('int', '', 'unknowns.')]
187+
for (arg, arg_type, desc), (arg_, arg_type_, end) in zip(section, truth):
188+
assert_equal(arg, arg_)
189+
assert_equal(arg_type, arg_type_)
190+
assert desc[0].startswith('The number of')
191+
assert desc[0].endswith(end)
192+
193+
def test_returnyield():
168194
doc_text = """
169-
Test generator
195+
Test having returns and yields.
196+
197+
Returns
198+
-------
199+
int
200+
The number of apples.
170201
171202
Yields
172203
------
@@ -176,15 +207,7 @@ def test_yields():
176207
The number of bananas.
177208
178209
"""
179-
doc = NumpyDocString(doc_text)
180-
section = doc['Yields']
181-
assert_equal(len(section), 2)
182-
truth = [('a', 'apples.'), ('b', 'bananas.')]
183-
for (arg, arg_type, desc), (arg_true, ending) in zip(section, truth):
184-
assert_equal(arg, arg_true)
185-
assert_equal(arg_type, 'int')
186-
assert desc[0].startswith('The number of')
187-
assert desc[0].endswith(ending)
210+
assert_raises(ValueError, NumpyDocString, doc_text)
188211

189212
def test_notes():
190213
assert doc['Notes'][0].startswith('Instead')
@@ -215,6 +238,9 @@ def non_blank_line_by_line_compare(a,b):
215238
"\n>>> %s\n<<< %s\n" %
216239
(n,line,b[n]))
217240
def test_str():
241+
# doc_txt has the order of Notes and See Also sections flipped.
242+
# This should be handled automatically, and so, one thing this test does
243+
# is to make sure that See Also precedes Notes in the output.
218244
non_blank_line_by_line_compare(str(doc),
219245
"""numpy.multivariate_normal(mean, cov, shape=None, spam=None)
220246
@@ -324,6 +350,22 @@ def test_str():
324350
:refguide: random;distributions, random;gauss""")
325351

326352

353+
def test_yield_str():
354+
non_blank_line_by_line_compare(str(doc_yields),
355+
"""Test generator
356+
357+
Yields
358+
------
359+
a : int
360+
The number of apples.
361+
b : int
362+
The number of bananas.
363+
int
364+
The number of unknowns.
365+
366+
.. index:: """)
367+
368+
327369
def test_sphinx_str():
328370
sphinx_doc = SphinxDocString(doc_txt)
329371
non_blank_line_by_line_compare(str(sphinx_doc),
@@ -449,6 +491,27 @@ def test_sphinx_str():
449491
""")
450492

451493

494+
def test_sphinx_yields_str():
495+
sphinx_doc = SphinxDocString(doc_yields_txt)
496+
non_blank_line_by_line_compare(str(sphinx_doc),
497+
"""Test generator
498+
499+
:Yields:
500+
501+
**a** : int
502+
503+
The number of apples.
504+
505+
**b** : int
506+
507+
The number of bananas.
508+
509+
int
510+
511+
The number of unknowns.
512+
""")
513+
514+
452515
doc2 = NumpyDocString("""
453516
Returns array of indices of the maximum values of along the given axis.
454517

0 commit comments

Comments
 (0)