3
3
4
4
import sys
5
5
import textwrap
6
+ import warnings
6
7
7
8
import jinja2
8
9
@@ -151,13 +152,16 @@ def test_signature():
151
152
assert doc ['Signature' ].startswith ('numpy.multivariate_normal(' )
152
153
assert doc ['Signature' ].endswith ('spam=None)' )
153
154
155
+
154
156
def test_summary ():
155
157
assert doc ['Summary' ][0 ].startswith ('Draw values' )
156
158
assert doc ['Summary' ][- 1 ].endswith ('covariance.' )
157
159
160
+
158
161
def test_extended_summary ():
159
162
assert doc ['Extended Summary' ][0 ].startswith ('The multivariate normal' )
160
163
164
+
161
165
def test_parameters ():
162
166
assert_equal (len (doc ['Parameters' ]), 3 )
163
167
assert_equal ([n for n ,_ ,_ in doc ['Parameters' ]], ['mean' ,'cov' ,'shape' ])
@@ -167,13 +171,15 @@ def test_parameters():
167
171
assert desc [0 ].startswith ('Covariance matrix' )
168
172
assert doc ['Parameters' ][0 ][- 1 ][- 2 ] == ' (1+2+3)/3'
169
173
174
+
170
175
def test_other_parameters ():
171
176
assert_equal (len (doc ['Other Parameters' ]), 1 )
172
177
assert_equal ([n for n ,_ ,_ in doc ['Other Parameters' ]], ['spam' ])
173
178
arg , arg_type , desc = doc ['Other Parameters' ][0 ]
174
179
assert_equal (arg_type , 'parrot' )
175
180
assert desc [0 ].startswith ('A parrot off its mortal coil' )
176
181
182
+
177
183
def test_returns ():
178
184
assert_equal (len (doc ['Returns' ]), 2 )
179
185
arg , arg_type , desc = doc ['Returns' ][0 ]
@@ -188,6 +194,7 @@ def test_returns():
188
194
assert desc [0 ].startswith ('This is not a real' )
189
195
assert desc [- 1 ].endswith ('anonymous return values.' )
190
196
197
+
191
198
def test_yields ():
192
199
section = doc_yields ['Yields' ]
193
200
assert_equal (len (section ), 3 )
@@ -200,6 +207,7 @@ def test_yields():
200
207
assert desc [0 ].startswith ('The number of' )
201
208
assert desc [0 ].endswith (end )
202
209
210
+
203
211
def test_returnyield ():
204
212
doc_text = """
205
213
Test having returns and yields.
@@ -289,26 +297,31 @@ def test_notes():
289
297
assert doc ['Notes' ][- 1 ].endswith ('definite.' )
290
298
assert_equal (len (doc ['Notes' ]), 17 )
291
299
300
+
292
301
def test_references ():
293
302
assert doc ['References' ][0 ].startswith ('..' )
294
303
assert doc ['References' ][- 1 ].endswith ('2001.' )
295
304
305
+
296
306
def test_examples ():
297
307
assert doc ['Examples' ][0 ].startswith ('>>>' )
298
308
assert doc ['Examples' ][- 1 ].endswith ('True]' )
299
309
310
+
300
311
def test_index ():
301
312
assert_equal (doc ['index' ]['default' ], 'random' )
302
313
assert_equal (len (doc ['index' ]), 2 )
303
314
assert_equal (len (doc ['index' ]['refguide' ]), 2 )
304
315
305
- def non_blank_line_by_line_compare (a ,b ):
316
+
317
+ def non_blank_line_by_line_compare (a , b ):
306
318
a = textwrap .dedent (a )
307
319
b = textwrap .dedent (b )
308
320
a = [l .rstrip () for l in a .split ('\n ' ) if l .strip ()]
309
321
b = [l .rstrip () for l in b .split ('\n ' ) if l .strip ()]
310
322
assert_list_equal (a , b )
311
323
324
+
312
325
def test_str ():
313
326
# doc_txt has the order of Notes and See Also sections flipped.
314
327
# This should be handled automatically, and so, one thing this test does
@@ -595,15 +608,18 @@ def test_sphinx_yields_str():
595
608
If None, the index is into the flattened array, otherwise along
596
609
the specified axis""" )
597
610
611
+
598
612
def test_parameters_without_extended_description ():
599
613
assert_equal (len (doc2 ['Parameters' ]), 2 )
600
614
615
+
601
616
doc3 = NumpyDocString ("""
602
617
my_signature(*params, **kwds)
603
618
604
619
Return this and that.
605
620
""" )
606
621
622
+
607
623
def test_escape_stars ():
608
624
signature = str (doc3 ).split ('\n ' )[0 ]
609
625
assert_equal (signature , 'my_signature(\*params, \*\*kwds)' )
@@ -614,14 +630,17 @@ def my_func(a, b, **kwargs):
614
630
fdoc = FunctionDoc (func = my_func )
615
631
assert_equal (fdoc ['Signature' ], 'my_func(a, b, \*\*kwargs)' )
616
632
633
+
617
634
doc4 = NumpyDocString (
618
635
"""a.conj()
619
636
620
637
Return an array with all complex-valued elements conjugated.""" )
621
638
639
+
622
640
def test_empty_extended_summary ():
623
641
assert_equal (doc4 ['Extended Summary' ], [])
624
642
643
+
625
644
doc5 = NumpyDocString (
626
645
"""
627
646
a.something()
@@ -637,18 +656,21 @@ def test_empty_extended_summary():
637
656
If needed
638
657
""" )
639
658
659
+
640
660
def test_raises ():
641
661
assert_equal (len (doc5 ['Raises' ]), 1 )
642
662
name ,_ ,desc = doc5 ['Raises' ][0 ]
643
663
assert_equal (name ,'LinAlgException' )
644
664
assert_equal (desc ,['If array is singular.' ])
645
665
666
+
646
667
def test_warns ():
647
668
assert_equal (len (doc5 ['Warns' ]), 1 )
648
669
name ,_ ,desc = doc5 ['Warns' ][0 ]
649
670
assert_equal (name ,'SomeWarning' )
650
671
assert_equal (desc ,['If needed' ])
651
672
673
+
652
674
def test_see_also ():
653
675
doc6 = NumpyDocString (
654
676
"""
@@ -726,12 +748,45 @@ class Dummy(object):
726
748
assert (' some relationship' in s )
727
749
assert (':func:`func_d`' in s )
728
750
751
+
752
+ def test_unknown_section ():
753
+ doc_text = """
754
+ Test having an unknown section
755
+
756
+ Mope
757
+ ----
758
+ This should be ignored and warned about
759
+ """
760
+
761
+ class BadSection (object ):
762
+ """Class with bad section.
763
+
764
+ Nope
765
+ ----
766
+ This class has a nope section.
767
+ """
768
+ pass
769
+
770
+ with warnings .catch_warnings (record = True ) as w :
771
+ NumpyDocString (doc_text )
772
+ assert len (w ) == 1
773
+ assert "Unknown section Mope" == str (w [0 ].message )
774
+
775
+ with warnings .catch_warnings (record = True ) as w :
776
+ SphinxClassDoc (BadSection )
777
+ assert len (w ) == 1
778
+ assert_true ('test_docscrape.test_unknown_section.<locals>.BadSection'
779
+ in str (w [0 ].message )
780
+ or 'test_docscrape.BadSection' in str (w [0 ].message ))
781
+
782
+
729
783
doc7 = NumpyDocString ("""
730
784
731
785
Doc starts on second line.
732
786
733
787
""" )
734
788
789
+
735
790
def test_empty_first_line ():
736
791
assert doc7 ['Summary' ][0 ].startswith ('Doc starts' )
737
792
@@ -762,6 +817,7 @@ def test_unicode():
762
817
assert isinstance (doc ['Summary' ][0 ], str )
763
818
assert doc ['Summary' ][0 ] == 'öäöäöäöäöåååå'
764
819
820
+
765
821
def test_plot_examples ():
766
822
cfg = dict (use_plots = True )
767
823
@@ -785,6 +841,7 @@ def test_plot_examples():
785
841
""" , config = cfg )
786
842
assert str (doc ).count ('plot::' ) == 1 , str (doc )
787
843
844
+
788
845
def test_class_members ():
789
846
790
847
class Dummy (object ):
@@ -866,6 +923,7 @@ def bar(self, a, b):
866
923
else :
867
924
assert 'Spammity index' in str (doc ), str (doc )
868
925
926
+
869
927
def test_duplicate_signature ():
870
928
# Duplicate function signatures occur e.g. in ufuncs, when the
871
929
# automatic mechanism adds one, and a more detailed comes from the
@@ -911,6 +969,7 @@ def test_duplicate_signature():
911
969
For usage examples, see `ode`.
912
970
"""
913
971
972
+
914
973
def test_class_members_doc ():
915
974
doc = ClassDoc (None , class_doc_txt )
916
975
non_blank_line_by_line_compare (str (doc ),
@@ -949,6 +1008,7 @@ def test_class_members_doc():
949
1008
950
1009
""" )
951
1010
1011
+
952
1012
def test_class_members_doc_sphinx ():
953
1013
class Foo :
954
1014
@property
@@ -997,6 +1057,7 @@ def x(self):
997
1057
998
1058
""" )
999
1059
1060
+
1000
1061
def test_templated_sections ():
1001
1062
doc = SphinxClassDoc (None , class_doc_txt ,
1002
1063
config = {'template' : jinja2 .Template ('{{examples}}{{parameters}}' )})
@@ -1020,8 +1081,6 @@ def test_templated_sections():
1020
1081
""" )
1021
1082
1022
1083
1023
-
1024
-
1025
1084
if __name__ == "__main__" :
1026
1085
import nose
1027
1086
nose .run ()
0 commit comments