|
| 1 | +import pytest |
| 2 | +import textwrap |
| 3 | +import difflib |
| 4 | + |
| 5 | + |
| 6 | +def _strip_and_dedent(s): |
| 7 | + """For triple-quote strings""" |
| 8 | + return textwrap.dedent(s.lstrip('\n').rstrip()) |
| 9 | + |
| 10 | + |
| 11 | +def _split_and_sort(s): |
| 12 | + """For output which does not require specific line order""" |
| 13 | + return sorted(_strip_and_dedent(s).splitlines()) |
| 14 | + |
| 15 | + |
| 16 | +def _make_explanation(a, b): |
| 17 | + """The arguments are List[str]""" |
| 18 | + return ["--- actual / +++ expected"] + [line.strip('\n') for line in difflib.ndiff(a, b)] |
| 19 | + |
| 20 | + |
| 21 | +class Output(object): |
| 22 | + """Basic output and comparison""" |
| 23 | + def __init__(self, string): |
| 24 | + self.string = string |
| 25 | + self.explanation = [] |
| 26 | + |
| 27 | + def __str__(self): |
| 28 | + return self.string |
| 29 | + |
| 30 | + def __eq__(self, other): |
| 31 | + a = self.string.strip() |
| 32 | + b = _strip_and_dedent(other) |
| 33 | + if a == b: |
| 34 | + return True |
| 35 | + else: |
| 36 | + self.explanation = _make_explanation(a.splitlines(), b.splitlines()) |
| 37 | + return False |
| 38 | + |
| 39 | + |
| 40 | +class Unordered(Output): |
| 41 | + """Custom comparison for output without strict line ordering""" |
| 42 | + def __eq__(self, other): |
| 43 | + a = _split_and_sort(self.string) |
| 44 | + b = _split_and_sort(other) |
| 45 | + if a == b: |
| 46 | + return True |
| 47 | + else: |
| 48 | + self.explanation = _make_explanation(a, b) |
| 49 | + return False |
| 50 | + |
| 51 | + |
| 52 | +class Relaxed(Unordered): |
| 53 | + """Like Unordered, but may also be ignored completely for some compilers""" |
| 54 | + try: |
| 55 | + # noinspection PyUnresolvedReferences |
| 56 | + from pybind11_tests import relaxed |
| 57 | + is_relaxed = True |
| 58 | + except ImportError: |
| 59 | + is_relaxed = False |
| 60 | + |
| 61 | + def __eq__(self, other): |
| 62 | + if self.is_relaxed: |
| 63 | + return True |
| 64 | + else: |
| 65 | + return super(self.__class__, self).__eq__(other) |
| 66 | + |
| 67 | + |
| 68 | +class Capture(object): |
| 69 | + def __init__(self, capfd): |
| 70 | + self.capfd = capfd |
| 71 | + self.out = "" |
| 72 | + self.err = "" |
| 73 | + |
| 74 | + def __enter__(self): |
| 75 | + self.capfd.readouterr() |
| 76 | + return self |
| 77 | + |
| 78 | + def __exit__(self, *_): |
| 79 | + self.out, self.err = self.capfd.readouterr() |
| 80 | + |
| 81 | + @property |
| 82 | + def output(self): |
| 83 | + return Output(self.out) |
| 84 | + |
| 85 | + @property |
| 86 | + def unordered(self): |
| 87 | + return Unordered(self.out) |
| 88 | + |
| 89 | + @property |
| 90 | + def relaxed(self): |
| 91 | + return Relaxed(self.out) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.fixture |
| 95 | +def capture(capfd): |
| 96 | + """Extended `capfd` with context manager and custom equality operators""" |
| 97 | + return Capture(capfd) |
| 98 | + |
| 99 | + |
| 100 | +def _sanitize_docstring(s): |
| 101 | + s = s.strip() |
| 102 | + s = s.replace("unicode", "str") |
| 103 | + s = s.replace("pybind11_tests.", "m.") |
| 104 | + return s |
| 105 | + |
| 106 | + |
| 107 | +class Doc(object): |
| 108 | + def __init__(self, thing): |
| 109 | + self.doc = _sanitize_docstring(thing.__doc__) |
| 110 | + self.explanation = [] |
| 111 | + |
| 112 | + def __eq__(self, other): |
| 113 | + a = self.doc |
| 114 | + b = _strip_and_dedent(other) |
| 115 | + if a == b: |
| 116 | + return True |
| 117 | + else: |
| 118 | + self.explanation = _make_explanation(a.splitlines(), b.splitlines()) |
| 119 | + return False |
| 120 | + |
| 121 | + |
| 122 | +@pytest.fixture |
| 123 | +def doc(): |
| 124 | + """Sanitize docstrings and add custom failure explanation""" |
| 125 | + return Doc |
| 126 | + |
| 127 | + |
| 128 | +# noinspection PyUnusedLocal |
| 129 | +def pytest_assertrepr_compare(op, left, right): |
| 130 | + """Hook to insert custom failure explanation""" |
| 131 | + if hasattr(left, 'explanation'): |
| 132 | + return left.explanation |
0 commit comments