コード例 #1
0
ファイル: testfile2.py プロジェクト: ipsums19/docTest
def testfile(filename, fileErrors):
    """Funció testfile() que rep per paràmetres el filename que seria el fitxer
    doctest que volem mostrar per pantalla, i el fitxer fileErrors que serà
    els links dels errors que volem mostrar. La funció crearà una instància
    de myDocTestRunner, i instanciarà un paràmetre strings que serà una llista
    d'strings els cuals l[n] i l[n+1] seran els strings en el cual el n text estarà
    entremig"""
    fitxer = open(filename, "r").read()
    l = DocTestParser().parse(fitxer)

    l1 = []
    for i in l:
        if isinstance(i, str):
            l1.append(i)
    l2 = DocTestParser().get_examples(fitxer)

    mapp = dict()
    doc = doctest.DocTest(l2, mapp, "", None, None, None)

    runner = MyDocTestRunner()
    runner.strings = l1
    runner.run(doc)

    print("\n.. Enllacos als errors")
    with open(fileErrors, "r") as f:
        print(f.read())
コード例 #2
0
ファイル: __init__.py プロジェクト: xwfgit/search-engine
    def _patched_DocFileTest(path,
                             module_relative=True,
                             package=None,
                             globs=None,
                             parser=DocTestParser(),
                             **options):
        if globs is None:
            globs = {}

        if package and not module_relative:
            raise ValueError("Package may only be specified for module-"
                             "relative paths.")

        # Relativize the path.
        if module_relative:
            package = _normalize_module(package)
            path = _module_relative_path(package, path)

        # Find the file and read it.
        name = os.path.basename(path)
        doc = open(path, 'U').read()

        # Convert it to a test, and wrap it in a DocFileCase.
        test = parser.get_doctest(doc, globs, name, path, 0)
        return DocFileCase(test, **options)
コード例 #3
0
ファイル: readme_tests.py プロジェクト: saintamh/tdds
def t():
    if sys.version_info[0] == 2:
        # This test is disabled in Python2. There are too many subtle differences in the syntax ('str' has to be renamed 'unicode',
        # 'u' prefix is needed in front of string literals, etc), it's too hacky to preserve compatibility.
        #
        # In any case this test isn't to verify that the library works in Python2, it's too check that the README is up to date
        # with the code. So it doesn't matter.
        #
        return

    readme_file_path = path.join(path.dirname(__file__), '..', 'README.md')
    with open(readme_file_path, 'rb') as file_in:
        doctest_str = '\n\n'.join(
            re.findall(
                r'```python\s+(.+?)```',
                file_in.read().decode('UTF-8'),
                flags=re.S,
            ), )
    assert doctest_str
    parser = DocTestParser()
    runner = DocTestRunner()
    runner.run(
        parser.get_doctest(
            doctest_str,
            dict(globals(), json=json, pickle=pickle),
            'README.md',
            'README.md',
            0,
        ), )
    assert runner.failures == 0
コード例 #4
0
ファイル: utils.py プロジェクト: zzk0/oneflow
def _test_docstr(docstr, verbose=True, optionflags=0, raise_on_error=True):
    parser = DocTestParser()
    if raise_on_error:
        runner = DebugRunner(verbose=verbose, optionflags=optionflags)
    else:
        runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
    test = parser.get_doctest(docstr, {}, __name__, __file__, 0)
    runner.run(test)
コード例 #5
0
ファイル: testing.py プロジェクト: Master-Alcy/MyLearningSpot
 def __init__(self, args, assignment):
     super().__init__(args, assignment)
     # The environment in which the doctests are run (global vars)
     self.good_env = {}
     self.verb = self.args.verbose
     # Initialize the doctest module objects that will do the testing/parse
     self.parser = DocTestParser()
     self.runner = DocTestRunner(verbose=self.verb, optionflags=FAIL_FAST)
     self.lines_exec = 0
     self.lines_total = 0
コード例 #6
0
 def get_parts(self):
     parser = DocTestParser()
     try:
         return parser.parse(self.doctest, self.file_path)
     except ValueError as error:
         # Output code without unicode literals needs to be normalised
         # largely for the test suite, and somewhat for the person reading
         # message.
         message = str(error).replace("u'", "'")
         self._print_message(message, 0)
         return []
コード例 #7
0
ファイル: __init__.py プロジェクト: xwfgit/search-engine
    def _patched_testfile(filename,
                          module_relative=True,
                          name=None,
                          package=None,
                          globs=None,
                          verbose=None,
                          report=True,
                          optionflags=0,
                          extraglobs=None,
                          raise_on_error=False,
                          parser=DocTestParser()):
        global master

        if package and not module_relative:
            raise ValueError("Package may only be specified for module-"
                             "relative paths.")

        # Relativize the path
        if module_relative:
            package = _normalize_module(package)
            filename = _module_relative_path(package, filename)

        # If no name was given, then use the file's name.
        if name is None:
            name = os.path.basename(filename)

        # Assemble the globals.
        if globs is None:
            globs = {}
        else:
            globs = globs.copy()
        if extraglobs is not None:
            globs.update(extraglobs)

        if raise_on_error:
            runner = DebugRunner(verbose=verbose, optionflags=optionflags)
        else:
            runner = DocTestRunner(verbose=verbose, optionflags=optionflags)

        # Read the file, convert it to a test, and run it.
        s = open(filename, 'U').read()
        test = parser.get_doctest(s, globs, name, filename, 0)
        runner.run(test)

        if report:
            runner.summarize()

        if master is None:
            master = runner
        else:
            master.merge(runner)

        return runner.failures, runner.tries
コード例 #8
0
 def __init__(self,
              baseurl='',
              apiref_baseurl='',
              glossary_baseurl='',
              doctree_parser_settings=None):
     self._doctree_parser_settings = None
     if doctree_parser_settings is None:
         # shut up
         self._doctree_parser_settings = {'report_level': 5}
     self._baseurl = baseurl
     self._apiref_baseurl = apiref_baseurl
     self._glossary_baseurl = glossary_baseurl
     self._doctest_parser = DocTestParser()
     self._reset_state()
コード例 #9
0
def test_readme_examples():
    readme_file_path = path.join(path.dirname(__file__), '..', 'README.md')
    with open(readme_file_path, 'rt', encoding='UTF-8') as file_in:
        all_blocks = re.findall(r'```(\w+)\s+(.+?)```',
                                file_in.read(),
                                flags=re.S)
    with TemporaryDirectory() as temp_dir:
        chdir(temp_dir)
        for syntax, block in all_blocks:
            if syntax == 'console':
                command_match = re.search(r'^\$ (\w+) (.+)\s+', block)
                if not command_match:
                    raise ValueError(block)
                print(command_match.group().rstrip())
                command, args = command_match.groups()
                block = block[command_match.end():]

                if command == 'cat':
                    # save the sample file to an actual file
                    file_name = args
                    with open(path.join(temp_dir, file_name),
                              'wt',
                              encoding='UTF-8') as file_out:
                        file_out.write(block)

                else:
                    # check that the command output is as expcted
                    actual_output = check_output(
                        '%s %s' % (command, args),
                        shell=True,
                        cwd=temp_dir,
                        encoding='UTF-8',
                        env={
                            **environ,
                            # `canif --help` reads this, and it can vary in the CI environment, so make it fixed
                            'COLUMNS':
                            '71',
                        },
                    )
                    print(actual_output)
                    assert actual_output == block

            elif syntax == 'python':
                parser = DocTestParser()
                test = parser.get_doctest(block, {'canif': canif}, 'README.md',
                                          'README.md', 0)
                runner = DocTestRunner()
                runner.run(test)
                assert not runner.failures
コード例 #10
0
    def _run_scripted_test(self, test):
        """Helper function to run a scripted doctest"""

        script = (XML(p)
                  for p in DocTestParser().parse(test._dt_test.docstring)
                  if isinstance(p, str) and p.strip())

        self.proxy.photos_recentlyUpdated.mock_returns = None
        self.proxy.photos_recentlyUpdated.mock_returns_iter = script

        _ = self.index.refresh()

        test._dt_test.globs['_'] = _
        test._dt_test.globs['index'] = self.index
        test._dt_test.globs['proxy'] = self.proxy
        test.runTest()
コード例 #11
0
    def __init__(self, title, raw_code, globs, locs):
        self.title = title
        self.raw_code = raw_code

        self.globs = globs
        self.locs = locs
        dt_parser = DocTestParser()
        doctests = dt_parser.get_examples(raw_code)

        if any(doctests):
            self.code = DocTest(examples=doctests,
                                globs=self.globs,
                                name=title,
                                filename=None,
                                lineno=None,
                                docstring=None)
        else:
            self.code = compile(raw_code, title, "exec")
コード例 #12
0
def print_deprecated_uses(paths):
    dep_names = set()
    dep_files = set()
    for path in sorted(paths):
        if os.path.isdir(path):
            dep_names.update(print_deprecated_uses(
                [os.path.join(path,f) for f in os.listdir(path)]))
        elif path.endswith('.py'):
            print_deprecated_uses_in(open(path).readline, path,
                                     dep_files, dep_names, 0)
        elif path.endswith('.doctest') or path.endswith('.txt'):
            for example in DocTestParser().get_examples(open(path).read()):
                ex = StringIO(example.source)
                try:
                    print_deprecated_uses_in(ex.readline, path, dep_files,
                                             dep_names, example.lineno)
                except tokenize.TokenError:
                    print(term.RED + 'Caught TokenError -- '
                           'malformatted doctest?' + term.NORMAL)
    return dep_names
コード例 #13
0
ファイル: post.py プロジェクト: mahmoud/PythonDoesBlog
def get_parts(string):
    ret = []
    import ast
    from ast import Expr, Str, Assign
    from doctest import DocTestParser, Example
    dtp = DocTestParser()
    lines = string.split("\n")
    m_ast = ast.parse(string)
    str_linestarts = [
        x.lineno for x in m_ast.body
        if isinstance(x, Expr) and isinstance(x.value, Str)
    ]
    for i, node in enumerate(m_ast.body):
        lineno = node.lineno
        if isinstance(node, Assign) and node.targets[0].id in metadata_attrs:
            continue
        elif isinstance(node, Expr) and isinstance(node.value, Str):
            for s in dtp.parse(node.value.s):
                if isinstance(s, Example):
                    if ret and isinstance(ret[-1], DocTestPart):
                        ret[-1].add(s)
                    else:
                        ret.append(DocTestPart(s))
                elif len(s.strip()) > 0:
                    ret.append(TextPart(s.strip()))
                else:
                    continue
        else:
            last_line = 0
            for subnode in ast.walk(node):
                last_line = max(getattr(subnode, 'lineno', 0), last_line)
            code_str = '\n'.join(lines[lineno - 1:last_line]).strip()
            if ret and isinstance(ret[-1], CodePart):
                ret[-1].add(code_str)
            else:
                ret.append(CodePart(code_str))

    return ret
コード例 #14
0
def _import_docstring(documenter):
    code_content = _import_docstring_code_content(documenter)
    if code_content:
        # noinspection PyBroadException
        try:
            code, content = code_content
            parser = DocTestParser()
            runner = DocTestRunner(verbose=0,
                                   optionflags=NORMALIZE_WHITESPACE | ELLIPSIS)

            glob = {}
            if documenter.modname:
                exec('from %s import *\n' % documenter.modname, glob)

            tests = parser.get_doctest(code, glob, '', '', 0)
            runner.run(tests, clear_globs=False)

            documenter.object = tests.globs[documenter.name]
            documenter.code = content
            documenter.is_doctest = True
            return True
        except Exception:
            pass
コード例 #15
0
def _import_docstring(documenter):
    if getattr(documenter.directive, 'content', None):
        # noinspection PyBroadException
        try:
            import textwrap

            content = documenter.directive.content

            def get_code(source, c=''):
                s = "\n%s" % c
                return textwrap.dedent(s.join(map(str, source)))

            is_doctest = contains_doctest(get_code(content))
            offset = documenter.directive.content_offset
            if is_doctest:
                parent, parent_offset = get_grandfather_content(content)
                parent = parent[:offset + len(content) - parent_offset]
                code = get_code(parent)
            else:
                code = get_code(content, '>>> ')

            parser = DocTestParser()
            runner = DocTestRunner(verbose=0,
                                   optionflags=NORMALIZE_WHITESPACE | ELLIPSIS)

            glob = {}
            exec('import %s as mdl\n' % documenter.modname, glob)
            glob = glob['mdl'].__dict__
            tests = parser.get_doctest(code, glob, '', '', 0)
            runner.run(tests, clear_globs=False)

            documenter.object = tests.globs[documenter.name]
            documenter.code = content
            documenter.is_doctest = True
            return True
        except:
            return False
コード例 #16
0
ファイル: manager.py プロジェクト: wadoon/pytagsfs
    def get_doc_test_cases_from_string(
      self,
      string,
      name = '<string>',
      filename = '<string>',
      globs = None,
    ):
        if globs is None:
            globs = {}

        # Make sure __name__ == '__main__' checks fail:
        globs = dict(globs)
        globs['__name__'] = None

        parser = DocTestParser()
        test = parser.get_doctest(
          string,
          globs = globs,
          name = name,
          filename = filename,
          lineno = 0,
        )
        test_case = self.make_doc_test_case(test)
        return [test_case]
コード例 #17
0
ファイル: graphex.py プロジェクト: eggee/python-siggen
def main():
    global args

    args = parse_args()
    logging.basicConfig(level=args.loglevel)

    parser = DocTestParser()
    with open(args.input) as fd:
        chunks = parser.parse(fd.read())

    excount = 0
    ctx = {}
    for chunk in chunks:
        if isinstance(chunk, Example):
            exec(chunk.source, ctx)
            if args.var in ctx:
                excount += 1
                graph(excount,
                      ctx,
                      args.var,
                      args.output,
                      width=args.width,
                      height=args.height)
                del ctx[args.var]
コード例 #18
0
ファイル: tester.py プロジェクト: tazjel/rad2py
def doctestfile(filename,
                module_relative=True,
                name=None,
                package=None,
                globs=None,
                verbose=None,
                report=True,
                optionflags=0,
                extraglobs=None,
                raise_on_error=False,
                parser=DocTestParser(),
                encoding=None,
                compileflags=None):

    text = open(filename).read()

    # If no name was given, then use the file's name.
    if name is None:
        name = os.path.basename(filename)

    # Assemble the globals.
    if globs is None:
        globs = {}
    else:
        globs = globs.copy()
    if extraglobs is not None:
        globs.update(extraglobs)

    defects = []

    class CustomDocTestRunner(DocTestRunner):
        def report_failure(self, out, test, example, got):
            text = "doctest for %s failed, want %s got %s" % (
                example.source.strip(), example.want.split()[0], got)
            lineno = example.lineno
            error = dict(summary=text,
                         type=60,
                         filename=filename,
                         lineno=lineno + 1,
                         offset=1)
            defects.append(error)

        def report_unexpected_exception(self, out, test, example, exc_info):
            text = "doctest for %s failed, exception %s" % (
                example.source.strip(), repr(exc_info[1]))
            lineno = example.lineno
            error = dict(summary=text,
                         type=80,
                         filename=filename,
                         lineno=lineno + 1,
                         offset=1)
            defects.append(error)

    runner = CustomDocTestRunner(verbose=verbose, optionflags=optionflags)

    if encoding is not None:
        text = text.decode(encoding)

    # compile and execute the file to get the global definitions
    exec compile(text, filename, "exec") in globs

    # Read the file, convert it to a test, and run it.
    tests = parser.get_doctest(text, globs, name, filename, 0)

    count, fail = runner.run(tests)

    if not count:
        wx.MessageBox("No doc tests could be run from: %s module" % filename,
                      "No doc tests found", wx.ICON_ERROR)

    for defect in defects:
        yield defect
コード例 #19
0
from cognite.client.experimental import CogniteClient


def collect_apis(obj, done):
    if done.get(obj.__class__):
        return []
    done[obj.__class__] = True
    apis = inspect.getmembers(
        obj, lambda m: isinstance(m, APIClient) and not done.get(m.__class__))
    sub = [(n + "." + sn, sa) for n, c in apis
           for sn, sa in collect_apis(c, done)]
    return apis + sub


client = CogniteClient(project="_", api_key="_", client_name="_")
parser = DocTestParser()

apis = collect_apis(client, {})

snippets = {
    "language": "Python",
    "label": "Python SDK",
    "operations": defaultdict(str)
}
filter_out = [
    "from cognite.client import CogniteClient", "c = CogniteClient()", ""
]

duplicate_operations = {
    "listAssets": "getAssets",
    "advancedListEvents": "listEvents",