File: test_self_doctests.py

package info (click to toggle)
pycodestyle 2.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 624 kB
  • sloc: python: 4,697; makefile: 135; sh: 12
file content (38 lines) | stat: -rw-r--r-- 975 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import re

import pytest

import pycodestyle
from testing.support import errors_from_src

SELFTEST_REGEX = re.compile(r'\b(Okay|[EW]\d{3}): (.*)')


def get_tests():
    ret = [
        pytest.param(
            match[1],
            match[2],
            id=f'pycodestyle.py:{f.__code__.co_firstlineno}:{f.__name__}@{i}',
        )
        for group in pycodestyle._checks.values()
        for f in group
        if f.__doc__ is not None
        for i, match in enumerate(SELFTEST_REGEX.finditer(f.__doc__))
    ]
    assert ret
    return tuple(ret)


@pytest.mark.parametrize(('expected', 's'), get_tests())
def test(expected, s):
    s = '\n'.join((*s.replace(r'\t', '\t').split(r'\n'), ''))
    errors = errors_from_src(s)
    if expected == 'Okay':
        assert errors == []
    else:
        for error in errors:
            if error.startswith(f'{expected}:'):
                break
        else:
            raise AssertionError(f'expected {expected} from {s!r}')