@@ -125,6 +125,15 @@ def __init__(self, config):
125
125
def parse (self , string , name = '<string>' ):
126
126
return self .xd .parse (string )
127
127
128
+ def get_examples (self , string , name = '<string>' ):
129
+ """
130
+ Similar to doctest.DocTestParser.get_examples, only
131
+ account for the fact that individual examples
132
+ are instances of DoctestPart not doctest.Example
133
+ """
134
+ return [x for x in self .parse (string , name )
135
+ if isinstance (x , xdoctest .doctest_part .DoctestPart )]
136
+
128
137
129
138
class TestParserDropIn :
130
139
""" Test an alternative DoctestParser
@@ -139,9 +148,18 @@ def test_vanilla_parser(self):
139
148
140
149
@pytest .mark .skipif (not HAVE_XDOCTEST , reason = "needs xdoctest" )
141
150
def test_xdoctest_parser (self ):
151
+ # Note that the # of examples differ from DTParser:
152
+ # - xdoctest groups doctest lines with no 'want' output into a single
153
+ # example.
154
+ # - "examples" here are DoctestPart instances, which _almost_ quack
155
+ # like `doctest.Example` but not completely.
142
156
config = DTConfig (ParserKlass = XDParser )
143
157
runner = DebugDTRunner (config = config )
144
158
tests = DTFinder (config = config ).find (module_cases .func3 )
145
159
146
160
assert len (tests ) == 1
147
- assert len (tests [0 ].examples ) == 3
161
+ assert len (tests [0 ].examples ) == 2
162
+ assert (tests [0 ].examples [0 ].source ==
163
+ 'import numpy as np\n a = np.array([1, 2, 3, 4]) / 3'
164
+ )
165
+ assert tests [0 ].examples [1 ].source == 'print(a)'
0 commit comments