2016-08-12 11:50:00 +00:00
|
|
|
"""pytest configuration
|
|
|
|
|
|
|
|
Extends output capture as needed by pybind11: ignore constructors, optional unordered lines.
|
|
|
|
Adds docstring and exceptions message sanitizers: ignore Python 2 vs 3 differences.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import textwrap
|
|
|
|
import difflib
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import contextlib
|
2016-12-16 14:00:46 +00:00
|
|
|
import platform
|
|
|
|
import gc
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
_unicode_marker = re.compile(r'u(\'[^\']*\')')
|
2016-11-20 20:21:54 +00:00
|
|
|
_long_marker = re.compile(r'([0-9])L')
|
|
|
|
_hexadecimal = re.compile(r'0x[0-9a-fA-F]+')
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _strip_and_dedent(s):
|
|
|
|
"""For triple-quote strings"""
|
|
|
|
return textwrap.dedent(s.lstrip('\n').rstrip())
|
|
|
|
|
|
|
|
|
|
|
|
def _split_and_sort(s):
|
|
|
|
"""For output which does not require specific line order"""
|
|
|
|
return sorted(_strip_and_dedent(s).splitlines())
|
|
|
|
|
|
|
|
|
|
|
|
def _make_explanation(a, b):
|
|
|
|
"""Explanation for a failed assert -- the a and b arguments are List[str]"""
|
|
|
|
return ["--- actual / +++ expected"] + [line.strip('\n') for line in difflib.ndiff(a, b)]
|
|
|
|
|
|
|
|
|
|
|
|
class Output(object):
|
|
|
|
"""Basic output post-processing and comparison"""
|
|
|
|
def __init__(self, string):
|
|
|
|
self.string = string
|
|
|
|
self.explanation = []
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.string
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
# Ignore constructor/destructor output which is prefixed with "###"
|
|
|
|
a = [line for line in self.string.strip().splitlines() if not line.startswith("###")]
|
|
|
|
b = _strip_and_dedent(other).splitlines()
|
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.explanation = _make_explanation(a, b)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class Unordered(Output):
|
|
|
|
"""Custom comparison for output without strict line ordering"""
|
|
|
|
def __eq__(self, other):
|
|
|
|
a = _split_and_sort(self.string)
|
|
|
|
b = _split_and_sort(other)
|
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.explanation = _make_explanation(a, b)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class Capture(object):
|
|
|
|
def __init__(self, capfd):
|
|
|
|
self.capfd = capfd
|
|
|
|
self.out = ""
|
2016-08-29 16:03:34 +00:00
|
|
|
self.err = ""
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
2016-09-06 22:50:10 +00:00
|
|
|
self.capfd.readouterr()
|
2016-08-12 11:50:00 +00:00
|
|
|
return self
|
|
|
|
|
2019-02-04 16:09:47 +00:00
|
|
|
def __exit__(self, *args):
|
2016-09-06 22:50:10 +00:00
|
|
|
self.out, self.err = self.capfd.readouterr()
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
a = Output(self.out)
|
|
|
|
b = other
|
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.explanation = a.explanation
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.out
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self.out
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unordered(self):
|
|
|
|
return Unordered(self.out)
|
|
|
|
|
2016-08-29 16:03:34 +00:00
|
|
|
@property
|
|
|
|
def stderr(self):
|
|
|
|
return Output(self.err)
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
2017-03-10 14:42:42 +00:00
|
|
|
def capture(capsys):
|
|
|
|
"""Extended `capsys` with context manager and custom equality operators"""
|
|
|
|
return Capture(capsys)
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SanitizedString(object):
|
|
|
|
def __init__(self, sanitizer):
|
|
|
|
self.sanitizer = sanitizer
|
|
|
|
self.string = ""
|
|
|
|
self.explanation = []
|
|
|
|
|
|
|
|
def __call__(self, thing):
|
|
|
|
self.string = self.sanitizer(thing)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
a = self.string
|
|
|
|
b = _strip_and_dedent(other)
|
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.explanation = _make_explanation(a.splitlines(), b.splitlines())
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _sanitize_general(s):
|
|
|
|
s = s.strip()
|
|
|
|
s = s.replace("pybind11_tests.", "m.")
|
|
|
|
s = s.replace("unicode", "str")
|
|
|
|
s = _long_marker.sub(r"\1", s)
|
|
|
|
s = _unicode_marker.sub(r"\1", s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def _sanitize_docstring(thing):
|
|
|
|
s = thing.__doc__
|
|
|
|
s = _sanitize_general(s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def doc():
|
|
|
|
"""Sanitize docstrings and add custom failure explanation"""
|
|
|
|
return SanitizedString(_sanitize_docstring)
|
|
|
|
|
|
|
|
|
|
|
|
def _sanitize_message(thing):
|
|
|
|
s = str(thing)
|
|
|
|
s = _sanitize_general(s)
|
|
|
|
s = _hexadecimal.sub("0", s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def msg():
|
|
|
|
"""Sanitize messages and add custom failure explanation"""
|
|
|
|
return SanitizedString(_sanitize_message)
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
def pytest_assertrepr_compare(op, left, right):
|
|
|
|
"""Hook to insert custom failure explanation"""
|
|
|
|
if hasattr(left, 'explanation'):
|
|
|
|
return left.explanation
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def suppress(exception):
|
|
|
|
"""Suppress the desired exception"""
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
except exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-12-16 14:00:46 +00:00
|
|
|
def gc_collect():
|
|
|
|
''' Run the garbage collector twice (needed when running
|
|
|
|
reference counting tests with PyPy) '''
|
|
|
|
gc.collect()
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
2019-01-23 13:22:39 +00:00
|
|
|
def pytest_configure():
|
2016-08-12 11:50:00 +00:00
|
|
|
"""Add import suppression and test requirements to `pytest` namespace"""
|
|
|
|
try:
|
|
|
|
import numpy as np
|
|
|
|
except ImportError:
|
|
|
|
np = None
|
|
|
|
try:
|
|
|
|
import scipy
|
|
|
|
except ImportError:
|
|
|
|
scipy = None
|
|
|
|
try:
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
from pybind11_tests.eigen import have_eigen
|
2016-08-12 11:50:00 +00:00
|
|
|
except ImportError:
|
|
|
|
have_eigen = False
|
2016-12-16 14:00:46 +00:00
|
|
|
pypy = platform.python_implementation() == "PyPy"
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
skipif = pytest.mark.skipif
|
2019-01-23 13:22:39 +00:00
|
|
|
pytest.suppress = suppress
|
|
|
|
pytest.requires_numpy = skipif(not np, reason="numpy is not installed")
|
|
|
|
pytest.requires_scipy = skipif(not np, reason="scipy is not installed")
|
|
|
|
pytest.requires_eigen_and_numpy = skipif(not have_eigen or not np,
|
|
|
|
reason="eigen and/or numpy are not installed")
|
|
|
|
pytest.requires_eigen_and_scipy = skipif(
|
|
|
|
not have_eigen or not scipy, reason="eigen and/or scipy are not installed")
|
|
|
|
pytest.unsupported_on_pypy = skipif(pypy, reason="unsupported on PyPy")
|
|
|
|
pytest.unsupported_on_py2 = skipif(sys.version_info.major < 3,
|
|
|
|
reason="unsupported on Python 2.x")
|
|
|
|
pytest.gc_collect = gc_collect
|
2016-08-25 15:08:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _test_import_pybind11():
|
|
|
|
"""Early diagnostic for test module initialization errors
|
|
|
|
|
|
|
|
When there is an error during initialization, the first import will report the
|
|
|
|
real error while all subsequent imports will report nonsense. This import test
|
|
|
|
is done early (in the pytest configuration file, before any tests) in order to
|
|
|
|
avoid the noise of having all tests fail with identical error messages.
|
|
|
|
|
|
|
|
Any possible exception is caught here and reported manually *without* the stack
|
|
|
|
trace. This further reduces noise since the trace would only show pytest internals
|
|
|
|
which are not useful for debugging pybind11 module issues.
|
|
|
|
"""
|
|
|
|
# noinspection PyBroadException
|
|
|
|
try:
|
2016-11-20 20:21:54 +00:00
|
|
|
import pybind11_tests # noqa: F401 imported but unused
|
2016-08-25 15:08:09 +00:00
|
|
|
except Exception as e:
|
|
|
|
print("Failed to import pybind11_tests from pytest:")
|
|
|
|
print(" {}: {}".format(type(e).__name__, e))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
_test_import_pybind11()
|