Skip to content

Commit

Permalink
tests: Smoke test __str__() and __repr__()
Browse files Browse the repository at this point in the history
It is extremely annoying to have an exception obscured by an exception
in repr or str.  The concrete output of str and repr, however, is bound
to change and therefore, it needs not be tested.
  • Loading branch information
Synss committed Jan 2, 2021
1 parent b43175a commit 56cc642
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/test_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def digest_size(self):
def block_size(self):
raise NotImplementedError

@pytest.mark.parametrize("repr_", (repr, str), ids=lambda f: f.__name__)
def test_repr(self, repr_, algorithm):
assert isinstance(repr_(algorithm), str)

def test_digest_size_accessor(self, algorithm, digest_size):
assert algorithm.digest_size == digest_size

Expand Down
5 changes: 5 additions & 0 deletions tests/test_mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def test_from_int(value):
assert mpi == mpi


@pytest.mark.parametrize("repr_", (repr, str), ids=lambda f: f.__name__)
def test_repr(repr_):
assert isinstance(repr_(MPI(69)), str)


def test_is_integral():
assert isinstance(MPI(42), numbers.Integral)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def psk_store(self):
def proxy(self, psk_store):
return PSKStoreProxy(psk_store)

@pytest.mark.parametrize("repr_", (repr, str), ids=lambda f: f.__name__)
def test_repr(self, repr_, psk_store):
assert isinstance(repr_(psk_store), str)

def test_unwrap(self, proxy, psk_store):
assert proxy.unwrap() == psk_store

Expand Down Expand Up @@ -123,6 +127,10 @@ def length(self):
def header(self, record_type, version, length):
return TLSRecordHeader(record_type, version, length)

@pytest.mark.parametrize("repr_", (repr, str), ids=lambda f: f.__name__)
def test_repr(self, repr_, record_type):
assert isinstance(repr_(record_type), str)

def test_accessors(self, header, record_type, version, length):
assert len(header) == 5
assert header.record_type is record_type
Expand Down Expand Up @@ -200,6 +208,10 @@ class TestTrustStore(Chain):
def store(self):
return TrustStore.system()

@pytest.mark.parametrize("repr_", (repr, str), ids=lambda f: f.__name__)
def test_repr(self, repr_, store):
assert isinstance(repr_(store), str)

def test_eq(self, store):
other = TrustStore(store)
assert store is not other
Expand Down Expand Up @@ -252,6 +264,10 @@ def conf(self):
def version(self):
raise NotImplementedError

@pytest.mark.parametrize("repr_", (repr, str), ids=lambda f: f.__name__)
def test_repr(self, repr_, conf):
assert isinstance(repr_(conf), str)

@pytest.mark.parametrize("validate", [True, False])
def test_set_validate_certificates(self, conf, validate):
conf_ = conf.update(validate_certificates=validate)
Expand Down Expand Up @@ -383,6 +399,14 @@ def test_get_channel_binding(self, context):
# def test_negotiated_tls_version(self, context):
# assert context._negotiated_tls_version() is TLSVersion.SSLv3

@pytest.fixture
def tls_wrapped_buffer(self, context):
return TLSWrappedBuffer(context)

@pytest.mark.parametrize("repr_", (repr, str), ids=lambda f: f.__name__)
def test_repr_tls_wrapped_buffer(self, repr_, tls_wrapped_buffer):
assert isinstance(repr_(tls_wrapped_buffer), str)


class TestClientContext(TestBaseContext):
@pytest.fixture(params=[None, "hostname", "localhost"])
Expand Down
8 changes: 7 additions & 1 deletion tests/test_x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def subject_key():


class _X509Base:
# Derive and provide `x509`.
@pytest.fixture
def x509(self):
raise NotImplementedError

@pytest.fixture
def der(self, x509):
Expand All @@ -61,6 +63,10 @@ def pem(self, x509):


class _CommonTests(_X509Base):
@pytest.mark.parametrize("repr_", (repr, str), ids=lambda f: f.__name__)
def test_repr(self, repr_, x509):
assert isinstance(repr_(x509), str)

def test_from_buffer(self, x509, der):
assert type(x509).from_buffer(der) == x509

Expand Down

0 comments on commit 56cc642

Please sign in to comment.