def collect(self): from xdoctest import core encoding = self.config.getini("xdoctest_encoding") text = self.fspath.read_text(encoding) filename = str(self.fspath) name = self.fspath.basename global_namespace = {'__name__': '__main__'} self._prepare_internal_config() style = self.config.getvalue('xdoctest_style') _example_iter = core.parse_docstr_examples(text, name, fpath=filename, style=style) for example in _example_iter: example.global_namespace.update(global_namespace) example.config.update(self._examp_conf) if hasattr(XDoctestItem, 'from_parent'): yield XDoctestItem.from_parent(self, name=name, example=example) else: # direct construction is deprecated yield XDoctestItem(name, self, example)
def doctest_callable(func): """ Executes doctests an in-memory function or class. Args: func (callable): live method or class for which we will run its doctests. Example: >>> def inception(text): >>> ''' >>> Example: >>> >>> inception("I heard you liked doctests") >>> ''' >>> print(text) >>> func = inception >>> doctest_callable(func) """ from xdoctest.core import parse_docstr_examples doctests = list(parse_docstr_examples(func.__doc__, callname=func.__name__)) # TODO: can this be hooked up into runner to get nice summaries? for doctest in doctests: # FIXME: each doctest needs a way of getting the globals of the scope # that the parent function was defined in. # HACK: to add module context, this might not be robust. doctest.module = sys.modules[func.__module__] doctest.global_namespace[func.__name__] = func doctest.run(verbose=3)
def test_parse_syntax_error(): """ CommandLine: python testing/test_errors.py test_parse_syntax_error """ docstr = utils.codeblock( ''' Example: >>> x = 0 >>> 3 = 5 ''') info = {'callname': 'test_synerr', 'lineno': 42} # Eager parsing should cause no doctests with errors to be found # and warnings should be raised with warnings.catch_warnings(record=True) as f_warnlist: f_doctests = list(core.parse_docstr_examples(docstr, style='freeform', **info)) with warnings.catch_warnings(record=True) as g_warnlist: g_doctests = list(core.parse_docstr_examples(docstr, style='google', **info)) for w in g_warnlist: print(w.message) for w in f_warnlist: print(w.message) assert len(g_warnlist) == 1 assert len(f_warnlist) == 1 assert len(g_doctests) == 0 assert len(f_doctests) == 0 # Google style can find doctests with bad syntax, but parsing them # results in an error. g_doctests2 = list(core.parse_google_docstr_examples(docstr, eager_parse=False, **info)) assert len(g_doctests2) == 1 for example in g_doctests2: with pytest.raises(exceptions.DoctestParseError): example._parse()
def collect(self): from xdoctest import core encoding = self.config.getini("xdoctest_encoding") text = self.fspath.read_text(encoding) filename = str(self.fspath) name = self.fspath.basename global_namespace = {'__name__': '__main__'} self._prepare_internal_config() style = self.config.getvalue('xdoctest_style') for example in core.parse_docstr_examples(text, name, fpath=filename, style=style): example.global_namespace.update(global_namespace) example.config.update(self._examp_conf) yield XDoctestItem(name, self, example)