Python doctest.Example() Examples
The following are 30
code examples of doctest.Example().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
doctest
, or try the search function
.
Example #1
Source File: test_doctest.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def old_test2(): r""" >>> from doctest import Tester >>> t = Tester(globs={}, verbose=1) >>> test = r''' ... # just an example ... >>> x = 1 + 2 ... >>> x ... 3 ... ''' >>> t.runstring(test, "Example") Running string Example Trying: x = 1 + 2 Expecting nothing ok Trying: x Expecting: 3 ok 0 of 2 examples failed in string Example TestResults(failed=0, attempted=2) """
Example #2
Source File: test_doctest.py From medicare-demo with Apache License 2.0 | 6 votes |
def old_test2(): r""" >>> from doctest import Tester >>> t = Tester(globs={}, verbose=1) >>> test = r''' ... # just an example ... >>> x = 1 + 2 ... >>> x ... 3 ... ''' >>> t.runstring(test, "Example") Running string Example Trying: x = 1 + 2 Expecting nothing ok Trying: x Expecting: 3 ok 0 of 2 examples failed in string Example (0, 2) """
Example #3
Source File: test_doctest.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
def old_test2(): r""" >>> from doctest import Tester >>> t = Tester(globs={}, verbose=1) >>> test = r''' ... # just an example ... >>> x = 1 + 2 ... >>> x ... 3 ... ''' >>> t.runstring(test, "Example") Running string Example Trying: x = 1 + 2 Expecting nothing ok Trying: x Expecting: 3 ok 0 of 2 examples failed in string Example TestResults(failed=0, attempted=2) """
Example #4
Source File: iterators.py From quick-nlp with MIT License | 6 votes |
def pad(self, example: Example, max_sl: int, max_conv: int, field: Field, target_roles: Optional[Roles] = None) -> \ Tuple[Conversations, Lengths, Roles]: """Pad a hierarchical example to the max sequence length and max conv length provided. Optionally if target_roles parameter is provided every sentence whose role, found from example.roles, is not matching the target_roles will be padded completely. """ indices = [0] + np.cumsum(example.sl).tolist() minibatch = self.get_minibatch_text(example, indices, backwards=self.backwards) field.fix_length = max_sl field.include_lengths = True padded, lens = field.pad(minibatch=minibatch) padded_roles = list(example.roles) padded_sentence = [field.pad_token for _ in range(max_sl)] if target_roles is not None: padded = [p if r in target_roles else padded_sentence for p, r in zip(padded, padded_roles)] for _ in range(max_conv - len(padded)): padded.append(padded_sentence) lens.append(0) padded_roles.append(field.pad_token) return padded, lens, padded_roles
Example #5
Source File: iterators.py From quick-nlp with MIT License | 6 votes |
def pad(self, example: Example, max_sl: int, max_conv: int, field: Field, target_roles: Optional[Roles] = None) -> \ Tuple[Conversations, Lengths, Roles]: """Pad a hierarchical example to the max sequence length and max conv length provided. Optionally if target_roles parameter is provided every sentence whose role, found from example.roles, is not matching the target_roles will be padded completely. """ indices = [0] + np.cumsum(example.sl).tolist() minibatch = self.get_minibatch_text(example, indices, backwards=self.backwards) field.fix_length = max_sl field.include_lengths = True padded, lens = field.pad(minibatch=minibatch) padded_roles = list(example.roles) padded_sentence = [field.pad_token for _ in range(max_sl)] if target_roles is not None: padded = [p if r in target_roles else padded_sentence for p, r in zip(padded, padded_roles)] for _ in range(max_conv - len(padded)): padded.append(padded_sentence) lens.append(0) padded_roles.append(field.pad_token) return padded, lens, padded_roles
Example #6
Source File: test_doctest.py From oss-ftp with MIT License | 6 votes |
def old_test2(): r""" >>> from doctest import Tester >>> t = Tester(globs={}, verbose=1) >>> test = r''' ... # just an example ... >>> x = 1 + 2 ... >>> x ... 3 ... ''' >>> t.runstring(test, "Example") Running string Example Trying: x = 1 + 2 Expecting nothing ok Trying: x Expecting: 3 ok 0 of 2 examples failed in string Example TestResults(failed=0, attempted=2) """
Example #7
Source File: testing.py From ok-client with Apache License 2.0 | 6 votes |
def get_suite_examples(self, suite, case): # suite/case specified, so only parse relevant text into Examples exs = collections.OrderedDict() case_ex = collections.OrderedDict() # get the shared lines that should impact all the cases in the suite. shrd_txt = self.shared_case_data[suite] if shrd_txt: parse_shared = self.parser.parse(shrd_txt.group(0), self.tstfile_name) shrd_ex = [i for i in parse_shared if isinstance(i, Example)] if shrd_ex: case_ex['shared'] = shrd_ex if case: if str(case[0]) not in self.data[suite]: raise KeyError parsed_temp_examples = self.parser.parse(self.data[suite][case[0]], self.tstfile_name) case_examples = [i for i in parsed_temp_examples if isinstance(i, Example)] case_ex[str(case[0])] = case_examples else: for itemcase in self.data[suite].keys(): parsed_temp_examples = self.parser.parse(self.data[suite][itemcase], self.tstfile_name) case_examples = [i for i in parsed_temp_examples if isinstance(i, Example)] case_ex[itemcase] = case_examples exs[suite] = case_ex return exs
Example #8
Source File: test_doctest.py From BinderFilter with MIT License | 6 votes |
def old_test2(): r""" >>> from doctest import Tester >>> t = Tester(globs={}, verbose=1) >>> test = r''' ... # just an example ... >>> x = 1 + 2 ... >>> x ... 3 ... ''' >>> t.runstring(test, "Example") Running string Example Trying: x = 1 + 2 Expecting nothing ok Trying: x Expecting: 3 ok 0 of 2 examples failed in string Example TestResults(failed=0, attempted=2) """
Example #9
Source File: testing.py From ok-client with Apache License 2.0 | 6 votes |
def get_all_examples(self): # no suite/case flag, so parses all text into Example objects exs = collections.OrderedDict() for sui in self.data.keys(): case_ex = collections.OrderedDict() # get the shared lines that should impact all the cases in the suite. shrd_txt = self.shared_case_data[sui] if shrd_txt: parse_shared = self.parser.parse(shrd_txt.group(0), self.tstfile_name) shrd_ex = [i for i in parse_shared if isinstance(i, Example)] if shrd_ex: case_ex['shared'] = shrd_ex for itemcase in self.data[sui].keys(): parsed_temp_examples = self.parser.parse(self.data[sui][itemcase], self.tstfile_name) case_examples = [i for i in parsed_temp_examples if isinstance(i, Example)] case_ex[itemcase] = case_examples exs[sui] = case_ex return exs # catch inf loops/ recur err
Example #10
Source File: testing.py From ok-client with Apache License 2.0 | 6 votes |
def run_examples(self, exs): # runs the Example objects, keeps track of right/wrong etc total_failed = 0 total_attempted = 0 case = 'shared' for sui in exs.keys(): if not total_failed: final_env = dict(self.good_env) if 'shared' in exs[sui].keys(): dtest = DocTest(exs[sui]['shared'], self.good_env, 'shared', None, None, None) result = self.runner.run(dtest, clear_globs=False) # take the env from shared dtest and save it for other exs final_env = dict(self.good_env, **dtest.globs) total_failed += result.failed total_attempted += result.attempted for case in exs[sui].keys(): if case != 'shared': if not total_failed: example_name = "Suite {}, Case {}".format(sui, case) dtest = DocTest(exs[sui][case], final_env, example_name, None, None, None) result = self.runner.run(dtest) total_failed += result.failed total_attempted += result.attempted return total_failed, total_attempted
Example #11
Source File: test_doctest.py From ironpython2 with Apache License 2.0 | 6 votes |
def old_test2(): r""" >>> from doctest import Tester >>> t = Tester(globs={}, verbose=1) >>> test = r''' ... # just an example ... >>> x = 1 + 2 ... >>> x ... 3 ... ''' >>> t.runstring(test, "Example") Running string Example Trying: x = 1 + 2 Expecting nothing ok Trying: x Expecting: 3 ok 0 of 2 examples failed in string Example TestResults(failed=0, attempted=2) """
Example #12
Source File: test_doctest.py From CTFCrackTools with GNU General Public License v3.0 | 6 votes |
def old_test2(): r""" >>> from doctest import Tester >>> t = Tester(globs={}, verbose=1) >>> test = r''' ... # just an example ... >>> x = 1 + 2 ... >>> x ... 3 ... ''' >>> t.runstring(test, "Example") Running string Example Trying: x = 1 + 2 Expecting nothing ok Trying: x Expecting: 3 ok 0 of 2 examples failed in string Example TestResults(failed=0, attempted=2) """
Example #13
Source File: compat.py From conllu with MIT License | 5 votes |
def modify_example(example): new_want = Python2DocTestParser.add_u_before_strings(example.want) example = doctest.Example( source=example.source, want=new_want, exc_msg=example.exc_msg, lineno=example.lineno, indent=example.indent, options=example.options ) return example
Example #14
Source File: ok.py From Gofer-Grader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def run_doctest(name, doctest_string, global_environment): """ Run a single test with given global_environment. Returns (True, '') if the doctest passes. Returns (False, failure_message) if the doctest fails. """ examples = doctest.DocTestParser().parse( doctest_string, name ) test = doctest.DocTest( [e for e in examples if isinstance(e, doctest.Example)], global_environment, name, None, None, doctest_string ) doctestrunner = doctest.DocTestRunner(verbose=True) runresults = io.StringIO() with redirect_stdout(runresults), redirect_stderr(runresults), hide_outputs(): doctestrunner.run(test, clear_globs=False) with open(os.devnull, 'w') as f, redirect_stderr(f), redirect_stdout(f): result = doctestrunner.summarize(verbose=True) # An individual test can only pass or fail if result.failed == 0: return (True, '') else: return False, runresults.getvalue()
Example #15
Source File: iterators.py From quick-nlp with MIT License | 5 votes |
def get_minibatch_text(self, example: Example, indices: List[int], backwards: bool = False) -> List[List[str]]: minibatch = [example.text[indices[index]:indices[index + 1]] for index in range(len(indices) - 1)] if backwards: minibatch = [i[::-1] for i in minibatch] return minibatch
Example #16
Source File: iterators.py From quick-nlp with MIT License | 5 votes |
def get_minibatch_text(self, example: Example, indices: List[int], backwards: bool = False) -> List[List[str]]: minibatch = [example.text[indices[index]:indices[index + 1]] for index in range(len(indices) - 1)] if backwards: minibatch = [i[::-1] for i in minibatch] return minibatch
Example #17
Source File: unittest.py From gmail-filters with MIT License | 5 votes |
def assertXmlEqual(self, got, want): checker = LXMLOutputChecker() if not checker.check_output(want, got, 0): message = checker.output_difference(Example("", want), got, 0) raise AssertionError(message)
Example #18
Source File: gofer.py From otter-grader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def run_doctest(name, doctest_string, global_environment): """ Run a single test with given global_environment. Returns (True, '') if the doctest passes. Returns (False, failure_message) if the doctest fails. Args: name (str): Name of doctest doctest_string (str): Doctest in string form global_environment (dict of str: str): Global environment resulting from the execution of a python script/notebook Returns: (bool, str): Results from running the test """ examples = doctest.DocTestParser().parse( doctest_string, name ) test = doctest.DocTest( [e for e in examples if isinstance(e, doctest.Example)], global_environment, name, None, None, doctest_string ) doctestrunner = doctest.DocTestRunner(verbose=True) runresults = io.StringIO() with redirect_stdout(runresults), redirect_stderr(runresults), hide_outputs(): doctestrunner.run(test, clear_globs=False) with open(os.devnull, 'w') as f, redirect_stderr(f), redirect_stdout(f): result = doctestrunner.summarize(verbose=True) # An individual test can only pass or fail if result.failed == 0: return (True, '') else: return False, runresults.getvalue()
Example #19
Source File: iterators.py From quick-nlp with MIT License | 5 votes |
def process_minibatch(self, minibatch: List[Example]) -> Tuple[LT, LT, LT]: max_sl = max([max(ex.sl) for ex in minibatch]) max_conv = max([len(ex.roles) for ex in minibatch]) padded_examples, padded_targets, padded_lengths, padded_roles = [], [], [], [] for example in minibatch: examples, lens, roles = self.pad(example, max_sl=max_sl, max_conv=max_conv, field=self.text_field) padded_examples.extend(examples) padded_lengths.extend(lens) padded_roles.append(roles) # if self.target_roles is not None we will pad the roles we do not want to train on # this allows for learning only the responses we are interested in targets, *_ = self.pad(example, max_sl=max_sl, max_conv=max_conv, field=self.text_field, target_roles=self.target_roles) padded_targets.extend(targets) self.text_field.include_lengths = False data = self.text_field.numericalize(padded_examples, device=self.device, train=self.train) batch_size = len(minibatch) assert_dims(data, [max_sl, max_conv * batch_size]) data = data.view(max_sl, batch_size, max_conv).transpose(2, 0).transpose(2, 1).contiguous() source = data[:-1] # we remove the extra padding sentence added here targets = self.text_field.numericalize(padded_targets, device=self.device, train=self.train) targets = targets.view(max_sl, batch_size, max_conv).transpose(2, 0).transpose(2, 1).contiguous() # shapes will be max_conv -1 , max_sl, batch_size assert_dims(source, [max_conv - 1, max_sl, batch_size]) assert_dims(targets, [max_conv, max_sl, batch_size]) return source, targets[1:], targets[1:, 1:]
Example #20
Source File: doctest_webapp.py From mishkal with GNU General Public License v3.0 | 5 votes |
def parse(self, string, name='<string>'): """ Divide the given string into examples and intervening text, and return them as a list of alternating Examples and strings. Line numbers for the Examples are 0-based. The optional argument `name` is a name identifying this string, and is only used for error messages. """ string = string.expandtabs() # If all lines begin with the same indentation, then strip it. min_indent = self._min_indent(string) if min_indent > 0: string = '\n'.join([l[min_indent:] for l in string.split('\n')]) output = [] charno, lineno = 0, 0 # Find all doctest examples in the string: for m in self._EXAMPLE_RE.finditer(string): # Add the pre-example text to `output`. output.append(string[charno:m.start()]) # Update lineno (lines before this example) lineno += string.count('\n', charno, m.start()) # Extract info from the regexp match. (source, options, want, exc_msg) = \ self._parse_example(m, name, lineno) # Create an Example, and add it to the list. if not self._IS_BLANK_OR_COMMENT(source): # @@: Erg, this is the only line I need to change... output.append(doctest.Example( source, want, exc_msg, lineno=lineno, indent=min_indent+len(m.group('indent') or m.group('runindent')), options=options)) # Update lineno (lines inside this example) lineno += string.count('\n', m.start(), m.end()) # Update charno. charno = m.end() # Add any remaining post-example text to `output`. output.append(string[charno:]) return output
Example #21
Source File: test_readme.py From conllu with MIT License | 5 votes |
def modify_example(example): new_source = example.source new_want = example.want # README is formatted without "..." before multi-line input to make code easy to copy-paste if new_source.endswith('"""\n'): new_source += new_want + '\n"""' new_want = "" # doctest sometimes incorrectly includes markdown in returned example if new_want.endswith("```\n"): new_want = new_want[:new_want.index("```")] # README's serialize() has spaces instead of tabs to make output easier to read if new_want.startswith("# text"): new_want = re.sub(r" {2,}", "\t", new_want) new_want = new_want.rstrip() + "\n\n" # README cheats and prints return value without quotes new_want = repr(new_want) # README has examples with lists formatted in multiple lines to make them easier to read if new_want.startswith(("[", "Token([", "Metadata([")): new_want = ReadmeTestParser.normalize_whitespace(new_want) example = doctest.Example( source=new_source, want=new_want, exc_msg=example.exc_msg, lineno=example.lineno, indent=example.indent, options=example.options ) return example
Example #22
Source File: test_integration.py From cr8 with MIT License | 5 votes |
def parse(self, string, name='<string>'): r = super().parse(string, name) for s in r: if isinstance(s, doctest.Example): s.source = transform(s.source) return r
Example #23
Source File: rundoctests.py From cysignals with GNU Lesser General Public License v3.0 | 5 votes |
def parse(self, *args, **kwargs): examples = DocTestParser.parse(self, *args, **kwargs) for example in examples: if not isinstance(example, Example): continue if any(flag in example.options for flag in skipflags): example.options[SKIP] = True return examples
Example #24
Source File: ipdoctest.py From Computable with MIT License | 5 votes |
def __init__(self, source, want, exc_msg=None, lineno=0, indent=0, options=None): # Parent constructor doctest.Example.__init__(self,source,want,exc_msg,lineno,indent,options) # An EXTRA newline is needed to prevent pexpect hangs self.source += '\n'
Example #25
Source File: test_doctest.py From oss-ftp with MIT License | 4 votes |
def test_DocTestParser(): r""" Unit tests for the `DocTestParser` class. DocTestParser is used to parse docstrings containing doctest examples. The `parse` method divides a docstring into examples and intervening text: >>> s = ''' ... >>> x, y = 2, 3 # no output expected ... >>> if 1: ... ... print x ... ... print y ... 2 ... 3 ... ... Some text. ... >>> x+y ... 5 ... ''' >>> parser = doctest.DocTestParser() >>> for piece in parser.parse(s): ... if isinstance(piece, doctest.Example): ... print 'Example:', (piece.source, piece.want, piece.lineno) ... else: ... print ' Text:', `piece` Text: '\n' Example: ('x, y = 2, 3 # no output expected\n', '', 1) Text: '' Example: ('if 1:\n print x\n print y\n', '2\n3\n', 2) Text: '\nSome text.\n' Example: ('x+y\n', '5\n', 9) Text: '' The `get_examples` method returns just the examples: >>> for piece in parser.get_examples(s): ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) The `get_doctest` method creates a Test from the examples, along with the given arguments: >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) >>> (test.name, test.filename, test.lineno) ('name', 'filename', 5) >>> for piece in test.examples: ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) """
Example #26
Source File: test_doctest.py From CTFCrackTools with GNU General Public License v3.0 | 4 votes |
def test_DocTestParser(): r""" Unit tests for the `DocTestParser` class. DocTestParser is used to parse docstrings containing doctest examples. The `parse` method divides a docstring into examples and intervening text: >>> s = ''' ... >>> x, y = 2, 3 # no output expected ... >>> if 1: ... ... print x ... ... print y ... 2 ... 3 ... ... Some text. ... >>> x+y ... 5 ... ''' >>> parser = doctest.DocTestParser() >>> for piece in parser.parse(s): ... if isinstance(piece, doctest.Example): ... print 'Example:', (piece.source, piece.want, piece.lineno) ... else: ... print ' Text:', `piece` Text: '\n' Example: ('x, y = 2, 3 # no output expected\n', '', 1) Text: '' Example: ('if 1:\n print x\n print y\n', '2\n3\n', 2) Text: '\nSome text.\n' Example: ('x+y\n', '5\n', 9) Text: '' The `get_examples` method returns just the examples: >>> for piece in parser.get_examples(s): ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) The `get_doctest` method creates a Test from the examples, along with the given arguments: >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) >>> (test.name, test.filename, test.lineno) ('name', 'filename', 5) >>> for piece in test.examples: ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) """
Example #27
Source File: test_doctest.py From ironpython2 with Apache License 2.0 | 4 votes |
def test_DocTestParser(): r""" Unit tests for the `DocTestParser` class. DocTestParser is used to parse docstrings containing doctest examples. The `parse` method divides a docstring into examples and intervening text: >>> s = ''' ... >>> x, y = 2, 3 # no output expected ... >>> if 1: ... ... print x ... ... print y ... 2 ... 3 ... ... Some text. ... >>> x+y ... 5 ... ''' >>> parser = doctest.DocTestParser() >>> for piece in parser.parse(s): ... if isinstance(piece, doctest.Example): ... print 'Example:', (piece.source, piece.want, piece.lineno) ... else: ... print ' Text:', `piece` Text: '\n' Example: ('x, y = 2, 3 # no output expected\n', '', 1) Text: '' Example: ('if 1:\n print x\n print y\n', '2\n3\n', 2) Text: '\nSome text.\n' Example: ('x+y\n', '5\n', 9) Text: '' The `get_examples` method returns just the examples: >>> for piece in parser.get_examples(s): ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) The `get_doctest` method creates a Test from the examples, along with the given arguments: >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) >>> (test.name, test.filename, test.lineno) ('name', 'filename', 5) >>> for piece in test.examples: ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) """
Example #28
Source File: test_doctest.py From android_universal with MIT License | 4 votes |
def test_DocTestParser(): r""" Unit tests for the `DocTestParser` class. DocTestParser is used to parse docstrings containing doctest examples. The `parse` method divides a docstring into examples and intervening text: >>> s = ''' ... >>> x, y = 2, 3 # no output expected ... >>> if 1: ... ... print(x) ... ... print(y) ... 2 ... 3 ... ... Some text. ... >>> x+y ... 5 ... ''' >>> parser = doctest.DocTestParser() >>> for piece in parser.parse(s): ... if isinstance(piece, doctest.Example): ... print('Example:', (piece.source, piece.want, piece.lineno)) ... else: ... print(' Text:', repr(piece)) Text: '\n' Example: ('x, y = 2, 3 # no output expected\n', '', 1) Text: '' Example: ('if 1:\n print(x)\n print(y)\n', '2\n3\n', 2) Text: '\nSome text.\n' Example: ('x+y\n', '5\n', 9) Text: '' The `get_examples` method returns just the examples: >>> for piece in parser.get_examples(s): ... print((piece.source, piece.want, piece.lineno)) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print(x)\n print(y)\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) The `get_doctest` method creates a Test from the examples, along with the given arguments: >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) >>> (test.name, test.filename, test.lineno) ('name', 'filename', 5) >>> for piece in test.examples: ... print((piece.source, piece.want, piece.lineno)) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print(x)\n print(y)\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) """
Example #29
Source File: test_doctest.py From BinderFilter with MIT License | 4 votes |
def test_DocTestParser(): r""" Unit tests for the `DocTestParser` class. DocTestParser is used to parse docstrings containing doctest examples. The `parse` method divides a docstring into examples and intervening text: >>> s = ''' ... >>> x, y = 2, 3 # no output expected ... >>> if 1: ... ... print x ... ... print y ... 2 ... 3 ... ... Some text. ... >>> x+y ... 5 ... ''' >>> parser = doctest.DocTestParser() >>> for piece in parser.parse(s): ... if isinstance(piece, doctest.Example): ... print 'Example:', (piece.source, piece.want, piece.lineno) ... else: ... print ' Text:', `piece` Text: '\n' Example: ('x, y = 2, 3 # no output expected\n', '', 1) Text: '' Example: ('if 1:\n print x\n print y\n', '2\n3\n', 2) Text: '\nSome text.\n' Example: ('x+y\n', '5\n', 9) Text: '' The `get_examples` method returns just the examples: >>> for piece in parser.get_examples(s): ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) The `get_doctest` method creates a Test from the examples, along with the given arguments: >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) >>> (test.name, test.filename, test.lineno) ('name', 'filename', 5) >>> for piece in test.examples: ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) """
Example #30
Source File: test_doctest.py From medicare-demo with Apache License 2.0 | 4 votes |
def test_DocTestParser(): r""" Unit tests for the `DocTestParser` class. DocTestParser is used to parse docstrings containing doctest examples. The `parse` method divides a docstring into examples and intervening text: >>> s = ''' ... >>> x, y = 2, 3 # no output expected ... >>> if 1: ... ... print x ... ... print y ... 2 ... 3 ... ... Some text. ... >>> x+y ... 5 ... ''' >>> parser = doctest.DocTestParser() >>> for piece in parser.parse(s): ... if isinstance(piece, doctest.Example): ... print 'Example:', (piece.source, piece.want, piece.lineno) ... else: ... print ' Text:', `piece` Text: '\n' Example: ('x, y = 2, 3 # no output expected\n', '', 1) Text: '' Example: ('if 1:\n print x\n print y\n', '2\n3\n', 2) Text: '\nSome text.\n' Example: ('x+y\n', '5\n', 9) Text: '' The `get_examples` method returns just the examples: >>> for piece in parser.get_examples(s): ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) The `get_doctest` method creates a Test from the examples, along with the given arguments: >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) >>> (test.name, test.filename, test.lineno) ('name', 'filename', 5) >>> for piece in test.examples: ... print (piece.source, piece.want, piece.lineno) ('x, y = 2, 3 # no output expected\n', '', 1) ('if 1:\n print x\n print y\n', '2\n3\n', 2) ('x+y\n', '5\n', 9) """