コード例 #1
0
def test_namespace(capsys):
    sybil = Sybil([parse], path='./samples', pattern='*.txt')
    for document in sybil.all_documents():
        for example in document:
            print(split(example.document.path)[-1], example.line)
            example.evaluate()

    out, _ = capsys.readouterr()
    assert out.split('\n') == [
        'sample1.txt 1', '[0]', 'sample1.txt 3', '[0, 14]', 'sample2.txt 1',
        '[0]', 'sample2.txt 3', '[0, 13]', ''
    ]
コード例 #2
0
 def test_filenames(self, tmp_path):
     (tmp_path / 'foo.txt').write_text(u'')
     (tmp_path / 'bar.txt').write_text(u'')
     (tmp_path / 'baz').mkdir()
     (tmp_path / 'baz' / 'bar.txt').write_text(u'')
     sybil = Sybil([], path=str(tmp_path), filenames=['bar.txt'])
     self.check(tmp_path, sybil, expected=[['bar.txt'], ['baz', 'bar.txt']])
コード例 #3
0
ファイル: test_pytest.py プロジェクト: scoder/sybil
 def test_fnmatch_pattern(self, tmp_path):
     pytest_collect_file = Sybil(parsers=[],
                                 pattern='**/*.rst').pytest(MockFile)
     path = (tmp_path / 'test.rst')
     path.write_text(u'')
     local_path = local(path)
     assert pytest_collect_file(None, local_path).path == local_path
コード例 #4
0
ファイル: conftest.py プロジェクト: wucke13/nunavut
def _pytest_integration_that_actually_works() -> typing.Callable:
    """
    Sybil matching is pretty broken. We'll have to help it out here. The problem is that
    exclude patterns passed into the Sybil object are matched against file name stems such that
    files cannot be excluded by path.
    """

    _sy = Sybil(parsers=[
        DocTestParser(optionflags=ELLIPSIS),
        CodeBlockParser(),
    ],
                pattern='**/nunavut/**/*.py',
                excludes=[
                    '**/markupsafe/*',
                    '**/jinja2/*',
                ],
                fixtures=[
                    'jinja_filter_tester', 'gen_paths',
                    'assert_language_config_value',
                    'configurable_language_context_factory'
                ])

    def pytest_collect_file(parent: typing.Any,
                            path: typing.Any) -> typing.Optional[typing.Any]:
        if _sy.should_test_filename(str(path)):
            return NewSybilFile.from_parent(parent, fspath=path, sybil=_sy)
        else:
            return None

    return pytest_collect_file
コード例 #5
0
ファイル: conftest.py プロジェクト: SSSnow/nunavut
def _pytest_integration_that_actually_works() -> typing.Callable:
    """
    Sybil matching is pretty broken. We'll have to help it out here. The problem is that
    exclude patterns passed into the Sybil object are matched against file name stems such that
    files cannot be excluded by path.
    """

    _excludes = [
        '**/markupsafe/*',
        '**/jinja2/*',
    ]

    _sy = Sybil(parsers=[
        DocTestParser(optionflags=ELLIPSIS),
        CodeBlockParser(),
    ],
                fixtures=['jinja_filter_tester'])

    def pytest_collect_file(parent: str,
                            path: str) -> typing.Optional[SybilFile]:
        if fnmatch(path, '**/nunavut/**/*.py') and not any(
                fnmatch(path, pattern) for pattern in _excludes):
            return SybilFile(path, parent, _sy)
        else:
            return None

    return pytest_collect_file
コード例 #6
0
ファイル: test_pytest.py プロジェクト: scoder/sybil
 def test_filenames(self, tmp_path):
     pytest_collect_file = Sybil(parsers=[],
                                 filenames=['test.rst']).pytest(MockFile)
     path = (tmp_path / 'test.rst')
     path.write_text(u'')
     local_path = local(path)
     assert pytest_collect_file(None, local_path).path == local_path
コード例 #7
0
 def test_excludes(self, tmp_path):
     (tmp_path / 'foo.txt').write_text(u'')
     (tmp_path / 'bar.txt').write_text(u'')
     sybil = Sybil([],
                   path=str(tmp_path),
                   pattern='*.txt',
                   excludes=['bar.txt'])
     self.check(sybil, expected=['foo.txt'])
コード例 #8
0
ファイル: test_sybil.py プロジェクト: lamby/sybil
 def test_all_paths_with_base_directory(self):
     sybil = Sybil([parse_for_x, parse_for_y],
                   path='./samples', pattern='*.txt')
     assert (self._evaluate_examples(self._all_examples(sybil), 42) ==
             ['X count was 4, as expected',
              'Y count was 3, as expected',
              'X count was 3 instead of 4',
              'Y count was 3, as expected'])
コード例 #9
0
 def test_explicit_encoding(self, tmp_path):
     (tmp_path / 'encoded.txt').write_text(u'X 1 check\n\xa3',
                                           encoding='charmap')
     sybil = Sybil([parse_for_x],
                   path=str(tmp_path),
                   pattern='*.txt',
                   encoding='charmap')
     assert (self._evaluate_examples(self._all_examples(sybil),
                                     42) == ['X count was 1, as expected'])
コード例 #10
0
ファイル: test_pytest.py プロジェクト: scoder/sybil
    def test_fnmatch_patterns(self, tmp_path):
        pytest_collect_file = Sybil(parsers=[],
                                    patterns=['*.rst',
                                              '*.py']).pytest(MockFile)
        rst_path = (tmp_path / 'test.rst')
        rst_path.write_text(u'')
        py_path = (tmp_path / 'test.py')
        py_path.write_text(u'')

        local_path = local(rst_path)
        assert pytest_collect_file(None, local_path).path == local_path

        local_path = local(py_path)
        assert pytest_collect_file(None, local_path).path == local_path
コード例 #11
0
 def test_glob_patterns(self, tmp_path):
     (tmp_path / 'middle').mkdir()
     interesting = (tmp_path / 'middle' / 'interesting')
     interesting.mkdir()
     boring = (tmp_path / 'middle' / 'boring')
     boring.mkdir()
     (interesting / 'foo.txt').write_text(u'')
     (boring / 'bad1.txt').write_text(u'')
     (tmp_path / 'bad2.txt').write_text(u'')
     sybil = Sybil([],
                   path=str(tmp_path),
                   pattern='**middle/*.txt',
                   excludes=['**/boring/*.txt'])
     self.check(tmp_path,
                sybil,
                expected=[['middle', 'interesting', 'foo.txt']])
コード例 #12
0
    )

    doctest_namespace["spin_system_1H_13C"] = SpinSystem(sites=[site1, site2])
    doctest_namespace["spin_systems"] = SpinSystem(sites=[site1, site2, site3])

    spin_systems = [SpinSystem(sites=[site]) for site in [site1, site2, site3]]
    sim = Simulator()
    sim.spin_systems += spin_systems
    doctest_namespace["sim"] = sim

    # Transitions
    t1 = Transition(initial=[0.5, 0.5], final=[0.5, -0.5])
    doctest_namespace["t1"] = t1

    t2 = Transition(initial=[0.5, 0.5], final=[-0.5, 0.5])
    doctest_namespace["t2"] = t2

    path = TransitionPathway([t1, t2])
    doctest_namespace["path"] = path


pytest_collect_file = Sybil(
    parsers=[
        PythonCodeBlockParser(),
        PythonPlotParser(),
        skip,
    ],
    patterns=["*.rst"],
    fixtures=["add_site", "test_models"],
).pytest()
コード例 #13
0
    with io.open(literalinclude_python, 'r', encoding='utf-8') as f:
        filecontent = f.read()
        exec(filecontent)


def parse_literalinclude_python_blocks(document):
    for start_match, end_match, source in document.find_region_sources(
        LITERALPYTHON_START, LITERALPYTHON_END
    ):
        literalinclude_python = start_match.groups()[0].strip()

        if any([fnmatch.fnmatch(literalinclude_python, ignore_pattern) for ignore_pattern in IGNORES]):
            continue

        print(literalinclude_python)
        yield Region(start_match.start(), end_match.end(),
                     literalinclude_python, evaluate_literalinclude_python_blocks)


pytest_collect_file = Sybil(
    parsers=[
        DocTestParser(),
        CodeBlockParser(),
        parse_literalinclude_python_blocks
    ],
    pattern='*.rst',
    fixtures=[],
    excludes=[]
).pytest()
コード例 #14
0
ファイル: conftest.py プロジェクト: jammykam/scrapy
from doctest import ELLIPSIS

from sybil import Sybil
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.doctest import DocTestParser
from sybil.parsers.skip import skip


pytest_collect_file = Sybil(
    parsers=[
        DocTestParser(optionflags=ELLIPSIS),
        CodeBlockParser(future_imports=['print_function']),
        skip,
    ],
    pattern='*.rst',
).pytest()
コード例 #15
0
ファイル: test_sybil.py プロジェクト: lamby/sybil
 def test_parse(self):
     sybil = Sybil([parse_for_x, parse_for_y], '*')
     document = sybil.parse(sample_path('sample1.txt'))
     assert (self._evaluate_examples(document, 42) ==
             ['X count was 4, as expected',
              'Y count was 3, as expected'])
コード例 #16
0
from doctest import ELLIPSIS

import pytest

from sybil import Sybil
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.doctest import DocTestParser, FIX_BYTE_UNICODE_REPR
from sybil.parsers.skip import skip

pytest_collect_file = Sybil(
    parsers=[
        DocTestParser(optionflags=ELLIPSIS | FIX_BYTE_UNICODE_REPR),
        CodeBlockParser(),
        skip,
    ],
    pattern='*.rst',
).pytest()
コード例 #17
0
ファイル: conftest.py プロジェクト: gitter-badger/hissp
            parser.compiler.ns = example.namespace
            hissp = parser.reads(lissp)
            compiled = parser.compiler.compile(hissp) + "\n"
            assert norm_gensym_eq(compiled, python), dedent(
                f"""
                EXPECTED PYTHON:
                {indent(python, "  ")}
                ACTUALLY COMPILED TO:
                {indent(compiled, "  ")}
                .
                """
            )
        return super().evaluate(example)


def norm_gensym_eq(compiled, python):
    """The special gensym suffix ``xAUTO..._`` will match any number."""
    return re.fullmatch(re.sub(r'xAUTO\\\.\\\.\\\._', r'xAUTO\\d+_', re.escape(python)), compiled)

class Globs(Container):
    def __init__(self, *globs):
        self.globs = globs

    def __contains__(self, item):
        return any(fnmatch(item, glob) for glob in self.globs)


pytest_collect_file = Sybil(
    parsers=[ParseLissp(optionflags=ELLIPSIS)], filenames=Globs("*.md", "*.rst")
).pytest()
コード例 #18
0
            raise AssertionError(msg)
        return rendered

    return _make_filter_test_template


_sy = Sybil(
    parsers=[
        DocTestParser(optionflags=ELLIPSIS),
        CodeBlockParser(),
    ],
    pattern='**/*',
    excludes=[
        '**/markupsafe/*',
        '**/jinja2/*',
        '**/static/*',
        '**/.*/*',
        '**/.*',
        '**/CONTRIBUTING.rst',
        '**/verification/*',
        '**/prof/*'
    ],
    fixtures=['jinja_filter_tester',
              'gen_paths',
              'assert_language_config_value',
              'configurable_language_context_factory']
)


pytest_collect_file = _sy.pytest()
コード例 #19
0
ファイル: conftest.py プロジェクト: rnth/mush
from doctest import REPORT_NDIFF, ELLIPSIS

from sybil import Sybil
from sybil.parsers.capture import parse_captures
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.doctest import DocTestParser

from mush.compat import PY2

sybil_collector = Sybil(
    parsers=[
        DocTestParser(optionflags=REPORT_NDIFF | ELLIPSIS),
        CodeBlockParser(),
        parse_captures,
    ],
    pattern='*.txt',
).pytest()


def pytest_collect_file(parent, path):
    if not PY2:
        return sybil_collector(parent, path)
コード例 #20
0
from doctest import REPORT_NDIFF, ELLIPSIS

from sybil import Sybil
from sybil.parsers.doctest import DocTestParser, FIX_BYTE_UNICODE_REPR
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.capture import parse_captures

from testfixtures.sybil import FileParser

pytest_collect_file = Sybil(
    parsers=[
        DocTestParser(optionflags=REPORT_NDIFF | ELLIPSIS
                      | FIX_BYTE_UNICODE_REPR),
        CodeBlockParser(['print_function']),
        parse_captures,
        FileParser('tempdir'),
    ],
    pattern='*.txt',
).pytest()
コード例 #21
0
ファイル: conftest.py プロジェクト: shantikumar/Web-Scraper
import os
from doctest import ELLIPSIS, NORMALIZE_WHITESPACE

from scrapy.http.response.html import HtmlResponse
from sybil import Sybil
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.doctest import DocTestParser
from sybil.parsers.skip import skip


def load_response(url, filename):
    input_path = os.path.join(os.path.dirname(__file__), '_tests', filename)
    with open(input_path, 'rb') as input_file:
        return HtmlResponse(url, body=input_file.read())


def setup(namespace):
    namespace['load_response'] = load_response


pytest_collect_file = Sybil(
    parsers=[
        DocTestParser(optionflags=ELLIPSIS | NORMALIZE_WHITESPACE),
        CodeBlockParser(future_imports=['print_function']),
        skip,
    ],
    pattern='*.rst',
    setup=setup,
).pytest()
コード例 #22
0
        DocTestParser.__init__(self, optionflags=optionflags)
        self.runner._checker = SbtOutputChecker()

    def evaluate(self, sybil_example: Example) -> str:
        example = sybil_example.parsed
        namespace = sybil_example.namespace
        output = []
        mod_name = sybil_example.path.rsplit(sep=".", maxsplit=1)[0]
        mod_name = mod_name.rsplit(sep="\\", maxsplit=1)[1]
        self.runner._checker.mod_name = mod_name

        self.runner.run(DocTest([example],
                                namespace,
                                name=None,
                                filename=None,
                                lineno=example.lineno,
                                docstring=None),
                        clear_globs=False,
                        out=output.append)
        # print(f'{self.runner._checker.msgs=}')
        self.runner._checker.msgs = []
        return ''.join(output)


pytest_collect_file = Sybil(parsers=[
    SbtDocTestParser(optionflags=ELLIPSIS),
    PythonCodeBlockParser(),
],
                            patterns=['*.rst', '*.py'],
                            excludes=['log_verifier.py']).pytest()
コード例 #23
0
ファイル: conftest.py プロジェクト: kwx4github/pycantonese
"""

from doctest import ELLIPSIS
from os import chdir, getcwd
from shutil import rmtree
from tempfile import mkdtemp

import pytest
from sybil import Sybil
from sybil.parsers.doctest import DocTestParser
from sybil.parsers.skip import skip


@pytest.fixture(scope="module")
def tempdir():
    path = mkdtemp()
    cwd = getcwd()
    try:
        chdir(path)
        yield path
    finally:
        chdir(cwd)
        rmtree(path)


pytest_collect_file = Sybil(
    parsers=[DocTestParser(optionflags=ELLIPSIS), skip],
    pattern="*.rst",
    fixtures=["tempdir"],
).pytest()
コード例 #24
0
ファイル: conftest.py プロジェクト: jmaggio14/imagepypelines
from os import chdir, getcwd
from shutil import rmtree
from tempfile import mkdtemp
import pytest
from sybil import Sybil
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.doctest import DocTestParser
from doctest import ELLIPSIS
import doctest

doctest.ELLIPSIS_MARKER = '[...]'

# run all code samples in the website blocks
pytest_collect_file = Sybil(
    parsers=[
        DocTestParser(optionflags=ELLIPSIS),
        CodeBlockParser(),
    ],
    pattern='*.rst',
).pytest()
コード例 #25
0
        return message


def parse_for(letter, document):
    for m in re.finditer(r'(%s+) (\d+) check' % letter, document.text):
        yield Region(m.start(), m.end(), (m.group(1), int(m.group(2))),
                     partial(check, letter))


def sybil_setup(namespace):
    print('sybil setup', end=' ')
    namespace['x'] = 0


def sybil_teardown(namespace):
    print('sybil teardown', namespace['x'])


pytest_collect_file = Sybil(parsers=[
    partial(parse_for, 'X'),
    partial(parse_for, 'Y'),
    CodeBlockParser(['print_function'])
],
                            pattern='*.rst',
                            setup=sybil_setup,
                            teardown=sybil_teardown,
                            fixtures=[
                                'function_fixture', 'class_fixture',
                                'module_fixture', 'session_fixture'
                            ]).pytest()
コード例 #26
0
from doctest import ELLIPSIS
from sybil import Sybil
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.doctest import DocTestParser, FIX_BYTE_UNICODE_REPR
from sybil.parsers.skip import skip
import pytest


@pytest.fixture(scope="session")
def rows_equal():
    def _rows_equal(r1, r2):
        return r1.asDict(recursive=True) == r2.asDict(recursive=True)
    return _rows_equal

pytest_collect_file = Sybil(
    parsers=[
        CodeBlockParser(future_imports=['print_function']),
    ],
    pattern='*.rst',
    fixtures=['rows_equal', 'spark'],
).pytest()
コード例 #27
0
ファイル: test_sybil.py プロジェクト: lamby/sybil
 def test_all_paths(self):
     sybil = Sybil([parse_first_line], '__init__.py')
     assert ([e.region.parsed for e in self._all_examples(sybil)] ==
             ['# believe it or not,'])
コード例 #28
0
from os import chdir, getcwd
from shutil import rmtree
from tempfile import mkdtemp
from sybil import Sybil
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.doctest import DocTestParser


def sybil_setup(namespace):
    # there are better ways to do temp directories, but it's a simple example:
    namespace['path'] = path = mkdtemp()
    namespace['cwd'] = getcwd()
    chdir(path)


def sybil_teardown(namespace):
    chdir(namespace['cwd'])
    rmtree(namespace['path'])


load_tests = Sybil(parsers=[
    DocTestParser(),
    CodeBlockParser(future_imports=['print_function']),
],
                   path='../docs',
                   pattern='*.rst',
                   setup=sybil_setup,
                   teardown=sybil_teardown).unittest()
コード例 #29
0
ファイル: conftest.py プロジェクト: chaaklau/pycantonese
"""

from doctest import NORMALIZE_WHITESPACE
from os import chdir, getcwd
from shutil import rmtree
from tempfile import mkdtemp

import pytest
from sybil import Sybil
from sybil.parsers.doctest import DocTestParser
from sybil.parsers.skip import skip


@pytest.fixture(scope="module")
def tempdir():
    path = mkdtemp()
    cwd = getcwd()
    try:
        chdir(path)
        yield path
    finally:
        chdir(cwd)
        rmtree(path)


pytest_collect_file = Sybil(
    parsers=[DocTestParser(optionflags=NORMALIZE_WHITESPACE), skip],
    pattern="*.rst",
    fixtures=["tempdir"],
).pytest()
コード例 #30
0
ファイル: test_nose.py プロジェクト: nicoddemus/sybil
    if actual != expected:
        message = '{} count was {} instead of {}'.format(
            letter, actual, expected
        )
        if letter=='X':
            raise ValueError(message)
        return message


def parse_for(letter, document):
    for m in re.finditer('(%s+) (\d+) check' % letter, document.text):
        yield Region(m.start(), m.end(),
                     (m.group(1), int(m.group(2))),
                     partial(check, letter))


def sybil_setup(namespace):
    print('sybil setup')
    namespace['x'] = 0


def sybil_teardown(namespace):
    print('sybil teardown', namespace['x'])


load_tests = Sybil(
    [partial(parse_for, 'X'), partial(parse_for, 'Y')],
    path='../pytest', pattern='*.rst',
    setup=sybil_setup, teardown=sybil_teardown
).nose()