3
3
4
4
import sys
5
5
import textwrap
6
+ import warnings
6
7
7
8
import jinja2
8
9
@@ -153,13 +154,16 @@ def test_signature():
153
154
assert doc ['Signature' ].startswith ('numpy.multivariate_normal(' )
154
155
assert doc ['Signature' ].endswith ('spam=None)' )
155
156
157
+
156
158
def test_summary ():
157
159
assert doc ['Summary' ][0 ].startswith ('Draw values' )
158
160
assert doc ['Summary' ][- 1 ].endswith ('covariance.' )
159
161
162
+
160
163
def test_extended_summary ():
161
164
assert doc ['Extended Summary' ][0 ].startswith ('The multivariate normal' )
162
165
166
+
163
167
def test_parameters ():
164
168
assert_equal (len (doc ['Parameters' ]), 3 )
165
169
assert_equal ([n for n ,_ ,_ in doc ['Parameters' ]], ['mean' ,'cov' ,'shape' ])
@@ -169,13 +173,15 @@ def test_parameters():
169
173
assert desc [0 ].startswith ('Covariance matrix' )
170
174
assert doc ['Parameters' ][0 ][- 1 ][- 2 ] == ' (1+2+3)/3'
171
175
176
+
172
177
def test_other_parameters ():
173
178
assert_equal (len (doc ['Other Parameters' ]), 1 )
174
179
assert_equal ([n for n ,_ ,_ in doc ['Other Parameters' ]], ['spam' ])
175
180
arg , arg_type , desc = doc ['Other Parameters' ][0 ]
176
181
assert_equal (arg_type , 'parrot' )
177
182
assert desc [0 ].startswith ('A parrot off its mortal coil' )
178
183
184
+
179
185
def test_returns ():
180
186
assert_equal (len (doc ['Returns' ]), 2 )
181
187
arg , arg_type , desc = doc ['Returns' ][0 ]
@@ -190,6 +196,7 @@ def test_returns():
190
196
assert desc [0 ].startswith ('This is not a real' )
191
197
assert desc [- 1 ].endswith ('anonymous return values.' )
192
198
199
+
193
200
def test_yields ():
194
201
section = doc_yields ['Yields' ]
195
202
assert_equal (len (section ), 3 )
@@ -202,6 +209,7 @@ def test_yields():
202
209
assert desc [0 ].startswith ('The number of' )
203
210
assert desc [0 ].endswith (end )
204
211
212
+
205
213
def test_returnyield ():
206
214
doc_text = """
207
215
Test having returns and yields.
@@ -291,26 +299,31 @@ def test_notes():
291
299
assert doc ['Notes' ][- 1 ].endswith ('definite.' )
292
300
assert_equal (len (doc ['Notes' ]), 17 )
293
301
302
+
294
303
def test_references ():
295
304
assert doc ['References' ][0 ].startswith ('..' )
296
305
assert doc ['References' ][- 1 ].endswith ('2001.' )
297
306
307
+
298
308
def test_examples ():
299
309
assert doc ['Examples' ][0 ].startswith ('>>>' )
300
310
assert doc ['Examples' ][- 1 ].endswith ('True]' )
301
311
312
+
302
313
def test_index ():
303
314
assert_equal (doc ['index' ]['default' ], 'random' )
304
315
assert_equal (len (doc ['index' ]), 2 )
305
316
assert_equal (len (doc ['index' ]['refguide' ]), 2 )
306
317
307
- def non_blank_line_by_line_compare (a ,b ):
318
+
319
+ def non_blank_line_by_line_compare (a , b ):
308
320
a = textwrap .dedent (a )
309
321
b = textwrap .dedent (b )
310
322
a = [l .rstrip () for l in a .split ('\n ' ) if l .strip ()]
311
323
b = [l .rstrip () for l in b .split ('\n ' ) if l .strip ()]
312
324
assert_list_equal (a , b )
313
325
326
+
314
327
def test_str ():
315
328
# doc_txt has the order of Notes and See Also sections flipped.
316
329
# This should be handled automatically, and so, one thing this test does
@@ -597,15 +610,18 @@ def test_sphinx_yields_str():
597
610
If None, the index is into the flattened array, otherwise along
598
611
the specified axis""" )
599
612
613
+
600
614
def test_parameters_without_extended_description ():
601
615
assert_equal (len (doc2 ['Parameters' ]), 2 )
602
616
617
+
603
618
doc3 = NumpyDocString ("""
604
619
my_signature(*params, **kwds)
605
620
606
621
Return this and that.
607
622
""" )
608
623
624
+
609
625
def test_escape_stars ():
610
626
signature = str (doc3 ).split ('\n ' )[0 ]
611
627
assert_equal (signature , 'my_signature(\*params, \*\*kwds)' )
@@ -616,14 +632,17 @@ def my_func(a, b, **kwargs):
616
632
fdoc = FunctionDoc (func = my_func )
617
633
assert_equal (fdoc ['Signature' ], 'my_func(a, b, \*\*kwargs)' )
618
634
635
+
619
636
doc4 = NumpyDocString (
620
637
"""a.conj()
621
638
622
639
Return an array with all complex-valued elements conjugated.""" )
623
640
641
+
624
642
def test_empty_extended_summary ():
625
643
assert_equal (doc4 ['Extended Summary' ], [])
626
644
645
+
627
646
doc5 = NumpyDocString (
628
647
"""
629
648
a.something()
@@ -639,18 +658,21 @@ def test_empty_extended_summary():
639
658
If needed
640
659
""" )
641
660
661
+
642
662
def test_raises ():
643
663
assert_equal (len (doc5 ['Raises' ]), 1 )
644
664
name ,_ ,desc = doc5 ['Raises' ][0 ]
645
665
assert_equal (name ,'LinAlgException' )
646
666
assert_equal (desc ,['If array is singular.' ])
647
667
668
+
648
669
def test_warns ():
649
670
assert_equal (len (doc5 ['Warns' ]), 1 )
650
671
name ,_ ,desc = doc5 ['Warns' ][0 ]
651
672
assert_equal (name ,'SomeWarning' )
652
673
assert_equal (desc ,['If needed' ])
653
674
675
+
654
676
def test_see_also ():
655
677
doc6 = NumpyDocString (
656
678
"""
@@ -728,12 +750,45 @@ class Dummy(object):
728
750
assert (' some relationship' in s )
729
751
assert (':func:`func_d`' in s )
730
752
753
+
754
+ def test_unknown_section ():
755
+ doc_text = """
756
+ Test having an unknown section
757
+
758
+ Mope
759
+ ----
760
+ This should be ignored and warned about
761
+ """
762
+
763
+ class BadSection (object ):
764
+ """Class with bad section.
765
+
766
+ Nope
767
+ ----
768
+ This class has a nope section.
769
+ """
770
+ pass
771
+
772
+ with warnings .catch_warnings (record = True ) as w :
773
+ NumpyDocString (doc_text )
774
+ assert len (w ) == 1
775
+ assert "Unknown section Mope" == str (w [0 ].message )
776
+
777
+ with warnings .catch_warnings (record = True ) as w :
778
+ SphinxClassDoc (BadSection )
779
+ assert len (w ) == 1
780
+ assert_true ('test_docscrape.test_unknown_section.<locals>.BadSection'
781
+ in str (w [0 ].message )
782
+ or 'test_docscrape.BadSection' in str (w [0 ].message ))
783
+
784
+
731
785
doc7 = NumpyDocString ("""
732
786
733
787
Doc starts on second line.
734
788
735
789
""" )
736
790
791
+
737
792
def test_empty_first_line ():
738
793
assert doc7 ['Summary' ][0 ].startswith ('Doc starts' )
739
794
@@ -764,6 +819,7 @@ def test_unicode():
764
819
assert isinstance (doc ['Summary' ][0 ], str )
765
820
assert doc ['Summary' ][0 ] == 'öäöäöäöäöåååå'
766
821
822
+
767
823
def test_plot_examples ():
768
824
cfg = dict (use_plots = True )
769
825
@@ -787,6 +843,7 @@ def test_plot_examples():
787
843
""" , config = cfg )
788
844
assert str (doc ).count ('plot::' ) == 1 , str (doc )
789
845
846
+
790
847
def test_class_members ():
791
848
792
849
class Dummy (object ):
@@ -868,6 +925,7 @@ def bar(self, a, b):
868
925
else :
869
926
assert 'Spammity index' in str (doc ), str (doc )
870
927
928
+
871
929
def test_duplicate_signature ():
872
930
# Duplicate function signatures occur e.g. in ufuncs, when the
873
931
# automatic mechanism adds one, and a more detailed comes from the
@@ -922,6 +980,7 @@ def test_duplicate_signature():
922
980
For usage examples, see `ode`.
923
981
"""
924
982
983
+
925
984
def test_class_members_doc ():
926
985
doc = ClassDoc (None , class_doc_txt )
927
986
non_blank_line_by_line_compare (str (doc ),
@@ -969,6 +1028,7 @@ def test_class_members_doc():
969
1028
970
1029
""" )
971
1030
1031
+
972
1032
def test_class_members_doc_sphinx ():
973
1033
class Foo :
974
1034
@property
@@ -1064,6 +1124,7 @@ def no_period(self):
1064
1124
1065
1125
""" )
1066
1126
1127
+
1067
1128
def test_templated_sections ():
1068
1129
doc = SphinxClassDoc (None , class_doc_txt ,
1069
1130
config = {'template' : jinja2 .Template ('{{examples}}{{parameters}}' )})
@@ -1087,8 +1148,6 @@ def test_templated_sections():
1087
1148
""" )
1088
1149
1089
1150
1090
-
1091
-
1092
1151
if __name__ == "__main__" :
1093
1152
import nose
1094
1153
nose .run ()
0 commit comments