Commit 78b58f38 authored by Edward Loper's avatar Edward Loper

Changed Parser.get_examples() to return a list of Example objects,

rather than a list of triples.
parent 34fcb147
...@@ -493,11 +493,12 @@ class Example: ...@@ -493,11 +493,12 @@ class Example:
A single doctest example, consisting of source code and expected A single doctest example, consisting of source code and expected
output. Example defines the following attributes: output. Example defines the following attributes:
- source: The source code that should be run. It ends with a - source: A single python statement, ending in a newline iff the
newline iff the source spans more than one line. statement spans more than one line.
- want: The expected output from running the source code. If - want: The expected output from running the source code (either
not empty, then this string ends with a newline. from stdout, or a traceback in case of exception). `want`
should always end with a newline, unless no output is expected,
- lineno: The line number within the DocTest string containing - lineno: The line number within the DocTest string containing
this Example where the Example begins. This line number is this Example where the Example begins. This line number is
...@@ -550,8 +551,7 @@ class DocTest: ...@@ -550,8 +551,7 @@ class DocTest:
self.lineno = lineno self.lineno = lineno
# Parse the docstring. # Parse the docstring.
self.docstring = docstring self.docstring = docstring
examples = Parser(name, docstring).get_examples() self.examples = Parser(name, docstring).get_examples()
self.examples = [Example(*example) for example in examples]
def __repr__(self): def __repr__(self):
if len(self.examples) == 0: if len(self.examples) == 0:
...@@ -605,19 +605,11 @@ class Parser: ...@@ -605,19 +605,11 @@ class Parser:
def get_examples(self): def get_examples(self):
""" """
Return the doctest examples from the string. Extract all doctest examples, from the string, and return them
as a list of `Example` objects. Line numbers are 0-based,
This is a list of (source, want, lineno) triples, one per example because it's most common in doctests that nothing interesting
in the string. "source" is a single Python statement; it ends appears on the same line as opening triple-quote, and so the
with a newline iff the statement contains more than one first interesting line is called \"line 1\" then.
physical line. "want" is the expected output from running the
example (either from stdout, or a traceback in case of exception).
"want" always ends with a newline, unless no output is expected,
in which case "want" is an empty string. "lineno" is the 0-based
line number of the first line of "source" within the string. It's
0-based because it's most common in doctests that nothing
interesting appears on the same line as opening triple-quote,
and so the first interesting line is called "line 1" then.
>>> text = ''' >>> text = '''
... >>> x, y = 2, 3 # no output expected ... >>> x, y = 2, 3 # no output expected
...@@ -632,7 +624,7 @@ class Parser: ...@@ -632,7 +624,7 @@ class Parser:
... 5 ... 5
... ''' ... '''
>>> for x in Parser('<string>', text).get_examples(): >>> for x in Parser('<string>', text).get_examples():
... print x ... print (x.source, x.want, x.lineno)
('x, y = 2, 3 # no output expected', '', 1) ('x, y = 2, 3 # no output expected', '', 1)
('if 1:\\n print x\\n print y\\n', '2\\n3\\n', 2) ('if 1:\\n print x\\n print y\\n', '2\\n3\\n', 2)
('x+y', '5\\n', 9) ('x+y', '5\\n', 9)
...@@ -648,7 +640,7 @@ class Parser: ...@@ -648,7 +640,7 @@ class Parser:
(source, want) = self._parse_example(m, lineno) (source, want) = self._parse_example(m, lineno)
if self._IS_BLANK_OR_COMMENT.match(source): if self._IS_BLANK_OR_COMMENT.match(source):
continue continue
examples.append( (source, want, lineno) ) examples.append( Example(source, want, lineno) )
# Update lineno (lines inside this example) # Update lineno (lines inside this example)
lineno += self.string.count('\n', m.start(), m.end()) lineno += self.string.count('\n', m.start(), m.end())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment